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