Actual source code: matptap.c

  1: /*
  2:   Defines projective product routines where A is a SeqAIJ matrix
  3:           C = P^T * A * P
  4: */

  6: #include <../src/mat/impls/aij/seq/aij.h>
  7: #include <../src/mat/utils/freespace.h>
  8: #include <petscbt.h>
  9: #include <petsctime.h>

 11: #if PetscDefined(HAVE_HYPRE)
 12: PETSC_INTERN PetscErrorCode MatPtAPSymbolic_AIJ_AIJ_wHYPRE(Mat, Mat, PetscReal, Mat);
 13: #endif

 15: PetscErrorCode MatProductSymbolic_PtAP_SeqAIJ_SeqAIJ(Mat C)
 16: {
 17:   Mat_Product        *product = C->product;
 18:   Mat                 A = product->A, P = product->B;
 19:   MatProductAlgorithm alg  = product->alg;
 20:   PetscReal           fill = product->fill;
 21:   PetscBool           flg;
 22:   Mat                 Pt;

 24:   PetscFunctionBegin;
 25:   /* "scalable" */
 26:   PetscCall(PetscStrcmp(alg, "scalable", &flg));
 27:   if (flg) {
 28:     PetscCall(MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy(A, P, fill, C));
 29:     C->ops->productnumeric = MatProductNumeric_PtAP;
 30:     PetscFunctionReturn(PETSC_SUCCESS);
 31:   }

 33:   /* "rap" */
 34:   PetscCall(PetscStrcmp(alg, "rap", &flg));
 35:   if (flg) {
 36:     MatProductCtx_MatTransMatMult *atb;

 38:     PetscCall(PetscNew(&atb));
 39:     PetscCall(MatTranspose(P, MAT_INITIAL_MATRIX, &Pt));
 40:     PetscCall(MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ(Pt, A, P, fill, C));

 42:     atb->At                = Pt;
 43:     atb->data              = C->product->data;
 44:     atb->destroy           = C->product->destroy;
 45:     C->product->data       = atb;
 46:     C->product->destroy    = MatProductCtxDestroy_SeqAIJ_MatTransMatMult;
 47:     C->ops->ptapnumeric    = MatPtAPNumeric_SeqAIJ_SeqAIJ;
 48:     C->ops->productnumeric = MatProductNumeric_PtAP;
 49:     PetscFunctionReturn(PETSC_SUCCESS);
 50:   }

 52:   /* hypre */
 53: #if PetscDefined(HAVE_HYPRE)
 54:   PetscCall(PetscStrcmp(alg, "hypre", &flg));
 55:   if (flg) {
 56:     PetscCall(MatPtAPSymbolic_AIJ_AIJ_wHYPRE(A, P, fill, C));
 57:     PetscFunctionReturn(PETSC_SUCCESS);
 58:   }
 59: #endif

 61:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "MatProductType is not supported");
 62: }

 64: PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy(Mat A, Mat P, PetscReal fill, Mat C)
 65: {
 66:   PetscFreeSpaceList free_space = NULL, current_space = NULL;
 67:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data, *p = (Mat_SeqAIJ *)P->data, *c;
 68:   PetscInt          *pti, *ptj, *ptJ, *ai = a->i, *aj = a->j, *ajj, *pi = p->i, *pj = p->j, *pjj;
 69:   PetscInt          *ci, *cj, *ptadenserow, *ptasparserow, *ptaj, nspacedouble = 0;
 70:   PetscInt           an = A->cmap->N, am = A->rmap->N, pn = P->cmap->N, pm = P->rmap->N;
 71:   PetscInt           i, j, k, ptnzi, arow, anzj, ptanzi, prow, pnzj, cnzi, nlnk, *lnk;
 72:   MatScalar         *ca;
 73:   PetscBT            lnkbt;
 74:   PetscReal          afill;

 76:   PetscFunctionBegin;
 77:   /* Get ij structure of P^T */
 78:   PetscCall(MatGetSymbolicTranspose_SeqAIJ(P, &pti, &ptj));
 79:   ptJ = ptj;

 81:   /* Allocate ci array, arrays for fill computation and */
 82:   /* free space for accumulating nonzero column info */
 83:   PetscCall(PetscMalloc1(pn + 1, &ci));
 84:   ci[0] = 0;

 86:   PetscCall(PetscCalloc1(2 * an + 1, &ptadenserow));
 87:   ptasparserow = ptadenserow + an;

 89:   /* create and initialize a linked list */
 90:   nlnk = pn + 1;
 91:   PetscCall(PetscLLCreate(pn, pn, nlnk, lnk, lnkbt));

 93:   /* Set initial free space to be fill*(nnz(A)+ nnz(P)) */
 94:   PetscCall(PetscFreeSpaceGet(PetscRealIntMultTruncate(fill, PetscIntSumTruncate(ai[am], pi[pm])), &free_space));
 95:   current_space = free_space;

 97:   /* Determine symbolic info for each row of C: */
 98:   for (i = 0; i < pn; i++) {
 99:     ptnzi  = pti[i + 1] - pti[i];
100:     ptanzi = 0;
101:     /* Determine symbolic row of PtA: */
102:     for (j = 0; j < ptnzi; j++) {
103:       arow = *ptJ++;
104:       anzj = ai[arow + 1] - ai[arow];
105:       ajj  = aj + ai[arow];
106:       for (k = 0; k < anzj; k++) {
107:         if (!ptadenserow[ajj[k]]) {
108:           ptadenserow[ajj[k]]    = -1;
109:           ptasparserow[ptanzi++] = ajj[k];
110:         }
111:       }
112:     }
113:     /* Using symbolic info for row of PtA, determine symbolic info for row of C: */
114:     ptaj = ptasparserow;
115:     cnzi = 0;
116:     for (j = 0; j < ptanzi; j++) {
117:       prow = *ptaj++;
118:       pnzj = pi[prow + 1] - pi[prow];
119:       pjj  = pj + pi[prow];
120:       /* add non-zero cols of P into the sorted linked list lnk */
121:       PetscCall(PetscLLAddSorted(pnzj, pjj, pn, &nlnk, lnk, lnkbt));
122:       cnzi += nlnk;
123:     }

125:     /* If free space is not available, make more free space */
126:     /* Double the amount of total space in the list */
127:     if (current_space->local_remaining < cnzi) {
128:       PetscCall(PetscFreeSpaceGet(PetscIntSumTruncate(cnzi, current_space->total_array_size), &current_space));
129:       nspacedouble++;
130:     }

132:     /* Copy data into free space, and zero out denserows */
133:     PetscCall(PetscLLClean(pn, pn, cnzi, lnk, current_space->array, lnkbt));

135:     current_space->array += cnzi;
136:     current_space->local_used += cnzi;
137:     current_space->local_remaining -= cnzi;

139:     for (j = 0; j < ptanzi; j++) ptadenserow[ptasparserow[j]] = 0;

141:     /* Aside: Perhaps we should save the pta info for the numerical factorization. */
142:     /*        For now, we will recompute what is needed. */
143:     ci[i + 1] = ci[i] + cnzi;
144:   }
145:   /* nnz is now stored in ci[ptm], column indices are in the list of free space */
146:   /* Allocate space for cj, initialize cj, and */
147:   /* destroy list of free space and other temporary array(s) */
148:   PetscCall(PetscMalloc1(ci[pn], &cj));
149:   PetscCall(PetscFreeSpaceContiguous(&free_space, cj));
150:   PetscCall(PetscFree(ptadenserow));
151:   PetscCall(PetscLLDestroy(lnk, lnkbt));

153:   PetscCall(PetscCalloc1(ci[pn], &ca));

155:   /* put together the new matrix */
156:   PetscCall(MatSetSeqAIJWithArrays_private(PetscObjectComm((PetscObject)A), pn, pn, ci, cj, ca, ((PetscObject)A)->type_name, C));
157:   PetscCall(MatSetBlockSizes(C, P->cmap->bs, P->cmap->bs));

159:   /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
160:   /* Since these are PETSc arrays, change flags to free them as necessary. */
161:   c          = (Mat_SeqAIJ *)C->data;
162:   c->free_a  = PETSC_TRUE;
163:   c->free_ij = PETSC_TRUE;
164:   c->nonew   = 0;

166:   C->ops->ptapnumeric = MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy;

168:   /* set MatInfo */
169:   afill = (PetscReal)ci[pn] / (ai[am] + pi[pm] + 1.e-5);
170:   if (afill < 1.0) afill = 1.0;
171:   C->info.mallocs           = nspacedouble;
172:   C->info.fill_ratio_given  = fill;
173:   C->info.fill_ratio_needed = afill;

175:   /* Clean up. */
176:   PetscCall(MatRestoreSymbolicTranspose_SeqAIJ(P, &pti, &ptj));
177:   if (PetscDefined(USE_INFO)) {
178:     if (ci[pn] != 0) {
179:       PetscCall(PetscInfo(C, "Reallocs %" PetscInt_FMT "; Fill ratio: given %g needed %g.\n", nspacedouble, (double)fill, (double)afill));
180:       PetscCall(PetscInfo(C, "Use MatPtAP(A,P,MatReuse,%g,&C) for best performance.\n", (double)afill));
181:     } else PetscCall(PetscInfo(C, "Empty matrix product\n"));
182:   }
183:   PetscFunctionReturn(PETSC_SUCCESS);
184: }

