1 |
jgs |
472 |
|
2 |
ksteube |
1312 |
/******************************************************* |
3 |
ksteube |
1811 |
* |
4 |
|
|
* Copyright (c) 2003-2008 by University of Queensland |
5 |
|
|
* Earth Systems Science Computational Center (ESSCC) |
6 |
|
|
* http://www.uq.edu.au/esscc |
7 |
|
|
* |
8 |
|
|
* Primary Business: Queensland, Australia |
9 |
|
|
* Licensed under the Open Software License version 3.0 |
10 |
|
|
* http://www.opensource.org/licenses/osl-3.0.php |
11 |
|
|
* |
12 |
|
|
*******************************************************/ |
13 |
ksteube |
1312 |
|
14 |
ksteube |
1811 |
|
15 |
ksteube |
817 |
#ifdef PASO_MPI |
16 |
|
|
#include <mpi.h> |
17 |
|
|
#endif |
18 |
jgs |
203 |
#include "SystemMatrixAdapter.h" |
19 |
jgs |
82 |
|
20 |
|
|
using namespace std; |
21 |
|
|
|
22 |
|
|
namespace finley { |
23 |
|
|
|
24 |
jgs |
102 |
struct null_deleter |
25 |
|
|
{ |
26 |
phornby |
1628 |
void operator()(void const *ptr) const |
27 |
|
|
{ |
28 |
|
|
} |
29 |
jgs |
102 |
}; |
30 |
|
|
|
31 |
|
|
|
32 |
jgs |
82 |
SystemMatrixAdapter::SystemMatrixAdapter() |
33 |
|
|
{ |
34 |
|
|
throw FinleyAdapterException("Error - Illegal to generate default SystemMatrixAdapter."); |
35 |
|
|
} |
36 |
|
|
|
37 |
jgs |
150 |
SystemMatrixAdapter::SystemMatrixAdapter(Paso_SystemMatrix* system_matrix, |
38 |
jgs |
82 |
const int row_blocksize, |
39 |
|
|
const escript::FunctionSpace& row_functionspace, |
40 |
|
|
const int column_blocksize, |
41 |
|
|
const escript::FunctionSpace& column_functionspace): |
42 |
jgs |
102 |
AbstractSystemMatrix(row_blocksize,row_functionspace,column_blocksize,column_functionspace) |
43 |
jgs |
82 |
{ |
44 |
phornby |
1628 |
m_system_matrix.reset(system_matrix,null_deleter()); |
45 |
jgs |
82 |
} |
46 |
|
|
|
47 |
|
|
SystemMatrixAdapter::~SystemMatrixAdapter() |
48 |
|
|
{ |
49 |
phornby |
1628 |
if (m_system_matrix.unique()) { |
50 |
|
|
Paso_SystemMatrix* mat=m_system_matrix.get(); |
51 |
|
|
Paso_SystemMatrix_free(mat); |
52 |
|
|
} |
53 |
jgs |
82 |
} |
54 |
|
|
|
55 |
jgs |
150 |
Paso_SystemMatrix* SystemMatrixAdapter::getPaso_SystemMatrix() const |
56 |
jgs |
82 |
{ |
57 |
|
|
return m_system_matrix.get(); |
58 |
|
|
} |
59 |
|
|
|
60 |
jgs |
153 |
void SystemMatrixAdapter::ypAx(escript::Data& y,escript::Data& x) const |
61 |
jgs |
82 |
{ |
62 |
jgs |
150 |
Paso_SystemMatrix* mat=getPaso_SystemMatrix(); |
63 |
|
|
|
64 |
phornby |
1628 |
if ( x.getDataPointSize() != getColumnBlockSize()) { |
65 |
|
|
throw FinleyAdapterException("matrix vector product : column block size does not match the number of components in input."); |
66 |
|
|
} else if (y.getDataPointSize() != getRowBlockSize()) { |
67 |
|
|
throw FinleyAdapterException("matrix vector product : row block size does not match the number of components in output."); |
68 |
|
|
} else if ( x.getFunctionSpace() != getColumnFunctionSpace()) { |
69 |
|
|
throw FinleyAdapterException("matrix vector product : column function space and function space of input don't match."); |
70 |
|
|
} else if (y.getFunctionSpace() != getRowFunctionSpace()) { |
71 |
|
|
throw FinleyAdapterException("matrix vector product : row function space and function space of output don't match."); |
72 |
|
|
} |
73 |
|
|
x.expand(); |
74 |
|
|
y.expand(); |
75 |
|
|
double* x_dp=x.getSampleData(0); |
76 |
|
|
double* y_dp=y.getSampleData(0); |
77 |
|
|
Paso_SystemMatrix_MatrixVector(1., mat,x_dp, 1.,y_dp); |
78 |
|
|
checkPasoError(); |
79 |
jgs |
82 |
} |
80 |
|
|
|
81 |
jgs |
150 |
int SystemMatrixAdapter::mapOptionToPaso(const int option) { |
82 |
|
|
switch (option) { |
83 |
gross |
1639 |
case ESCRIPT_DEFAULT: |
84 |
|
|
return PASO_DEFAULT; |
85 |
|
|
case ESCRIPT_DIRECT: |
86 |
|
|
return PASO_DIRECT; |
87 |
|
|
case ESCRIPT_CHOLEVSKY: |
88 |
|
|
return PASO_CHOLEVSKY; |
89 |
|
|
case ESCRIPT_PCG: |
90 |
|
|
return PASO_PCG; |
91 |
|
|
case ESCRIPT_CR: |
92 |
|
|
return PASO_CR; |
93 |
|
|
case ESCRIPT_CGS: |
94 |
|
|
return PASO_CGS; |
95 |
|
|
case ESCRIPT_BICGSTAB: |
96 |
|
|
return PASO_BICGSTAB; |
97 |
|
|
case ESCRIPT_SSOR: |
98 |
|
|
return PASO_SSOR; |
99 |
|
|
case ESCRIPT_ILU0: |
100 |
|
|
return PASO_ILU0; |
101 |
|
|
case ESCRIPT_ILUT: |
102 |
|
|
return PASO_ILUT; |
103 |
|
|
case ESCRIPT_JACOBI: |
104 |
|
|
return PASO_JACOBI; |
105 |
|
|
case ESCRIPT_GMRES: |
106 |
|
|
return PASO_GMRES; |
107 |
|
|
case ESCRIPT_PRES20: |
108 |
|
|
return PASO_PRES20; |
109 |
|
|
case ESCRIPT_NO_REORDERING: |
110 |
|
|
return PASO_NO_REORDERING; |
111 |
|
|
case ESCRIPT_MINIMUM_FILL_IN: |
112 |
|
|
return PASO_MINIMUM_FILL_IN; |
113 |
|
|
case ESCRIPT_NESTED_DISSECTION: |
114 |
|
|
return PASO_NESTED_DISSECTION; |
115 |
|
|
case ESCRIPT_SCSL: |
116 |
|
|
return PASO_SCSL; |
117 |
|
|
case ESCRIPT_MKL: |
118 |
|
|
return PASO_MKL; |
119 |
|
|
case ESCRIPT_UMFPACK: |
120 |
|
|
return PASO_UMFPACK; |
121 |
|
|
case ESCRIPT_ITERATIVE: |
122 |
|
|
return PASO_ITERATIVE; |
123 |
|
|
case ESCRIPT_PASO: |
124 |
|
|
return PASO_PASO; |
125 |
|
|
case ESCRIPT_LUMPING: |
126 |
|
|
return PASO_LUMPING; |
127 |
|
|
case ESCRIPT_AMG: |
128 |
|
|
return PASO_AMG; |
129 |
|
|
case ESCRIPT_RILU: |
130 |
|
|
return PASO_RILU; |
131 |
|
|
case ESCRIPT_TRILINOS: |
132 |
|
|
return PASO_TRILINOS; |
133 |
|
|
case ESCRIPT_NONLINEAR_GMRES: |
134 |
|
|
return PASO_NONLINEAR_GMRES; |
135 |
artak |
1703 |
case ESCRIPT_TFQMR: |
136 |
|
|
return PASO_TFQMR; |
137 |
artak |
1787 |
case ESCRIPT_MINRES: |
138 |
|
|
return PASO_MINRES; |
139 |
artak |
1819 |
case ESCRIPT_GS: |
140 |
|
|
return PASO_GS; |
141 |
gross |
1639 |
default: |
142 |
|
|
stringstream temp; |
143 |
|
|
temp << "Error - Cannot map option value "<< option << " onto Paso"; |
144 |
|
|
throw FinleyAdapterException(temp.str()); |
145 |
|
|
} |
146 |
jgs |
150 |
} |
147 |
|
|
|
148 |
ksteube |
1339 |
void finley::SystemMatrixAdapter::Print_Matrix_Info(const bool full=false) const |
149 |
|
|
{ |
150 |
phornby |
1628 |
Paso_SystemMatrix* mat=m_system_matrix.get(); |
151 |
|
|
int first_row_index = mat->row_distribution->first_component[mat->mpi_info->rank]; |
152 |
|
|
int last_row_index = mat->row_distribution->first_component[mat->mpi_info->rank+1]-1; |
153 |
|
|
int first_col_index = mat->col_distribution->first_component[mat->mpi_info->rank]; |
154 |
|
|
int last_col_index = mat->col_distribution->first_component[mat->mpi_info->rank+1]-1; |
155 |
ksteube |
1339 |
|
156 |
phornby |
1628 |
fprintf(stdout, "Print_Matrix_Info running on CPU %d of %d\n", mat->mpi_info->rank, mat->mpi_info->size); |
157 |
ksteube |
1339 |
|
158 |
phornby |
1628 |
switch (mat->type) { |
159 |
|
|
case MATRIX_FORMAT_DEFAULT: fprintf(stdout, "\tMatrix type MATRIX_FORMAT_DEFAULT\n"); break; |
160 |
|
|
case MATRIX_FORMAT_CSC: fprintf(stdout, "\tMatrix type MATRIX_FORMAT_CSC\n"); break; |
161 |
|
|
case MATRIX_FORMAT_SYM: fprintf(stdout, "\tMatrix type MATRIX_FORMAT_SYM\n"); break; |
162 |
|
|
case MATRIX_FORMAT_BLK1: fprintf(stdout, "\tMatrix type MATRIX_FORMAT_BLK1\n"); break; |
163 |
|
|
case MATRIX_FORMAT_OFFSET1: fprintf(stdout, "\tMatrix type MATRIX_FORMAT_OFFSET1\n"); break; |
164 |
|
|
case MATRIX_FORMAT_TRILINOS_CRS: fprintf(stdout, "\tMatrix type MATRIX_FORMAT_TRILINOS_CRS\n"); break; |
165 |
|
|
default: fprintf(stdout, "\tMatrix type unknown\n"); break; |
166 |
|
|
} |
167 |
ksteube |
1339 |
|
168 |
phornby |
1628 |
fprintf(stdout, "\trow indices run from %d to %d\n", first_row_index, last_row_index); |
169 |
|
|
fprintf(stdout, "\tcol indices run from %d to %d\n", first_col_index, last_col_index); |
170 |
|
|
fprintf(stdout, "\tmainBlock numRows %d\n", mat->mainBlock->numRows); |
171 |
|
|
fprintf(stdout, "\tmainBlock numCols %d\n", mat->mainBlock->numCols); |
172 |
|
|
fprintf(stdout, "\tmainBlock pattern numOutput %d\n", mat->mainBlock->pattern->numOutput); |
173 |
|
|
fprintf(stdout, "\tcol_coupleBlock numRows %d\n", mat->col_coupleBlock->numRows); |
174 |
|
|
fprintf(stdout, "\tcol_coupleBlock numCols %d\n", mat->col_coupleBlock->numCols); |
175 |
|
|
fprintf(stdout, "\tcol_coupleBlock pattern numOutput %d\n", mat->col_coupleBlock->pattern->numOutput); |
176 |
|
|
fprintf(stdout, "\trow_coupleBlock numRows %d\n", mat->row_coupleBlock->numRows); |
177 |
|
|
fprintf(stdout, "\trow_coupleBlock numCols %d\n", mat->row_coupleBlock->numCols); |
178 |
|
|
fprintf(stdout, "\trow_coupleBlock pattern numOutput %d\n", mat->row_coupleBlock->pattern->numOutput); |
179 |
|
|
fprintf(stdout, "\trow_block_size %d\n", mat->row_block_size); |
180 |
|
|
fprintf(stdout, "\tcol_block_size %d\n", mat->col_block_size); |
181 |
|
|
fprintf(stdout, "\tblock_size %d\n", mat->block_size); |
182 |
|
|
fprintf(stdout, "\tlogical_row_block_size %d\n", mat->logical_row_block_size); |
183 |
|
|
fprintf(stdout, "\tlogical_col_block_size %d\n", mat->logical_col_block_size); |
184 |
|
|
fprintf(stdout, "\tlogical_block_size %d\n", mat->logical_block_size); |
185 |
ksteube |
1339 |
|
186 |
|
|
} |
187 |
|
|
|
188 |
jgs |
153 |
void SystemMatrixAdapter::setToSolution(escript::Data& out,escript::Data& in, const boost::python::dict& options) const |
189 |
jgs |
82 |
{ |
190 |
phornby |
1628 |
Paso_SystemMatrix* mat=getPaso_SystemMatrix(); |
191 |
|
|
Paso_Options paso_options; |
192 |
|
|
dictToPasoOptions(&paso_options,options); |
193 |
|
|
if ( out.getDataPointSize() != getColumnBlockSize()) { |
194 |
|
|
throw FinleyAdapterException("solve : column block size does not match the number of components of solution."); |
195 |
|
|
} else if ( in.getDataPointSize() != getRowBlockSize()) { |
196 |
|
|
throw FinleyAdapterException("solve : row block size does not match the number of components of right hand side."); |
197 |
|
|
} else if ( out.getFunctionSpace() != getColumnFunctionSpace()) { |
198 |
|
|
throw FinleyAdapterException("solve : column function space and function space of solution don't match."); |
199 |
|
|
} else if (in.getFunctionSpace() != getRowFunctionSpace()) { |
200 |
|
|
throw FinleyAdapterException("solve : row function space and function space of right hand side don't match."); |
201 |
|
|
} |
202 |
|
|
out.expand(); |
203 |
|
|
in.expand(); |
204 |
|
|
double* out_dp=out.getSampleData(0); |
205 |
|
|
double* in_dp=in.getSampleData(0); |
206 |
|
|
Paso_solve(mat,out_dp,in_dp,&paso_options); |
207 |
|
|
checkPasoError(); |
208 |
jgs |
82 |
} |
209 |
|
|
|
210 |
jgs |
153 |
void SystemMatrixAdapter::nullifyRowsAndCols(escript::Data& row_q,escript::Data& col_q, const double mdv) const |
211 |
jgs |
82 |
{ |
212 |
phornby |
1628 |
Paso_SystemMatrix* mat = getPaso_SystemMatrix(); |
213 |
|
|
if ( col_q.getDataPointSize() != getColumnBlockSize()) { |
214 |
|
|
throw FinleyAdapterException("nullifyRowsAndCols : column block size does not match the number of components of column mask."); |
215 |
|
|
} else if ( row_q.getDataPointSize() != getRowBlockSize()) { |
216 |
|
|
throw FinleyAdapterException("nullifyRowsAndCols : row block size does not match the number of components of row mask."); |
217 |
|
|
} else if ( col_q.getFunctionSpace() != getColumnFunctionSpace()) { |
218 |
|
|
throw FinleyAdapterException("nullifyRowsAndCols : column function space and function space of column mask don't match."); |
219 |
|
|
} else if (row_q.getFunctionSpace() != getRowFunctionSpace()) { |
220 |
|
|
throw FinleyAdapterException("nullifyRowsAndCols : row function space and function space of row mask don't match."); |
221 |
|
|
} |
222 |
|
|
row_q.expand(); |
223 |
|
|
col_q.expand(); |
224 |
|
|
double* row_q_dp=row_q.getSampleData(0); |
225 |
|
|
double* col_q_dp=col_q.getSampleData(0); |
226 |
|
|
Paso_SystemMatrix_nullifyRowsAndCols(mat,row_q_dp,col_q_dp, mdv); |
227 |
|
|
checkPasoError(); |
228 |
jgs |
82 |
} |
229 |
|
|
|
230 |
jgs |
102 |
void SystemMatrixAdapter::saveMM(const std::string& fileName) const |
231 |
|
|
{ |
232 |
phornby |
1628 |
if( fileName.size() == 0 ) |
233 |
|
|
{ |
234 |
|
|
throw FinleyAdapterException("Null file name!"); |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
char *fName = TMPMEMALLOC(fileName.size()+1,char); |
238 |
woo409 |
757 |
|
239 |
phornby |
1628 |
strcpy(fName,fileName.c_str()); |
240 |
|
|
Paso_SystemMatrix* mat = getPaso_SystemMatrix(); |
241 |
|
|
Paso_SystemMatrix_saveMM(mat,fName); |
242 |
|
|
checkPasoError(); |
243 |
|
|
TMPMEMFREE(fName); |
244 |
woo409 |
757 |
|
245 |
jgs |
102 |
} |
246 |
|
|
|
247 |
jgs |
123 |
void SystemMatrixAdapter::saveHB(const std::string& fileName) const |
248 |
|
|
{ |
249 |
phornby |
1628 |
if( fileName.size() == 0 ) |
250 |
|
|
{ |
251 |
|
|
throw FinleyAdapterException("Null file name!"); |
252 |
|
|
} |
253 |
woo409 |
757 |
|
254 |
phornby |
1628 |
char *fName = TMPMEMALLOC(fileName.size()+1,char); |
255 |
woo409 |
757 |
|
256 |
phornby |
1628 |
strcpy(fName,fileName.c_str()); |
257 |
|
|
Paso_SystemMatrix* mat = getPaso_SystemMatrix(); |
258 |
|
|
Paso_SystemMatrix_saveHB(mat,fName); |
259 |
|
|
checkPasoError(); |
260 |
|
|
TMPMEMFREE(fName); |
261 |
|
|
|
262 |
jgs |
123 |
} |
263 |
|
|
|
264 |
jgs |
149 |
void SystemMatrixAdapter::resetValues() const |
265 |
jgs |
102 |
{ |
266 |
jgs |
150 |
Paso_SystemMatrix* mat = getPaso_SystemMatrix(); |
267 |
|
|
Paso_SystemMatrix_setValues(mat,0.); |
268 |
|
|
Paso_solve_free(mat); |
269 |
|
|
checkPasoError(); |
270 |
jgs |
108 |
} |
271 |
jgs |
102 |
|
272 |
gross |
1364 |
void SystemMatrixAdapter::dictToPasoOptions(Paso_Options* paso_options, const boost::python::dict& options) |
273 |
|
|
{ |
274 |
phornby |
1628 |
Paso_Options_setDefaults(paso_options); |
275 |
|
|
#define EXTRACT(__key__,__val__,__type__) if ( options.has_key(__key__)) paso_options->__val__=boost::python::extract<__type__>(options.get(__key__)) |
276 |
|
|
#define EXTRACT_OPTION(__key__,__val__,__type__) if ( options.has_key(__key__)) paso_options->__val__=mapOptionToPaso(boost::python::extract<__type__>(options.get(__key__))) |
277 |
|
|
EXTRACT("verbose",verbose,int); |
278 |
|
|
EXTRACT_OPTION("reordering",reordering,int); |
279 |
|
|
EXTRACT(ESCRIPT_TOLERANCE_KEY,tolerance,double); |
280 |
|
|
EXTRACT_OPTION(ESCRIPT_METHOD_KEY,method,int); |
281 |
|
|
EXTRACT(ESCRIPT_SYMMETRY_KEY,symmetric,int); |
282 |
|
|
EXTRACT_OPTION(ESCRIPT_PACKAGE_KEY,package,int); |
283 |
|
|
EXTRACT_OPTION("preconditioner",preconditioner,int); |
284 |
|
|
EXTRACT("iter_max",iter_max,int); |
285 |
|
|
EXTRACT("drop_tolerance",drop_tolerance,double); |
286 |
|
|
EXTRACT("drop_storage",drop_storage,double); |
287 |
|
|
EXTRACT("truncation",truncation,int); |
288 |
|
|
EXTRACT("restart",restart,int); |
289 |
artak |
1823 |
EXTRACT("sweeps",sweeps,int); |
290 |
phornby |
1628 |
#undef EXTRACT |
291 |
|
|
#undef EXTRACT_OPTION |
292 |
gross |
1364 |
} |
293 |
phornby |
1628 |
|
294 |
gross |
1364 |
|
295 |
jgs |
82 |
} // end of namespace |