1 #define PETSCMAT_DLL 2 3 /* 4 Defines a matrix-vector product for the MATSEQAIJCRL matrix class. 5 This class is derived from the MATSEQAIJ class and retains the 6 compressed row storage (aka Yale sparse matrix format) but augments 7 it with a column oriented storage that is more efficient for 8 matrix vector products on Vector machines. 9 10 CRL stands for constant row length (that is the same number of columns 11 is kept (padded with zeros) for each row of the sparse matrix. 12 */ 13 #include "src/mat/impls/aij/seq/crl/crl.h" 14 15 #undef __FUNCT__ 16 #define __FUNCT__ "MatDestroy_SeqCRL" 17 PetscErrorCode MatDestroy_SeqCRL(Mat A) 18 { 19 PetscErrorCode ierr; 20 Mat_CRL *crl = (Mat_CRL *) A->spptr; 21 22 /* We are going to convert A back into a SEQAIJ matrix, since we are 23 * eventually going to use MatDestroy() to destroy everything 24 * that is not specific to CRL. 25 * In preparation for this, reset the operations pointers in A to 26 * their SeqAIJ versions. */ 27 A->ops->assemblyend = crl->AssemblyEnd; 28 A->ops->destroy = crl->MatDestroy; 29 A->ops->duplicate = crl->MatDuplicate; 30 31 /* Free everything in the Mat_CRL data structure. */ 32 ierr = PetscFree2(crl->acols,crl->icols);CHKERRQ(ierr); 33 34 /* Change the type of A back to SEQAIJ and use MatDestroy() 35 * to destroy everything that remains. */ 36 ierr = PetscObjectChangeTypeName( (PetscObject)A, MATSEQAIJ);CHKERRQ(ierr); 37 /* Note that I don't call MatSetType(). I believe this is because that 38 * is only to be called when *building* a matrix. */ 39 ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 40 PetscFunctionReturn(0); 41 } 42 43 PetscErrorCode MatDuplicate_CRL(Mat A, MatDuplicateOption op, Mat *M) 44 { 45 PetscErrorCode ierr; 46 Mat_CRL *crl = (Mat_CRL *) A->spptr; 47 48 PetscFunctionBegin; 49 ierr = (*crl->MatDuplicate)(A,op,M);CHKERRQ(ierr); 50 SETERRQ(PETSC_ERR_SUP,"Cannot duplicate CRL matrices yet"); 51 PetscFunctionReturn(0); 52 } 53 54 #undef __FUNCT__ 55 #define __FUNCT__ "SeqCRL_create_crl" 56 PetscErrorCode SeqCRL_create_crl(Mat A) 57 { 58 Mat_SeqAIJ *a = (Mat_SeqAIJ *)(A)->data; 59 Mat_CRL *crl = (Mat_CRL*) A->spptr; 60 PetscInt m = A->rmap.n; /* Number of rows in the matrix. */ 61 PetscInt *aj = a->j; /* From the CSR representation; points to the beginning of each row. */ 62 PetscInt i, j,rmax = a->rmax,*icols, *ilen = a->ilen; 63 PetscScalar *aa = a->a,*acols; 64 PetscErrorCode ierr; 65 66 PetscFunctionBegin; 67 crl->nz = a->nz; 68 crl->m = A->rmap.n; 69 crl->rmax = rmax; 70 ierr = PetscMalloc2(rmax*m,PetscScalar,&crl->acols,rmax*m,PetscInt,&crl->icols);CHKERRQ(ierr); 71 acols = crl->acols; 72 icols = crl->icols; 73 for (i=0; i<m; i++) { 74 for (j=0; j<ilen[i]; j++) { 75 acols[j*m+i] = *aa++; 76 icols[j*m+i] = *aj++; 77 } 78 for (;j<rmax; j++) { /* empty column entries */ 79 acols[j*m+i] = 0.0; 80 icols[j*m+i] = (j) ? icols[(j-1)*m+i] : 0; /* handle case where row is EMPTY */ 81 } 82 } 83 ierr = PetscInfo2(A,"Percentage of 0's introduced for vectorized multiply %G. Rmax= %D\n",1.0-((double)a->nz)/((double)(rmax*m)),rmax); 84 PetscFunctionReturn(0); 85 } 86 87 #undef __FUNCT__ 88 #define __FUNCT__ "MatAssemblyEnd_SeqCRL" 89 PetscErrorCode MatAssemblyEnd_SeqCRL(Mat A, MatAssemblyType mode) 90 { 91 PetscErrorCode ierr; 92 Mat_CRL *crl = (Mat_CRL*) A->spptr; 93 Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 94 95 PetscFunctionBegin; 96 if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 97 98 /* Since a MATSEQCRL matrix is really just a MATSEQAIJ with some 99 * extra information, call the AssemblyEnd routine for a MATSEQAIJ. 100 * I'm not sure if this is the best way to do this, but it avoids 101 * a lot of code duplication. 102 * I also note that currently MATSEQCRL doesn't know anything about 103 * the Mat_CompressedRow data structure that SeqAIJ now uses when there 104 * are many zero rows. If the SeqAIJ assembly end routine decides to use 105 * this, this may break things. (Don't know... haven't looked at it.) */ 106 a->inode.use = PETSC_FALSE; 107 (*crl->AssemblyEnd)(A, mode); 108 109 /* Now calculate the permutation and grouping information. */ 110 ierr = SeqCRL_create_crl(A);CHKERRQ(ierr); 111 PetscFunctionReturn(0); 112 } 113 114 #include "src/inline/dot.h" 115 116 #undef __FUNCT__ 117 #define __FUNCT__ "MatMult_CRL" 118 PetscErrorCode MatMult_CRL(Mat A,Vec xx,Vec yy) 119 { 120 Mat_CRL *crl = (Mat_CRL*) A->spptr; 121 PetscInt m = crl->m; /* Number of rows in the matrix. */ 122 PetscInt rmax = crl->rmax,*icols = crl->icols; 123 PetscScalar *acols = crl->acols; 124 PetscErrorCode ierr; 125 PetscScalar *x,*y; 126 #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL) 127 PetscInt i,j,ii; 128 #endif 129 130 131 #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 132 #pragma disjoint(*x,*y,*aa) 133 #endif 134 135 PetscFunctionBegin; 136 if (crl->xscat) { 137 ierr = VecCopy(xx,crl->xwork);CHKERRQ(ierr); 138 /* get remote values needed for local part of multiply */ 139 ierr = VecScatterBegin(xx,crl->fwork,INSERT_VALUES,SCATTER_FORWARD,crl->xscat);CHKERRQ(ierr); 140 ierr = VecScatterEnd(xx,crl->fwork,INSERT_VALUES,SCATTER_FORWARD,crl->xscat);CHKERRQ(ierr); 141 xx = crl->xwork; 142 }; 143 144 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 145 ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 146 147 #if defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL) 148 fortranmultcrl_(&m,&rmax,x,y,icols,acols); 149 #else 150 151 /* first column */ 152 for (j=0; j<m; j++) { 153 y[j] = acols[j]*x[icols[j]]; 154 } 155 156 /* other columns */ 157 #if defined(PETSC_HAVE_CRAYC) 158 #pragma _CRI preferstream 159 #endif 160 for (i=1; i<rmax; i++) { 161 ii = i*m; 162 #if defined(PETSC_HAVE_CRAYC) 163 #pragma _CRI prefervector 164 #endif 165 for (j=0; j<m; j++) { 166 y[j] = y[j] + acols[ii+j]*x[icols[ii+j]]; 167 } 168 } 169 #if defined(PETSC_HAVE_CRAYC) 170 #pragma _CRI ivdep 171 #endif 172 173 #endif 174 ierr = PetscLogFlops(2*crl->nz - m);CHKERRQ(ierr); 175 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 176 ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 177 PetscFunctionReturn(0); 178 } 179 180 181 /* MatConvert_SeqAIJ_SeqCRL converts a SeqAIJ matrix into a 182 * SeqCRL matrix. This routine is called by the MatCreate_SeqCRL() 183 * routine, but can also be used to convert an assembled SeqAIJ matrix 184 * into a SeqCRL one. */ 185 EXTERN_C_BEGIN 186 #undef __FUNCT__ 187 #define __FUNCT__ "MatConvert_SeqAIJ_SeqCRL" 188 PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat A,MatType type,MatReuse reuse,Mat *newmat) 189 { 190 /* This routine is only called to convert to MATSEQCRL 191 * from MATSEQAIJ, so we can ignore 'MatType Type'. */ 192 PetscErrorCode ierr; 193 Mat B = *newmat; 194 Mat_CRL *crl; 195 196 PetscFunctionBegin; 197 if (reuse == MAT_INITIAL_MATRIX) { 198 ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); 199 } 200 201 ierr = PetscNew(Mat_CRL,&crl);CHKERRQ(ierr); 202 B->spptr = (void *) crl; 203 204 /* Save a pointer to the original SeqAIJ assembly end routine, because we 205 * will want to use it later in the CRL assembly end routine. 206 * Also, save a pointer to the original SeqAIJ Destroy routine, because we 207 * will want to use it in the CRL destroy routine. */ 208 crl->AssemblyEnd = A->ops->assemblyend; 209 crl->MatDestroy = A->ops->destroy; 210 crl->MatDuplicate = A->ops->duplicate; 211 212 /* Set function pointers for methods that we inherit from AIJ but 213 * override. */ 214 B->ops->duplicate = MatDuplicate_CRL; 215 B->ops->assemblyend = MatAssemblyEnd_SeqCRL; 216 B->ops->destroy = MatDestroy_SeqCRL; 217 B->ops->mult = MatMult_CRL; 218 219 /* If A has already been assembled, compute the permutation. */ 220 if (A->assembled == PETSC_TRUE) { 221 ierr = SeqCRL_create_crl(B);CHKERRQ(ierr); 222 } 223 ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQCRL);CHKERRQ(ierr); 224 *newmat = B; 225 PetscFunctionReturn(0); 226 } 227 EXTERN_C_END 228 229 230 #undef __FUNCT__ 231 #define __FUNCT__ "MatCreateSeqCRL" 232 /*@C 233 MatCreateSeqCRL - Creates a sparse matrix of type SEQCRL. 234 This type inherits from AIJ, but stores some additional 235 information that is used to allow better vectorization of 236 the matrix-vector product. At the cost of increased storage, the AIJ formatted 237 matrix can be copied to a format in which pieces of the matrix are 238 stored in ELLPACK format, allowing the vectorized matrix multiply 239 routine to use stride-1 memory accesses. As with the AIJ type, it is 240 important to preallocate matrix storage in order to get good assembly 241 performance. 242 243 Collective on MPI_Comm 244 245 Input Parameters: 246 + comm - MPI communicator, set to PETSC_COMM_SELF 247 . m - number of rows 248 . n - number of columns 249 . nz - number of nonzeros per row (same for all rows) 250 - nnz - array containing the number of nonzeros in the various rows 251 (possibly different for each row) or PETSC_NULL 252 253 Output Parameter: 254 . A - the matrix 255 256 Notes: 257 If nnz is given then nz is ignored 258 259 Level: intermediate 260 261 .keywords: matrix, cray, sparse, parallel 262 263 .seealso: MatCreate(), MatCreateMPICSRPERM(), MatSetValues() 264 @*/ 265 PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqCRL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 266 { 267 PetscErrorCode ierr; 268 269 PetscFunctionBegin; 270 ierr = MatCreate(comm,A);CHKERRQ(ierr); 271 ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 272 ierr = MatSetType(*A,MATSEQCRL);CHKERRQ(ierr); 273 ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 274 PetscFunctionReturn(0); 275 } 276 277 278 EXTERN_C_BEGIN 279 #undef __FUNCT__ 280 #define __FUNCT__ "MatCreate_SeqCRL" 281 PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqCRL(Mat A) 282 { 283 PetscErrorCode ierr; 284 285 PetscFunctionBegin; 286 /* Change the type name before calling MatSetType() to force proper construction of SeqAIJ 287 and MATSEQCRL types. */ 288 ierr = PetscObjectChangeTypeName((PetscObject)A,MATSEQCRL);CHKERRQ(ierr); 289 ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 290 ierr = MatConvert_SeqAIJ_SeqCRL(A,MATSEQCRL,MAT_REUSE_MATRIX,&A);CHKERRQ(ierr); 291 PetscFunctionReturn(0); 292 } 293 EXTERN_C_END 294 295