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: PetscMalloc2(rmax*m,PetscScalar,&crl->acols,rmax*m,PetscInt,&crl->icols);
68: acols = crl->acols;
69: icols = crl->icols;
70: for (i=0; i<m; i++) {
71: for (j=0; j<ailen[i]; j++) {
72: acols[j*m+i] = *aa++;
73: icols[j*m+i] = *aj++;
74: }
75: for (;j<ailen[i]+bilen[i]; j++) {
76: acols[j*m+i] = *ba++;
77: icols[j*m+i] = nd + *bj++;
78: }
79: for (;j<rmax; j++) { /* empty column entries */
80: acols[j*m+i] = 0.0;
81: icols[j*m+i] = (j) ? icols[(j-1)*m+i] : 0; /* handle case where row is EMPTY */
82: }
83: }
84: PetscInfo1(A,"Percentage of 0's introduced for vectorized multiply %g\n",1.0-((double)(crl->nz))/((double)(rmax*m)));
86: PetscMalloc((a->B->cmap->n+nd)*sizeof(PetscScalar),&array);
87: /* xwork array is actually B->n+nd long, but we define xwork this length so can copy into it */
88: VecCreateMPIWithArray(((PetscObject)A)->comm,nd,PETSC_DECIDE,array,&crl->xwork);
89: VecCreateSeqWithArray(PETSC_COMM_SELF,a->B->cmap->n,array+nd,&crl->fwork);
90: crl->array = array;
91: crl->xscat = a->Mvctx;
92: return(0);
93: }
99: PetscErrorCode MatAssemblyEnd_MPICRL(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: MPICRL_create_crl(A);
113: return(0);
114: }
119: /* MatConvert_MPIAIJ_MPICRL converts a MPIAIJ matrix into a
120: * MPICRL matrix. This routine is called by the MatCreate_MPICRL()
121: * routine, but can also be used to convert an assembled MPIAIJ matrix
122: * into a MPICRL one. */
126: PetscErrorCode MatConvert_MPIAIJ_MPICRL(Mat A,const MatType type,MatReuse reuse,Mat *newmat)
127: {
129: Mat B = *newmat;
130: Mat_CRL *crl;
133: if (reuse == MAT_INITIAL_MATRIX) {
134: MatDuplicate(A,MAT_COPY_VALUES,&B);
135: }
137: PetscNewLog(B,Mat_CRL,&crl);
138: B->spptr = (void *) crl;
140: /* Set function pointers for methods that we inherit from AIJ but override. */
141: B->ops->duplicate = MatDuplicate_CRL;
142: B->ops->assemblyend = MatAssemblyEnd_MPICRL;
143: B->ops->destroy = MatDestroy_MPICRL;
144: B->ops->mult = MatMult_CRL;
146: /* If A has already been assembled, compute the permutation. */
147: if (A->assembled) {
148: MPICRL_create_crl(B);
149: }
150: PetscObjectChangeTypeName((PetscObject)B,MATMPICRL);
151: *newmat = B;
152: return(0);
153: }
159: /*@C
160: MatCreateMPICRL - Creates a sparse matrix of type MPICRL.
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(), MatCreateMPICSRPERM(), MatSetValues()
191: @*/
192: PetscErrorCode MatCreateMPICRL(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,MATMPICRL);
200: MatMPIAIJSetPreallocation_MPIAIJ(*A,nz,(PetscInt*)nnz,onz,(PetscInt*)onnz);
201: return(0);
202: }
208: PetscErrorCode MatCreate_MPICRL(Mat A)
209: {
213: MatSetType(A,MATMPIAIJ);
214: MatConvert_MPIAIJ_MPICRL(A,MATMPICRL,MAT_REUSE_MATRIX,&A);
215: return(0);
216: }