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


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

 28:   /* Free everything in the Mat_CRL data structure. */
 29:   PetscFree2(crl->acols,crl->icols);
 30:   if (crl->fwork) {
 31:     VecDestroy(crl->fwork);
 32:   }
 33:   if (crl->xwork) {
 34:     VecDestroy(crl->xwork);
 35:   }
 36:   PetscFree(crl->array);
 37:   PetscFree(crl);
 38:   A->spptr = 0;

 40:   PetscObjectChangeTypeName( (PetscObject)A, MATMPIAIJ);
 41:   MatDestroy_MPIAIJ(A);
 42:   return(0);
 43: }

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

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

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


103: PetscErrorCode MatAssemblyEnd_MPICRL(Mat A, MatAssemblyType mode)
104: {
106:   Mat_MPIAIJ     *a = (Mat_MPIAIJ*)A->data;
107:   Mat_SeqAIJ     *Aij = (Mat_SeqAIJ*)(a->A->data), *Bij = (Mat_SeqAIJ*)(a->A->data);

110:   Aij->inode.use = PETSC_FALSE;
111:   Bij->inode.use = PETSC_FALSE;
112:   MatAssemblyEnd_MPIAIJ(A,mode);
113:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);

115:   /* Now calculate the permutation and grouping information. */
116:   MPICRL_create_crl(A);
117:   return(0);
118: }


123: /* MatConvert_MPIAIJ_MPICRL converts a MPIAIJ matrix into a 
124:  * MPICRL matrix.  This routine is called by the MatCreate_MPICRL() 
125:  * routine, but can also be used to convert an assembled MPIAIJ matrix 
126:  * into a MPICRL one. */
130: PetscErrorCode  MatConvert_MPIAIJ_MPICRL(Mat A,const MatType type,MatReuse reuse,Mat *newmat)
131: {
133:   Mat            B = *newmat;
134:   Mat_CRL        *crl;

137:   if (reuse == MAT_INITIAL_MATRIX) {
138:     MatDuplicate(A,MAT_COPY_VALUES,&B);
139:   }

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

144:   /* Set function pointers for methods that we inherit from AIJ but override. */
145:   B->ops->duplicate   = MatDuplicate_CRL;
146:   B->ops->assemblyend = MatAssemblyEnd_MPICRL;
147:   B->ops->destroy     = MatDestroy_MPICRL;
148:   B->ops->mult        = MatMult_CRL;

150:   /* If A has already been assembled, compute the permutation. */
151:   if (A->assembled) {
152:     MPICRL_create_crl(B);
153:   }
154:   PetscObjectChangeTypeName((PetscObject)B,MATMPICRL);
155:   *newmat = B;
156:   return(0);
157: }


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

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

184:    Output Parameter:
185: .  A - the matrix 

187:    Notes:
188:    If nnz is given then nz is ignored

190:    Level: intermediate

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

194: .seealso: MatCreate(), MatCreateMPICSRPERM(), MatSetValues()
195: @*/
196: PetscErrorCode  MatCreateMPICRL(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],PetscInt onz,const PetscInt onnz[],Mat *A)
197: {

201:   MatCreate(comm,A);
202:   MatSetSizes(*A,m,n,m,n);
203:   MatSetType(*A,MATMPICRL);
204:   MatMPIAIJSetPreallocation_MPIAIJ(*A,nz,(PetscInt*)nnz,onz,(PetscInt*)onnz);
205:   return(0);
206: }


212: PetscErrorCode  MatCreate_MPICRL(Mat A)
213: {

217:   MatSetType(A,MATMPIAIJ);
218:   MatConvert_MPIAIJ_MPICRL(A,MATMPICRL,MAT_REUSE_MATRIX,&A);
219:   return(0);
220: }