Actual source code: aij.h


 5:  #include private/matimpl.h

  7: /*  
  8:     Struct header shared by SeqAIJ, SeqBAIJ and SeqSBAIJ matrix formats
  9: */
 10: #define SEQAIJHEADER(datatype)        \
 11:   PetscTruth        roworiented;      /* if true, row-oriented input, default */\
 12:   PetscInt          nonew;            /* 1 don't add new nonzeros, -1 generate error on new */\
 13:   PetscInt          nounused;         /* -1 generate error on unused space */\
 14:   PetscTruth        singlemalloc;     /* if true a, i, and j have been obtained with one big malloc */\
 15:   PetscInt          maxnz;            /* allocated nonzeros */\
 16:   PetscInt          *imax;            /* maximum space allocated for each row */\
 17:   PetscInt          *ilen;            /* actual length of each row */\
 18:   PetscTruth        free_imax_ilen;  \
 19:   PetscInt          reallocs;         /* number of mallocs done during MatSetValues() \
 20:                                         as more values are set than were prealloced */\
 21:   PetscInt          rmax;             /* max nonzeros in any row */\
 22:   PetscTruth        keepnonzeropattern;   /* keeps matrix structure same in calls to MatZeroRows()*/\
 23:   PetscTruth        ignorezeroentries; \
 24:   PetscInt          *xtoy,*xtoyB;     /* map nonzero pattern of X into Y's, used by MatAXPY() */\
 25:   Mat               XtoY;             /* used by MatAXPY() */\
 26:   PetscTruth        free_ij;          /* free the column indices j and row offsets i when the matrix is destroyed */ \
 27:   PetscTruth        free_a;           /* free the numerical values when matrix is destroy */ \
 28:   Mat_CompressedRow compressedrow;    /* use compressed row format */                      \
 29:   PetscInt          nz;               /* nonzeros */                                       \
 30:   PetscInt          *i;               /* pointer to beginning of each row */               \
 31:   PetscInt          *j;               /* column values: j + i[k] - 1 is start of row k */  \
 32:   PetscInt          *diag;            /* pointers to diagonal elements */                  \
 33:   PetscTruth        free_diag;         \
 34:   datatype          *a;               /* nonzero elements */                               \
 35:   PetscScalar       *solve_work;      /* work space used in MatSolve */                    \
 36:   IS                row, col, icol;   /* index sets, used for reorderings */ \
 37:   PetscTruth        pivotinblocks;    /* pivot inside factorization of each diagonal block */ \
 38:   Mat               parent             /* set if this matrix was formed with MatDuplicate(...,MAT_SHARE_NONZERO_PATTERN,....); 
 39:                                          means that this shares some data structures with the parent including diag, ilen, imax, i, j */

 41: /*  
 42:   MATSEQAIJ format - Compressed row storage (also called Yale sparse matrix
 43:   format) or compressed sparse row (CSR).  The i[] and j[] arrays start at 0. For example,
 44:   j[i[k]+p] is the pth column in row k.  Note that the diagonal
 45:   matrix elements are stored with the rest of the nonzeros (not separately).
 46: */

 48: /* Info about i-nodes (identical nodes) helper class for SeqAIJ */
 49: typedef struct {
 50:   MatScalar   *bdiag,*ibdiag,*ssor_work;      /* diagonal blocks of matrix used for MatSOR_SeqAIJ_Inode() */
 51:   PetscInt    bdiagsize;                       /* length of bdiag and ibdiag */
 52:   PetscTruth  ibdiagvalid;                     /* do ibdiag[] and bdiag[] contain the most recent values */

 54:   PetscTruth use;
 55:   PetscInt   node_count;                    /* number of inodes */
 56:   PetscInt   *size;                         /* size of each inode */
 57:   PetscInt   limit;                         /* inode limit */
 58:   PetscInt   max_limit;                     /* maximum supported inode limit */
 59:   PetscTruth checked;                       /* if inodes have been checked for */
 60: } Mat_SeqAIJ_Inode;

 62: EXTERN PetscErrorCode MatView_SeqAIJ_Inode(Mat,PetscViewer);
 63: EXTERN PetscErrorCode MatAssemblyEnd_SeqAIJ_Inode(Mat,MatAssemblyType);
 64: EXTERN PetscErrorCode MatDestroy_SeqAIJ_Inode(Mat);
 65: EXTERN PetscErrorCode MatCreate_SeqAIJ_Inode(Mat);
 66: EXTERN PetscErrorCode MatSetOption_SeqAIJ_Inode(Mat,MatOption,PetscTruth);
 67: EXTERN PetscErrorCode MatDuplicate_SeqAIJ_Inode(Mat,MatDuplicateOption,Mat*);
 68: EXTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ_Inode_inplace(Mat,Mat,const MatFactorInfo*);
 69: EXTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ_Inode(Mat,Mat,const MatFactorInfo*);

 71: typedef struct {
 72:   SEQAIJHEADER(MatScalar);
 73:   Mat_SeqAIJ_Inode inode;
 74:   MatScalar        *saved_values;             /* location for stashing nonzero values of matrix */

 76:   PetscScalar      *idiag,*mdiag,*ssor_work;  /* inverse of diagonal entries, diagonal values and workspace for Eisenstat trick */
 77:   PetscTruth       idiagvalid;                     /* current idiag[] and mdiag[] are valid */
 78:   PetscScalar      fshift,omega;                   /* last used omega and fshift */

 80:   ISColoring       coloring;                  /* set with MatADSetColoring() used by MatADSetValues() */
 81: } Mat_SeqAIJ;

 83: /*
 84:     Frees the a, i, and j arrays from the XAIJ (AIJ, BAIJ, and SBAIJ) matrix types
 85: */
 88: PETSC_STATIC_INLINE PetscErrorCode MatSeqXAIJFreeAIJ(Mat AA,MatScalar **a,PetscInt **j,PetscInt **i)
 89: {
 91:                                      Mat_SeqAIJ     *A = (Mat_SeqAIJ*) AA->data;
 92:                                      if (A->singlemalloc) {
 93:                                        PetscFree3(*a,*j,*i);
 94:                                      } else {
 95:                                        if (A->free_a  && *a) {PetscFree(*a);}
 96:                                        if (A->free_ij && *j) {PetscFree(*j);}
 97:                                        if (A->free_ij && *i) {PetscFree(*i);}
 98:                                      }
 99:                                      *a = 0; *j = 0; *i = 0;
100:                                      return 0;
101:                                    }

