Actual source code: mcrl.c

  1: #define PETSCMAT_DLL

  3: /*
  4:   Defines a matrix-vector product for the MATMPIAIJCRL matrix class.
  5:   This class is derived from the MATMPIAIJ 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.

 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.

 13:    See src/mat/impls/aij/seq/crl/crl.c for the sequential version
 14: */

 16:  #include ../src/mat/impls/aij/mpi/mpiaij.h
 17:  #include ../src/mat/impls/aij/seq/crl/crl.h

 21: PetscErrorCode MatDestroy_MPICRL(Mat A)
 22: {
 24:   Mat_CRL        *crl = (Mat_CRL *) A->spptr;

 26:   /* We are going to convert A back into a MPIAIJ matrix, since we are 
 27:    * eventually going to use MatDestroy_MPIAIJ() to destroy everything 
 28:    * that is not specific to CRL.
 29:    * In preparation for this, reset the operations pointers in A to 
 30:    * their MPIAIJ versions. */
 31:   A->ops->assemblyend = crl->AssemblyEnd;
 32:   A->ops->destroy     = crl->MatDestroy;
 33:   A->ops->duplicate   = crl->MatDuplicate;

 35:   /* Free everything in the Mat_CRL data structure. */
 36:   PetscFree2(crl->acols,crl->icols);
 37:   if (crl->fwork) {
 38:     VecDestroy(crl->fwork);
 39:   }
 40:   if (crl->xwork) {
 41:     VecDestroy(crl->xwork);
 42:   }
 43:   PetscFree(crl->array);
 44:   PetscFree(crl);
 45:   A->spptr = 0;

 47:   /* Change the type of A back to MPIAIJ and use MatDestroy_MPIAIJ() 
 48:    * to destroy everything that remains. */
 49:   PetscObjectChangeTypeName( (PetscObject)A, MATMPIAIJ);
 50:   /* Note that I don't call MatSetType().  I believe this is because that 
 51:    * is only to be called when *building* a matrix. */
 52:   (*A->ops->destroy)(A);
 53:   return(0);
 54: }

 58: PetscErrorCode MPICRL_create_crl(Mat A)
 59: {
 60:   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)(A)->data;
 61:   Mat_SeqAIJ     *Aij = (Mat_SeqAIJ*)(a->A->data), *Bij = (Mat_SeqAIJ*)(a->B->data);
 62:   Mat_CRL        *crl = (Mat_CRL*) A->spptr;
 63:   PetscInt       m = A->rmap->n;  /* Number of rows in the matrix. */
 64:   PetscInt       nd = a->A->cmap->n; /* number of columns in diagonal portion */
 65:   PetscInt       *aj = Aij->j,*bj = Bij->j;  /* From the CSR representation; points to the beginning  of each row. */
 66:   PetscInt       i, j,rmax = 0,*icols, *ailen = Aij->ilen, *bilen = Bij->ilen;
 67:   PetscScalar    *aa = Aij->a,*ba = Bij->a,*acols,*array;

 71:   /* determine the row with the most columns */
 72:   for (i=0; i<m; i++) {
 73:     rmax = PetscMax(rmax,ailen[i]+bilen[i]);
 74:   }
 75:   crl->nz   = Aij->nz+Bij->nz;
 76:   crl->m    = A->rmap->n;
 77:   crl->rmax = rmax;
 78:   PetscMalloc2(rmax*m,PetscScalar,&crl->acols,rmax*m,PetscInt,&crl->icols);
 79:   acols = crl->acols;
 80:   icols = crl->icols;
 81:   for (i=0; i<m; i++) {
 82:     for (j=0; j<ailen[i]; j++) {
 83:       acols[j*m+i] = *aa++;
 84:       icols[j*m+i] = *aj++;
 85:     }
 86:     for (;j<ailen[i]+bilen[i]; j++) {
 87:       acols[j*m+i] = *ba++;
 88:       icols[j*m+i] = nd + *bj++;
 89:     }
 90:     for (;j<rmax; j++) { /* empty column entries */
 91:       acols[j*m+i] = 0.0;
 92:       icols[j*m+i] = (j) ? icols[(j-1)*m+i] : 0;  /* handle case where row is EMPTY */
 93:     }
 94:   }
 95:   PetscInfo1(A,"Percentage of 0's introduced for vectorized multiply %g\n",1.0-((double)(crl->nz))/((double)(rmax*m)));

 97:   PetscMalloc((a->B->cmap->n+nd)*sizeof(PetscScalar),&array);
 98:   /* xwork array is actually B->n+nd long, but we define xwork this length so can copy into it */
 99:   VecCreateMPIWithArray(((PetscObject)A)->comm,nd,PETSC_DECIDE,array,&crl->xwork);
100:   VecCreateSeqWithArray(PETSC_COMM_SELF,a->B->cmap->n,array+nd,&crl->fwork);
101:   crl->array = array;
102:   crl->xscat = a->Mvctx;
103:   return(0);
104: }