186: PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy(Mat A, Mat P, Mat C)
187: {
188:   Mat_SeqAIJ *a  = (Mat_SeqAIJ *)A->data;
189:   Mat_SeqAIJ *p  = (Mat_SeqAIJ *)P->data;
190:   Mat_SeqAIJ *c  = (Mat_SeqAIJ *)C->data;
191:   PetscInt   *ai = a->i, *aj = a->j, *apj, *apjdense, *pi = p->i, *pj = p->j, *pJ = p->j, *pjj;
192:   PetscInt   *ci = c->i, *cj = c->j, *cjj;
193:   PetscInt    am = A->rmap->N, cn = C->cmap->N, cm = C->rmap->N;
194:   PetscInt    i, j, k, anzi, pnzi, apnzj, nextap, pnzj, prow, crow;
195:   MatScalar  *aa, *apa, *pa, *pA, *paj, *ca, *caj;

197:   PetscFunctionBegin;
198:   /* Allocate temporary array for storage of one row of A*P (cn: non-scalable) */
199:   PetscCall(PetscCalloc2(cn, &apa, cn, &apjdense));
200:   PetscCall(PetscMalloc1(cn, &apj));
201:   /* trigger CPU copies if needed and flag CPU mask for C */
202: #if PetscDefined(HAVE_DEVICE)
203:   {
204:     const PetscScalar *unused;
205:     PetscCall(MatSeqAIJGetArrayRead(A, &unused));
206:     PetscCall(MatSeqAIJRestoreArrayRead(A, &unused));
207:     PetscCall(MatSeqAIJGetArrayRead(P, &unused));
208:     PetscCall(MatSeqAIJRestoreArrayRead(P, &unused));
209:     if (C->offloadmask != PETSC_OFFLOAD_UNALLOCATED) C->offloadmask = PETSC_OFFLOAD_CPU;
210:   }
211: #endif
212:   aa = a->a;
213:   pa = p->a;
214:   pA = p->a;
215:   ca = c->a;

217:   /* Clear old values in C */
218:   PetscCall(PetscArrayzero(ca, ci[cm]));

220:   for (i = 0; i < am; i++) {
221:     /* Form sparse row of A*P */
222:     anzi  = ai[i + 1] - ai[i];
223:     apnzj = 0;
224:     for (j = 0; j < anzi; j++) {
225:       prow = *aj++;
226:       pnzj = pi[prow + 1] - pi[prow];
227:       pjj  = pj + pi[prow];
228:       paj  = pa + pi[prow];
229:       for (k = 0; k < pnzj; k++) {
230:         if (!apjdense[pjj[k]]) {
231:           apjdense[pjj[k]] = -1;
232:           apj[apnzj++]     = pjj[k];
233:         }
234:         apa[pjj[k]] += (*aa) * paj[k];
235:       }
236:       PetscCall(PetscLogFlops(2.0 * pnzj));
237:       aa++;
238:     }

240:     /* Sort the j index array for quick sparse axpy. */
241:     /* Note: a array does not need sorting as it is in dense storage locations. */
242:     PetscCall(PetscSortInt(apnzj, apj));

244:     /* Compute P^T*A*P using outer product (P^T)[:,j]*(A*P)[j,:]. */
245:     pnzi = pi[i + 1] - pi[i];
246:     for (j = 0; j < pnzi; j++) {
247:       nextap = 0;
248:       crow   = *pJ++;
249:       cjj    = cj + ci[crow];
250:       caj    = ca + ci[crow];
251:       /* Perform sparse axpy operation.  Note cjj includes apj. */
252:       for (k = 0; nextap < apnzj; k++) {
253:         PetscAssert(k < ci[crow + 1] - ci[crow], PETSC_COMM_SELF, PETSC_ERR_PLIB, "k too large k %" PetscInt_FMT ", crow %" PetscInt_FMT, k, crow);
254:         if (cjj[k] == apj[nextap]) caj[k] += (*pA) * apa[apj[nextap++]];
255:       }
256:       PetscCall(PetscLogFlops(2.0 * apnzj));
257:       pA++;
258:     }

260:     /* Zero the current row info for A*P */
261:     for (j = 0; j < apnzj; j++) {
262:       apa[apj[j]]      = 0.;
263:       apjdense[apj[j]] = 0;
264:     }
265:   }

267:   /* Assemble the final matrix and clean up */
268:   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
269:   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));

271:   PetscCall(PetscFree2(apa, apjdense));
272:   PetscCall(PetscFree(apj));
273:   PetscFunctionReturn(PETSC_SUCCESS);
274: }

276: PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat A, Mat P, Mat C)
277: {
278:   MatProductCtx_MatTransMatMult *atb;

280:   PetscFunctionBegin;
281:   MatCheckProduct(C, 3);
282:   atb = (MatProductCtx_MatTransMatMult *)C->product->data;
283:   PetscCheck(atb, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Missing data structure");
284:   PetscCall(MatTranspose(P, MAT_REUSE_MATRIX, &atb->At));
285:   PetscCheck(C->ops->matmultnumeric, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Missing numeric operation");
286:   /* when using rap, MatMatMatMultSymbolic used a different data */
287:   if (atb->data) C->product->data = atb->data;
288:   PetscCall((*C->ops->matmatmultnumeric)(atb->At, A, P, C));
289:   C->product->data = atb;
290:   PetscFunctionReturn(PETSC_SUCCESS);
291: }