103: /*
104:     Allocates larger a, i, and j arrays for the XAIJ (AIJ, BAIJ, and SBAIJ) matrix types
105: */
106: #define MatSeqXAIJReallocateAIJ(Amat,AM,BS2,NROW,ROW,COL,RMAX,AA,AI,AJ,RP,AP,AIMAX,NONEW,datatype) \
107:   if (NROW >= RMAX) {\
108:         Mat_SeqAIJ *Ain = (Mat_SeqAIJ*)Amat->data;\
109:         /* there is no extra room in row, therefore enlarge */ \
110:         PetscInt   new_nz = AI[AM] + CHUNKSIZE,len,*new_i=0,*new_j=0; \
111:         datatype   *new_a; \
112:  \
113:         if (NONEW == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"New nonzero at (%D,%D) caused a malloc",ROW,COL); \
114:         /* malloc new storage space */ \
115:         PetscMalloc3(BS2*new_nz,datatype,&new_a,new_nz,PetscInt,&new_j,AM+1,PetscInt,&new_i);\
116:  \
117:         /* copy over old data into new slots */ \
118:         for (ii=0; ii<ROW+1; ii++) {new_i[ii] = AI[ii];} \
119:         for (ii=ROW+1; ii<AM+1; ii++) {new_i[ii] = AI[ii]+CHUNKSIZE;} \
120:         PetscMemcpy(new_j,AJ,(AI[ROW]+NROW)*sizeof(PetscInt)); \
121:         len = (new_nz - CHUNKSIZE - AI[ROW] - NROW); \
122:         PetscMemcpy(new_j+AI[ROW]+NROW+CHUNKSIZE,AJ+AI[ROW]+NROW,len*sizeof(PetscInt)); \
123:         PetscMemcpy(new_a,AA,BS2*(AI[ROW]+NROW)*sizeof(datatype)); \
124:         PetscMemzero(new_a+BS2*(AI[ROW]+NROW),BS2*CHUNKSIZE*sizeof(datatype));\
125:         PetscMemcpy(new_a+BS2*(AI[ROW]+NROW+CHUNKSIZE),AA+BS2*(AI[ROW]+NROW),BS2*len*sizeof(datatype));  \
126:         /* free up old matrix storage */ \
127:         MatSeqXAIJFreeAIJ(A,&Ain->a,&Ain->j,&Ain->i);\
128:         AA = new_a; \
129:         Ain->a = (MatScalar*) new_a;                   \
130:         AI = Ain->i = new_i; AJ = Ain->j = new_j;  \
131:         Ain->singlemalloc = PETSC_TRUE; \
132:  \
133:         RP          = AJ + AI[ROW]; AP = AA + BS2*AI[ROW]; \
134:         RMAX        = AIMAX[ROW] = AIMAX[ROW] + CHUNKSIZE; \
135:         Ain->maxnz += CHUNKSIZE; \
136:         Ain->reallocs++; \
137:       } \