108: PetscErrorCode MatAssemblyEnd_MPICRL(Mat A, MatAssemblyType mode)
109: {
111:   Mat_CRL        *crl = (Mat_CRL*) A->spptr;
112:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
113:   Mat_SeqAIJ     *Aij = (Mat_SeqAIJ*)(a->A->data), *Bij = (Mat_SeqAIJ*)(a->A->data);

116:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);
117: 
118:   /* Since a MATMPICRL matrix is really just a MATMPIAIJ with some 
119:    * extra information, call the AssemblyEnd routine for a MATMPIAIJ. 
120:    * I'm not sure if this is the best way to do this, but it avoids 
121:    * a lot of code duplication.
122:    * I also note that currently MATMPICRL doesn't know anything about 
123:    * the Mat_CompressedRow data structure that MPIAIJ now uses when there 
124:    * are many zero rows.  If the MPIAIJ assembly end routine decides to use 
125:    * this, this may break things.  (Don't know... haven't looked at it.) */
126:   Aij->inode.use = PETSC_FALSE;
127:   Bij->inode.use = PETSC_FALSE;
128:   (*crl->AssemblyEnd)(A, mode);

130:   /* Now calculate the permutation and grouping information. */
131:   MPICRL_create_crl(A);
132:   return(0);
133: }


138: /* MatConvert_MPIAIJ_MPICRL converts a MPIAIJ matrix into a 
139:  * MPICRL matrix.  This routine is called by the MatCreate_MPICRL() 
140:  * routine, but can also be used to convert an assembled MPIAIJ matrix 
141:  * into a MPICRL one. */
145: PetscErrorCode  MatConvert_MPIAIJ_MPICRL(Mat A,const MatType type,MatReuse reuse,Mat *newmat)
146: {
148:   Mat            B = *newmat;
149:   Mat_CRL        *crl;

152:   if (reuse == MAT_INITIAL_MATRIX) {
153:     MatDuplicate(A,MAT_COPY_VALUES,&B);
154:   }

156:   PetscNewLog(B,Mat_CRL,&crl);
157:   B->spptr = (void *) crl;

159:   crl->AssemblyEnd  = A->ops->assemblyend;
160:   crl->MatDestroy   = A->ops->destroy;
161:   crl->MatDuplicate = A->ops->duplicate;

163:   /* Set function pointers for methods that we inherit from AIJ but override. */
164:   B->ops->duplicate   = MatDuplicate_CRL;
165:   B->ops->assemblyend = MatAssemblyEnd_MPICRL;
166:   B->ops->destroy     = MatDestroy_MPICRL;
167:   B->ops->mult        = MatMult_CRL;

169:   /* If A has already been assembled, compute the permutation. */
170:   if (A->assembled == PETSC_TRUE) {
171:     MPICRL_create_crl(B);
172:   }
173:   PetscObjectChangeTypeName((PetscObject)B,MATMPICRL);
174:   *newmat = B;
175:   return(0);
176: }


182: /*@C
183:    MatCreateMPICRL - Creates a sparse matrix of type MPICRL.
184:    This type inherits from AIJ, but stores some additional
185:    information that is used to allow better vectorization of 
186:    the matrix-vector product. At the cost of increased storage, the AIJ formatted 
187:    matrix can be copied to a format in which pieces of the matrix are 
188:    stored in ELLPACK format, allowing the vectorized matrix multiply 
189:    routine to use stride-1 memory accesses.  As with the AIJ type, it is 
190:    important to preallocate matrix storage in order to get good assembly 
191:    performance.
192:    
193:    Collective on MPI_Comm

195:    Input Parameters:
196: +  comm - MPI communicator, set to PETSC_COMM_SELF
197: .  m - number of rows
198: .  n - number of columns
199: .  nz - number of nonzeros per row (same for all rows)
200: -  nnz - array containing the number of nonzeros in the various rows 
201:          (possibly different for each row) or PETSC_NULL

203:    Output Parameter:
204: .  A - the matrix 

206:    Notes:
207:    If nnz is given then nz is ignored

209:    Level: intermediate

211: .keywords: matrix, cray, sparse, parallel

213: .seealso: MatCreate(), MatCreateMPICSRPERM(), MatSetValues()
214: @*/
215: PetscErrorCode  MatCreateMPICRL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],PetscInt onz,const PetscInt onnz[],Mat *A)
216: {

220:   MatCreate(comm,A);
221:   MatSetSizes(*A,m,n,m,n);
222:   MatSetType(*A,MATMPICRL);
223:   MatMPIAIJSetPreallocation_MPIAIJ(*A,nz,(PetscInt*)nnz,onz,(PetscInt*)onnz);
224:   return(0);
225: }


231: PetscErrorCode  MatCreate_MPICRL(Mat A)
232: {

236:   MatSetType(A,MATMPIAIJ);
237:   MatConvert_MPIAIJ_MPICRL(A,MATMPICRL,MAT_REUSE_MATRIX,&A);
238:   return(0);
239: }