xref: /petsc/src/mat/impls/aij/seq/matmatmult.c (revision 32a4b47a72620a1b77aa3c4e8ddc8668bd2d097b)
1 /*$Id: matmatmult.c,v 1.15 2001/09/07 20:04:44 buschelm Exp $*/
2 /*
3   Defines a matrix-matrix product for 2 SeqAIJ matrices
4           C = A * B
5 */
6 
7 #include "src/mat/impls/aij/seq/aij.h"
8 
9 static int logkey_matmatmult            = 0;
10 static int logkey_matmatmult_symbolic   = 0;
11 static int logkey_matmatmult_numeric    = 0;
12 
13 static int logkey_matapplyptap          = 0;
14 static int logkey_matapplyptap_symbolic = 0;
15 static int logkey_matapplyptap_numeric  = 0;
16 
17 typedef struct _Space *FreeSpaceList;
18 typedef struct _Space {
19   FreeSpaceList more_space;
20   int           *array;
21   int           *array_head;
22   int           total_array_size;
23   int           local_used;
24   int           local_remaining;
25 } FreeSpace;
26 
27 #undef __FUNCT__
28 #define __FUNCT__ "GetMoreSpace"
29 int GetMoreSpace(int size,FreeSpaceList *list) {
30   FreeSpaceList a;
31   int ierr;
32 
33   PetscFunctionBegin;
34   ierr = PetscMalloc(sizeof(FreeSpace),&a);CHKERRQ(ierr);
35   ierr = PetscMalloc(size*sizeof(int),&(a->array_head));CHKERRQ(ierr);
36   a->array            = a->array_head;
37   a->local_remaining  = size;
38   a->local_used       = 0;
39   a->total_array_size = 0;
40   a->more_space       = NULL;
41 
42   if (*list) {
43     (*list)->more_space = a;
44     a->total_array_size = (*list)->total_array_size;
45   }
46 
47   a->total_array_size += size;
48   *list               =  a;
49   PetscFunctionReturn(0);
50 }
51 
52 #undef __FUNCT__
53 #define __FUNCT__ "MakeSpaceContiguous"
54 int MakeSpaceContiguous(int *space,FreeSpaceList *head) {
55   FreeSpaceList a;
56   int           ierr;
57 
58   PetscFunctionBegin;
59   while ((*head)!=NULL) {
60     a     =  (*head)->more_space;
61     ierr  =  PetscMemcpy(space,(*head)->array_head,((*head)->local_used)*sizeof(int));CHKERRQ(ierr);
62     space += (*head)->local_used;
63     ierr  =  PetscFree((*head)->array_head);CHKERRQ(ierr);
64     ierr  =  PetscFree(*head);CHKERRQ(ierr);
65     *head =  a;
66   }
67   PetscFunctionReturn(0);
68 }
69 
70 /*
71      MatMatMult_SeqAIJ_SeqAIJ_Symbolic - Forms the symbolic product of two SeqAIJ matrices
72            C=A*B;
73 
74      Note: C is assumed to be uninitialized.
75            If this is not the case, Destroy C before calling this routine.
76 */
77 #undef __FUNCT__
78 #define __FUNCT__ "MatMatMult_SeqAIJ_SeqAIJ_Symbolic"
79 int MatMatMult_SeqAIJ_SeqAIJ_Symbolic(Mat A,Mat B,Mat *C)
80 {
81   int            ierr;
82   FreeSpaceList  free_space=PETSC_NULL,current_space=PETSC_NULL;
83   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data,*c;
84   int            aishift=a->indexshift,bishift=b->indexshift;
85   int            *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*bjj;
86   int            *ci,*cj,*densefill,*sparsefill;
87   int            an=A->N,am=A->M,bn=B->N,bm=B->M;
88   int            i,j,k,anzi,brow,bnzj,cnzi;
89   MatScalar      *ca;
90 
91   PetscFunctionBegin;
92   /* some error checking which could be moved into interface layer */
93   if (aishift || bishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
94   if (an!=bm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",an,bm);
95 
96   if (!logkey_matmatmult_symbolic) {
97     ierr = PetscLogEventRegister(&logkey_matmatmult_symbolic,"MatMatMult_Symbolic",MAT_COOKIE);CHKERRQ(ierr);
98   }
99   ierr = PetscLogEventBegin(logkey_matmatmult_symbolic,A,B,0,0);CHKERRQ(ierr);
100 
101   /* Set up */
102   /* Allocate ci array, arrays for fill computation and */
103   /* free space for accumulating nonzero column info */
104   ierr = PetscMalloc(((am+1)+1)*sizeof(int),&ci);CHKERRQ(ierr);
105   ci[0] = 0;
106 
107   ierr = PetscMalloc((2*bn+1)*sizeof(int),&densefill);CHKERRQ(ierr);
108   ierr = PetscMemzero(densefill,(2*bn+1)*sizeof(int));CHKERRQ(ierr);
109   sparsefill = densefill + bn;
110 
111   /* Initial FreeSpace size is nnz(B)=bi[bm] */
112   ierr          = GetMoreSpace(bi[bm],&free_space);CHKERRQ(ierr);
113   current_space = free_space;
114 
115   /* Determine fill for each row: */
116   for (i=0;i<am;i++) {
117     anzi = ai[i+1] - ai[i];
118     cnzi = 0;
119     for (j=0;j<anzi;j++) {
120       brow = *aj++;
121       bnzj = bi[brow+1] - bi[brow];
122       bjj  = bj + bi[brow];
123       for (k=0;k<bnzj;k++) {
124         /* If column is not marked, mark it in compressed and uncompressed locations. */
125         /* For simplicity, leave uncompressed row unsorted until finished with row, */
126         /* and increment nonzero count for this row. */
127         if (!densefill[bjj[k]]) {
128           densefill[bjj[k]]  = -1;
129           sparsefill[cnzi++] = bjj[k];
130         }
131       }
132     }
133 
134     /* sort sparsefill */
135     ierr = PetscSortInt(cnzi,sparsefill);CHKERRQ(ierr);
136 
137     /* If free space is not available, make more free space */
138     /* Double the amount of total space in the list */
139     if (current_space->local_remaining<cnzi) {
140       ierr = GetMoreSpace(current_space->total_array_size,&current_space);CHKERRQ(ierr);
141     }
142 
143     /* Copy data into free space, and zero out densefill */
144     ierr = PetscMemcpy(current_space->array,sparsefill,cnzi*sizeof(int));CHKERRQ(ierr);
145     current_space->array           += cnzi;
146     current_space->local_used      += cnzi;
147     current_space->local_remaining -= cnzi;
148     for (j=0;j<cnzi;j++) {
149       densefill[sparsefill[j]] = 0;
150     }
151     ci[i+1] = ci[i] + cnzi;
152   }
153 
154   /* nnz is now stored in ci[am], column indices are in the list of free space */
155   /* Allocate space for cj, initialize cj, and */
156   /* destroy list of free space and other temporary array(s) */
157   ierr = PetscMalloc((ci[am]+1)*sizeof(int),&cj);CHKERRQ(ierr);
158   ierr = MakeSpaceContiguous(cj,&free_space);CHKERRQ(ierr);
159   ierr = PetscFree(densefill);CHKERRQ(ierr);
160 
161   /* Allocate space for ca */
162   ierr = PetscMalloc((ci[am]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr);
163   ierr = PetscMemzero(ca,(ci[am]+1)*sizeof(MatScalar));CHKERRQ(ierr);
164 
165   /* put together the new matrix */
166   ierr = MatCreateSeqAIJWithArrays(A->comm,am,bn,ci,cj,ca,C);CHKERRQ(ierr);
167 
168   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
169   /* These are PETSc arrays, so change flags so arrays can be deleted by PETSc */
170   c = (Mat_SeqAIJ *)((*C)->data);
171   c->freedata = PETSC_TRUE;
172   c->nonew    = 0;
173 
174   ierr = PetscLogEventEnd(logkey_matmatmult_symbolic,A,B,0,0);CHKERRQ(ierr);
175   PetscFunctionReturn(0);
176 }
177 
178 /*
179      MatMatMult_SeqAIJ_SeqAIJ_Numeric - Forms the numeric product of two SeqAIJ matrices
180            C=A*B;
181      Note: C must have been created by calling MatMatMult_SeqAIJ_SeqAIJ_Symbolic.
182 */
183 #undef __FUNCT__
184 #define __FUNCT__ "MatMatMult_SeqAIJ_SeqAIJ_Numeric"
185 int MatMatMult_SeqAIJ_SeqAIJ_Numeric(Mat A,Mat B,Mat C)
186 {
187   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
188   Mat_SeqAIJ *b = (Mat_SeqAIJ *)B->data;
189   Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data;
190   int        aishift=a->indexshift,bishift=b->indexshift,cishift=c->indexshift;
191   int        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*bjj,*ci=c->i,*cj=c->j;
192   int        an=A->N,am=A->M,bn=B->N,bm=B->M,cn=C->N,cm=C->M;
193   int        ierr,i,j,k,anzi,bnzi,cnzi,brow,flops;
194   MatScalar  *aa=a->a,*ba=b->a,*baj,*ca=c->a,*temp;
195 
196   PetscFunctionBegin;
197 
198   /* This error checking should be unnecessary if the symbolic was performed */
199   if (aishift || bishift || cishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
200   if (am!=cm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",am,cm);
201   if (an!=bm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",an,bm);
202   if (bn!=cn) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",bn,cn);
203 
204   if (!logkey_matmatmult_numeric) {
205     ierr = PetscLogEventRegister(&logkey_matmatmult_numeric,"MatMatMult_Numeric",MAT_COOKIE);CHKERRQ(ierr);
206   }
207   ierr = PetscLogEventBegin(logkey_matmatmult_numeric,A,B,C,0);CHKERRQ(ierr);
208   flops = 0;
209   /* Allocate temp accumulation space to avoid searching for nonzero columns in C */
210   ierr = PetscMalloc((cn+1)*sizeof(MatScalar),&temp);CHKERRQ(ierr);
211   ierr = PetscMemzero(temp,cn*sizeof(MatScalar));CHKERRQ(ierr);
212   /* Traverse A row-wise. */
213   /* Build the ith row in C by summing over nonzero columns in A, */
214   /* the rows of B corresponding to nonzeros of A. */
215   for (i=0;i<am;i++) {
216     anzi = ai[i+1] - ai[i];
217     for (j=0;j<anzi;j++) {
218       brow = *aj++;
219       bnzi = bi[brow+1] - bi[brow];
220       bjj  = bj + bi[brow];
221       baj  = ba + bi[brow];
222       for (k=0;k<bnzi;k++) {
223         temp[bjj[k]] += (*aa)*baj[k];
224       }
225       flops += 2*bnzi;
226       aa++;
227     }
228     /* Store row back into C, and re-zero temp */
229     cnzi = ci[i+1] - ci[i];
230     for (j=0;j<cnzi;j++) {
231       ca[j] = temp[cj[j]];
232       temp[cj[j]] = 0.0;
233     }
234     ca += cnzi;
235     cj += cnzi;
236   }
237   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
238   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
239 
240   /* Free temp */
241   ierr = PetscFree(temp);CHKERRQ(ierr);
242   ierr = PetscLogFlops(flops);CHKERRQ(ierr);
243   ierr = PetscLogEventEnd(logkey_matmatmult_numeric,A,B,C,0);CHKERRQ(ierr);
244   PetscFunctionReturn(0);
245 }
246 
247 #undef __FUNCT__
248 #define __FUNCT__ "MatMatMult_SeqAIJ_SeqAIJ"
249 int MatMatMult_SeqAIJ_SeqAIJ(Mat A,Mat B,Mat *C) {
250   int ierr;
251 
252   PetscFunctionBegin;
253   if (!logkey_matmatmult) {
254     ierr = PetscLogEventRegister(&logkey_matmatmult,"MatMatMult",MAT_COOKIE);CHKERRQ(ierr);
255   }
256   ierr = PetscLogEventBegin(logkey_matmatmult,A,B,0,0);CHKERRQ(ierr);
257 
258   ierr = MatMatMult_SeqAIJ_SeqAIJ_Symbolic(A,B,C);CHKERRQ(ierr);
259   ierr = MatMatMult_SeqAIJ_SeqAIJ_Numeric(A,B,*C);CHKERRQ(ierr);
260   ierr = PetscLogEventEnd(logkey_matmatmult,A,B,0,0);CHKERRQ(ierr);
261   PetscFunctionReturn(0);
262 }
263 
264 #undef __FUNCT__
265 #define __FUNCT__ "MatApplyPtAP_SeqAIJ_Symbolic"
266 int MatApplyPtAP_SeqAIJ_Symbolic(Mat A,Mat P,Mat *C) {
267   int ierr;
268   FreeSpaceList  free_space=PETSC_NULL,current_space=PETSC_NULL;
269   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*p=(Mat_SeqAIJ*)P->data,*c;
270   int            aishift=a->indexshift,pishift=p->indexshift;
271   int            *pti,*ptj,*ptfill,*ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pjj;
272   int            *ci,*cj,*densefill,*sparsefill,*ptadensefill,*ptasparsefill,*ptaj;
273   int            an=A->N,am=A->M,pn=P->N,pm=P->M;
274   int            i,j,k,ptnzi,arow,anzj,ptanzi,prow,pnzj,cnzi;
275   MatScalar      *ca;
276 
277   PetscFunctionBegin;
278 
279   /* some error checking which could be moved into interface layer */
280   if (aishift || pishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
281   if (pm!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pm,an);
282   if (am!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %d != %d",am, an);
283 
284   if (!logkey_matapplyptap_symbolic) {
285     ierr = PetscLogEventRegister(&logkey_matapplyptap_symbolic,"MatApplyPtAP_Symbolic",MAT_COOKIE);CHKERRQ(ierr);
286   }
287   ierr = PetscLogEventBegin(logkey_matapplyptap_symbolic,A,P,0,0);CHKERRQ(ierr);
288 
289   /* Create ij structure of P^T */
290   /* Recall in P^T there are pn rows and pi[pm] nonzeros. */
291   ierr = PetscMalloc((pn+1+pi[pm])*sizeof(int),&pti);CHKERRQ(ierr);
292   ierr = PetscMemzero(pti,(pn+1+pi[pm])*sizeof(int));CHKERRQ(ierr);
293   ptj = pti + pn+1;
294 
295   /* Walk through pj and count ## of non-zeros in each row of P^T. */
296   for (i=0;i<pi[pm];i++) {
297     pti[pj[i]+1] += 1;
298   }
299   /* Form pti for csr format of P^T. */
300   for (i=0;i<pn;i++) {
301     pti[i+1] += pti[i];
302   }
303 
304   /* Allocate temporary space for next insert location in each row of P^T. */
305   ierr = PetscMalloc(pn*sizeof(int),&ptfill);CHKERRQ(ierr);
306   ierr = PetscMemcpy(ptfill,pti,pn*sizeof(int));CHKERRQ(ierr);
307 
308   /* Walk through P row-wise and mark nonzero entries of P^T. */
309   for (i=0;i<pm;i++) {
310     pnzj = pi[i+1] - pi[i];
311     for (j=0;j<pnzj;j++) {
312       ptj[ptfill[*pj]] =  i;
313       ptfill[*pj++]      += 1;
314     }
315   }
316   pj = p->j;
317 
318   /* Clean-up temporary space. */
319   ierr = PetscFree(ptfill);CHKERRQ(ierr);
320 
321   /* Allocate ci array, arrays for fill computation and */
322   /* free space for accumulating nonzero column info */
323   ierr = PetscMalloc(((pn+1)*1)*sizeof(int),&ci);CHKERRQ(ierr);
324   ci[0] = 0;
325 
326   ierr = PetscMalloc((2*pn+2*an+1)*sizeof(int),&ptadensefill);CHKERRQ(ierr);
327   ierr = PetscMemzero(ptadensefill,(2*pn+2*an+1)*sizeof(int));CHKERRQ(ierr);
328   ptasparsefill = ptadensefill  + an;
329   densefill     = ptasparsefill + an;
330   sparsefill    = densefill     + pn;
331 
332   /* Set initial free space to be nnz(A) scaled by aspect ratio of P. */
333   /* Reason: Take pn/pm = 1/2. */
334   /*         P^T*A*P will take A(NxN) and create C(N/2xN/2). */
335   /*         If C has same sparsity pattern as A, nnz(C)~1/2*nnz(A). */
336   /*         Is this reasonable???? */
337   ierr          = GetMoreSpace((ai[am]/pm)*pn,&free_space);
338   current_space = free_space;
339 
340   /* Determine fill for each row of C: */
341   for (i=0;i<pn;i++) {
342     ptnzi  = pti[i+1] - pti[i];
343     ptanzi = 0;
344     /* Determine fill for row of PtA: */
345     for (j=0;j<ptnzi;j++) {
346       arow = *ptj++;
347       anzj = ai[arow+1] - ai[arow];
348       ajj  = aj + ai[arow];
349       for (k=0;k<anzj;k++) {
350         if (!ptadensefill[ajj[k]]) {
351           ptadensefill[ajj[k]]    = -1;
352           ptasparsefill[ptanzi++] = ajj[k];
353         }
354       }
355     }
356     /* Using fill info for row of PtA, determine fill for row of C: */
357     ptaj = ptasparsefill;
358     cnzi   = 0;
359     for (j=0;j<ptanzi;j++) {
360       prow = *ptaj++;
361       pnzj = pi[prow+1] - pi[prow];
362       pjj  = pj + pi[prow];
363       for (k=0;k<pnzj;k++) {
364         if (!densefill[pjj[k]]) {
365           densefill[pjj[k]]  = -1;
366           sparsefill[cnzi++] = pjj[k];
367         }
368       }
369     }
370 
371     /* sort sparsefill */
372     ierr = PetscSortInt(cnzi,sparsefill);CHKERRQ(ierr);
373 
374     /* If free space is not available, make more free space */
375     /* Double the amount of total space in the list */
376     if (current_space->local_remaining<cnzi) {
377       ierr = GetMoreSpace(current_space->total_array_size,&current_space);CHKERRQ(ierr);
378     }
379 
380     /* Copy data into free space, and zero out densefills */
381     ierr = PetscMemcpy(current_space->array,sparsefill,cnzi*sizeof(int));CHKERRQ(ierr);
382     current_space->array           += cnzi;
383     current_space->local_used      += cnzi;
384     current_space->local_remaining -= cnzi;
385 
386     for (j=0;j<ptanzi;j++) {
387       ptadensefill[ptasparsefill[j]] = 0;
388     }
389     for (j=0;j<cnzi;j++) {
390       densefill[sparsefill[j]] = 0;
391     }
392     /* Aside: Perhaps we should save the pta info for the numerical factorization. */
393     /*        For now, we will recompute what is needed. */
394     ci[i+1] = ci[i] + cnzi;
395   }
396   /* nnz is now stored in ci[ptm], column indices are in the list of free space */
397   /* Allocate space for cj, initialize cj, and */
398   /* destroy list of free space and other temporary array(s) */
399   ierr = PetscMalloc((ci[pn]+1)*sizeof(int),&cj);CHKERRQ(ierr);
400   ierr = MakeSpaceContiguous(cj,&free_space);CHKERRQ(ierr);
401   ierr = PetscFree(ptadensefill);CHKERRQ(ierr);
402 
403   /* Allocate space for ca */
404   ierr = PetscMalloc((ci[pn]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr);
405   ierr = PetscMemzero(ca,(ci[pn]+1)*sizeof(MatScalar));CHKERRQ(ierr);
406 
407   /* put together the new matrix */
408   ierr = MatCreateSeqAIJWithArrays(A->comm,pn,pn,ci,cj,ca,C);CHKERRQ(ierr);
409 
410   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
411   /* Since these are PETSc arrays, change flags to free them as necessary. */
412   c = (Mat_SeqAIJ *)((*C)->data);
413   c->freedata = PETSC_TRUE;
414   c->nonew    = 0;
415 
416   /* Clean up. */
417   /* Perhaps we should attach the (i,j) info for P^T to P for future use. */
418   /* For now, we won't. */
419   ierr = PetscFree(pti);
420 
421   ierr = PetscLogEventEnd(logkey_matapplyptap_symbolic,A,P,0,0);CHKERRQ(ierr);
422   PetscFunctionReturn(0);
423 }
424 
425 #undef __FUNCT__
426 #define __FUNCT__ "MatApplyPtAP_SeqAIJ_Numeric"
427 int MatApplyPtAP_SeqAIJ_Numeric(Mat A,Mat P,Mat C) {
428   int ierr,flops;
429   Mat_SeqAIJ *a  = (Mat_SeqAIJ *) A->data;
430   Mat_SeqAIJ *p  = (Mat_SeqAIJ *) P->data;
431   Mat_SeqAIJ *c  = (Mat_SeqAIJ *) C->data;
432   int        aishift=a->indexshift,pishift=p->indexshift,cishift=c->indexshift;
433   int        *ai=a->i,*aj=a->j,*apj,*apjdense,*pi=p->i,*pj=p->j,*pJ=p->j,*pjj;
434   int        *ci=c->i,*cj=c->j,*cjj;
435   int        an=A->N,am=A->M,pn=P->N,pm=P->M,cn=C->N,cm=C->M;
436   int        i,j,k,anzi,pnzi,apnzj,pnzj,cnzj,prow,crow;
437   MatScalar  *aa=a->a,*apa,*pa=p->a,*pA=p->a,*paj,*ca=c->a,*caj;
438 
439   PetscFunctionBegin;
440 
441   /* This error checking should be unnecessary if the symbolic was performed */
442   if (aishift || pishift || cishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
443   if (pn!=cm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pn,cm);
444   if (pm!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pm,an);
445   if (am!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %d != %d",am, an);
446   if (pn!=cn) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pn, cn);
447 
448   if (!logkey_matapplyptap_numeric) {
449     ierr = PetscLogEventRegister(&logkey_matapplyptap_numeric,"MatApplyPtAP_Numeric",MAT_COOKIE);CHKERRQ(ierr);
450   }
451   ierr = PetscLogEventBegin(logkey_matapplyptap_numeric,A,P,C,0);CHKERRQ(ierr);
452   flops = 0;
453 
454   ierr = PetscMalloc(cn*(sizeof(MatScalar)+2*sizeof(int)),&apa);CHKERRQ(ierr);
455   ierr = PetscMemzero(apa,cn*(sizeof(MatScalar)+2*sizeof(int)));CHKERRQ(ierr);
456   ierr = PetscMemzero(ca,ci[cm]*sizeof(MatScalar));CHKERRQ(ierr);
457 
458   apj      = (int *)(apa + cn);
459   apjdense = apj + cn;
460 
461   for (i=0;i<am;i++) {
462     /* Form sparse row of A*P */
463     anzi  = ai[i+1] - ai[i];
464     apnzj = 0;
465     for (j=0;j<anzi;j++) {
466       prow = *aj++;
467       pnzj = pi[prow+1] - pi[prow];
468       pjj  = pj + pi[prow];
469       paj  = pa + pi[prow];
470       for (k=0;k<pnzj;k++) {
471         if (!apjdense[pjj[k]]) {
472           apjdense[pjj[k]] = -1;
473           apj[apnzj++]     = pjj[k];
474         }
475         apa[pjj[k]] += (*aa)*paj[k];
476       }
477       flops += 2*pnzj;
478       aa++;
479     }
480 
481     /* Sort the j index array for quick sparse axpy. */
482     ierr = PetscSortInt(apnzj,apj);CHKERRQ(ierr);
483 
484     /* Compute P^T*A*P using outer product (P^T)[:,j]*(A*P)[j,:]. */
485     pnzi = pi[i+1] - pi[i];
486     for (j=0;j<pnzi;j++) {
487       int nextap=0;
488       crow = *pJ++;
489       cnzj = ci[crow+1] - ci[crow];
490       cjj  = cj + ci[crow];
491       caj  = ca + ci[crow];
492       /* Perform the sparse axpy operation.  Note cjj includes apj. */
493       for (k=0;nextap<apnzj;k++) {
494         if (cjj[k]==apj[nextap]) {
495           caj[k] += (*pA)*apa[apj[nextap++]];
496         }
497       }
498       flops += 2*apnzj;
499       pA++;
500     }
501 
502     /* Zero the current row info for A*P */
503     for (j=0;j<apnzj;j++) {
504       apa[apj[j]]      = 0.;
505       apjdense[apj[j]] = 0;
506     }
507   }
508 
509   /* Assemble the final matrix and clean up */
510   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
511   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
512   ierr = PetscFree(apa);CHKERRQ(ierr);
513   ierr = PetscLogFlops(flops);CHKERRQ(ierr);
514   ierr = PetscLogEventEnd(logkey_matapplyptap_numeric,A,P,C,0);CHKERRQ(ierr);
515 
516   PetscFunctionReturn(0);
517 }
518 
519 #undef __FUNCT__
520 #define __FUNCT__ "MatApplyPtAP_SeqAIJ"
521 int MatApplyPtAP_SeqAIJ(Mat A,Mat P,Mat *C) {
522   int ierr;
523 
524   PetscFunctionBegin;
525   if (!logkey_matapplyptap) {
526     ierr = PetscLogEventRegister(&logkey_matapplyptap,"MatApplyPtAP",MAT_COOKIE);CHKERRQ(ierr);
527   }
528   ierr = PetscLogEventBegin(logkey_matapplyptap,A,P,0,0);CHKERRQ(ierr);
529   ierr = MatApplyPtAP_SeqAIJ_Symbolic(A,P,C);CHKERRQ(ierr);
530   ierr = MatApplyPtAP_SeqAIJ_Numeric(A,P,*C);CHKERRQ(ierr);
531   ierr = PetscLogEventEnd(logkey_matapplyptap,A,P,0,0);CHKERRQ(ierr);
532   PetscFunctionReturn(0);
533 }
534