Actual source code: mcrl.c

petsc-3.3-p7 2013-05-11
  2: /*
  3:   Defines a matrix-vector product for the MATMPIAIJCRL matrix class.
  4:   This class is derived from the MATMPIAIJ class and retains the 
  5:   compressed row storage (aka Yale sparse matrix format) but augments 
  6:   it with a column oriented storage that is more efficient for 
  7:   matrix vector products on Vector machines.

  9:   CRL stands for constant row length (that is the same number of columns
 10:   is kept (padded with zeros) for each row of the sparse matrix.

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

 15: #include <../src/mat/impls/aij/mpi/mpiaij.h>
 16: #include <../src/mat/impls/aij/seq/crl/crl.h>

 18: extern PetscErrorCode MatDestroy_MPIAIJ(Mat);

 22: PetscErrorCode MatDestroy_MPIAIJCRL(Mat A)
 23: {
 25:   Mat_AIJCRL     *aijcrl = (Mat_AIJCRL *) A->spptr;

 27:   /* Free everything in the Mat_AIJCRL data structure. */
 28:   if (aijcrl) {
 29:     PetscFree2(aijcrl->acols,aijcrl->icols);
 30:     VecDestroy(&aijcrl->fwork);
 31:     VecDestroy(&aijcrl->xwork);
 32:     PetscFree(aijcrl->array);
 33:   }
 34:   PetscFree(A->spptr);

 36:   PetscObjectChangeTypeName( (PetscObject)A, MATMPIAIJ);
 37:   MatDestroy_MPIAIJ(A);
 38:   return(0);
 39: }

 43: PetscErrorCode MatMPIAIJCRL_create_aijcrl(Mat A)
 44: {
 45:   Mat_MPIAIJ     *a = (Mat_MPIAIJ *)(A)->data;
 46:   Mat_SeqAIJ     *Aij = (Mat_SeqAIJ*)(a->A->data), *Bij = (Mat_SeqAIJ*)(a->B->data);
 47:   Mat_AIJCRL     *aijcrl = (Mat_AIJCRL*) A->spptr;
 48:   PetscInt       m = A->rmap->n;  /* Number of rows in the matrix. */
 49:   PetscInt       nd = a->A->cmap->n; /* number of columns in diagonal portion */
 50:   PetscInt       *aj = Aij->j,*bj = Bij->j;  /* From the CSR representation; points to the beginning  of each row. */
 51:   PetscInt       i, j,rmax = 0,*icols, *ailen = Aij->ilen, *bilen = Bij->ilen;
 52:   PetscScalar    *aa = Aij->a,*ba = Bij->a,*acols,*array;

 56:   /* determine the row with the most columns */
 57:   for (i=0; i<m; i++) {
 58:     rmax = PetscMax(rmax,ailen[i]+bilen[i]);
 59:   }
 60:   aijcrl->nz   = Aij->nz+Bij->nz;
 61:   aijcrl->m    = A->rmap->n;
 62:   aijcrl->rmax = rmax;
 63:   PetscFree2(aijcrl->acols,aijcrl->icols);
 64:   PetscMalloc2(rmax*m,PetscScalar,&aijcrl->acols,rmax*m,PetscInt,&aijcrl->icols);
 65:   acols = aijcrl->acols;
 66:   icols = aijcrl->icols;
 67:   for (i=0; i<m; i++) {
 68:     for (j=0; j<ailen[i]; j++) {
 69:       acols[j*m+i] = *aa++;
 70:       icols[j*m+i] = *aj++;
 71:     }
 72:     for (;j<ailen[i]+bilen[i]; j++) {
 73:       acols[j*m+i] = *ba++;
 74:       icols[j*m+i] = nd + *bj++;
 75:     }
 76:     for (;j<rmax; j++) { /* empty column entries */
 77:       acols[j*m+i] = 0.0;
 78:       icols[j*m+i] = (j) ? icols[(j-1)*m+i] : 0;  /* handle case where row is EMPTY */
 79:     }
 80:   }
 81:   PetscInfo1(A,"Percentage of 0's introduced for vectorized multiply %g\n",1.0-((double)(aijcrl->nz))/((double)(rmax*m)));

 83:   PetscFree(aijcrl->array);
 84:   PetscMalloc((a->B->cmap->n+nd)*sizeof(PetscScalar),&array);
 85:   /* xwork array is actually B->n+nd long, but we define xwork this length so can copy into it */
 86:   VecDestroy(&aijcrl->xwork);
 87:   VecCreateMPIWithArray(((PetscObject)A)->comm,1,nd,PETSC_DECIDE,array,&aijcrl->xwork);
 88:   VecDestroy(&aijcrl->fwork);
 89:   VecCreateSeqWithArray(PETSC_COMM_SELF,1,a->B->cmap->n,array+nd,&aijcrl->fwork);
 90:   aijcrl->array = array;
 91:   aijcrl->xscat = a->Mvctx;
 92:   return(0);
 93: }

 95: extern PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat,MatAssemblyType);

 99: PetscErrorCode MatAssemblyEnd_MPIAIJCRL(Mat A, MatAssemblyType mode)
