9 #ifndef _ISSM_MPI_VEC_H_
10 #define _ISSM_MPI_VEC_H_
17 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
20 #include "../../shared/Exceptions/exceptions.h"
21 #include "../../shared/MemOps/MemOps.h"
22 #include "../../shared/io/io.h"
23 #include "../mpi/issmmpi.h"
33 template <
class doubletype>
53 this->
Init(Min,
false);
61 this->
Init(Min,fromlocalsize);
66 this->
Init(Min,
false);
72 this->vector=xNew<doubletype>(this->m,
"t");
74 this->vector=xNew<doubletype>(this->m);
77 xMemCpy<doubletype>(this->vector,buffer,this->m);
92 this->vector=xNew<doubletype>(this->m,
"t");
94 this->vector=xNew<doubletype>(this->m);
97 xMemCpy<doubletype>(this->vector,buffer,this->m);
121 if (
m)this->vector=xNewZeroInit<doubletype>(this->m,
"t");
123 if (
m)this->vector=xNewZeroInit<doubletype>(this->m);
129 xDelete<doubletype>(this->vector);
154 int *row_indices_forcpu = NULL;
155 int *modes_forcpu = NULL;
156 doubletype *values_forcpu = NULL;
157 int *numvalues_forcpu = NULL;
158 DataSet **bucketsforcpu = NULL;
160 int **row_indices_fromcpu = NULL;
161 int **col_indices_fromcpu = NULL;
162 int **modes_fromcpu = NULL;
163 doubletype **values_fromcpu = NULL;
164 int *numvalues_fromcpu = NULL;
168 int* sendcnts = NULL;
180 bucketsforcpu=xNew<DataSet*>(num_procs);
182 for(i=0;i<num_procs;i++){
188 bucketsforcpu[i]=bucketsofcpu_i;
200 numvalues_fromcpu = xNew<int>(num_procs);
201 for(i=0;i<num_procs;i++){
205 row_indices_fromcpu=xNew<int*>(num_procs);
206 values_fromcpu=xNew<doubletype*>(num_procs);
207 modes_fromcpu=xNew<int*>(num_procs);
208 for(i=0;i<num_procs;i++){
209 int size=numvalues_fromcpu[i];
211 row_indices_fromcpu[i]=xNew<int>(size);
215 values_fromcpu[i]=xNew<doubletype>(size,
"t");
217 values_fromcpu[i]=xNew<doubletype>(size);
220 modes_fromcpu[i]=xNew<int>(size);
223 row_indices_fromcpu[i]=NULL;
224 values_fromcpu[i]=NULL;
225 modes_fromcpu[i]=NULL;
234 sendcnts=xNew<int>(num_procs);
235 displs=xNew<int>(num_procs);
237 for(i=0;i<num_procs;i++){
238 sendcnts[i]=numvalues_forcpu[i];
240 count+=numvalues_forcpu[i];
243 for(i=0;i<num_procs;i++){
245 ISSM_MPI_Scatterv( values_forcpu, sendcnts, displs, TypeToMPIType<doubletype>(), values_fromcpu[i], numvalues_fromcpu[i], TypeToMPIType<doubletype>(), i, comm);
252 for(i=0;i<num_procs;i++){
253 int numvalues=numvalues_fromcpu[i];
254 int* rows=row_indices_fromcpu[i];
255 doubletype* values=values_fromcpu[i];
256 int* mods=modes_fromcpu[i];
258 for(j=0;j<numvalues;j++){
259 if(mods[j]==
ADD_VAL) *(
vector+(rows[j]-lower_row))+=values[j];
260 else *(
vector+(rows[j]-lower_row))=values[j];
266 xDelete<int>(RowRank);
267 xDelete<int>(row_indices_forcpu);
268 xDelete<int>(modes_forcpu);
269 xDelete<doubletype>(values_forcpu);
270 xDelete<int>(numvalues_forcpu);
272 for(i=0;i<num_procs;i++){
273 DataSet* bucketsn=bucketsforcpu[i];
276 xDelete<DataSet*>(bucketsforcpu);
278 for(i=0;i<num_procs;i++){
279 int* rows=row_indices_fromcpu[i];
280 int* modes=modes_fromcpu[i];
281 doubletype* values=values_fromcpu[i];
285 xDelete<doubletype>(values);
287 xDelete<int*>(row_indices_fromcpu);
288 xDelete<int*>(modes_fromcpu);
289 xDelete<doubletype*>(values_fromcpu);
290 xDelete<int>(numvalues_fromcpu);
292 xDelete<int>(sendcnts);
293 xDelete<int>(displs);
324 _error_(
"Get value on a MpiVec vector not implemented yet!");
351 int lower_row,upper_row;
353 int range=upper_row-lower_row;
363 int* indices=xNew<int>(range);
364 for(
int i=0;i<range;i++) indices[i]=lower_row+i;
368 doubletype* values =xNew<doubletype>(range);
369 xMemCpy<doubletype>(values,this->vector,this->m);
380 void Set(doubletype value){
382 for(
int i=0;i<this->
m;i++)this->vector[i]=value;
396 for(i=0;i<this->
m;i++)this->vector[i]=a*X->
vector[i]+this->vector[i];
409 for(i=0;i<this->
m;i++)this->vector[i]=X->
vector[i]+a*this->vector[i];
420 int lower_row,upper_row;
421 int* recvcounts=NULL;
425 doubletype* buffer=NULL;
436 buffer=xNew<doubletype>(
M,
"t");
438 buffer=xNew<doubletype>(
M);
441 recvcounts=xNew<int>(num_procs);
442 displs=xNew<int>(num_procs);
454 ISSM_MPI_Allgatherv(this->vector, this->m, TypeToMPIType<doubletype>(), buffer, recvcounts, displs, TypeToMPIType<doubletype>(),comm);
456 xDelete<int>(recvcounts);
457 xDelete<int>(displs);
472 for(
int i=0;i<this->
m;i++)this->vector[i]+=shift;
485 for(i=0;i<this->
m;i++)to->
vector[i]=this->vector[i];
491 doubletype local_norm;
497 local_norm=0.;
for(i=0;i<this->
m;i++)local_norm=
max(local_norm,fabs(this->vector[i]));
505 for(i=0;i<this->
m;i++)local_norm+=this->vector[i]*this->vector[i];
516 void Scale(doubletype scale_factor){
519 for(i=0;i<this->
M;i++)this->vector[i]=scale_factor*this->vector[i];
526 doubletype local_dot=0;
534 for(i=0;i<this->
m;i++)local_dot+=this->vector[i]*input->
vector[i];
555 for(i=0;i<this->
m;i++)this->vector[i]=x->
vector[i]/y->
vector[i];
564 int *temp_row_indices_forcpu = NULL;
565 doubletype *temp_values_forcpu = NULL;
566 int *temp_modes_forcpu = NULL;
569 int *numvalues_forcpu = NULL;
570 int *row_indices_forcpu = NULL;
571 doubletype *values_forcpu = NULL;
572 int *modes_forcpu = NULL;
576 numvalues_forcpu=xNew<int>(num_procs);
577 for(i=0;i<num_procs;i++){
586 numvalues_forcpu[i]=count;
591 for(i=0;i<num_procs;i++){
592 count+=numvalues_forcpu[i];
597 row_indices_forcpu = xNew<int>(total_size);
601 values_forcpu = xNew<doubletype>(total_size,
"t");
603 values_forcpu = xNew<doubletype>(total_size);
606 modes_forcpu = xNew<int>(total_size);
611 temp_row_indices_forcpu=row_indices_forcpu;
612 temp_values_forcpu=values_forcpu;
613 temp_modes_forcpu=modes_forcpu;
616 for(i=0;i<num_procs;i++){
620 bucket->
Marshall(&temp_row_indices_forcpu,&temp_values_forcpu,&temp_modes_forcpu);
625 if (temp_row_indices_forcpu!=row_indices_forcpu+total_size)
_error_(
"problem with marshalling of buckets");
626 if (temp_values_forcpu!=values_forcpu+total_size)
_error_(
"problem with marshalling of buckets");
627 if (temp_modes_forcpu!=modes_forcpu+total_size)
_error_(
"problem with marshalling of buckets");
630 *pnumvalues_forcpu = numvalues_forcpu;
631 *prow_indices_forcpu = row_indices_forcpu;
632 *pvalues_forcpu = values_forcpu;
633 *pmodes_forcpu = modes_forcpu;
637 #endif //#ifndef _ISSM_MPI_VEC_H_