Ice Sheet System Model  4.18
Code documentation
MatMultPatch.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 /*Petsc includes: */
12 #include <petscmat.h>
13 #include <petscvec.h>
14 #include <petscksp.h>
15 
16 #include "../../mpi/issmmpi.h"
17 #include "../../../shared/shared.h"
18 
19 /*Function prototypes: */
20 int MatMultCompatible(Mat A,Vec x,ISSM_MPI_Comm comm);
21 void VecRelocalize(Vec* outvector,Vec vector,int m,ISSM_MPI_Comm comm);
22 
23 void MatMultPatch(Mat A,Vec X, Vec AX,ISSM_MPI_Comm comm){ //same prototype as MatMult in Petsc
24 
25  int m,n;
26  Vec X_rel=NULL;
27 
28  _assert_(A); _assert_(X);
29 
30  if (MatMultCompatible(A,X,comm)){
31  MatMult(A,X,AX);
32  }
33  else{
34  MatGetLocalSize(A,&m,&n);;
35  VecRelocalize(&X_rel,X,n,comm);
36  MatMult(A,X_rel,AX); ;
37  #if _PETSC_MAJOR_ < 3 || (_PETSC_MAJOR_ == 3 && _PETSC_MINOR_ < 2)
38  VecDestroy(X_rel);
39  #else
40  VecDestroy(&X_rel);
41  #endif
42  }
43 }
44 
45 int MatMultCompatible(Mat A,Vec x,ISSM_MPI_Comm comm){
46 
47  /*error management*/
48 
49  int local_m,local_n;
50  int range;
51  int result=1;
52  int sumresult;
53  int num_procs;
54 
55  /*recover num_procs:*/
56  ISSM_MPI_Comm_size(comm,&num_procs);
57 
58  MatGetLocalSize(A,&local_m,&local_n);;
59  VecGetLocalSize(x,&range);;
60 
61  if (local_n!=range)result=0;
62 
63  /*synchronize result: */
64  ISSM_MPI_Reduce (&result,&sumresult,1,ISSM_MPI_INT,ISSM_MPI_SUM,0,comm );
65  ISSM_MPI_Bcast(&sumresult,1,ISSM_MPI_INT,0,comm);
66  if (sumresult!=num_procs){
67  result=0;
68  }
69  else{
70  result=1;
71  }
72  return result;
73 }
74 
75 void VecRelocalize(Vec* poutvector,Vec vector,int m,ISSM_MPI_Comm comm){
76 
77  /*vector index and vector values*/
78  int* index=NULL;
79  double* values=NULL;
80  int lower_row,upper_row,range;
81 
82  /*output: */
83  Vec outvector=NULL;
84 
85  /*Create outvector with local size m*/
86  VecCreate(comm,&outvector); ;
87  VecSetSizes(outvector,m,PETSC_DECIDE); ;
88  VecSetFromOptions(outvector); ;
89 
90  /*Go through vector, get values, and plug them into outvector*/
91  VecGetOwnershipRange(vector,&lower_row,&upper_row); ;
92  upper_row--;
93  range=upper_row-lower_row+1;
94  if (range){
95  index=xNew<int>(range);
96  values=xNew<double>(range);
97  for (int i=0;i<range;i++){
98  *(index+i)=lower_row+i;
99  }
100  VecGetValues(vector,range,index,values);
101  VecSetValues(outvector,range,index,values,INSERT_VALUES);
102  }
103 
104  /*Assemble outvector*/
105  VecAssemblyBegin(outvector);;
106  VecAssemblyEnd(outvector);;
107 
108  /*Free ressources:*/
109  xDelete<int>(index);
110  xDelete<double>(values);
111 
112  /*Assign output pointers:*/
113  *poutvector=outvector;
114 
115 }
_assert_
#define _assert_(ignore)
Definition: exceptions.h:37
VecRelocalize
void VecRelocalize(Vec *outvector, Vec vector, int m, ISSM_MPI_Comm comm)
Definition: MatMultPatch.cpp:75
ISSM_MPI_SUM
#define ISSM_MPI_SUM
Definition: issmmpi.h:134
ISSM_MPI_INT
#define ISSM_MPI_INT
Definition: issmmpi.h:127
MatMultPatch
void MatMultPatch(Mat A, Vec X, Vec AX, ISSM_MPI_Comm comm)
Definition: MatMultPatch.cpp:23
ISSM_MPI_Bcast
int ISSM_MPI_Bcast(void *buffer, int count, ISSM_MPI_Datatype datatype, int root, ISSM_MPI_Comm comm)
Definition: issmmpi.cpp:162
ISSM_MPI_Comm
int ISSM_MPI_Comm
Definition: issmmpi.h:118
ISSM_MPI_Reduce
int ISSM_MPI_Reduce(void *sendbuf, void *recvbuf, int count, ISSM_MPI_Datatype datatype, ISSM_MPI_Op op, int root, ISSM_MPI_Comm comm)
Definition: issmmpi.cpp:373
MatMultCompatible
int MatMultCompatible(Mat A, Vec x, ISSM_MPI_Comm comm)
Definition: MatMultPatch.cpp:45
ISSM_MPI_Comm_size
int ISSM_MPI_Comm_size(ISSM_MPI_Comm comm, int *size)
Definition: issmmpi.cpp:209