140: EXTERN PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat,PetscInt,PetscInt*);
142: EXTERN PetscErrorCode MatILUFactorSymbolic_SeqAIJ_inplace(Mat,Mat,IS,IS,const MatFactorInfo*);
143: EXTERN PetscErrorCode MatILUFactorSymbolic_SeqAIJ(Mat,Mat,IS,IS,const MatFactorInfo*);
144: EXTERN PetscErrorCode MatILUFactorSymbolic_SeqAIJ_ilu0(Mat,Mat,IS,IS,const MatFactorInfo*);

146: EXTERN PetscErrorCode MatICCFactorSymbolic_SeqAIJ_inplace(Mat,Mat,IS,const MatFactorInfo*);
147: EXTERN PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat,Mat,IS,const MatFactorInfo*);
148: EXTERN PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ_inplace(Mat,Mat,IS,const MatFactorInfo*);
149: EXTERN PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat,Mat,IS,const MatFactorInfo*);
150: EXTERN PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ_inplace(Mat,Mat,const MatFactorInfo*);
151: EXTERN PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ(Mat,Mat,const MatFactorInfo*);
152: EXTERN PetscErrorCode MatDuplicate_SeqAIJ(Mat,MatDuplicateOption,Mat*);
153: EXTERN PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat,PetscTruth*,PetscInt*);
154: EXTERN PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat);

156: EXTERN PetscErrorCode MatMult_SeqAIJ(Mat A,Vec,Vec);
157: EXTERN PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec,Vec,Vec);
158: EXTERN PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec,Vec);
159: EXTERN PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec,Vec,Vec);
160: EXTERN PetscErrorCode MatSOR_SeqAIJ(Mat,Vec,PetscReal,MatSORType,PetscReal,PetscInt,PetscInt,Vec);

162: EXTERN PetscErrorCode MatSetColoring_SeqAIJ(Mat,ISColoring);
163: EXTERN PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat,void*);
164: EXTERN PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat,PetscInt,void*);

