Ice Sheet System Model  4.18
Code documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PetscMat.cpp
Go to the documentation of this file.
1 
5 /*Headers:*/
6 /*{{{*/
7 #ifdef HAVE_CONFIG_H
8  #include <config.h>
9 #else
10 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
11 #endif
12 
13 #include <stdio.h>
14 #include <string.h>
15 #include "../petscincludes.h"
16 #include "../../../shared/shared.h"
17 
18 /*}}}*/
19 
20 /*PetscMat constructors and destructor*/
22  this->matrix=NULL;
23  #ifdef _HAVE_AD_
24  this->amatrix=NULL;
25  #endif
26 
27 }
28 /*}}}*/
29 PetscMat::PetscMat(int M,int N){/*{{{*/
30 
31  this->matrix=NewMat(M,N,IssmComm::GetComm());
32 }
33 /*}}}*/
34 PetscMat::PetscMat(int M,int N, IssmDouble sparsity){/*{{{*/
35 
36  this->matrix=NewMat(M,N,sparsity,IssmComm::GetComm());
37 }
38 /*}}}*/
39 PetscMat::PetscMat(int m,int n,int M,int N,int* d_nnz,int* o_nnz){/*{{{*/
40 
41  MatCreate(IssmComm::GetComm(),&this->matrix);
42  MatSetSizes(this->matrix,m,n,M,N);
43  MatSetFromOptions(this->matrix);
44 
45  /*
46  * Versions of Petsc beyond 3.3 have changed the use of preallocation
47  * routines to distinguish between parallel builds and sequential. Since
48  * our Windows builds are currently only sequential, we need to change
49  * the way we use these functions.
50  *
51  * The following code computes the total number of non-zeroes per row of the
52  * matrix in question. In parallel builds it is nescessary to kep track of
53  * diagonal non zeros and off-diagonal (d_nnz and o_nnz). Sequential does
54  * not make that distinction.
55  */
56  #ifdef _HAVE_PETSC_MPI_
57  int* nnz = new int[M];
58  for(int i = 0; i < M; i++)
59  nnz[i] = o_nnz[i] + d_nnz[i];
60 
61  PetscErrorCode ierr = MatSeqAIJSetPreallocation(this->matrix,0,nnz);
62  delete[] nnz;
63  #else
64  PetscErrorCode ierr = MatMPIAIJSetPreallocation(this->matrix,0,d_nnz,0,o_nnz);
65  #endif
66  if(ierr) _error_("PETSc could not allocate matrix (probably not enough memory)");
67 // MatSetOption(this->matrix,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
68 
69 }
70 /*}}}*/
71 PetscMat::PetscMat(IssmDouble* serial_mat,int M,int N,IssmDouble sparsity){/*{{{*/
72 
73  int i;
74  int* idxm=NULL;
75  int* idxn=NULL;
76 
77  if(M)idxm=xNew<int>(M);
78  if(N)idxn=xNew<int>(N);
79 
80  for(i=0;i<M;i++)idxm[i]=i;
81  for(i=0;i<N;i++)idxn[i]=i;
82 
83  this->matrix=NewMat(M,N,sparsity,IssmComm::GetComm());
84  MatSetValues(this->matrix,M,idxm,N,idxn,serial_mat,INSERT_VALUES);
85  MatAssemblyBegin(this->matrix,MAT_FINAL_ASSEMBLY);
86  MatAssemblyEnd(this->matrix,MAT_FINAL_ASSEMBLY);
87 
88  xDelete<int>(idxm);
89  xDelete<int>(idxn);
90 
91 }
92 /*}}}*/
93 PetscMat::PetscMat(int M,int N, int connectivity,int numberofdofspernode){/*{{{*/
94 
95  this->matrix=NewMat(M,N,connectivity,numberofdofspernode,IssmComm::GetComm());
96 
97 }
98 /*}}}*/
100  MatFree(&this->matrix);
101 }
102 /*}}}*/
103 
104 /*PetscMat specific routines: */
105 void PetscMat::AllocationInfo(void){/*{{{*/
106 
107  MatInfo info;
108  MatGetInfo(this->matrix,MAT_GLOBAL_SUM,&info);
109  _printf0_("=========================== Stiffness matrix allocation info ===========================\n");
110  _printf0_("\n");
111  _printf0_(" Block size : "<<info.block_size << "\n");
112  _printf0_(" nz_allocated: "<<info.nz_allocated << "\n");
113  _printf0_(" nz_used : "<<info.nz_used << "\n");
114  _printf0_(" nz_unneeded : "<<info.nz_unneeded<<" ("<<double(info.nz_unneeded)/double(info.nz_allocated)*100.<<"%)\n");
115  _printf0_("\n");
116  _printf0_("========================================================================================\n");
117 }
118 /*}}}*/
119 void PetscMat::Echo(void){/*{{{*/
120 
121  MatView(this->matrix,PETSC_VIEWER_STDOUT_WORLD);
122 }
123 /*}}}*/
124 void PetscMat::Assemble(void){/*{{{*/
125 
126  _assert_(this->matrix);
127  MatAssemblyBegin(this->matrix,MAT_FINAL_ASSEMBLY);
128  MatAssemblyEnd(this->matrix,MAT_FINAL_ASSEMBLY);
129 
130 }
131 /*}}}*/
133 
134  IssmDouble norm=0;
135  _assert_(this->matrix);
136  MatNorm(this->matrix,ISSMToPetscNormMode(mode),&norm);
137 
138  return norm;
139 
140 }
141 /*}}}*/
142 void PetscMat::GetSize(int* pM,int* pN){/*{{{*/
143 
144  _assert_(this->matrix);
145  MatGetSize(this->matrix,pM,pN);
146 }
147 /*}}}*/
148 void PetscMat::GetLocalSize(int* pM,int* pN){/*{{{*/
149 
150  _assert_(this->matrix);
151  MatGetLocalSize(this->matrix,pM,pN);
152 
153 }
154 /*}}}*/
155 void PetscMat::MatMult(PetscVec* X,PetscVec* AX){/*{{{*/
156 
157  _assert_(this->matrix);
158  _assert_(X->vector);
160 
161 }
162 /*}}}*/
164 
165  PetscMat* output=new PetscMat();
166  _assert_(this->matrix);
167  MatDuplicate(this->matrix,MAT_COPY_VALUES,&output->matrix);
168 
169  return output;
170 
171 }
172 /*}}}*/
174 
175  IssmDouble* output=NULL;
176 
177  MatToSerial(&output,this->matrix,IssmComm::GetComm());
178  return output;
179 
180 }
181 /*}}}*/
182 void PetscMat::SetValues(int m,int* idxm,int n,int* idxn,IssmDouble* values,InsMode mode){/*{{{*/
183 
184  PetscErrorCode ierr = MatSetValues(this->matrix,m,idxm,n,idxn,values,ISSMToPetscInsertMode(mode));
185  if(ierr) _error_("PETSc's MatSetValues reported an error");
186 
187 }
188 /*}}}*/
189 void PetscMat::Convert(MatrixType type){/*{{{*/
190 
191  MatConvert(this->matrix,ISSMToPetscMatrixType(type),MAT_REUSE_MATRIX,&this->matrix);
192 
193 }
194 /*}}}*/
195 void PetscMat::SetZero(void){/*{{{*/
196  MatZeroEntries(this->matrix);
197 }
198 /*}}}*/
PetscMat::~PetscMat
~PetscMat()
Definition: PetscMat.cpp:99
NewMat
Mat NewMat(int M, int N, ISSM_MPI_Comm comm)
Definition: NewMat.cpp:21
_assert_
#define _assert_(ignore)
Definition: exceptions.h:37
IssmDouble
double IssmDouble
Definition: types.h:37
PetscMat::AllocationInfo
void AllocationInfo(void)
Definition: PetscMat.cpp:105
_printf0_
#define _printf0_(StreamArgs)
Definition: Print.h:29
IssmComm::GetComm
static ISSM_MPI_Comm GetComm(void)
Definition: IssmComm.cpp:30
ISSMToPetscMatrixType
MatType ISSMToPetscMatrixType(MatrixType type)
Definition: ISSMToPetscMatrixType.cpp:20
PetscVec::vector
Vec vector
Definition: PetscVec.h:25
PetscMat::SetZero
void SetZero(void)
Definition: PetscMat.cpp:195
PetscMat::GetLocalSize
void GetLocalSize(int *pM, int *pN)
Definition: PetscMat.cpp:148
PetscMat::MatMult
void MatMult(PetscVec *X, PetscVec *AX)
Definition: PetscMat.cpp:155
ISSMToPetscNormMode
NormType ISSMToPetscNormMode(NormMode mode)
Definition: ISSMToPetscNormMode.cpp:20
PetscMat::ToSerial
IssmDouble * ToSerial(void)
Definition: PetscMat.cpp:173
MatrixType
MatrixType
Definition: toolkitsenums.h:16
PetscMat::Norm
IssmDouble Norm(NormMode norm_type)
Definition: PetscMat.cpp:132
MatMultPatch
void MatMultPatch(Mat A, Vec X, Vec AX, ISSM_MPI_Comm comm)
Definition: MatMultPatch.cpp:23
MatToSerial
void MatToSerial(double **poutmatrix, Mat matrix, ISSM_MPI_Comm comm)
Definition: MatToSerial.cpp:14
PetscMat::SetValues
void SetValues(int m, int *idxm, int n, int *idxn, IssmDouble *values, InsMode mode)
Definition: PetscMat.cpp:182
NormMode
NormMode
Definition: toolkitsenums.h:15
MatFree
void MatFree(Mat *pmat)
Definition: MatFree.cpp:16
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
ISSMToPetscInsertMode
InsertMode ISSMToPetscInsertMode(InsMode mode)
Definition: ISSMToPetscInsertMode.cpp:20
PetscVec
Definition: PetscVec.h:22
PetscMat::GetSize
void GetSize(int *pM, int *pN)
Definition: PetscMat.cpp:142
InsMode
InsMode
Definition: toolkitsenums.h:14
PetscMat::Assemble
void Assemble(void)
Definition: PetscMat.cpp:124
PetscMat::Echo
void Echo(void)
Definition: PetscMat.cpp:119
PetscMat::matrix
Mat matrix
Definition: PetscMat.h:27
PetscMat::Convert
void Convert(MatrixType type)
Definition: PetscMat.cpp:189
PetscMat
Definition: PetscMat.h:24
PetscMat::PetscMat
PetscMat()
Definition: PetscMat.cpp:21
PetscMat::Duplicate
PetscMat * Duplicate(void)
Definition: PetscMat.cpp:163