Ice Sheet System Model  4.18
Code documentation
MatToSerial.cpp
Go to the documentation of this file.
1 
5 #ifdef HAVE_CONFIG_H
6  #include <config.h>
7 #else
8 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9 #endif
10 
11 #include "../petscincludes.h"
12 #include "../../../shared/shared.h"
13 
14 void MatToSerial(double** poutmatrix,Mat matrix,ISSM_MPI_Comm comm){
15 
16  int i;
17  int my_rank;
18  int num_procs;
19 
20  /*Petsc variables*/
21  PetscInt lower_row,upper_row;
22  int range;
23  int M,N; //size of matrix
24  ISSM_MPI_Status status;
25  int* idxm=NULL;
26  int* idxn=NULL;
27  double* local_matrix=NULL; /*matrix local to each node used for temporary holding matrix values*/
28  int buffer[3];
29 
30  /*recover my_rank and num_procs:*/
31  ISSM_MPI_Comm_size(comm,&num_procs);
32  ISSM_MPI_Comm_rank(comm,&my_rank);
33 
34  /*Output*/
35  double* outmatrix=NULL;
36 
37  /*get matrix size: */
38  MatGetSize(matrix,&M,&N);
39 
40  /*partition: */
41  MatGetOwnershipRange(matrix,&lower_row,&upper_row);
42  upper_row--;
43  range=upper_row-lower_row+1;
44 
45  /*Local and global allocation*/
46  if (my_rank==0)outmatrix=xNew<double>(M*N);
47 
48  if (range){
49  local_matrix=xNew<double>(N*range);
50  idxm=xNew<int>(range);
51  idxn=xNew<int>(N);
52 
53  for (i=0;i<N;i++){
54  *(idxn+i)=i;
55  }
56  for (i=0;i<range;i++){
57  *(idxm+i)=lower_row+i;
58  }
59 
60  MatGetValues(matrix,range,idxm,N,idxn,local_matrix);
61  }
62 
63  /*Now each node holds its local_matrix containing range rows.
64  * We send these rows to the matrix on node 0*/
65 
66  for (i=1;i<num_procs;i++){
67  if (my_rank==i){
68  buffer[0]=my_rank;
69  buffer[1]=lower_row;
70  buffer[2]=range;
71  ISSM_MPI_Send(buffer,3,ISSM_MPI_INT,0,1,comm);
72  if (range)ISSM_MPI_Send(local_matrix,N*range,ISSM_MPI_PDOUBLE,0,1,comm);
73  }
74  if (my_rank==0){
75  ISSM_MPI_Recv(buffer,3,ISSM_MPI_INT,i,1,comm,&status);
76  if (buffer[2])ISSM_MPI_Recv(outmatrix+(buffer[1]*N),N*buffer[2],ISSM_MPI_PDOUBLE,i,1,comm,&status);
77  }
78  }
79  if (my_rank==0){
80  //Still have the local_matrix on node 0 to take care of.
81  memcpy(outmatrix,local_matrix,N*range*sizeof(double));
82  }
83 
84  /*Assign output pointer: */
85  *poutmatrix=outmatrix;
86  xDelete<int>(idxm);
87  xDelete<int>(idxn);
88  xDelete<double>(local_matrix);
89 }
ISSM_MPI_Comm_rank
int ISSM_MPI_Comm_rank(ISSM_MPI_Comm comm, int *rank)
Definition: issmmpi.cpp:198
ISSM_MPI_INT
#define ISSM_MPI_INT
Definition: issmmpi.h:127
MatToSerial
void MatToSerial(double **poutmatrix, Mat matrix, ISSM_MPI_Comm comm)
Definition: MatToSerial.cpp:14
ISSM_MPI_Send
int ISSM_MPI_Send(void *buf, int count, ISSM_MPI_Datatype datatype, int dest, int tag, ISSM_MPI_Comm comm)
Definition: issmmpi.cpp:484
ISSM_MPI_Status
int ISSM_MPI_Status
Definition: issmmpi.h:121
ISSM_MPI_PDOUBLE
#define ISSM_MPI_PDOUBLE
Definition: issmmpi.h:126
ISSM_MPI_Comm
int ISSM_MPI_Comm
Definition: issmmpi.h:118
ISSM_MPI_Recv
int ISSM_MPI_Recv(void *buf, int count, ISSM_MPI_Datatype datatype, int source, int tag, ISSM_MPI_Comm comm, ISSM_MPI_Status *status)
Definition: issmmpi.cpp:342
ISSM_MPI_Comm_size
int ISSM_MPI_Comm_size(ISSM_MPI_Comm comm, int *size)
Definition: issmmpi.cpp:209