100: {
102:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
103:   Mat_SeqAIJ     *Aij = (Mat_SeqAIJ*)(a->A->data), *Bij = (Mat_SeqAIJ*)(a->A->data);

106:   Aij->inode.use = PETSC_FALSE;
107:   Bij->inode.use = PETSC_FALSE;
108:   MatAssemblyEnd_MPIAIJ(A,mode);
109:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);

111:   /* Now calculate the permutation and grouping information. */
112:   MatMPIAIJCRL_create_aijcrl(A);
113:   return(0);
114: }

116: extern PetscErrorCode MatMult_AIJCRL(Mat,Vec,Vec);
117: extern PetscErrorCode MatDuplicate_AIJCRL(Mat,MatDuplicateOption,Mat*);

119: /* MatConvert_MPIAIJ_MPIAIJCRL converts a MPIAIJ matrix into a 
120:  * MPIAIJCRL matrix.  This routine is called by the MatCreate_MPIAIJCRL() 
121:  * routine, but can also be used to convert an assembled MPIAIJ matrix 
122:  * into a MPIAIJCRL one. */
123: EXTERN_C_BEGIN
126: PetscErrorCode  MatConvert_MPIAIJ_MPIAIJCRL(Mat A,const MatType type,MatReuse reuse,Mat *newmat)
127: {
129:   Mat            B = *newmat;
130:   Mat_AIJCRL     *aijcrl;

133:   if (reuse == MAT_INITIAL_MATRIX) {
134:     MatDuplicate(A,MAT_COPY_VALUES,&B);
135:   }

137:   PetscNewLog(B,Mat_AIJCRL,&aijcrl);
138:   B->spptr = (void *) aijcrl;

140:   /* Set function pointers for methods that we inherit from AIJ but override. */
141:   B->ops->duplicate   = MatDuplicate_AIJCRL;
142:   B->ops->assemblyend = MatAssemblyEnd_MPIAIJCRL;
143:   B->ops->destroy     = MatDestroy_MPIAIJCRL;
144:   B->ops->mult        = MatMult_AIJCRL;

146:   /* If A has already been assembled, compute the permutation. */
147:   if (A->assembled) {
148:     MatMPIAIJCRL_create_aijcrl(B);
149:   }
150:   PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJCRL);
151:   *newmat = B;
152:   return(0);
153: }
154: EXTERN_C_END


159: /*@C
160:    MatCreateMPIAIJCRL - Creates a sparse matrix of type MPIAIJCRL.
161:    This type inherits from AIJ, but stores some additional
162:    information that is used to allow better vectorization of 
163:    the matrix-vector product. At the cost of increased storage, the AIJ formatted 
164:    matrix can be copied to a format in which pieces of the matrix are 
165:    stored in ELLPACK format, allowing the vectorized matrix multiply 
166:    routine to use stride-1 memory accesses.  As with the AIJ type, it is 
167:    important to preallocate matrix storage in order to get good assembly 
168:    performance.
169:    
170:    Collective on MPI_Comm

172:    Input Parameters:
173: +  comm - MPI communicator, set to PETSC_COMM_SELF
174: .  m - number of rows
175: .  n - number of columns
176: .  nz - number of nonzeros per row (same for all rows)
177: -  nnz - array containing the number of nonzeros in the various rows 
178:          (possibly different for each row) or PETSC_NULL

180:    Output Parameter:
181: .  A - the matrix 

183:    Notes:
184:    If nnz is given then nz is ignored

186:    Level: intermediate

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

190: .seealso: MatCreate(), MatCreateMPIAIJPERM(), MatSetValues()
191: @*/
192: PetscErrorCode  MatCreateMPIAIJCRL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],PetscInt onz,const PetscInt onnz[],Mat *A)
193: {

197:   MatCreate(comm,A);
198:   MatSetSizes(*A,m,n,m,n);
199:   MatSetType(*A,MATMPIAIJCRL);
200:   MatMPIAIJSetPreallocation_MPIAIJ(*A,nz,(PetscInt*)nnz,onz,(PetscInt*)onnz);
201:   return(0);
202: }


205: EXTERN_C_BEGIN
208: PetscErrorCode  MatCreate_MPIAIJCRL(Mat A)
209: {

213:   MatSetType(A,MATMPIAIJ);
214:   MatConvert_MPIAIJ_MPIAIJCRL(A,MATMPIAIJCRL,MAT_REUSE_MATRIX,&A);
215:   return(0);
216: }
217: EXTERN_C_END