166: EXTERN PetscErrorCode MatGetSymbolicTranspose_SeqAIJ(Mat,PetscInt *[],PetscInt *[]);
167: EXTERN PetscErrorCode MatGetSymbolicTransposeReduced_SeqAIJ(Mat,PetscInt,PetscInt,PetscInt *[],PetscInt *[]);
168: EXTERN PetscErrorCode MatRestoreSymbolicTranspose_SeqAIJ(Mat,PetscInt *[],PetscInt *[]);
169: EXTERN PetscErrorCode MatToSymmetricIJ_SeqAIJ(PetscInt,PetscInt*,PetscInt*,PetscInt,PetscInt,PetscInt**,PetscInt**);
170: EXTERN PetscErrorCode MatLUFactorSymbolic_SeqAIJ_inplace(Mat,Mat,IS,IS,const MatFactorInfo*);
171: EXTERN PetscErrorCode MatLUFactorSymbolic_SeqAIJ(Mat,Mat,IS,IS,const MatFactorInfo*);
172: EXTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ_inplace(Mat,Mat,const MatFactorInfo*);
173: EXTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ(Mat,Mat,const MatFactorInfo*);
174: EXTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(Mat,Mat,const MatFactorInfo*);
175: EXTERN PetscErrorCode MatLUFactor_SeqAIJ(Mat,IS,IS,const MatFactorInfo*);
176: EXTERN PetscErrorCode MatSolve_SeqAIJ_inplace(Mat,Vec,Vec);
177: EXTERN PetscErrorCode MatSolve_SeqAIJ(Mat,Vec,Vec);
178: EXTERN PetscErrorCode MatSolve_SeqAIJ_Inode_inplace(Mat,Vec,Vec);
179: EXTERN PetscErrorCode MatSolve_SeqAIJ_Inode(Mat,Vec,Vec);
180: EXTERN PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_inplace(Mat,Vec,Vec);
181: EXTERN PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering(Mat,Vec,Vec);
182: EXTERN PetscErrorCode MatSolve_SeqAIJ_InplaceWithPerm(Mat,Vec,Vec);
183: EXTERN PetscErrorCode MatSolveAdd_SeqAIJ_inplace(Mat,Vec,Vec,Vec);
184: EXTERN PetscErrorCode MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec);
185: EXTERN PetscErrorCode MatSolveTranspose_SeqAIJ_inplace(Mat,Vec,Vec);
186: EXTERN PetscErrorCode MatSolveTranspose_SeqAIJ(Mat,Vec,Vec);
187: EXTERN PetscErrorCode MatSolveTransposeAdd_SeqAIJ_inplace(Mat,Vec,Vec,Vec);
188: EXTERN PetscErrorCode MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec);
189: EXTERN PetscErrorCode MatMatSolve_SeqAIJ_inplace(Mat,Mat,Mat);
190: EXTERN PetscErrorCode MatMatSolve_SeqAIJ(Mat,Mat,Mat);
191: EXTERN PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg);
192: EXTERN PetscErrorCode MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring);
193: EXTERN PetscErrorCode MatLoad_SeqAIJ(PetscViewer, const MatType,Mat*);
194: EXTERN PetscErrorCode RegisterApplyPtAPRoutines_Private(Mat);
195: EXTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ(Mat,Mat,PetscReal,Mat*);
196: EXTERN PetscErrorCode MatMatMultNumeric_SeqAIJ_SeqAIJ(Mat,Mat,Mat);
197: EXTERN PetscErrorCode MatPtAPSymbolic_SeqAIJ(Mat,Mat,PetscReal,Mat*);
198: EXTERN PetscErrorCode MatPtAPNumeric_SeqAIJ(Mat,Mat,Mat);
199: EXTERN PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ(Mat,Mat,PetscReal,Mat*);
200: EXTERN PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat,Mat,Mat);
201: EXTERN PetscErrorCode MatMatMultTranspose_SeqAIJ_SeqAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);
202: EXTERN PetscErrorCode MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ(Mat,Mat,PetscReal,Mat*);
203: EXTERN PetscErrorCode MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ(Mat,Mat,Mat);
204: EXTERN PetscErrorCode MatSetValues_SeqAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
205: EXTERN PetscErrorCode MatGetRow_SeqAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**);
206: EXTERN PetscErrorCode MatRestoreRow_SeqAIJ(Mat,PetscInt,PetscInt*,PetscInt**,PetscScalar**);
207: EXTERN PetscErrorCode MatAXPY_SeqAIJ(Mat,PetscScalar,Mat,MatStructure);
208: EXTERN PetscErrorCode MatGetRowIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscTruth,PetscInt*,PetscInt *[],PetscInt *[],PetscTruth *);
209: EXTERN PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscTruth,PetscInt *,PetscInt *[],PetscInt *[],PetscTruth *);
210: EXTERN PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscTruth,PetscInt*,PetscInt *[],PetscInt *[],PetscTruth *);
211: EXTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat,PetscInt,PetscTruth,PetscTruth,PetscInt *,PetscInt *[],PetscInt *[],PetscTruth *);
212: EXTERN PetscErrorCode MatDestroy_SeqAIJ(Mat);
213: EXTERN PetscErrorCode MatView_SeqAIJ(Mat,PetscViewer);

215: EXTERN PetscErrorCode Mat_CheckInode(Mat,PetscTruth);
216: EXTERN PetscErrorCode Mat_CheckInode_FactorLU(Mat,PetscTruth);

219: EXTERN PetscErrorCode  MatConvert_SeqAIJ_SeqSBAIJ(Mat,const MatType,MatReuse,Mat*);
220: EXTERN PetscErrorCode  MatConvert_SeqAIJ_SeqBAIJ(Mat,const MatType,MatReuse,Mat*);
221: EXTERN PetscErrorCode  MatConvert_SeqAIJ_SeqCSRPERM(Mat,const MatType,MatReuse,Mat*);
222: EXTERN PetscErrorCode  MatReorderForNonzeroDiagonal_SeqAIJ(Mat,PetscReal,IS,IS);
223: EXTERN PetscErrorCode  MatMatMult_SeqAIJ_SeqAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);

