xref: /petsc/src/mat/impls/aij/seq/matmatmult.c (revision 94e3eecac3ce3c61d94e758b3e76b5c7eee71cbd)
1 /*$Id: matmatmult.c,v 1.15 2001/09/07 20:04:44 buschelm Exp $*/
2 /*
3   Defines a matrix-matrix product routines for pairs of SeqAIJ matrices
4           C = A * B
5           C = P^T * A * P
6           C = P * A * P^T
7 */
8 
9 #include "src/mat/impls/aij/seq/aij.h"
10 
11 static int logkey_matmatmult            = 0;
12 static int logkey_matmatmult_symbolic   = 0;
13 static int logkey_matmatmult_numeric    = 0;
14 
15 static int logkey_matgetsymtranspose    = 0;
16 static int logkey_mattranspose          = 0;
17 
18 static int logkey_matapplyptap          = 0;
19 static int logkey_matapplyptap_symbolic = 0;
20 static int logkey_matapplyptap_numeric  = 0;
21 
22 static int logkey_matapplypapt          = 0;
23 static int logkey_matapplypapt_symbolic = 0;
24 static int logkey_matapplypapt_numeric  = 0;
25 
26 typedef struct _Space *FreeSpaceList;
27 typedef struct _Space {
28   FreeSpaceList more_space;
29   int           *array;
30   int           *array_head;
31   int           total_array_size;
32   int           local_used;
33   int           local_remaining;
34 } FreeSpace;
35 
36 #undef __FUNCT__
37 #define __FUNCT__ "GetMoreSpace"
38 int GetMoreSpace(int size,FreeSpaceList *list) {
39   FreeSpaceList a;
40   int ierr;
41 
42   PetscFunctionBegin;
43   ierr = PetscMalloc(sizeof(FreeSpace),&a);CHKERRQ(ierr);
44   ierr = PetscMalloc(size*sizeof(int),&(a->array_head));CHKERRQ(ierr);
45   a->array            = a->array_head;
46   a->local_remaining  = size;
47   a->local_used       = 0;
48   a->total_array_size = 0;
49   a->more_space       = NULL;
50 
51   if (*list) {
52     (*list)->more_space = a;
53     a->total_array_size = (*list)->total_array_size;
54   }
55 
56   a->total_array_size += size;
57   *list               =  a;
58   PetscFunctionReturn(0);
59 }
60 
61 #undef __FUNCT__
62 #define __FUNCT__ "MakeSpaceContiguous"
63 int MakeSpaceContiguous(int *space,FreeSpaceList *head) {
64   FreeSpaceList a;
65   int           ierr;
66 
67   PetscFunctionBegin;
68   while ((*head)!=NULL) {
69     a     =  (*head)->more_space;
70     ierr  =  PetscMemcpy(space,(*head)->array_head,((*head)->local_used)*sizeof(int));CHKERRQ(ierr);
71     space += (*head)->local_used;
72     ierr  =  PetscFree((*head)->array_head);CHKERRQ(ierr);
73     ierr  =  PetscFree(*head);CHKERRQ(ierr);
74     *head =  a;
75   }
76   PetscFunctionReturn(0);
77 }
78 
79 /*
80      MatMatMult_Symbolic_SeqAIJ_SeqAIJ - Forms the symbolic product of two SeqAIJ matrices
81            C = A * B;
82 
83      Note: C is assumed to be uncreated.
84            If this is not the case, Destroy C before calling this routine.
85 */
86 #undef __FUNCT__
87 #define __FUNCT__ "MatMatMult_Symbolic_SeqAIJ_SeqAIJ"
88 int MatMatMult_Symbolic_SeqAIJ_SeqAIJ(Mat A,Mat B,Mat *C)
89 {
90   int            ierr;
91   FreeSpaceList  free_space=PETSC_NULL,current_space=PETSC_NULL;
92   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data,*c;
93   int            aishift=a->indexshift,bishift=b->indexshift;
94   int            *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*bjj;
95   int            *ci,*cj,*denserow,*sparserow;
96   int            an=A->N,am=A->M,bn=B->N,bm=B->M;
97   int            i,j,k,anzi,brow,bnzj,cnzi;
98   MatScalar      *ca;
99 
100   PetscFunctionBegin;
101   /* some error checking which could be moved into interface layer */
102   if (aishift || bishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
103   if (an!=bm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",an,bm);
104 
105   /* Set up timers */
106   if (!logkey_matmatmult_symbolic) {
107     ierr = PetscLogEventRegister(&logkey_matmatmult_symbolic,"MatMatMult_Symbolic",MAT_COOKIE);CHKERRQ(ierr);
108   }
109   ierr = PetscLogEventBegin(logkey_matmatmult_symbolic,A,B,0,0);CHKERRQ(ierr);
110 
111   /* Set up */
112   /* Allocate ci array, arrays for fill computation and */
113   /* free space for accumulating nonzero column info */
114   ierr = PetscMalloc(((am+1)+1)*sizeof(int),&ci);CHKERRQ(ierr);
115   ci[0] = 0;
116 
117   ierr = PetscMalloc((2*bn+1)*sizeof(int),&denserow);CHKERRQ(ierr);
118   ierr = PetscMemzero(denserow,(2*bn+1)*sizeof(int));CHKERRQ(ierr);
119   sparserow = denserow + bn;
120 
121   /* Initial FreeSpace size is nnz(B)=bi[bm] */
122   /* No idea what is most reasonable here. */
123   ierr          = GetMoreSpace(bi[bm],&free_space);CHKERRQ(ierr);
124   current_space = free_space;
125 
126   /* Determine symbolic info for each row of the product: */
127   for (i=0;i<am;i++) {
128     anzi = ai[i+1] - ai[i];
129     cnzi = 0;
130     for (j=0;j<anzi;j++) {
131       brow = *aj++;
132       bnzj = bi[brow+1] - bi[brow];
133       bjj  = bj + bi[brow];
134       for (k=0;k<bnzj;k++) {
135         /* If column is not marked, mark it in compressed and uncompressed locations. */
136         /* For simplicity, leave uncompressed row unsorted until finished with row, */
137         /* and increment nonzero count for this row. */
138         if (!denserow[bjj[k]]) {
139           denserow[bjj[k]]  = -1;
140           sparserow[cnzi++] = bjj[k];
141         }
142       }
143     }
144 
145     /* sort sparserow */
146     ierr = PetscSortInt(cnzi,sparserow);CHKERRQ(ierr);
147 
148     /* If free space is not available, make more free space */
149     /* Double the amount of total space in the list */
150     if (current_space->local_remaining<cnzi) {
151       ierr = GetMoreSpace(current_space->total_array_size,&current_space);CHKERRQ(ierr);
152     }
153 
154     /* Copy data into free space, and zero out denserow */
155     ierr = PetscMemcpy(current_space->array,sparserow,cnzi*sizeof(int));CHKERRQ(ierr);
156     current_space->array           += cnzi;
157     current_space->local_used      += cnzi;
158     current_space->local_remaining -= cnzi;
159     for (j=0;j<cnzi;j++) {
160       denserow[sparserow[j]] = 0;
161     }
162     ci[i+1] = ci[i] + cnzi;
163   }
164 
165   /* Column indices are in the list of free space */
166   /* Allocate space for cj, initialize cj, and */
167   /* destroy list of free space and other temporary array(s) */
168   ierr = PetscMalloc((ci[am]+1)*sizeof(int),&cj);CHKERRQ(ierr);
169   ierr = MakeSpaceContiguous(cj,&free_space);CHKERRQ(ierr);
170   ierr = PetscFree(denserow);CHKERRQ(ierr);
171 
172   /* Allocate space for ca */
173   ierr = PetscMalloc((ci[am]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr);
174   ierr = PetscMemzero(ca,(ci[am]+1)*sizeof(MatScalar));CHKERRQ(ierr);
175 
176   /* put together the new matrix */
177   ierr = MatCreateSeqAIJWithArrays(A->comm,am,bn,ci,cj,ca,C);CHKERRQ(ierr);
178 
179   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
180   /* These are PETSc arrays, so change flags so arrays can be deleted by PETSc */
181   c = (Mat_SeqAIJ *)((*C)->data);
182   c->freedata = PETSC_TRUE;
183   c->nonew    = 0;
184 
185   ierr = PetscLogEventEnd(logkey_matmatmult_symbolic,A,B,0,0);CHKERRQ(ierr);
186   PetscFunctionReturn(0);
187 }
188 
189 /*
190      MatMatMult_Numeric_SeqAIJ_SeqAIJ - Forms the numeric product of two SeqAIJ matrices
191            C=A*B;
192      Note: C must have been created by calling MatMatMult_Symbolic_SeqAIJ_SeqAIJ.
193 */
194 #undef __FUNCT__
195 #define __FUNCT__ "MatMatMult_Numeric_SeqAIJ_SeqAIJ"
196 int MatMatMult_Numeric_SeqAIJ_SeqAIJ(Mat A,Mat B,Mat C)
197 {
198   int        ierr,flops=0;
199   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
200   Mat_SeqAIJ *b = (Mat_SeqAIJ *)B->data;
201   Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data;
202   int        aishift=a->indexshift,bishift=b->indexshift,cishift=c->indexshift;
203   int        *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*bjj,*ci=c->i,*cj=c->j;
204   int        an=A->N,am=A->M,bn=B->N,bm=B->M,cn=C->N,cm=C->M;
205   int        i,j,k,anzi,bnzi,cnzi,brow;
206   MatScalar  *aa=a->a,*ba=b->a,*baj,*ca=c->a,*temp;
207 
208   PetscFunctionBegin;
209 
210   /* This error checking should be unnecessary if the symbolic was performed */
211   if (aishift || bishift || cishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
212   if (am!=cm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",am,cm);
213   if (an!=bm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",an,bm);
214   if (bn!=cn) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",bn,cn);
215 
216   /* Set up timers */
217   if (!logkey_matmatmult_numeric) {
218     ierr = PetscLogEventRegister(&logkey_matmatmult_numeric,"MatMatMult_Numeric",MAT_COOKIE);CHKERRQ(ierr);
219   }
220   ierr = PetscLogEventBegin(logkey_matmatmult_numeric,A,B,C,0);CHKERRQ(ierr);
221 
222   /* Allocate temp accumulation space to avoid searching for nonzero columns in C */
223   ierr = PetscMalloc((cn+1)*sizeof(MatScalar),&temp);CHKERRQ(ierr);
224   ierr = PetscMemzero(temp,cn*sizeof(MatScalar));CHKERRQ(ierr);
225   /* Traverse A row-wise. */
226   /* Build the ith row in C by summing over nonzero columns in A, */
227   /* the rows of B corresponding to nonzeros of A. */
228   for (i=0;i<am;i++) {
229     anzi = ai[i+1] - ai[i];
230     for (j=0;j<anzi;j++) {
231       brow = *aj++;
232       bnzi = bi[brow+1] - bi[brow];
233       bjj  = bj + bi[brow];
234       baj  = ba + bi[brow];
235       for (k=0;k<bnzi;k++) {
236         temp[bjj[k]] += (*aa)*baj[k];
237       }
238       flops += 2*bnzi;
239       aa++;
240     }
241     /* Store row back into C, and re-zero temp */
242     cnzi = ci[i+1] - ci[i];
243     for (j=0;j<cnzi;j++) {
244       ca[j] = temp[cj[j]];
245       temp[cj[j]] = 0.0;
246     }
247     ca += cnzi;
248     cj += cnzi;
249   }
250   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
251   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
252 
253   /* Free temp */
254   ierr = PetscFree(temp);CHKERRQ(ierr);
255   ierr = PetscLogFlops(flops);CHKERRQ(ierr);
256   ierr = PetscLogEventEnd(logkey_matmatmult_numeric,A,B,C,0);CHKERRQ(ierr);
257   PetscFunctionReturn(0);
258 }
259 
260 #undef __FUNCT__
261 #define __FUNCT__ "MatMatMult_SeqAIJ_SeqAIJ"
262 int MatMatMult_SeqAIJ_SeqAIJ(Mat A,Mat B,Mat *C) {
263   int ierr;
264 
265   PetscFunctionBegin;
266   if (!logkey_matmatmult) {
267     ierr = PetscLogEventRegister(&logkey_matmatmult,"MatMatMult",MAT_COOKIE);CHKERRQ(ierr);
268   }
269   ierr = PetscLogEventBegin(logkey_matmatmult,A,B,0,0);CHKERRQ(ierr);
270   ierr = MatMatMult_Symbolic_SeqAIJ_SeqAIJ(A,B,C);CHKERRQ(ierr);
271   ierr = MatMatMult_Numeric_SeqAIJ_SeqAIJ(A,B,*C);CHKERRQ(ierr);
272   ierr = PetscLogEventEnd(logkey_matmatmult,A,B,0,0);CHKERRQ(ierr);
273   PetscFunctionReturn(0);
274 }
275 
276 #undef __FUNCT__
277 #define __FUNCT__ "MatGetSymbolicTranspose_SeqIJ"
278 int MatGetSymbolicTranspose_SeqAIJ(Mat A,int *Ati[],int *Atj[]) {
279   int        ierr,i,j,anzj;
280   Mat_SeqAIJ *a=(Mat_SeqAIJ *)A->data;
281   int        aishift = a->indexshift,an=A->N,am=A->M;
282   int        *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
283 
284   PetscFunctionBegin;
285 
286   ierr = PetscLogInfo(A,"Getting Symbolic Transpose.\n");CHKERRQ(ierr);
287   if (aishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
288 
289   /* Set up timers */
290   if (!logkey_matgetsymtranspose) {
291     ierr = PetscLogEventRegister(&logkey_matgetsymtranspose,"MatGetSymbolicTranspose",MAT_COOKIE);CHKERRQ(ierr);
292   }
293   ierr = PetscLogEventBegin(logkey_matgetsymtranspose,A,0,0,0);CHKERRQ(ierr);
294 
295   /* Allocate space for symbolic transpose info and work array */
296   ierr = PetscMalloc((an+1)*sizeof(int),&ati);CHKERRQ(ierr);
297   ierr = PetscMalloc(ai[am]*sizeof(int),&atj);CHKERRQ(ierr);
298   ierr = PetscMalloc(an*sizeof(int),&atfill);CHKERRQ(ierr);
299   ierr = PetscMemzero(ati,(an+1)*sizeof(int));CHKERRQ(ierr);
300 
301   /* Walk through aj and count ## of non-zeros in each row of A^T. */
302   /* Note: offset by 1 for fast conversion into csr format. */
303   for (i=0;i<ai[am];i++) {
304     ati[aj[i]+1] += 1;
305   }
306   /* Form ati for csr format of A^T. */
307   for (i=0;i<an;i++) {
308     ati[i+1] += ati[i];
309   }
310 
311   /* Copy ati into atfill so we have locations of the next free space in atj */
312   ierr = PetscMemcpy(atfill,ati,an*sizeof(int));CHKERRQ(ierr);
313 
314   /* Walk through A row-wise and mark nonzero entries of A^T. */
315   for (i=0;i<am;i++) {
316     anzj = ai[i+1] - ai[i];
317     for (j=0;j<anzj;j++) {
318       atj[atfill[*aj]] = i;
319       atfill[*aj++]   += 1;
320     }
321   }
322 
323   /* Clean up temporary space and complete requests. */
324   ierr = PetscFree(atfill);CHKERRQ(ierr);
325   *Ati = ati;
326   *Atj = atj;
327 
328   ierr = PetscLogEventEnd(logkey_matgetsymtranspose,A,0,0,0);CHKERRQ(ierr);
329   PetscFunctionReturn(0);
330 }
331 
332 extern int MatTranspose_SeqAIJ(Mat A,Mat *B);
333 
334 #undef __FUNCT__
335 #define __FUNCT__ "MatTranspose_SeqIJ_FAST"
336 int MatTranspose_SeqAIJ_FAST(Mat A,Mat *B) {
337   int        ierr,i,j,anzj;
338   Mat        At;
339   Mat_SeqAIJ *a=(Mat_SeqAIJ *)A->data,*at;
340   int        aishift = a->indexshift,an=A->N,am=A->M;
341   int        *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
342   MatScalar  *ata,*aa=a->a;
343   PetscFunctionBegin;
344 
345   if (aishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
346 
347   /* Set up timers */
348   if (!logkey_mattranspose) {
349     ierr = PetscLogEventRegister(&logkey_mattranspose,"MatTranspose_SeqAIJ_FAST",MAT_COOKIE);CHKERRQ(ierr);
350   }
351   ierr = PetscLogEventBegin(logkey_mattranspose,A,0,0,0);CHKERRQ(ierr);
352 
353   /* Allocate space for symbolic transpose info and work array */
354   ierr = PetscMalloc((an+1)*sizeof(int),&ati);CHKERRQ(ierr);
355   ierr = PetscMalloc(ai[am]*sizeof(int),&atj);CHKERRQ(ierr);
356   ierr = PetscMalloc(ai[am]*sizeof(MatScalar),&ata);CHKERRQ(ierr);
357   ierr = PetscMalloc(an*sizeof(int),&atfill);CHKERRQ(ierr);
358   ierr = PetscMemzero(ati,(an+1)*sizeof(int));CHKERRQ(ierr);
359   /* Walk through aj and count ## of non-zeros in each row of A^T. */
360   /* Note: offset by 1 for fast conversion into csr format. */
361   for (i=0;i<ai[am];i++) {
362     ati[aj[i]+1] += 1;
363   }
364   /* Form ati for csr format of A^T. */
365   for (i=0;i<an;i++) {
366     ati[i+1] += ati[i];
367   }
368 
369   /* Copy ati into atfill so we have locations of the next free space in atj */
370   ierr = PetscMemcpy(atfill,ati,an*sizeof(int));CHKERRQ(ierr);
371 
372   /* Walk through A row-wise and mark nonzero entries of A^T. */
373   for (i=0;i<am;i++) {
374     anzj = ai[i+1] - ai[i];
375     for (j=0;j<anzj;j++) {
376       atj[atfill[*aj]] = i;
377       ata[atfill[*aj]] = *aa++;
378       atfill[*aj++]   += 1;
379     }
380   }
381 
382   /* Clean up temporary space and complete requests. */
383   ierr = PetscFree(atfill);CHKERRQ(ierr);
384   ierr = MatCreateSeqAIJWithArrays(A->comm,an,am,ati,atj,ata,&At);CHKERRQ(ierr);
385   at   = (Mat_SeqAIJ *)(At->data);
386   at->freedata = PETSC_TRUE;
387   at->nonew    = 0;
388   if (B) {
389     *B = At;
390   } else {
391     ierr = MatHeaderCopy(A,At);
392   }
393   ierr = PetscLogEventEnd(logkey_mattranspose,A,0,0,0);CHKERRQ(ierr);
394   PetscFunctionReturn(0);
395 }
396 
397 #undef __FUNCT__
398 #define __FUNCT__ "MatRestoreSymbolicTranspose"
399 int MatRestoreSymbolicTranspose(Mat A,int *ati[],int *atj[]) {
400   int ierr;
401 
402   PetscFunctionBegin;
403   ierr = PetscLogInfo(A,"Restoring Symbolic Transpose.\n");CHKERRQ(ierr);
404   ierr = PetscFree(*ati);CHKERRQ(ierr);
405   ati  = PETSC_NULL;
406   ierr = PetscFree(*atj);CHKERRQ(ierr);
407   atj  = PETSC_NULL;
408   PetscFunctionReturn(0);
409 }
410 
411 /*
412      MatApplyPtAP_Symbolic_SeqAIJ - Forms the symbolic product of two SeqAIJ matrices
413            C = P^T * A * P;
414 
415      Note: C is assumed to be uncreated.
416            If this is not the case, Destroy C before calling this routine.
417 */
418 #undef __FUNCT__
419 #define __FUNCT__ "MatApplyPtAP_Symbolic_SeqAIJ"
420 int MatApplyPtAP_Symbolic_SeqAIJ(Mat A,Mat P,Mat *C) {
421   int            ierr;
422   FreeSpaceList  free_space=PETSC_NULL,current_space=PETSC_NULL;
423   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*p=(Mat_SeqAIJ*)P->data,*c;
424   int            aishift=a->indexshift,pishift=p->indexshift;
425   int            *pti,*ptj,*ptJ,*ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pjj;
426   int            *ci,*cj,*denserow,*sparserow,*ptadenserow,*ptasparserow,*ptaj;
427   int            an=A->N,am=A->M,pn=P->N,pm=P->M;
428   int            i,j,k,ptnzi,arow,anzj,ptanzi,prow,pnzj,cnzi;
429   MatScalar      *ca;
430 
431   PetscFunctionBegin;
432 
433   /* some error checking which could be moved into interface layer */
434   if (aishift || pishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
435   if (pm!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pm,an);
436   if (am!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %d != %d",am, an);
437 
438   /* Set up timers */
439   if (!logkey_matapplyptap_symbolic) {
440     ierr = PetscLogEventRegister(&logkey_matapplyptap_symbolic,"MatApplyPtAP_Symbolic",MAT_COOKIE);CHKERRQ(ierr);
441   }
442   ierr = PetscLogEventBegin(logkey_matapplyptap_symbolic,A,P,0,0);CHKERRQ(ierr);
443 
444   /* Get ij structure of P^T */
445   ierr = MatGetSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr);
446   ptJ=ptj;
447 
448   /* Allocate ci array, arrays for fill computation and */
449   /* free space for accumulating nonzero column info */
450   ierr = PetscMalloc(((pn+1)*1)*sizeof(int),&ci);CHKERRQ(ierr);
451   ci[0] = 0;
452 
453   ierr = PetscMalloc((2*pn+2*an+1)*sizeof(int),&ptadenserow);CHKERRQ(ierr);
454   ierr = PetscMemzero(ptadenserow,(2*pn+2*an+1)*sizeof(int));CHKERRQ(ierr);
455   ptasparserow = ptadenserow  + an;
456   denserow     = ptasparserow + an;
457   sparserow    = denserow     + pn;
458 
459   /* Set initial free space to be nnz(A) scaled by aspect ratio of P. */
460   /* This should be reasonable if sparsity of PtAP is similar to that of A. */
461   ierr          = GetMoreSpace((ai[am]/pm)*pn,&free_space);
462   current_space = free_space;
463 
464   /* Determine symbolic info for each row of C: */
465   for (i=0;i<pn;i++) {
466     ptnzi  = pti[i+1] - pti[i];
467     ptanzi = 0;
468     /* Determine symbolic row of PtA: */
469     for (j=0;j<ptnzi;j++) {
470       arow = *ptJ++;
471       anzj = ai[arow+1] - ai[arow];
472       ajj  = aj + ai[arow];
473       for (k=0;k<anzj;k++) {
474         if (!ptadenserow[ajj[k]]) {
475           ptadenserow[ajj[k]]    = -1;
476           ptasparserow[ptanzi++] = ajj[k];
477         }
478       }
479     }
480     /* Using symbolic info for row of PtA, determine symbolic info for row of C: */
481     ptaj = ptasparserow;
482     cnzi   = 0;
483     for (j=0;j<ptanzi;j++) {
484       prow = *ptaj++;
485       pnzj = pi[prow+1] - pi[prow];
486       pjj  = pj + pi[prow];
487       for (k=0;k<pnzj;k++) {
488         if (!denserow[pjj[k]]) {
489           denserow[pjj[k]]  = -1;
490           sparserow[cnzi++] = pjj[k];
491         }
492       }
493     }
494 
495     /* sort sparserow */
496     ierr = PetscSortInt(cnzi,sparserow);CHKERRQ(ierr);
497 
498     /* If free space is not available, make more free space */
499     /* Double the amount of total space in the list */
500     if (current_space->local_remaining<cnzi) {
501       ierr = GetMoreSpace(current_space->total_array_size,&current_space);CHKERRQ(ierr);
502     }
503 
504     /* Copy data into free space, and zero out denserows */
505     ierr = PetscMemcpy(current_space->array,sparserow,cnzi*sizeof(int));CHKERRQ(ierr);
506     current_space->array           += cnzi;
507     current_space->local_used      += cnzi;
508     current_space->local_remaining -= cnzi;
509 
510     for (j=0;j<ptanzi;j++) {
511       ptadenserow[ptasparserow[j]] = 0;
512     }
513     for (j=0;j<cnzi;j++) {
514       denserow[sparserow[j]] = 0;
515     }
516     /* Aside: Perhaps we should save the pta info for the numerical factorization. */
517     /*        For now, we will recompute what is needed. */
518     ci[i+1] = ci[i] + cnzi;
519   }
520   /* nnz is now stored in ci[ptm], column indices are in the list of free space */
521   /* Allocate space for cj, initialize cj, and */
522   /* destroy list of free space and other temporary array(s) */
523   ierr = PetscMalloc((ci[pn]+1)*sizeof(int),&cj);CHKERRQ(ierr);
524   ierr = MakeSpaceContiguous(cj,&free_space);CHKERRQ(ierr);
525   ierr = PetscFree(ptadenserow);CHKERRQ(ierr);
526 
527   /* Allocate space for ca */
528   ierr = PetscMalloc((ci[pn]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr);
529   ierr = PetscMemzero(ca,(ci[pn]+1)*sizeof(MatScalar));CHKERRQ(ierr);
530 
531   /* put together the new matrix */
532   ierr = MatCreateSeqAIJWithArrays(A->comm,pn,pn,ci,cj,ca,C);CHKERRQ(ierr);
533 
534   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
535   /* Since these are PETSc arrays, change flags to free them as necessary. */
536   c = (Mat_SeqAIJ *)((*C)->data);
537   c->freedata = PETSC_TRUE;
538   c->nonew    = 0;
539 
540   /* Clean up. */
541   ierr = MatRestoreSymbolicTranspose(P,&pti,&ptj);CHKERRQ(ierr);
542 
543   ierr = PetscLogEventEnd(logkey_matapplyptap_symbolic,A,P,0,0);CHKERRQ(ierr);
544   PetscFunctionReturn(0);
545 }
546 
547 /*
548      MatApplyPtAP_Numeric_SeqAIJ - Forms the numeric product of two SeqAIJ matrices
549            C = P^T * A * P;
550      Note: C must have been created by calling MatApplyPtAP_Symbolic_SeqAIJ.
551 */
552 #undef __FUNCT__
553 #define __FUNCT__ "MatApplyPtAP_Numeric_SeqAIJ"
554 int MatApplyPtAP_Numeric_SeqAIJ(Mat A,Mat P,Mat C) {
555   int        ierr,flops=0;
556   Mat_SeqAIJ *a  = (Mat_SeqAIJ *) A->data;
557   Mat_SeqAIJ *p  = (Mat_SeqAIJ *) P->data;
558   Mat_SeqAIJ *c  = (Mat_SeqAIJ *) C->data;
559   int        aishift=a->indexshift,pishift=p->indexshift,cishift=c->indexshift;
560   int        *ai=a->i,*aj=a->j,*apj,*apjdense,*pi=p->i,*pj=p->j,*pJ=p->j,*pjj;
561   int        *ci=c->i,*cj=c->j,*cjj;
562   int        an=A->N,am=A->M,pn=P->N,pm=P->M,cn=C->N,cm=C->M;
563   int        i,j,k,anzi,pnzi,apnzj,nextap,pnzj,cnzj,prow,crow;
564   MatScalar  *aa=a->a,*apa,*pa=p->a,*pA=p->a,*paj,*ca=c->a,*caj;
565 
566   PetscFunctionBegin;
567 
568   /* This error checking should be unnecessary if the symbolic was performed */
569   if (aishift || pishift || cishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
570   if (pn!=cm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pn,cm);
571   if (pm!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pm,an);
572   if (am!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %d != %d",am, an);
573   if (pn!=cn) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pn, cn);
574 
575   /* Set up timers */
576   if (!logkey_matapplyptap_numeric) {
577     ierr = PetscLogEventRegister(&logkey_matapplyptap_numeric,"MatApplyPtAP_Numeric",MAT_COOKIE);CHKERRQ(ierr);
578   }
579   ierr = PetscLogEventBegin(logkey_matapplyptap_numeric,A,P,C,0);CHKERRQ(ierr);
580 
581   ierr = PetscMalloc(cn*(sizeof(MatScalar)+2*sizeof(int)),&apa);CHKERRQ(ierr);
582   ierr = PetscMemzero(apa,cn*(sizeof(MatScalar)+2*sizeof(int)));CHKERRQ(ierr);
583   ierr = PetscMemzero(ca,ci[cm]*sizeof(MatScalar));CHKERRQ(ierr);
584 
585   apj      = (int *)(apa + cn);
586   apjdense = apj + cn;
587 
588   for (i=0;i<am;i++) {
589     /* Form sparse row of A*P */
590     anzi  = ai[i+1] - ai[i];
591     apnzj = 0;
592     for (j=0;j<anzi;j++) {
593       prow = *aj++;
594       pnzj = pi[prow+1] - pi[prow];
595       pjj  = pj + pi[prow];
596       paj  = pa + pi[prow];
597       for (k=0;k<pnzj;k++) {
598         if (!apjdense[pjj[k]]) {
599           apjdense[pjj[k]] = -1;
600           apj[apnzj++]     = pjj[k];
601         }
602         apa[pjj[k]] += (*aa)*paj[k];
603       }
604       flops += 2*pnzj;
605       aa++;
606     }
607 
608     /* Sort the j index array for quick sparse axpy. */
609     ierr = PetscSortInt(apnzj,apj);CHKERRQ(ierr);
610 
611     /* Compute P^T*A*P using outer product (P^T)[:,j]*(A*P)[j,:]. */
612     pnzi = pi[i+1] - pi[i];
613     for (j=0;j<pnzi;j++) {
614       nextap = 0;
615       crow   = *pJ++;
616       cnzj   = ci[crow+1] - ci[crow];
617       cjj    = cj + ci[crow];
618       caj    = ca + ci[crow];
619       /* Perform sparse axpy operation.  Note cjj includes apj. */
620       for (k=0;nextap<apnzj;k++) {
621         if (cjj[k]==apj[nextap]) {
622           caj[k] += (*pA)*apa[apj[nextap++]];
623         }
624       }
625       flops += 2*apnzj;
626       pA++;
627     }
628 
629     /* Zero the current row info for A*P */
630     for (j=0;j<apnzj;j++) {
631       apa[apj[j]]      = 0.;
632       apjdense[apj[j]] = 0;
633     }
634   }
635 
636   /* Assemble the final matrix and clean up */
637   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
638   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
639   ierr = PetscFree(apa);CHKERRQ(ierr);
640   ierr = PetscLogFlops(flops);CHKERRQ(ierr);
641   ierr = PetscLogEventEnd(logkey_matapplyptap_numeric,A,P,C,0);CHKERRQ(ierr);
642 
643   PetscFunctionReturn(0);
644 }
645 
646 
647 #undef __FUNCT__
648 #define __FUNCT__ "MatApplyPtAP_SeqAIJ"
649 int MatApplyPtAP_SeqAIJ(Mat A,Mat P,Mat *C) {
650   int ierr;
651 
652   PetscFunctionBegin;
653   if (!logkey_matapplyptap) {
654     ierr = PetscLogEventRegister(&logkey_matapplyptap,"MatApplyPtAP",MAT_COOKIE);CHKERRQ(ierr);
655   }
656   ierr = PetscLogEventBegin(logkey_matapplyptap,A,P,0,0);CHKERRQ(ierr);
657 
658   ierr = MatApplyPtAP_Symbolic_SeqAIJ(A,P,C);CHKERRQ(ierr);
659   ierr = MatApplyPtAP_Numeric_SeqAIJ(A,P,*C);CHKERRQ(ierr);
660 
661   ierr = PetscLogEventEnd(logkey_matapplyptap,A,P,0,0);CHKERRQ(ierr);
662   PetscFunctionReturn(0);
663 }
664 
665 /*
666      MatApplyPAPt_Symbolic_SeqAIJ - Forms the symbolic product of two SeqAIJ matrices
667            C = P * A * P^T;
668 
669      Note: C is assumed to be uncreated.
670            If this is not the case, Destroy C before calling this routine.
671 */
672 #undef __FUNCT__
673 #define __FUNCT__ "MatApplyPAPt_Symbolic_SeqAIJ"
674 int MatApplyPAPt_Symbolic_SeqAIJ(Mat A,Mat P,Mat *C) {
675   /* Note: This code is virtually identical to that of MatApplyPtAP_SeqAIJ_Symbolic */
676   /*        and MatMatMult_SeqAIJ_SeqAIJ_Symbolic.  Perhaps they could be merged nicely. */
677   int            ierr;
678   FreeSpaceList  free_space=PETSC_NULL,current_space=PETSC_NULL;
679   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*p=(Mat_SeqAIJ*)P->data,*c;
680   int            aishift=a->indexshift,pishift=p->indexshift;
681   int            *ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pti,*ptj,*ptjj;
682   int            *ci,*cj,*paj,*padenserow,*pasparserow,*denserow,*sparserow;
683   int            an=A->N,am=A->M,pn=P->N,pm=P->M;
684   int            i,j,k,pnzi,arow,anzj,panzi,ptrow,ptnzj,cnzi;
685   MatScalar      *ca;
686 
687   PetscFunctionBegin;
688 
689   /* some error checking which could be moved into interface layer */
690   if (aishift || pishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
691   if (pn!=am) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pn,am);
692   if (am!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %d != %d",am, an);
693 
694   /* Set up timers */
695   if (!logkey_matapplypapt_symbolic) {
696     ierr = PetscLogEventRegister(&logkey_matapplypapt_symbolic,"MatApplyPAPt_Symbolic",MAT_COOKIE);CHKERRQ(ierr);
697   }
698   ierr = PetscLogEventBegin(logkey_matapplypapt_symbolic,A,P,0,0);CHKERRQ(ierr);
699 
700   /* Create ij structure of P^T */
701   ierr = MatGetSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr);
702 
703   /* Allocate ci array, arrays for fill computation and */
704   /* free space for accumulating nonzero column info */
705   ierr = PetscMalloc(((pm+1)*1)*sizeof(int),&ci);CHKERRQ(ierr);
706   ci[0] = 0;
707 
708   ierr = PetscMalloc((2*an+2*pm+1)*sizeof(int),&padenserow);CHKERRQ(ierr);
709   ierr = PetscMemzero(padenserow,(2*an+2*pm+1)*sizeof(int));CHKERRQ(ierr);
710   pasparserow  = padenserow  + an;
711   denserow     = pasparserow + an;
712   sparserow    = denserow    + pm;
713 
714   /* Set initial free space to be nnz(A) scaled by aspect ratio of Pt. */
715   /* This should be reasonable if sparsity of PAPt is similar to that of A. */
716   ierr          = GetMoreSpace((ai[am]/pn)*pm,&free_space);
717   current_space = free_space;
718 
719   /* Determine fill for each row of C: */
720   for (i=0;i<pm;i++) {
721     pnzi  = pi[i+1] - pi[i];
722     panzi = 0;
723     /* Get symbolic sparse row of PA: */
724     for (j=0;j<pnzi;j++) {
725       arow = *pj++;
726       anzj = ai[arow+1] - ai[arow];
727       ajj  = aj + ai[arow];
728       for (k=0;k<anzj;k++) {
729         if (!padenserow[ajj[k]]) {
730           padenserow[ajj[k]]   = -1;
731           pasparserow[panzi++] = ajj[k];
732         }
733       }
734     }
735     /* Using symbolic row of PA, determine symbolic row of C: */
736     paj    = pasparserow;
737     cnzi   = 0;
738     for (j=0;j<panzi;j++) {
739       ptrow = *paj++;
740       ptnzj = pti[ptrow+1] - pti[ptrow];
741       ptjj  = ptj + pti[ptrow];
742       for (k=0;k<ptnzj;k++) {
743         if (!denserow[ptjj[k]]) {
744           denserow[ptjj[k]] = -1;
745           sparserow[cnzi++] = ptjj[k];
746         }
747       }
748     }
749 
750     /* sort sparse representation */
751     ierr = PetscSortInt(cnzi,sparserow);CHKERRQ(ierr);
752 
753     /* If free space is not available, make more free space */
754     /* Double the amount of total space in the list */
755     if (current_space->local_remaining<cnzi) {
756       ierr = GetMoreSpace(current_space->total_array_size,&current_space);CHKERRQ(ierr);
757     }
758 
759     /* Copy data into free space, and zero out dense row */
760     ierr = PetscMemcpy(current_space->array,sparserow,cnzi*sizeof(int));CHKERRQ(ierr);
761     current_space->array           += cnzi;
762     current_space->local_used      += cnzi;
763     current_space->local_remaining -= cnzi;
764 
765     for (j=0;j<panzi;j++) {
766       padenserow[pasparserow[j]] = 0;
767     }
768     for (j=0;j<cnzi;j++) {
769       denserow[sparserow[j]] = 0;
770     }
771     ci[i+1] = ci[i] + cnzi;
772   }
773   /* column indices are in the list of free space */
774   /* Allocate space for cj, initialize cj, and */
775   /* destroy list of free space and other temporary array(s) */
776   ierr = PetscMalloc((ci[pm]+1)*sizeof(int),&cj);CHKERRQ(ierr);
777   ierr = MakeSpaceContiguous(cj,&free_space);CHKERRQ(ierr);
778   ierr = PetscFree(padenserow);CHKERRQ(ierr);
779 
780   /* Allocate space for ca */
781   ierr = PetscMalloc((ci[pm]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr);
782   ierr = PetscMemzero(ca,(ci[pm]+1)*sizeof(MatScalar));CHKERRQ(ierr);
783 
784   /* put together the new matrix */
785   ierr = MatCreateSeqAIJWithArrays(A->comm,pm,pm,ci,cj,ca,C);CHKERRQ(ierr);
786 
787   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
788   /* Since these are PETSc arrays, change flags to free them as necessary. */
789   c = (Mat_SeqAIJ *)((*C)->data);
790   c->freedata = PETSC_TRUE;
791   c->nonew    = 0;
792 
793   /* Clean up. */
794   ierr = MatRestoreSymbolicTranspose(P,&pti,&ptj);CHKERRQ(ierr);
795 
796   ierr = PetscLogEventEnd(logkey_matapplypapt_symbolic,A,P,0,0);CHKERRQ(ierr);
797   PetscFunctionReturn(0);
798 }
799 
800 /*
801      MatApplyPAPt_Numeric_SeqAIJ - Forms the numeric product of two SeqAIJ matrices
802            C = P * A * P^T;
803      Note: C must have been created by calling MatApplyPAPt_Symbolic_SeqAIJ.
804 */
805 #undef __FUNCT__
806 #define __FUNCT__ "MatApplyPAPt_Numeric_SeqAIJ"
807 int MatApplyPAPt_Numeric_SeqAIJ(Mat A,Mat P,Mat C) {
808   int        ierr,flops=0;
809   Mat_SeqAIJ *a  = (Mat_SeqAIJ *) A->data;
810   Mat_SeqAIJ *p  = (Mat_SeqAIJ *) P->data;
811   Mat_SeqAIJ *c  = (Mat_SeqAIJ *) C->data;
812   int        aishift=a->indexshift,pishift=p->indexshift,cishift=c->indexshift;
813   int        *ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pjj=p->j,*paj,*pajdense,*ptj;
814   int        *ci=c->i,*cj=c->j;
815   int        an=A->N,am=A->M,pn=P->N,pm=P->M,cn=C->N,cm=C->M;
816   int        i,j,k,k1,k2,pnzi,anzj,panzj,arow,ptcol,ptnzj,cnzi;
817   MatScalar  *aa=a->a,*pa=p->a,*pta=p->a,*ptaj,*paa,*aaj,*ca=c->a,sum;
818 
819   PetscFunctionBegin;
820 
821   /* This error checking should be unnecessary if the symbolic was performed */
822   if (aishift || pishift || cishift) SETERRQ(PETSC_ERR_SUP,"Shifted matrix indices are not supported.");
823   if (pm!=cm) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pm,cm);
824   if (pn!=am) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pn,am);
825   if (am!=an) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %d != %d",am, an);
826   if (pm!=cn) SETERRQ2(PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %d != %d",pm, cn);
827 
828   /* Set up timers */
829   if (!logkey_matapplypapt_numeric) {
830     ierr = PetscLogEventRegister(&logkey_matapplypapt_numeric,"MatApplyPAPt_Numeric",MAT_COOKIE);CHKERRQ(ierr);
831   }
832   ierr = PetscLogEventBegin(logkey_matapplypapt_numeric,A,P,C,0);CHKERRQ(ierr);
833 
834   ierr = PetscMalloc(an*(sizeof(MatScalar)+2*sizeof(int)),&paa);CHKERRQ(ierr);
835   ierr = PetscMemzero(paa,an*(sizeof(MatScalar)+2*sizeof(int)));CHKERRQ(ierr);
836   ierr = PetscMemzero(ca,ci[cm]*sizeof(MatScalar));CHKERRQ(ierr);
837 
838   paj      = (int *)(paa + an);
839   pajdense = paj + an;
840 
841   for (i=0;i<pm;i++) {
842     /* Form sparse row of P*A */
843     pnzi  = pi[i+1] - pi[i];
844     panzj = 0;
845     for (j=0;j<pnzi;j++) {
846       arow = *pj++;
847       anzj = ai[arow+1] - ai[arow];
848       ajj  = aj + ai[arow];
849       aaj  = aa + ai[arow];
850       for (k=0;k<anzj;k++) {
851         if (!pajdense[ajj[k]]) {
852           pajdense[ajj[k]] = -1;
853           paj[panzj++]     = ajj[k];
854         }
855         paa[ajj[k]] += (*pa)*aaj[k];
856       }
857       flops += 2*anzj;
858       pa++;
859     }
860 
861     /* Sort the j index array for quick sparse axpy. */
862     ierr = PetscSortInt(panzj,paj);CHKERRQ(ierr);
863 
864     /* Compute P*A*P^T using sparse inner products. */
865     /* Take advantage of pre-computed (i,j) of C for locations of non-zeros. */
866     cnzi = ci[i+1] - ci[i];
867     for (j=0;j<cnzi;j++) {
868       /* Form sparse inner product of current row of P*A with (*cj++) col of P^T. */
869       ptcol = *cj++;
870       ptnzj = pi[ptcol+1] - pi[ptcol];
871       ptj   = pjj + pi[ptcol];
872       ptaj  = pta + pi[ptcol];
873       sum   = 0.;
874       k1    = 0;
875       k2    = 0;
876       while ((k1<panzj) && (k2<ptnzj)) {
877         if (paj[k1]==ptj[k2]) {
878           sum += paa[paj[k1++]]*pta[k2++];
879         } else if (paj[k1] < ptj[k2]) {
880           k1++;
881         } else /* if (paj[k1] > ptj[k2]) */ {
882           k2++;
883         }
884       }
885       *ca++ = sum;
886     }
887 
888     /* Zero the current row info for P*A */
889     for (j=0;j<panzj;j++) {
890       paa[paj[j]]      = 0.;
891       pajdense[paj[j]] = 0;
892     }
893   }
894 
895   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
896   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
897   ierr = PetscLogFlops(flops);CHKERRQ(ierr);
898   ierr = PetscLogEventEnd(logkey_matapplypapt_numeric,A,P,C,0);CHKERRQ(ierr);
899   PetscFunctionReturn(0);
900 }
901 
902 #undef __FUNCT__
903 #define __FUNCT__ "MatApplyPAPt_SeqAIJ"
904 int MatApplyPAPt_SeqAIJ(Mat A,Mat P,Mat *C) {
905   int ierr;
906 
907   PetscFunctionBegin;
908   if (!logkey_matapplypapt) {
909     ierr = PetscLogEventRegister(&logkey_matapplypapt,"MatApplyPAPt",MAT_COOKIE);CHKERRQ(ierr);
910   }
911   ierr = PetscLogEventBegin(logkey_matapplypapt,A,P,0,0);CHKERRQ(ierr);
912   ierr = MatApplyPAPt_Symbolic_SeqAIJ(A,P,C);CHKERRQ(ierr);
913   ierr = MatApplyPAPt_Numeric_SeqAIJ(A,P,*C);CHKERRQ(ierr);
914   ierr = PetscLogEventEnd(logkey_matapplypapt,A,P,0,0);CHKERRQ(ierr);
915   PetscFunctionReturn(0);
916 }
917