Ice Sheet System Model  4.18
Code documentation
NewMat.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 "./petscpatches.h"
17 #include "../../../shared/shared.h"
18 #include "../../mpi/issmmpi.h"
19 
20 /*NewMat(int M,int N){{{*/
21 Mat NewMat(int M,int N,ISSM_MPI_Comm comm){
22 
23  /*output:*/
24  Mat outmatrix=NULL;
25 
26  /*parameters: */
27  double sparsity=0.001; //default
28  int m,n;
29  int d_nz,o_nz,nnz;
30 
31  /*Determine local sizes: */
32  m=DetermineLocalSize(M,comm);
33  n=DetermineLocalSize(N,comm);
34 
35  nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
36  d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
37  o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
38 
39  #if _PETSC_MAJOR_ == 3 && _PETSC_MINOR_ > 2
40  MatCreateAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
41  #else
42  MatCreateMPIAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
43  #endif
44 
45  return outmatrix;
46 }
47 /*}}}*/
48 /*NewMat(int M,int N,double sparsity,ISSM_MPI_Comm comm){{{*/
49 Mat NewMat(int M,int N,double sparsity,ISSM_MPI_Comm comm){
50 
51  /*output:*/
52  Mat outmatrix=NULL;
53 
54  /*parameters: */
55  int m,n;
56  int d_nz,o_nz;
57  int nnz;
58 
59  /*Determine local sizes: */
60  m=DetermineLocalSize(M,comm);
61  n=DetermineLocalSize(N,comm);
62 
63  nnz=(int)((double)M*(double)N*sparsity); //number of non zeros.
64  d_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
65  o_nz=(int)((double)nnz/(double)M/2.0); //number of non zeros per row/2
66 
67  #if _PETSC_MAJOR_ == 3 && _PETSC_MINOR_ > 2
68  if(sparsity==1){
69  MatCreateDense(comm,m,n,M,N,NULL,&outmatrix);
70  }
71  else{
72  MatCreateAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
73  }
74  #else
75  MatCreateMPIAIJ(comm,m,n,M,N,d_nz,NULL,o_nz,NULL,&outmatrix);
76  #endif
77 
78  return outmatrix;
79 }
80 /*}}}*/
81 /*NewMat(int M,int N,int connectivity,int numberofdofspernode){{{*/
82 Mat NewMat(int M,int N,int connectivity,int numberofdofspernode,ISSM_MPI_Comm comm){
83 
84  /*output:*/
85  Mat outmatrix=NULL;
86 
87  /*parameters: */
88  int m,n;
89  int d_nz,o_nz;
90 
91  #if _PETSC_MAJOR_ >= 3
92  #if defined(_HAVE_PETSCDEV_) || _PETSC_MINOR_ >=4
93  MatType type;
94  #else
95  const MatType type;
96  #endif
97  #else
98  MatType type;
99  #endif
100 
101  /*Determine local sizes: */
102  m=DetermineLocalSize(M,comm);
103  n=DetermineLocalSize(N,comm);
104 
105  /*Figure out number of non zeros per row: */
106  d_nz=(int)connectivity*numberofdofspernode/2;
107  o_nz=(int)connectivity*numberofdofspernode/2;
108 
109  MatCreate(comm,&outmatrix);
110  MatSetSizes(outmatrix,m,n,M,N);
111  MatSetFromOptions(outmatrix);
112 
113  /*preallocation according to type: */
114  MatGetType(outmatrix,&type);
115 
116  if((strcmp(type,"mpiaij")==0) || (strcmp(type,"mpidense")==0)){
117  MatMPIAIJSetPreallocation(outmatrix,d_nz,NULL,o_nz,NULL);
118  }
119 
120  return outmatrix;
121 }
122 /*}}}*/
NewMat
Mat NewMat(int M, int N, ISSM_MPI_Comm comm)
Definition: NewMat.cpp:21
DetermineLocalSize
int DetermineLocalSize(int global_size, ISSM_MPI_Comm comm)
Definition: DetermineLocalSize.cpp:9
petscpatches.h
ISSM_MPI_Comm
int ISSM_MPI_Comm
Definition: issmmpi.h:118