/[escript]/branches/arrexp_2137_win_merge/finley/src/CPPAdapter/SystemMatrixAdapter.cpp
ViewVC logotype

Contents of /branches/arrexp_2137_win_merge/finley/src/CPPAdapter/SystemMatrixAdapter.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26