226: /*
227:     PetscSparseDenseMinusDot - The inner kernel of triangular solves and Gauss-Siedel smoothing. \sum_i xv[i] * r[xi[i]] for CSR storage

229:   Input Parameters:
230: +  nnz - the number of entries
231: .  r - the array of vector values
232: .  xv - the matrix values for the row
233: -  xi - the column indices of the nonzeros in the row

235:   Output Parameter:
236: .  sum - negative the sum of results

238:   PETSc compile flags:
239: +   PETSC_KERNEL_USE_UNROLL_4 -   don't use this; it changes nnz and hence is WRONG
240: -   PETSC_KERNEL_USE_UNROLL_2 -

242: .seealso: PetscSparseDensePlusDot()

244: */
245: #ifdef PETSC_KERNEL_USE_UNROLL_4
246: #define PetscSparseDenseMinusDot(sum,r,xv,xi,nnz) {\
247: if (nnz > 0) {\
248: switch (nnz & 0x3) {\
249: case 3: sum -= *xv++ * r[*xi++];\
250: case 2: sum -= *xv++ * r[*xi++];\
251: case 1: sum -= *xv++ * r[*xi++];\
252: nnz -= 4;}\
253: while (nnz > 0) {\
254: sum -=  xv[0] * r[xi[0]] - xv[1] * r[xi[1]] -\
255:         xv[2] * r[xi[2]] - xv[3] * r[xi[3]];\
256: xv  += 4; xi += 4; nnz -= 4; }}}

258: #elif defined(PETSC_KERNEL_USE_UNROLL_2)
259: #define PetscSparseDenseMinusDot(sum,r,xv,xi,nnz) {\
260: PetscInt __i,__i1,__i2;\
261: for(__i=0;__i<nnz-1;__i+=2) {__i1 = xi[__i]; __i2=xi[__i+1];\
262: sum -= (xv[__i]*r[__i1] + xv[__i+1]*r[__i2]);}\
263: if (nnz & 0x1) sum -= xv[__i] * r[xi[__i]];}

265: #else
266: #define PetscSparseDenseMinusDot(sum,r,xv,xi,nnz) {\
267: PetscInt __i;\
268: for(__i=0;__i<nnz;__i++) sum -= xv[__i] * r[xi[__i]];}
269: #endif



273: /*
274:     PetscSparseDensePlusDot - The inner kernel of matrix-vector product \sum_i xv[i] * r[xi[i]] for CSR storage

276:   Input Parameters:
277: +  nnz - the number of entries
278: .  r - the array of vector values
279: .  xv - the matrix values for the row
280: -  xi - the column indices of the nonzeros in the row

282:   Output Parameter:
283: .  sum - the sum of results

285:   PETSc compile flags:
286: +   PETSC_KERNEL_USE_UNROLL_4 -  don't use this; it changes nnz and hence is WRONG
287: -   PETSC_KERNEL_USE_UNROLL_2 -

289: .seealso: PetscSparseDenseMinusDot()

291: */
292: #ifdef PETSC_KERNEL_USE_UNROLL_4
293: #define PetscSparseDensePlusDot(sum,r,xv,xi,nnz) {\
294: if (nnz > 0) {\
295: switch (nnz & 0x3) {\
296: case 3: sum += *xv++ * r[*xi++];\
297: case 2: sum += *xv++ * r[*xi++];\
298: case 1: sum += *xv++ * r[*xi++];\
299: nnz -= 4;}\
300: while (nnz > 0) {\
301: sum +=  xv[0] * r[xi[0]] + xv[1] * r[xi[1]] +\
302:         xv[2] * r[xi[2]] + xv[3] * r[xi[3]];\
303: xv  += 4; xi += 4; nnz -= 4; }}}

305: #elif defined(PETSC_KERNEL_USE_UNROLL_2)
306: #define PetscSparseDensePlusDot(sum,r,xv,xi,nnz) {\
307: PetscInt __i,__i1,__i2;\
308: for(__i=0;__i<nnz-1;__i+=2) {__i1 = xi[__i]; __i2=xi[__i+1];\
309: sum += (xv[__i]*r[__i1] + xv[__i+1]*r[__i2]);}\
310: if (nnz & 0x1) sum += xv[__i] * r[xi[__i]];}

312: #else
313: #define PetscSparseDensePlusDot(sum,r,xv,xi,nnz) {\
314:  PetscInt __i;\
315: for(__i=0;__i<nnz;__i++) sum += xv[__i] * r[xi[__i]];}
316: #endif

318: #endif