8 #ifndef _ISSM_MPI_SPARSE_MAT_H_
9 #define _ISSM_MPI_SPARSE_MAT_H_
16 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
19 #include "../../datastructures/datastructures.h"
20 #include "../../shared/shared.h"
21 #include "../mumps/mumpsincludes.h"
36 template <
class doubletype>
79 this->matrix=xNew<SparseRow<doubletype>*>(
m);
106 this->matrix=xNew<SparseRow<doubletype>*>(
m);
107 for(
int i=0;i<
m;i++){
115 for(
int i=0;i<
m;i++){
116 delete this->matrix[i];
118 xDelete<SparseRow<doubletype>*>(this->
matrix);
134 _printf_(
"cpu " << i <<
" #rows: " << this->m <<
"\n");
135 for(
int j=0;j<this->
m;j++){
137 this->matrix[j]->
Echo();
149 int *row_indices_forcpu = NULL;
150 int *col_indices_forcpu = NULL;
151 int *modes_forcpu = NULL;
152 doubletype *values_forcpu = NULL;
153 int *numvalues_forcpu = NULL;
154 DataSet **bucketsforcpu = NULL;
155 int **row_indices_fromcpu = NULL;
156 int **col_indices_fromcpu = NULL;
157 int **modes_fromcpu = NULL;
158 doubletype **values_fromcpu = NULL;
159 int *numvalues_fromcpu = NULL;
162 int *sendcnts = NULL;
164 int this_row_numvalues;
165 int *this_row_cols = NULL;
166 int *this_row_mods = NULL;
167 int *numvalues_perrow = NULL;
169 doubletype **values_perrow = NULL;
170 int **cols_perrow = NULL;
171 int **mods_perrow = NULL;
172 int *counters_perrow = NULL;
175 if(this->M*this->N==0)
return;
185 bucketsforcpu=xNew<DataSet*>(num_procs);
186 for(
int i=0;i<num_procs;i++){
192 bucketsforcpu[i]=bucketsofcpu_i;
198 this->
BucketsBuildScatterBuffers(&numvalues_forcpu,&row_indices_forcpu,&col_indices_forcpu,&values_forcpu,&modes_forcpu,bucketsforcpu,num_procs);
202 numvalues_fromcpu = xNew<int>(num_procs);
205 row_indices_fromcpu=xNew<int*>(num_procs);
206 col_indices_fromcpu=xNew<int*>(num_procs);
207 values_fromcpu=xNew<doubletype*>(num_procs);
208 modes_fromcpu=xNew<int*>(num_procs);
209 for(
int i=0;i<num_procs;i++){
210 int size=numvalues_fromcpu[i];
212 row_indices_fromcpu[i]=xNew<int>(size);
213 col_indices_fromcpu[i]=xNew<int>(size);
217 values_fromcpu[i]=xNew<doubletype>(size,
"t");
219 values_fromcpu[i]=xNew<doubletype>(size);
221 modes_fromcpu[i]=xNew<int>(size);
224 row_indices_fromcpu[i] = NULL;
225 col_indices_fromcpu[i] = NULL;
226 values_fromcpu[i] = NULL;
227 modes_fromcpu[i] = NULL;
235 sendcnts=xNew<int>(num_procs);
236 displs=xNew<int>(num_procs);
238 for(
int i=0;i<num_procs;i++){
239 sendcnts[i]=numvalues_forcpu[i];
241 count+=numvalues_forcpu[i];
244 for(
int i=0;i<num_procs;i++){
255 numvalues_perrow=xNewZeroInit<int>(this->m);
256 for(
int i=0;i<num_procs;i++){
257 int numvalues=numvalues_fromcpu[i];
258 int* rows=row_indices_fromcpu[i];
259 for(
int j=0;j<numvalues;j++)numvalues_perrow[rows[j]-lower_row]++;
263 values_perrow=xNew<doubletype*>(this->m);
264 cols_perrow=xNew<int*>(this->m);
265 mods_perrow=xNew<int*>(this->m);
266 counters_perrow=xNewZeroInit<int>(this->m);
268 for(
int i=0;i<this->
m;i++){
269 values_perrow[i]=xNewZeroInit<doubletype>(numvalues_perrow[i]);
270 cols_perrow[i]=xNewZeroInit<int>(numvalues_perrow[i]);
271 mods_perrow[i]=xNewZeroInit<int>(numvalues_perrow[i]);
275 for(
int i=0;i<num_procs;i++){
276 int numvalues=numvalues_fromcpu[i];
277 int* rows=row_indices_fromcpu[i];
278 int* cols=col_indices_fromcpu[i];
279 doubletype* values=values_fromcpu[i];
280 int* mods=modes_fromcpu[i];
281 for(
int j=0;j<numvalues;j++){
282 int row=rows[j]-lower_row;
283 int counter=counters_perrow[row];
284 values_perrow[row][counter]=values[j];
285 cols_perrow[row][counter]=cols[j];
286 mods_perrow[row][counter]=mods[j];
287 counter=counters_perrow[row]++;
292 for(
int i=0;i<this->
m;i++) this->matrix[i]->
SetValues(numvalues_perrow[i],cols_perrow[i],values_perrow[i],mods_perrow[i]);
295 xDelete<int>(numvalues_perrow);
296 xDelete<int>(RowRank);
297 xDelete<int>(row_indices_forcpu);
298 xDelete<int>(col_indices_forcpu);
299 xDelete<int>(modes_forcpu);
300 xDelete<doubletype>(values_forcpu);
301 xDelete<int>(numvalues_forcpu);
302 for(
int i=0;i<num_procs;i++){
306 xDelete<DataSet*>(bucketsforcpu);
307 for(
int i=0;i<num_procs;i++){
308 int* rows=row_indices_fromcpu[i];
309 int* cols=col_indices_fromcpu[i];
310 int* modes=modes_fromcpu[i];
311 doubletype* values=values_fromcpu[i];
315 xDelete<doubletype>(values);
317 xDelete<int*>(row_indices_fromcpu);
318 xDelete<int*>(col_indices_fromcpu);
319 xDelete<int*>(modes_fromcpu);
320 xDelete<doubletype*>(values_fromcpu);
321 xDelete<int>(numvalues_fromcpu);
322 xDelete<int>(sendcnts);
323 xDelete<int>(displs);
324 for(
int i=0;i<this->
m;i++){
325 doubletype* values=values_perrow[i]; xDelete<doubletype>(values);
326 int* cols=cols_perrow[i]; xDelete<int>(cols);
327 int* mods=mods_perrow[i]; xDelete<int>(mods);
329 xDelete<int>(counters_perrow);
330 xDelete<doubletype*>(values_perrow);
331 xDelete<int*>(cols_perrow);
332 xDelete<int*>(mods_perrow);
336 doubletype norm,local_norm;
343 for(i=0;i<this->
m;i++){
344 local_norm=
max(local_norm,this->matrix[i]->
Norm(mode));
352 for(i=0;i<this->
m;i++){
353 local_norm+=this->matrix[i]->
Norm(mode);
390 for(
int i=0;i<this->
m;i++){
391 AX->
vector[i]=this->matrix[i]->
Mult(X_serial);
395 xDelete<doubletype>(X_serial);
427 for(
int i=0;i<
m;i++)
delete this->matrix[i];
428 xDelete<SparseRow<doubletype>*>(this->
matrix);
430 this->matrix=xNew<SparseRow<doubletype>*>(
m);
440 void BucketsBuildScatterBuffers(
int** pnumvalues_forcpu,
int** prow_indices_forcpu,
int** pcol_indices_forcpu,doubletype** pvalues_forcpu,
int** pmodes_forcpu,
DataSet** bucketsforcpu,
int num_procs){
444 int *temp_row_indices_forcpu = NULL;
445 int *temp_col_indices_forcpu = NULL;
446 doubletype *temp_values_forcpu = NULL;
447 int *temp_modes_forcpu = NULL;
450 int *numvalues_forcpu = NULL;
451 int *row_indices_forcpu = NULL;
452 int *col_indices_forcpu = NULL;
453 doubletype *values_forcpu = NULL;
454 int *modes_forcpu = NULL;
458 numvalues_forcpu=xNew<int>(num_procs);
459 for(
int i=0;i<num_procs;i++){
466 numvalues_forcpu[i]=count;
471 for(
int i=0;i<num_procs;i++) count+=numvalues_forcpu[i];
472 int total_size=count;
475 row_indices_forcpu = xNew<int>(total_size);
476 col_indices_forcpu = xNew<int>(total_size);
480 values_forcpu = xNew<doubletype>(total_size,
"t");
482 values_forcpu = xNew<doubletype>(total_size);
484 modes_forcpu = xNew<int>(total_size);
489 temp_row_indices_forcpu = row_indices_forcpu;
490 temp_col_indices_forcpu = col_indices_forcpu;
491 temp_values_forcpu = values_forcpu;
492 temp_modes_forcpu = modes_forcpu;
495 for(
int i=0;i<num_procs;i++){
499 bucket->
Marshall(&temp_row_indices_forcpu,&temp_col_indices_forcpu,&temp_values_forcpu,&temp_modes_forcpu);
504 if(temp_row_indices_forcpu!=row_indices_forcpu+total_size)
_error_(
"problem with marshalling of buckets");
505 if(temp_col_indices_forcpu!=col_indices_forcpu+total_size)
_error_(
"problem with marshalling of buckets");
506 if(temp_values_forcpu!=values_forcpu+total_size)
_error_(
"problem with marshalling of buckets");
507 if(temp_modes_forcpu!=modes_forcpu+total_size)
_error_(
"problem with marshalling of buckets");
510 *pnumvalues_forcpu = numvalues_forcpu;
511 *prow_indices_forcpu = row_indices_forcpu;
512 *pcol_indices_forcpu = col_indices_forcpu;
513 *pvalues_forcpu = values_forcpu;
514 *pmodes_forcpu = modes_forcpu;
517 #ifndef _HAVE_WRAPPERS_
533 MpiSparseMumpsSolve( uf->
vector,uf->
M,uf->
m, this->matrix,this->M,this->N,this->m, pf->
vector,pf->
M,pf->
m,parameters);
535 _error_(
"IssmMpiSparseMat solver requires MUMPS solver");
544 _error_(
"not implemented yet!");
551 _error_(
"GSL support not compiled in!");
555 _error_(
"solver type not supported yet!");
562 #endif //#ifndef _ISSM_MPI_SPARSE_MAT_H_