1 /* 2 Defines basic operations for the MATSEQAIJSELL matrix class. 3 This class is derived from the MATAIJCLASS, but maintains a "shadow" copy 4 of the matrix stored in MATSEQSELL format, which is used as appropriate for 5 performing operations for which this format is more suitable. 6 */ 7 8 #include <../src/mat/impls/aij/seq/aij.h> 9 #include <../src/mat/impls/sell/seq/sell.h> 10 11 typedef struct { 12 Mat S; /* The SELL formatted "shadow" matrix. */ 13 PetscBool eager_shadow; 14 PetscObjectState state; /* State of the matrix when shadow matrix was last constructed. */ 15 } Mat_SeqAIJSELL; 16 17 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJSELL_SeqAIJ(Mat A,MatType type,MatReuse reuse,Mat *newmat) 18 { 19 /* This routine is only called to convert a MATAIJSELL to its base PETSc type, */ 20 /* so we will ignore 'MatType type'. */ 21 Mat B = *newmat; 22 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*) A->spptr; 23 24 PetscFunctionBegin; 25 if (reuse == MAT_INITIAL_MATRIX) { 26 PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&B)); 27 } 28 29 /* Reset the original function pointers. */ 30 B->ops->duplicate = MatDuplicate_SeqAIJ; 31 B->ops->assemblyend = MatAssemblyEnd_SeqAIJ; 32 B->ops->destroy = MatDestroy_SeqAIJ; 33 B->ops->mult = MatMult_SeqAIJ; 34 B->ops->multtranspose = MatMultTranspose_SeqAIJ; 35 B->ops->multadd = MatMultAdd_SeqAIJ; 36 B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJ; 37 B->ops->sor = MatSOR_SeqAIJ; 38 39 PetscCall(PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijsell_seqaij_C",NULL)); 40 41 if (reuse == MAT_INITIAL_MATRIX) aijsell = (Mat_SeqAIJSELL*)B->spptr; 42 43 /* Clean up the Mat_SeqAIJSELL data structure. 44 * Note that MatDestroy() simply returns if passed a NULL value, so it's OK to call even if the shadow matrix was never constructed. */ 45 PetscCall(MatDestroy(&aijsell->S)); 46 PetscCall(PetscFree(B->spptr)); 47 48 /* Change the type of B to MATSEQAIJ. */ 49 PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ)); 50 51 *newmat = B; 52 PetscFunctionReturn(0); 53 } 54 55 PetscErrorCode MatDestroy_SeqAIJSELL(Mat A) 56 { 57 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*) A->spptr; 58 59 PetscFunctionBegin; 60 61 /* If MatHeaderMerge() was used, then this SeqAIJSELL matrix will not have an 62 * spptr pointer. */ 63 if (aijsell) { 64 /* Clean up everything in the Mat_SeqAIJSELL data structure, then free A->spptr. */ 65 PetscCall(MatDestroy(&aijsell->S)); 66 PetscCall(PetscFree(A->spptr)); 67 } 68 69 /* Change the type of A back to SEQAIJ and use MatDestroy_SeqAIJ() 70 * to destroy everything that remains. */ 71 PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATSEQAIJ)); 72 /* Note that I don't call MatSetType(). I believe this is because that 73 * is only to be called when *building* a matrix. I could be wrong, but 74 * that is how things work for the SuperLU matrix class. */ 75 PetscCall(PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaijsell_seqaij_C",NULL)); 76 PetscCall(MatDestroy_SeqAIJ(A)); 77 PetscFunctionReturn(0); 78 } 79 80 /* Build or update the shadow matrix if and only if needed. 81 * We track the ObjectState to determine when this needs to be done. */ 82 PETSC_INTERN PetscErrorCode MatSeqAIJSELL_build_shadow(Mat A) 83 { 84 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*) A->spptr; 85 PetscObjectState state; 86 87 PetscFunctionBegin; 88 PetscCall(PetscObjectStateGet((PetscObject)A,&state)); 89 if (aijsell->S && aijsell->state == state) { 90 /* The existing shadow matrix is up-to-date, so simply exit. */ 91 PetscFunctionReturn(0); 92 } 93 94 PetscCall(PetscLogEventBegin(MAT_Convert,A,0,0,0)); 95 if (aijsell->S) { 96 PetscCall(MatConvert_SeqAIJ_SeqSELL(A,MATSEQSELL,MAT_REUSE_MATRIX,&aijsell->S)); 97 } else { 98 PetscCall(MatConvert_SeqAIJ_SeqSELL(A,MATSEQSELL,MAT_INITIAL_MATRIX,&aijsell->S)); 99 } 100 PetscCall(PetscLogEventEnd(MAT_Convert,A,0,0,0)); 101 102 /* Record the ObjectState so that we can tell when the shadow matrix needs updating */ 103 PetscCall(PetscObjectStateGet((PetscObject)A,&aijsell->state)); 104 105 PetscFunctionReturn(0); 106 } 107 108 PetscErrorCode MatDuplicate_SeqAIJSELL(Mat A, MatDuplicateOption op, Mat *M) 109 { 110 Mat_SeqAIJSELL *aijsell; 111 Mat_SeqAIJSELL *aijsell_dest; 112 113 PetscFunctionBegin; 114 PetscCall(MatDuplicate_SeqAIJ(A,op,M)); 115 aijsell = (Mat_SeqAIJSELL*) A->spptr; 116 aijsell_dest = (Mat_SeqAIJSELL*) (*M)->spptr; 117 PetscCall(PetscArraycpy(aijsell_dest,aijsell,1)); 118 /* We don't duplicate the shadow matrix -- that will be constructed as needed. */ 119 aijsell_dest->S = NULL; 120 if (aijsell->eager_shadow) PetscCall(MatSeqAIJSELL_build_shadow(A)); 121 PetscFunctionReturn(0); 122 } 123 124 PetscErrorCode MatAssemblyEnd_SeqAIJSELL(Mat A, MatAssemblyType mode) 125 { 126 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 127 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*)A->spptr; 128 129 PetscFunctionBegin; 130 if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 131 132 /* I disable the use of the inode routines so that the AIJSELL ones will be 133 * used instead, but I wonder if it might make sense (and is feasible) to 134 * use some of them. */ 135 a->inode.use = PETSC_FALSE; 136 137 /* Since a MATSEQAIJSELL matrix is really just a MATSEQAIJ with some 138 * extra information and some different methods, call the AssemblyEnd 139 * routine for a MATSEQAIJ. 140 * I'm not sure if this is the best way to do this, but it avoids 141 * a lot of code duplication. */ 142 143 PetscCall(MatAssemblyEnd_SeqAIJ(A, mode)); 144 145 /* If the user has requested "eager" shadowing, create the SELL shadow matrix (if needed; the function checks). 146 * (The default is to take a "lazy" approach, deferring this until something like MatMult() is called.) */ 147 if (aijsell->eager_shadow) PetscCall(MatSeqAIJSELL_build_shadow(A)); 148 149 PetscFunctionReturn(0); 150 } 151 152 PetscErrorCode MatMult_SeqAIJSELL(Mat A,Vec xx,Vec yy) 153 { 154 Mat_SeqAIJSELL *aijsell = (Mat_SeqAIJSELL*)A->spptr; 155 156 PetscFunctionBegin; 157 PetscCall(MatSeqAIJSELL_build_shadow(A)); 158 PetscCall(MatMult_SeqSELL(aijsell->S,xx,yy)); 159 PetscFunctionReturn(0); 160 } 161 162 PetscErrorCode MatMultTranspose_SeqAIJSELL(Mat A,Vec xx,Vec yy) 163 { 164 Mat_SeqAIJSELL *aijsell=(Mat_SeqAIJSELL*)A->spptr; 165 166 PetscFunctionBegin; 167 PetscCall(MatSeqAIJSELL_build_shadow(A)); 168 PetscCall(MatMultTranspose_SeqSELL(aijsell->S,xx,yy)); 169 PetscFunctionReturn(0); 170 } 171 172 PetscErrorCode MatMultAdd_SeqAIJSELL(Mat A,Vec xx,Vec yy,Vec zz) 173 { 174 Mat_SeqAIJSELL *aijsell=(Mat_SeqAIJSELL*)A->spptr; 175 176 PetscFunctionBegin; 177 PetscCall(MatSeqAIJSELL_build_shadow(A)); 178 PetscCall(MatMultAdd_SeqSELL(aijsell->S,xx,yy,zz)); 179 PetscFunctionReturn(0); 180 } 181 182 PetscErrorCode MatMultTransposeAdd_SeqAIJSELL(Mat A,Vec xx,Vec yy,Vec zz) 183 { 184 Mat_SeqAIJSELL *aijsell=(Mat_SeqAIJSELL*)A->spptr; 185 186 PetscFunctionBegin; 187 PetscCall(MatSeqAIJSELL_build_shadow(A)); 188 PetscCall(MatMultTransposeAdd_SeqSELL(aijsell->S,xx,yy,zz)); 189 PetscFunctionReturn(0); 190 } 191 192 PetscErrorCode MatSOR_SeqAIJSELL(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 193 { 194 Mat_SeqAIJSELL *aijsell=(Mat_SeqAIJSELL*)A->spptr; 195 196 PetscFunctionBegin; 197 PetscCall(MatSeqAIJSELL_build_shadow(A)); 198 PetscCall(MatSOR_SeqSELL(aijsell->S,bb,omega,flag,fshift,its,lits,xx)); 199 PetscFunctionReturn(0); 200 } 201 202 /* MatConvert_SeqAIJ_SeqAIJSELL converts a SeqAIJ matrix into a 203 * SeqAIJSELL matrix. This routine is called by the MatCreate_SeqAIJSELL() 204 * routine, but can also be used to convert an assembled SeqAIJ matrix 205 * into a SeqAIJSELL one. */ 206 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJSELL(Mat A,MatType type,MatReuse reuse,Mat *newmat) 207 { 208 Mat B = *newmat; 209 Mat_SeqAIJ *b; 210 Mat_SeqAIJSELL *aijsell; 211 PetscBool set; 212 PetscBool sametype; 213 214 PetscFunctionBegin; 215 if (reuse == MAT_INITIAL_MATRIX) { 216 PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&B)); 217 } 218 219 PetscCall(PetscObjectTypeCompare((PetscObject)A,type,&sametype)); 220 if (sametype) PetscFunctionReturn(0); 221 222 PetscCall(PetscNewLog(B,&aijsell)); 223 b = (Mat_SeqAIJ*) B->data; 224 B->spptr = (void*) aijsell; 225 226 /* Disable use of the inode routines so that the AIJSELL ones will be used instead. 227 * This happens in MatAssemblyEnd_SeqAIJSELL as well, but the assembly end may not be called, so set it here, too. 228 * As noted elsewhere, I wonder if it might make sense and be feasible to use some of the inode routines. */ 229 b->inode.use = PETSC_FALSE; 230 231 /* Set function pointers for methods that we inherit from AIJ but override. 232 * We also parse some command line options below, since those determine some of the methods we point to. */ 233 B->ops->duplicate = MatDuplicate_SeqAIJSELL; 234 B->ops->assemblyend = MatAssemblyEnd_SeqAIJSELL; 235 B->ops->destroy = MatDestroy_SeqAIJSELL; 236 237 aijsell->S = NULL; 238 aijsell->eager_shadow = PETSC_FALSE; 239 240 /* Parse command line options. */ 241 PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"AIJSELL Options","Mat"); 242 PetscCall(PetscOptionsBool("-mat_aijsell_eager_shadow","Eager Shadowing","None",(PetscBool)aijsell->eager_shadow,(PetscBool*)&aijsell->eager_shadow,&set)); 243 PetscOptionsEnd(); 244 245 /* If A has already been assembled and eager shadowing is specified, build the shadow matrix. */ 246 if (A->assembled && aijsell->eager_shadow) { 247 PetscCall(MatSeqAIJSELL_build_shadow(A)); 248 } 249 250 B->ops->mult = MatMult_SeqAIJSELL; 251 B->ops->multtranspose = MatMultTranspose_SeqAIJSELL; 252 B->ops->multadd = MatMultAdd_SeqAIJSELL; 253 B->ops->multtransposeadd = MatMultTransposeAdd_SeqAIJSELL; 254 B->ops->sor = MatSOR_SeqAIJSELL; 255 256 PetscCall(PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaijsell_seqaij_C",MatConvert_SeqAIJSELL_SeqAIJ)); 257 258 PetscCall(PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJSELL)); 259 *newmat = B; 260 PetscFunctionReturn(0); 261 } 262 263 /*@C 264 MatCreateSeqAIJSELL - Creates a sparse matrix of type SEQAIJSELL. 265 This type inherits from AIJ and is largely identical, but keeps a "shadow" 266 copy of the matrix in SEQSELL format, which is used when this format 267 may be more suitable for a requested operation. Currently, SEQSELL format 268 is used for MatMult, MatMultTranspose, MatMultAdd, MatMultTransposeAdd, 269 and MatSOR operations. 270 Because SEQAIJSELL is a subtype of SEQAIJ, the option "-mat_seqaij_type seqaijsell" can be used to make 271 sequential AIJ matrices default to being instances of MATSEQAIJSELL. 272 273 Collective 274 275 Input Parameters: 276 + comm - MPI communicator, set to PETSC_COMM_SELF 277 . m - number of rows 278 . n - number of columns 279 . nz - number of nonzeros per row (same for all rows) 280 - nnz - array containing the number of nonzeros in the various rows 281 (possibly different for each row) or NULL 282 283 Output Parameter: 284 . A - the matrix 285 286 Options Database Keys: 287 . -mat_aijsell_eager_shadow - Construct shadow matrix upon matrix assembly; default is to take a "lazy" approach, performing this step the first time the matrix is applied 288 289 Notes: 290 If nnz is given then nz is ignored 291 292 Level: intermediate 293 294 .seealso: `MatCreate()`, `MatCreateMPIAIJSELL()`, `MatSetValues()` 295 @*/ 296 PetscErrorCode MatCreateSeqAIJSELL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 297 { 298 PetscFunctionBegin; 299 PetscCall(MatCreate(comm,A)); 300 PetscCall(MatSetSizes(*A,m,n,m,n)); 301 PetscCall(MatSetType(*A,MATSEQAIJSELL)); 302 PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz)); 303 PetscFunctionReturn(0); 304 } 305 306 PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJSELL(Mat A) 307 { 308 PetscFunctionBegin; 309 PetscCall(MatSetType(A,MATSEQAIJ)); 310 PetscCall(MatConvert_SeqAIJ_SeqAIJSELL(A,MATSEQAIJSELL,MAT_INPLACE_MATRIX,&A)); 311 PetscFunctionReturn(0); 312 } 313