xref: /petsc/src/mat/impls/aij/seq/matptap.c (revision 09f3b4e5628a00a1eaf17d80982cfbcc515cc9c1)
1 #define PETSCMAT_DLL
2 
3 /*
4   Defines projective product routines where A is a SeqAIJ matrix
5           C = P^T * A * P
6 */
7 
8 #include "src/mat/impls/aij/seq/aij.h"   /*I "petscmat.h" I*/
9 #include "src/mat/utils/freespace.h"
10 #include "petscbt.h"
11 
12 #undef __FUNCT__
13 #define __FUNCT__ "MatPtAPSymbolic_SeqAIJ"
14 PetscErrorCode MatPtAPSymbolic_SeqAIJ(Mat A,Mat P,PetscReal fill,Mat *C)
15 {
16   PetscErrorCode ierr;
17 
18   PetscFunctionBegin;
19   if (!P->ops->ptapsymbolic_seqaij) {
20     SETERRQ2(PETSC_ERR_SUP,"Not implemented for A=%s and P=%s",A->type_name,P->type_name);
21   }
22   ierr = (*P->ops->ptapsymbolic_seqaij)(A,P,fill,C);CHKERRQ(ierr);
23   PetscFunctionReturn(0);
24 }
25 
26 #undef __FUNCT__
27 #define __FUNCT__ "MatPtAPNumeric_SeqAIJ"
28 PetscErrorCode MatPtAPNumeric_SeqAIJ(Mat A,Mat P,Mat C)
29 {
30   PetscErrorCode ierr;
31 
32   PetscFunctionBegin;
33   if (!P->ops->ptapnumeric_seqaij) {
34     SETERRQ2(PETSC_ERR_SUP,"Not implemented for A=%s and P=%s",A->type_name,P->type_name);
35   }
36   ierr = (*P->ops->ptapnumeric_seqaij)(A,P,C);CHKERRQ(ierr);
37   PetscFunctionReturn(0);
38 }
39 
40 #undef __FUNCT__
41 #define __FUNCT__ "MatPtAPSymbolic_SeqAIJ_SeqAIJ"
42 PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ(Mat A,Mat P,PetscReal fill,Mat *C)
43 {
44   PetscErrorCode     ierr;
45   PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
46   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data,*p = (Mat_SeqAIJ*)P->data,*c;
47   PetscInt           *pti,*ptj,*ptJ,*ai=a->i,*aj=a->j,*ajj,*pi=p->i,*pj=p->j,*pjj;
48   PetscInt           *ci,*cj,*ptadenserow,*ptasparserow,*ptaj;
49   PetscInt           an=A->N,am=A->M,pn=P->N,pm=P->M;
50   PetscInt           i,j,k,ptnzi,arow,anzj,ptanzi,prow,pnzj,cnzi,nlnk,*lnk;
51   MatScalar          *ca;
52   PetscBT            lnkbt;
53 
54   PetscFunctionBegin;
55   /* Get ij structure of P^T */
56   ierr = MatGetSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr);
57   ptJ=ptj;
58 
59   /* Allocate ci array, arrays for fill computation and */
60   /* free space for accumulating nonzero column info */
61   ierr = PetscMalloc((pn+1)*sizeof(PetscInt),&ci);CHKERRQ(ierr);
62   ci[0] = 0;
63 
64   ierr = PetscMalloc((2*an+1)*sizeof(PetscInt),&ptadenserow);CHKERRQ(ierr);
65   ierr = PetscMemzero(ptadenserow,(2*an+1)*sizeof(PetscInt));CHKERRQ(ierr);
66   ptasparserow = ptadenserow  + an;
67 
68   /* create and initialize a linked list */
69   nlnk = pn+1;
70   ierr = PetscLLCreate(pn,pn,nlnk,lnk,lnkbt);CHKERRQ(ierr);
71 
72   /* Set initial free space to be nnz(A) scaled by aspect ratio of P. */
73   /* This should be reasonable if sparsity of PtAP is similar to that of A. */
74   ierr          = PetscFreeSpaceGet((ai[am]/pm)*pn,&free_space);
75   current_space = free_space;
76 
77   /* Determine symbolic info for each row of C: */
78   for (i=0;i<pn;i++) {
79     ptnzi  = pti[i+1] - pti[i];
80     ptanzi = 0;
81     /* Determine symbolic row of PtA: */
82     for (j=0;j<ptnzi;j++) {
83       arow = *ptJ++;
84       anzj = ai[arow+1] - ai[arow];
85       ajj  = aj + ai[arow];
86       for (k=0;k<anzj;k++) {
87         if (!ptadenserow[ajj[k]]) {
88           ptadenserow[ajj[k]]    = -1;
89           ptasparserow[ptanzi++] = ajj[k];
90         }
91       }
92     }
93       /* Using symbolic info for row of PtA, determine symbolic info for row of C: */
94     ptaj = ptasparserow;
95     cnzi   = 0;
96     for (j=0;j<ptanzi;j++) {
97       prow = *ptaj++;
98       pnzj = pi[prow+1] - pi[prow];
99       pjj  = pj + pi[prow];
100       /* add non-zero cols of P into the sorted linked list lnk */
101       ierr = PetscLLAdd(pnzj,pjj,pn,nlnk,lnk,lnkbt);CHKERRQ(ierr);
102       cnzi += nlnk;
103     }
104 
105     /* If free space is not available, make more free space */
106     /* Double the amount of total space in the list */
107     if (current_space->local_remaining<cnzi) {
108       ierr = PetscFreeSpaceGet(current_space->total_array_size,&current_space);CHKERRQ(ierr);
109     }
110 
111     /* Copy data into free space, and zero out denserows */
112     ierr = PetscLLClean(pn,pn,cnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr);
113     current_space->array           += cnzi;
114     current_space->local_used      += cnzi;
115     current_space->local_remaining -= cnzi;
116 
117     for (j=0;j<ptanzi;j++) {
118       ptadenserow[ptasparserow[j]] = 0;
119     }
120     /* Aside: Perhaps we should save the pta info for the numerical factorization. */
121     /*        For now, we will recompute what is needed. */
122     ci[i+1] = ci[i] + cnzi;
123   }
124   /* nnz is now stored in ci[ptm], column indices are in the list of free space */
125   /* Allocate space for cj, initialize cj, and */
126   /* destroy list of free space and other temporary array(s) */
127   ierr = PetscMalloc((ci[pn]+1)*sizeof(PetscInt),&cj);CHKERRQ(ierr);
128   ierr = PetscFreeSpaceContiguous(&free_space,cj);CHKERRQ(ierr);
129   ierr = PetscFree(ptadenserow);CHKERRQ(ierr);
130   ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr);
131 
132   /* Allocate space for ca */
133   ierr = PetscMalloc((ci[pn]+1)*sizeof(MatScalar),&ca);CHKERRQ(ierr);
134   ierr = PetscMemzero(ca,(ci[pn]+1)*sizeof(MatScalar));CHKERRQ(ierr);
135 
136   /* put together the new matrix */
137   ierr = MatCreateSeqAIJWithArrays(A->comm,pn,pn,ci,cj,ca,C);CHKERRQ(ierr);
138 
139   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
140   /* Since these are PETSc arrays, change flags to free them as necessary. */
141   c = (Mat_SeqAIJ *)((*C)->data);
142   c->freedata = PETSC_TRUE;
143   c->nonew    = 0;
144 
145   /* Clean up. */
146   ierr = MatRestoreSymbolicTranspose_SeqAIJ(P,&pti,&ptj);CHKERRQ(ierr);
147 
148   PetscFunctionReturn(0);
149 }
150 
151 #undef __FUNCT__
152 #define __FUNCT__ "MatPtAPNumeric_SeqAIJ_SeqAIJ"
153 PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat A,Mat P,Mat C)
154 {
155   PetscErrorCode ierr;
156   PetscInt       flops=0;
157   Mat_SeqAIJ     *a  = (Mat_SeqAIJ *) A->data;
158   Mat_SeqAIJ     *p  = (Mat_SeqAIJ *) P->data;
159   Mat_SeqAIJ     *c  = (Mat_SeqAIJ *) C->data;
160   PetscInt       *ai=a->i,*aj=a->j,*apj,*apjdense,*pi=p->i,*pj=p->j,*pJ=p->j,*pjj;
161   PetscInt       *ci=c->i,*cj=c->j,*cjj;
162   PetscInt       am=A->M,cn=C->N,cm=C->M;
163   PetscInt       i,j,k,anzi,pnzi,apnzj,nextap,pnzj,prow,crow;
164   MatScalar      *aa=a->a,*apa,*pa=p->a,*pA=p->a,*paj,*ca=c->a,*caj;
165 
166   PetscFunctionBegin;
167   /* Allocate temporary array for storage of one row of A*P */
168   ierr = PetscMalloc(cn*(sizeof(MatScalar)+2*sizeof(PetscInt)),&apa);CHKERRQ(ierr);
169   ierr = PetscMemzero(apa,cn*(sizeof(MatScalar)+2*sizeof(PetscInt)));CHKERRQ(ierr);
170 
171   apj      = (PetscInt *)(apa + cn);
172   apjdense = apj + cn;
173 
174   /* Clear old values in C */
175   ierr = PetscMemzero(ca,ci[cm]*sizeof(MatScalar));CHKERRQ(ierr);
176 
177   for (i=0;i<am;i++) {
178     /* Form sparse row of A*P */
179     anzi  = ai[i+1] - ai[i];
180     apnzj = 0;
181     for (j=0;j<anzi;j++) {
182       prow = *aj++;
183       pnzj = pi[prow+1] - pi[prow];
184       pjj  = pj + pi[prow];
185       paj  = pa + pi[prow];
186       for (k=0;k<pnzj;k++) {
187         if (!apjdense[pjj[k]]) {
188           apjdense[pjj[k]] = -1;
189           apj[apnzj++]     = pjj[k];
190         }
191         apa[pjj[k]] += (*aa)*paj[k];
192       }
193       flops += 2*pnzj;
194       aa++;
195     }
196 
197     /* Sort the j index array for quick sparse axpy. */
198     /* Note: a array does not need sorting as it is in dense storage locations. */
199     ierr = PetscSortInt(apnzj,apj);CHKERRQ(ierr);
200 
201     /* Compute P^T*A*P using outer product (P^T)[:,j]*(A*P)[j,:]. */
202     pnzi = pi[i+1] - pi[i];
203     for (j=0;j<pnzi;j++) {
204       nextap = 0;
205       crow   = *pJ++;
206       cjj    = cj + ci[crow];
207       caj    = ca + ci[crow];
208       /* Perform sparse axpy operation.  Note cjj includes apj. */
209       for (k=0;nextap<apnzj;k++) {
210         if (cjj[k]==apj[nextap]) {
211           caj[k] += (*pA)*apa[apj[nextap++]];
212         }
213       }
214       flops += 2*apnzj;
215       pA++;
216     }
217 
218     /* Zero the current row info for A*P */
219     for (j=0;j<apnzj;j++) {
220       apa[apj[j]]      = 0.;
221       apjdense[apj[j]] = 0;
222     }
223   }
224 
225   /* Assemble the final matrix and clean up */
226   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
227   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
228   ierr = PetscFree(apa);CHKERRQ(ierr);
229   ierr = PetscLogFlops(flops);CHKERRQ(ierr);
230 
231   PetscFunctionReturn(0);
232 }
233