Actual source code: matelem.cxx
1: #include <petsc/private/petscelemental.h>
3: const char ElementalCitation[] = "@Article{Elemental2012,\n"
4: " author = {Jack Poulson and Bryan Marker and Jeff R. Hammond and Nichols A. Romero and Robert {v}an~{d}e~{G}eijn},\n"
5: " title = {Elemental: A New Framework for Distributed Memory Dense Matrix Computations},\n"
6: " journal = {{ACM} Transactions on Mathematical Software},\n"
7: " volume = {39},\n"
8: " number = {2},\n"
9: " year = {2013}\n"
10: "}\n";
11: static PetscBool ElementalCite = PETSC_FALSE;
13: /*
14: The variable Petsc_Elemental_keyval is used to indicate an MPI attribute that
15: is attached to a communicator, in this case the attribute is a Mat_Elemental_Grid
16: */
17: static PetscMPIInt Petsc_Elemental_keyval = MPI_KEYVAL_INVALID;
19: static PetscErrorCode MatView_Elemental(Mat A, PetscViewer viewer)
20: {
21: Mat_Elemental *a = (Mat_Elemental *)A->data;
22: PetscBool isascii;
24: PetscFunctionBegin;
25: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
26: if (isascii) {
27: PetscViewerFormat format;
28: PetscCall(PetscViewerGetFormat(viewer, &format));
29: if (format == PETSC_VIEWER_ASCII_INFO) {
30: /* call elemental viewing function */
31: PetscCall(PetscViewerASCIIPrintf(viewer, "Elemental run parameters:\n"));
32: PetscCall(PetscViewerASCIIPrintf(viewer, " allocated entries=%zu\n", (*a->emat).AllocatedMemory()));
33: PetscCall(PetscViewerASCIIPrintf(viewer, " grid height=%d, grid width=%d\n", (*a->emat).Grid().Height(), (*a->emat).Grid().Width()));
34: if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
35: /* call elemental viewing function */
36: PetscCall(PetscPrintf(PetscObjectComm((PetscObject)viewer), "test matview_elemental 2\n"));
37: }
39: } else if (format == PETSC_VIEWER_DEFAULT) {
40: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
41: El::Print(*a->emat, "Elemental matrix (cyclic ordering)");
42: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
43: if (A->factortype == MAT_FACTOR_NONE) {
44: Mat Adense;
45: PetscCall(MatConvert(A, MATDENSE, MAT_INITIAL_MATRIX, &Adense));
46: PetscCall(MatView(Adense, viewer));
47: PetscCall(MatDestroy(&Adense));
48: }
49: } else SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Format");
50: } else {
51: /* convert to dense format and call MatView() */
52: Mat Adense;
53: PetscCall(MatConvert(A, MATDENSE, MAT_INITIAL_MATRIX, &Adense));
54: PetscCall(MatView(Adense, viewer));
55: PetscCall(MatDestroy(&Adense));
56: }
57: PetscFunctionReturn(PETSC_SUCCESS);
58: }
60: static PetscErrorCode MatGetInfo_Elemental(Mat A, MatInfoType flag, MatInfo *info)
61: {
62: Mat_Elemental *a = (Mat_Elemental *)A->data;
64: PetscFunctionBegin;
65: info->block_size = 1.0;
67: if (flag == MAT_LOCAL) {
68: info->nz_allocated = (*a->emat).AllocatedMemory(); /* locally allocated */
69: info->nz_used = info->nz_allocated;
70: } else if (flag == MAT_GLOBAL_MAX) {
71: //PetscCallMPI(MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin)));
72: /* see MatGetInfo_MPIAIJ() for getting global info->nz_allocated! */
73: //SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP," MAT_GLOBAL_MAX not written yet");
74: } else if (flag == MAT_GLOBAL_SUM) {
75: //SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP," MAT_GLOBAL_SUM not written yet");
76: info->nz_allocated = (*a->emat).AllocatedMemory(); /* locally allocated */
77: info->nz_used = info->nz_allocated; /* assume Elemental does accurate allocation */
78: //PetscCallMPI(MPIU_Allreduce(isend,irecv,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A)));
79: //PetscPrintf(PETSC_COMM_SELF," ... [%d] locally allocated %g\n",rank,info->nz_allocated);
80: }
82: info->nz_unneeded = 0.0;
83: info->assemblies = A->num_ass;
84: info->mallocs = 0;
85: info->memory = 0; /* REVIEW ME */
86: info->fill_ratio_given = 0; /* determined by Elemental */
87: info->fill_ratio_needed = 0;
88: info->factor_mallocs = 0;
89: PetscFunctionReturn(PETSC_SUCCESS);
90: }
92: static PetscErrorCode MatSetOption_Elemental(Mat A, MatOption op, PetscBool flg)
93: {
94: Mat_Elemental *a = (Mat_Elemental *)A->data;
96: PetscFunctionBegin;
97: switch (op) {
98: case MAT_ROW_ORIENTED:
99: a->roworiented = flg;
100: break;
101: default:
102: break;
103: }
104: PetscFunctionReturn(PETSC_SUCCESS);
105: }
107: static PetscErrorCode MatSetValues_Elemental(Mat A, PetscInt nr, const PetscInt *rows, PetscInt nc, const PetscInt *cols, const PetscScalar *vals, InsertMode imode)
108: {
109: Mat_Elemental *a = (Mat_Elemental *)A->data;
110: PetscInt i, j, rrank, ridx, crank, cidx, erow, ecol, numQueues = 0;
112: PetscFunctionBegin;
113: // TODO: Initialize matrix to all zeros?
115: // Count the number of queues from this process
116: if (a->roworiented) {
117: for (i = 0; i < nr; i++) {
118: if (rows[i] < 0) continue;
119: P2RO(A, 0, rows[i], &rrank, &ridx);
120: RO2E(A, 0, rrank, ridx, &erow);
121: PetscCheck(rrank >= 0 && ridx >= 0 && erow >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Incorrect row translation");
122: for (j = 0; j < nc; j++) {
123: if (cols[j] < 0) continue;
124: P2RO(A, 1, cols[j], &crank, &cidx);
125: RO2E(A, 1, crank, cidx, &ecol);
126: PetscCheck(crank >= 0 && cidx >= 0 && ecol >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Incorrect col translation");
127: if (!a->emat->IsLocal(erow, ecol)) { /* off-proc entry */
128: /* printf("Will later remotely update (%d,%d)\n",erow,ecol); */
129: PetscCheck(imode == ADD_VALUES, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only ADD_VALUES to off-processor entry is supported");
130: ++numQueues;
131: continue;
132: }
133: /* printf("Locally updating (%d,%d)\n",erow,ecol); */
134: switch (imode) {
135: case INSERT_VALUES:
136: a->emat->Set(erow, ecol, (PetscElemScalar)vals[i * nc + j]);
137: break;
138: case ADD_VALUES:
139: a->emat->Update(erow, ecol, (PetscElemScalar)vals[i * nc + j]);
140: break;
141: default:
142: SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "No support for InsertMode %d", (int)imode);
143: }
144: }
145: }
147: /* printf("numQueues=%d\n",numQueues); */
148: a->emat->Reserve(numQueues);
149: for (i = 0; i < nr; i++) {
150: if (rows[i] < 0) continue;
151: P2RO(A, 0, rows[i], &rrank, &ridx);
152: RO2E(A, 0, rrank, ridx, &erow);
153: for (j = 0; j < nc; j++) {
154: if (cols[j] < 0) continue;
155: P2RO(A, 1, cols[j], &crank, &cidx);
156: RO2E(A, 1, crank, cidx, &ecol);
157: if (!a->emat->IsLocal(erow, ecol)) { /*off-proc entry*/
158: /* printf("Queueing remotely update of (%d,%d)\n",erow,ecol); */
159: a->emat->QueueUpdate(erow, ecol, vals[i * nc + j]);
160: }
161: }
162: }
163: } else { /* column-oriented */
164: for (j = 0; j < nc; j++) {
165: if (cols[j] < 0) continue;
166: P2RO(A, 1, cols[j], &crank, &cidx);
167: RO2E(A, 1, crank, cidx, &ecol);
168: PetscCheck(crank >= 0 && cidx >= 0 && ecol >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Incorrect col translation");
169: for (i = 0; i < nr; i++) {
170: if (rows[i] < 0) continue;
171: P2RO(A, 0, rows[i], &rrank, &ridx);
172: RO2E(A, 0, rrank, ridx, &erow);
173: PetscCheck(rrank >= 0 && ridx >= 0 && erow >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Incorrect row translation");
174: if (!a->emat->IsLocal(erow, ecol)) { /* off-proc entry */
175: /* printf("Will later remotely update (%d,%d)\n",erow,ecol); */
176: PetscCheck(imode == ADD_VALUES, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only ADD_VALUES to off-processor entry is supported");
177: ++numQueues;
178: continue;
179: }
180: /* printf("Locally updating (%d,%d)\n",erow,ecol); */
181: switch (imode) {
182: case INSERT_VALUES:
183: a->emat->Set(erow, ecol, (PetscElemScalar)vals[i + j * nr]);
184: break;
185: case ADD_VALUES:
186: a->emat->Update(erow, ecol, (PetscElemScalar)vals[i + j * nr]);
187: break;
188: default:
189: SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "No support for InsertMode %d", (int)imode);
190: }
191: }
192: }
194: /* printf("numQueues=%d\n",numQueues); */
195: a->emat->Reserve(numQueues);
196: for (j = 0; j < nc; j++) {
197: if (cols[j] < 0) continue;
198: P2RO(A, 1, cols[j], &crank, &cidx);
199: RO2E(A, 1, crank, cidx, &ecol);
201: for (i = 0; i < nr; i++) {
202: if (rows[i] < 0) continue;
203: P2RO(A, 0, rows[i], &rrank, &ridx);
204: RO2E(A, 0, rrank, ridx, &erow);
205: if (!a->emat->IsLocal(erow, ecol)) { /*off-proc entry*/
206: /* printf("Queueing remotely update of (%d,%d)\n",erow,ecol); */
207: a->emat->QueueUpdate(erow, ecol, vals[i + j * nr]);
208: }
209: }
210: }
211: }
212: PetscFunctionReturn(PETSC_SUCCESS);
213: }
215: static PetscErrorCode MatMult_Elemental(Mat A, Vec X, Vec Y)
216: {
217: Mat_Elemental *a = (Mat_Elemental *)A->data;
218: const PetscElemScalar *x;
219: PetscElemScalar *y;
220: PetscElemScalar one = 1, zero = 0;
222: PetscFunctionBegin;
223: PetscCall(VecGetArrayRead(X, (const PetscScalar **)&x));
224: PetscCall(VecGetArray(Y, (PetscScalar **)&y));
225: { /* Scoping so that constructor is called before pointer is returned */
226: El::DistMatrix<PetscElemScalar, El::VC, El::STAR> xe, ye;
227: xe.LockedAttach(A->cmap->N, 1, *a->grid, 0, 0, x, A->cmap->n);
228: ye.Attach(A->rmap->N, 1, *a->grid, 0, 0, y, A->rmap->n);
229: El::Gemv(El::NORMAL, one, *a->emat, xe, zero, ye);
230: }
231: PetscCall(VecRestoreArrayRead(X, (const PetscScalar **)&x));
232: PetscCall(VecRestoreArray(Y, (PetscScalar **)&y));
233: PetscFunctionReturn(PETSC_SUCCESS);
234: }
236: static PetscErrorCode MatMultTranspose_Elemental(Mat A, Vec X, Vec Y)
237: {
238: Mat_Elemental *a = (Mat_Elemental *)A->data;
239: const PetscElemScalar *x;
240: PetscElemScalar *y;
241: PetscElemScalar one = 1, zero = 0;
243: PetscFunctionBegin;
244: PetscCall(VecGetArrayRead(X, (const PetscScalar **)&x));
245: PetscCall(VecGetArray(Y, (PetscScalar **)&y));
246: { /* Scoping so that constructor is called before pointer is returned */
247: El::DistMatrix<PetscElemScalar, El::VC, El::STAR> xe, ye;
248: xe.LockedAttach(A->rmap->N, 1, *a->grid, 0, 0, x, A->rmap->n);
249: ye.Attach(A->cmap->N, 1, *a->grid, 0, 0, y, A->cmap->n);
250: El::Gemv(El::TRANSPOSE, one, *a->emat, xe, zero, ye);
251: }
252: PetscCall(VecRestoreArrayRead(X, (const PetscScalar **)&x));
253: PetscCall(VecRestoreArray(Y, (PetscScalar **)&y));
254: PetscFunctionReturn(PETSC_SUCCESS);
255: }
257: static PetscErrorCode MatMultAdd_Elemental(Mat A, Vec X, Vec Y, Vec Z)
258: {
259: Mat_Elemental *a = (Mat_Elemental *)A->data;
260: const PetscElemScalar *x;
261: PetscElemScalar *z;
262: PetscElemScalar one = 1;
264: PetscFunctionBegin;
265: if (Y != Z) PetscCall(VecCopy(Y, Z));
266: PetscCall(VecGetArrayRead(X, (const PetscScalar **)&x));
267: PetscCall(VecGetArray(Z, (PetscScalar **)&z));
268: { /* Scoping so that constructor is called before pointer is returned */
269: El::DistMatrix<PetscElemScalar, El::VC, El::STAR> xe, ze;
270: xe.LockedAttach(A->cmap->N, 1, *a->grid, 0, 0, x, A->cmap->n);
271: ze.Attach(A->rmap->N, 1, *a->grid, 0, 0, z, A->rmap->n);
272: El::Gemv(El::NORMAL, one, *a->emat, xe, one, ze);
273: }
274: PetscCall(VecRestoreArrayRead(X, (const PetscScalar **)&x));
275: PetscCall(VecRestoreArray(Z, (PetscScalar **)&z));
276: PetscFunctionReturn(PETSC_SUCCESS);
277: }
279: static PetscErrorCode MatMultTransposeAdd_Elemental(Mat A, Vec X, Vec Y, Vec Z)
280: {
281: Mat_Elemental *a = (Mat_Elemental *)A->data;
282: const PetscElemScalar *x;
283: PetscElemScalar *z;
284: PetscElemScalar one = 1;
286: PetscFunctionBegin;
287: if (Y != Z) PetscCall(VecCopy(Y, Z));
288: PetscCall(VecGetArrayRead(X, (const PetscScalar **)&x));
289: PetscCall(VecGetArray(Z, (PetscScalar **)&z));
290: { /* Scoping so that constructor is called before pointer is returned */
291: El::DistMatrix<PetscElemScalar, El::VC, El::STAR> xe, ze;
292: xe.LockedAttach(A->rmap->N, 1, *a->grid, 0, 0, x, A->rmap->n);
293: ze.Attach(A->cmap->N, 1, *a->grid, 0, 0, z, A->cmap->n);
294: El::Gemv(El::TRANSPOSE, one, *a->emat, xe, one, ze);
295: }
296: PetscCall(VecRestoreArrayRead(X, (const PetscScalar **)&x));
297: PetscCall(VecRestoreArray(Z, (PetscScalar **)&z));
298: PetscFunctionReturn(PETSC_SUCCESS);
299: }
301: PetscErrorCode MatMatMultNumeric_Elemental(Mat A, Mat B, Mat C)
302: {
303: Mat_Elemental *a = (Mat_Elemental *)A->data;
304: Mat_Elemental *b = (Mat_Elemental *)B->data;
305: Mat_Elemental *c = (Mat_Elemental *)C->data;
306: PetscElemScalar one = 1, zero = 0;
308: PetscFunctionBegin;
309: { /* Scoping so that constructor is called before pointer is returned */
310: El::Gemm(El::NORMAL, El::NORMAL, one, *a->emat, *b->emat, zero, *c->emat);
311: }
312: C->assembled = PETSC_TRUE;
313: PetscFunctionReturn(PETSC_SUCCESS);
314: }
316: PetscErrorCode MatMatMultSymbolic_Elemental(Mat A, Mat B, PetscReal, Mat Ce)
317: {
318: PetscFunctionBegin;
319: PetscCall(MatSetSizes(Ce, A->rmap->n, B->cmap->n, PETSC_DECIDE, PETSC_DECIDE));
320: PetscCall(MatSetType(Ce, MATELEMENTAL));
321: PetscCall(MatSetUp(Ce));
322: Ce->ops->matmultnumeric = MatMatMultNumeric_Elemental;
323: PetscFunctionReturn(PETSC_SUCCESS);
324: }
326: static PetscErrorCode MatMatTransposeMultNumeric_Elemental(Mat A, Mat B, Mat C)
327: {
328: Mat_Elemental *a = (Mat_Elemental *)A->data;
329: Mat_Elemental *b = (Mat_Elemental *)B->data;
330: Mat_Elemental *c = (Mat_Elemental *)C->data;
331: PetscElemScalar one = 1, zero = 0;
333: PetscFunctionBegin;
334: { /* Scoping so that constructor is called before pointer is returned */
335: El::Gemm(El::NORMAL, El::TRANSPOSE, one, *a->emat, *b->emat, zero, *c->emat);
336: }
337: C->assembled = PETSC_TRUE;
338: PetscFunctionReturn(PETSC_SUCCESS);
339: }
341: static PetscErrorCode MatMatTransposeMultSymbolic_Elemental(Mat A, Mat B, PetscReal, Mat C)
342: {
343: PetscFunctionBegin;
344: PetscCall(MatSetSizes(C, A->rmap->n, B->rmap->n, PETSC_DECIDE, PETSC_DECIDE));
345: PetscCall(MatSetType(C, MATELEMENTAL));
346: PetscCall(MatSetUp(C));
347: PetscFunctionReturn(PETSC_SUCCESS);
348: }
350: static PetscErrorCode MatProductSetFromOptions_Elemental_AB(Mat C)
351: {
352: PetscFunctionBegin;
353: C->ops->matmultsymbolic = MatMatMultSymbolic_Elemental;
354: C->ops->productsymbolic = MatProductSymbolic_AB;
355: PetscFunctionReturn(PETSC_SUCCESS);
356: }
358: static PetscErrorCode MatProductSetFromOptions_Elemental_ABt(Mat C)
359: {
360: PetscFunctionBegin;
361: C->ops->mattransposemultsymbolic = MatMatTransposeMultSymbolic_Elemental;
362: C->ops->productsymbolic = MatProductSymbolic_ABt;
363: PetscFunctionReturn(PETSC_SUCCESS);
364: }
366: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_Elemental(Mat C)
367: {
368: Mat_Product *product = C->product;
370: PetscFunctionBegin;
371: switch (product->type) {
372: case MATPRODUCT_AB:
373: PetscCall(MatProductSetFromOptions_Elemental_AB(C));
374: break;
375: case MATPRODUCT_ABt:
376: PetscCall(MatProductSetFromOptions_Elemental_ABt(C));
377: break;
378: default:
379: break;
380: }
381: PetscFunctionReturn(PETSC_SUCCESS);
382: }
384: static PetscErrorCode MatMatMultNumeric_Elemental_MPIDense(Mat A, Mat B, Mat C)
385: {
386: Mat Be, Ce;
388: PetscFunctionBegin;
389: PetscCall(MatConvert(B, MATELEMENTAL, MAT_INITIAL_MATRIX, &Be));
390: PetscCall(MatMatMult(A, Be, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &Ce));
391: PetscCall(MatConvert(Ce, MATMPIDENSE, MAT_REUSE_MATRIX, &C));
392: PetscCall(MatDestroy(&Be));
393: PetscCall(MatDestroy(&Ce));
394: PetscFunctionReturn(PETSC_SUCCESS);
395: }
397: static PetscErrorCode MatMatMultSymbolic_Elemental_MPIDense(Mat A, Mat B, PetscReal, Mat C)
398: {
399: PetscFunctionBegin;
400: PetscCall(MatDenseSetUpProduct_Private(C, A->rmap, B->cmap, B, MATMPIDENSE));
401: C->ops->matmultnumeric = MatMatMultNumeric_Elemental_MPIDense;
402: PetscFunctionReturn(PETSC_SUCCESS);
403: }
405: static PetscErrorCode MatProductSetFromOptions_Elemental_MPIDense_AB(Mat C)
406: {
407: PetscFunctionBegin;
408: C->ops->matmultsymbolic = MatMatMultSymbolic_Elemental_MPIDense;
409: C->ops->productsymbolic = MatProductSymbolic_AB;
410: PetscFunctionReturn(PETSC_SUCCESS);
411: }
413: static PetscErrorCode MatProductSetFromOptions_Elemental_MPIDense(Mat C)
414: {
415: Mat_Product *product = C->product;
417: PetscFunctionBegin;
418: if (product->type == MATPRODUCT_AB) PetscCall(MatProductSetFromOptions_Elemental_MPIDense_AB(C));
419: PetscFunctionReturn(PETSC_SUCCESS);
420: }
422: static PetscErrorCode MatGetDiagonal_Elemental(Mat A, Vec D)
423: {
424: PetscInt i, nrows, ncols, nD, rrank, ridx, crank, cidx;
425: Mat_Elemental *a = (Mat_Elemental *)A->data;
426: PetscElemScalar v;
427: MPI_Comm comm;
429: PetscFunctionBegin;
430: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
431: PetscCall(MatGetSize(A, &nrows, &ncols));
432: nD = nrows > ncols ? ncols : nrows;
433: for (i = 0; i < nD; i++) {
434: PetscInt erow, ecol;
435: P2RO(A, 0, i, &rrank, &ridx);
436: RO2E(A, 0, rrank, ridx, &erow);
437: PetscCheck(rrank >= 0 && ridx >= 0 && erow >= 0, comm, PETSC_ERR_PLIB, "Incorrect row translation");
438: P2RO(A, 1, i, &crank, &cidx);
439: RO2E(A, 1, crank, cidx, &ecol);
440: PetscCheck(crank >= 0 && cidx >= 0 && ecol >= 0, comm, PETSC_ERR_PLIB, "Incorrect col translation");
441: v = a->emat->Get(erow, ecol);
442: PetscCall(VecSetValues(D, 1, &i, (PetscScalar *)&v, INSERT_VALUES));
443: }
444: PetscCall(VecAssemblyBegin(D));
445: PetscCall(VecAssemblyEnd(D));
446: PetscFunctionReturn(PETSC_SUCCESS);
447: }
449: static PetscErrorCode MatDiagonalScale_Elemental(Mat X, Vec L, Vec R)
450: {
451: Mat_Elemental *x = (Mat_Elemental *)X->data;
452: const PetscElemScalar *d;
454: PetscFunctionBegin;
455: if (R) {
456: PetscCall(VecGetArrayRead(R, (const PetscScalar **)&d));
457: El::DistMatrix<PetscElemScalar, El::VC, El::STAR> de;
458: de.LockedAttach(X->cmap->N, 1, *x->grid, 0, 0, d, X->cmap->n);
459: El::DiagonalScale(El::RIGHT, El::NORMAL, de, *x->emat);
460: PetscCall(VecRestoreArrayRead(R, (const PetscScalar **)&d));
461: }
462: if (L) {
463: PetscCall(VecGetArrayRead(L, (const PetscScalar **)&d));
464: El::DistMatrix<PetscElemScalar, El::VC, El::STAR> de;
465: de.LockedAttach(X->rmap->N, 1, *x->grid, 0, 0, d, X->rmap->n);
466: El::DiagonalScale(El::LEFT, El::NORMAL, de, *x->emat);
467: PetscCall(VecRestoreArrayRead(L, (const PetscScalar **)&d));
468: }
469: PetscFunctionReturn(PETSC_SUCCESS);
470: }
472: static PetscErrorCode MatScale_Elemental(Mat X, PetscScalar a)
473: {
474: Mat_Elemental *x = (Mat_Elemental *)X->data;
476: PetscFunctionBegin;
477: El::Scale((PetscElemScalar)a, *x->emat);
478: PetscFunctionReturn(PETSC_SUCCESS);
479: }
481: /*
482: MatAXPY - Computes Y = a*X + Y.
483: */
484: static PetscErrorCode MatAXPY_Elemental(Mat Y, PetscScalar a, Mat X, MatStructure)
485: {
486: Mat_Elemental *x = (Mat_Elemental *)X->data;
487: Mat_Elemental *y = (Mat_Elemental *)Y->data;
489: PetscFunctionBegin;
490: El::Axpy((PetscElemScalar)a, *x->emat, *y->emat);
491: PetscCall(PetscObjectStateIncrease((PetscObject)Y));
492: PetscFunctionReturn(PETSC_SUCCESS);
493: }
495: static PetscErrorCode MatCopy_Elemental(Mat A, Mat B, MatStructure)
496: {
497: Mat_Elemental *a = (Mat_Elemental *)A->data;
498: Mat_Elemental *b = (Mat_Elemental *)B->data;
500: PetscFunctionBegin;
501: El::Copy(*a->emat, *b->emat);
502: PetscCall(PetscObjectStateIncrease((PetscObject)B));
503: PetscFunctionReturn(PETSC_SUCCESS);
504: }
506: static PetscErrorCode MatDuplicate_Elemental(Mat A, MatDuplicateOption op, Mat *B)
507: {
508: Mat Be;
509: MPI_Comm comm;
510: Mat_Elemental *a = (Mat_Elemental *)A->data;
512: PetscFunctionBegin;
513: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
514: PetscCall(MatCreate(comm, &Be));
515: PetscCall(MatSetSizes(Be, A->rmap->n, A->cmap->n, PETSC_DECIDE, PETSC_DECIDE));
516: PetscCall(MatSetType(Be, MATELEMENTAL));
517: PetscCall(MatSetUp(Be));
518: *B = Be;
519: if (op == MAT_COPY_VALUES) {
520: Mat_Elemental *b = (Mat_Elemental *)Be->data;
521: El::Copy(*a->emat, *b->emat);
522: }
523: Be->assembled = PETSC_TRUE;
524: PetscFunctionReturn(PETSC_SUCCESS);
525: }
527: static PetscErrorCode MatTranspose_Elemental(Mat A, MatReuse reuse, Mat *B)
528: {
529: Mat Be = *B;
530: MPI_Comm comm;
531: Mat_Elemental *a = (Mat_Elemental *)A->data, *b;
533: PetscFunctionBegin;
534: if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *B));
535: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
536: /* Only out-of-place supported */
537: PetscCheck(reuse != MAT_INPLACE_MATRIX, comm, PETSC_ERR_SUP, "Only out-of-place supported");
538: if (reuse == MAT_INITIAL_MATRIX) {
539: PetscCall(MatCreate(comm, &Be));
540: PetscCall(MatSetSizes(Be, A->cmap->n, A->rmap->n, PETSC_DECIDE, PETSC_DECIDE));
541: PetscCall(MatSetType(Be, MATELEMENTAL));
542: PetscCall(MatSetUp(Be));
543: *B = Be;
544: }
545: b = (Mat_Elemental *)Be->data;
546: El::Transpose(*a->emat, *b->emat);
547: Be->assembled = PETSC_TRUE;
548: PetscFunctionReturn(PETSC_SUCCESS);
549: }
551: static PetscErrorCode MatConjugate_Elemental(Mat A)
552: {
553: Mat_Elemental *a = (Mat_Elemental *)A->data;
555: PetscFunctionBegin;
556: El::Conjugate(*a->emat);
557: PetscFunctionReturn(PETSC_SUCCESS);
558: }
560: static PetscErrorCode MatHermitianTranspose_Elemental(Mat A, MatReuse reuse, Mat *B)
561: {
562: Mat Be = *B;
563: MPI_Comm comm;
564: Mat_Elemental *a = (Mat_Elemental *)A->data, *b;
566: PetscFunctionBegin;
567: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
568: /* Only out-of-place supported */
569: if (reuse == MAT_INITIAL_MATRIX) {
570: PetscCall(MatCreate(comm, &Be));
571: PetscCall(MatSetSizes(Be, A->cmap->n, A->rmap->n, PETSC_DECIDE, PETSC_DECIDE));
572: PetscCall(MatSetType(Be, MATELEMENTAL));
573: PetscCall(MatSetUp(Be));
574: *B = Be;
575: }
576: b = (Mat_Elemental *)Be->data;
577: El::Adjoint(*a->emat, *b->emat);
578: Be->assembled = PETSC_TRUE;
579: PetscFunctionReturn(PETSC_SUCCESS);
580: }
582: static PetscErrorCode MatSolve_Elemental(Mat A, Vec B, Vec X)
583: {
584: Mat_Elemental *a = (Mat_Elemental *)A->data;
585: PetscElemScalar *x;
586: PetscInt pivoting = a->pivoting;
588: PetscFunctionBegin;
589: PetscCall(VecCopy(B, X));
590: PetscCall(VecGetArray(X, (PetscScalar **)&x));
592: El::DistMatrix<PetscElemScalar, El::VC, El::STAR> xe;
593: xe.Attach(A->rmap->N, 1, *a->grid, 0, 0, x, A->rmap->n);
594: El::DistMatrix<PetscElemScalar, El::MC, El::MR> xer(xe);
595: switch (A->factortype) {
596: case MAT_FACTOR_LU:
597: if (pivoting == 0) {
598: El::lu::SolveAfter(El::NORMAL, *a->emat, xer);
599: } else if (pivoting == 1) {
600: El::lu::SolveAfter(El::NORMAL, *a->emat, *a->P, xer);
601: } else { /* pivoting == 2 */
602: El::lu::SolveAfter(El::NORMAL, *a->emat, *a->P, *a->Q, xer);
603: }
604: break;
605: case MAT_FACTOR_CHOLESKY:
606: El::cholesky::SolveAfter(El::UPPER, El::NORMAL, *a->emat, xer);
607: break;
608: default:
609: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unfactored Matrix or Unsupported MatFactorType");
610: break;
611: }
612: El::Copy(xer, xe);
614: PetscCall(VecRestoreArray(X, (PetscScalar **)&x));
615: PetscFunctionReturn(PETSC_SUCCESS);
616: }
618: static PetscErrorCode MatSolveAdd_Elemental(Mat A, Vec B, Vec Y, Vec X)
619: {
620: PetscFunctionBegin;
621: PetscCall(MatSolve_Elemental(A, B, X));
622: PetscCall(VecAXPY(X, 1, Y));
623: PetscFunctionReturn(PETSC_SUCCESS);
624: }
626: static PetscErrorCode MatMatSolve_Elemental(Mat A, Mat B, Mat X)
627: {
628: Mat_Elemental *a = (Mat_Elemental *)A->data;
629: Mat_Elemental *x;
630: Mat C;
631: PetscInt pivoting = a->pivoting;
632: PetscBool flg;
633: MatType type;
635: PetscFunctionBegin;
636: PetscCall(MatGetType(X, &type));
637: PetscCall(PetscStrcmp(type, MATELEMENTAL, &flg));
638: if (!flg) {
639: PetscCall(MatConvert(B, MATELEMENTAL, MAT_INITIAL_MATRIX, &C));
640: x = (Mat_Elemental *)C->data;
641: } else {
642: x = (Mat_Elemental *)X->data;
643: El::Copy(*((Mat_Elemental *)B->data)->emat, *x->emat);
644: }
645: switch (A->factortype) {
646: case MAT_FACTOR_LU:
647: if (pivoting == 0) {
648: El::lu::SolveAfter(El::NORMAL, *a->emat, *x->emat);
649: } else if (pivoting == 1) {
650: El::lu::SolveAfter(El::NORMAL, *a->emat, *a->P, *x->emat);
651: } else {
652: El::lu::SolveAfter(El::NORMAL, *a->emat, *a->P, *a->Q, *x->emat);
653: }
654: break;
655: case MAT_FACTOR_CHOLESKY:
656: El::cholesky::SolveAfter(El::UPPER, El::NORMAL, *a->emat, *x->emat);
657: break;
658: default:
659: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unfactored Matrix or Unsupported MatFactorType");
660: break;
661: }
662: if (!flg) {
663: PetscCall(MatConvert(C, type, MAT_REUSE_MATRIX, &X));
664: PetscCall(MatDestroy(&C));
665: }
666: PetscFunctionReturn(PETSC_SUCCESS);
667: }
669: static PetscErrorCode MatLUFactor_Elemental(Mat A, IS, IS, const MatFactorInfo *)
670: {
671: Mat_Elemental *a = (Mat_Elemental *)A->data;
672: PetscInt pivoting = a->pivoting;
674: PetscFunctionBegin;
675: if (pivoting == 0) {
676: El::LU(*a->emat);
677: } else if (pivoting == 1) {
678: El::LU(*a->emat, *a->P);
679: } else {
680: El::LU(*a->emat, *a->P, *a->Q);
681: }
682: A->factortype = MAT_FACTOR_LU;
683: A->assembled = PETSC_TRUE;
685: PetscCall(PetscFree(A->solvertype));
686: PetscCall(PetscStrallocpy(MATSOLVERELEMENTAL, &A->solvertype));
687: PetscFunctionReturn(PETSC_SUCCESS);
688: }
690: static PetscErrorCode MatLUFactorNumeric_Elemental(Mat F, Mat A, const MatFactorInfo *info)
691: {
692: PetscFunctionBegin;
693: PetscCall(MatCopy(A, F, SAME_NONZERO_PATTERN));
694: PetscCall(MatLUFactor_Elemental(F, nullptr, nullptr, info));
695: PetscFunctionReturn(PETSC_SUCCESS);
696: }
698: static PetscErrorCode MatLUFactorSymbolic_Elemental(Mat, Mat, IS, IS, const MatFactorInfo *)
699: {
700: PetscFunctionBegin;
701: /* F is created and allocated by MatGetFactor_elemental_petsc(), skip this routine. */
702: PetscFunctionReturn(PETSC_SUCCESS);
703: }
705: static PetscErrorCode MatCholeskyFactor_Elemental(Mat A, IS, const MatFactorInfo *)
706: {
707: Mat_Elemental *a = (Mat_Elemental *)A->data;
708: El::DistMatrix<PetscElemScalar, El::MC, El::STAR> d;
710: PetscFunctionBegin;
711: El::Cholesky(El::UPPER, *a->emat);
712: A->factortype = MAT_FACTOR_CHOLESKY;
713: A->assembled = PETSC_TRUE;
715: PetscCall(PetscFree(A->solvertype));
716: PetscCall(PetscStrallocpy(MATSOLVERELEMENTAL, &A->solvertype));
717: PetscFunctionReturn(PETSC_SUCCESS);
718: }
720: static PetscErrorCode MatCholeskyFactorNumeric_Elemental(Mat F, Mat A, const MatFactorInfo *info)
721: {
722: PetscFunctionBegin;
723: PetscCall(MatCopy(A, F, SAME_NONZERO_PATTERN));
724: PetscCall(MatCholeskyFactor_Elemental(F, nullptr, info));
725: PetscFunctionReturn(PETSC_SUCCESS);
726: }
728: static PetscErrorCode MatCholeskyFactorSymbolic_Elemental(Mat, Mat, IS, const MatFactorInfo *)
729: {
730: PetscFunctionBegin;
731: /* F is created and allocated by MatGetFactor_elemental_petsc(), skip this routine. */
732: PetscFunctionReturn(PETSC_SUCCESS);
733: }
735: static PetscErrorCode MatFactorGetSolverType_elemental_elemental(Mat, MatSolverType *type)
736: {
737: PetscFunctionBegin;
738: *type = MATSOLVERELEMENTAL;
739: PetscFunctionReturn(PETSC_SUCCESS);
740: }
742: static PetscErrorCode MatGetFactor_elemental_elemental(Mat A, MatFactorType ftype, Mat *F)
743: {
744: Mat B;
746: PetscFunctionBegin;
747: /* Create the factorization matrix */
748: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
749: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, PETSC_DECIDE, PETSC_DECIDE));
750: PetscCall(MatSetType(B, MATELEMENTAL));
751: PetscCall(MatSetUp(B));
752: B->factortype = ftype;
753: B->trivialsymbolic = PETSC_TRUE;
754: PetscCall(PetscFree(B->solvertype));
755: PetscCall(PetscStrallocpy(MATSOLVERELEMENTAL, &B->solvertype));
757: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatFactorGetSolverType_C", MatFactorGetSolverType_elemental_elemental));
758: *F = B;
759: PetscFunctionReturn(PETSC_SUCCESS);
760: }
762: PETSC_INTERN PetscErrorCode MatSolverTypeRegister_Elemental(void)
763: {
764: PetscFunctionBegin;
765: PetscCall(MatSolverTypeRegister(MATSOLVERELEMENTAL, MATELEMENTAL, MAT_FACTOR_LU, MatGetFactor_elemental_elemental));
766: PetscCall(MatSolverTypeRegister(MATSOLVERELEMENTAL, MATELEMENTAL, MAT_FACTOR_CHOLESKY, MatGetFactor_elemental_elemental));
767: PetscFunctionReturn(PETSC_SUCCESS);
768: }
770: static PetscErrorCode MatNorm_Elemental(Mat A, NormType type, PetscReal *nrm)
771: {
772: Mat_Elemental *a = (Mat_Elemental *)A->data;
774: PetscFunctionBegin;
775: switch (type) {
776: case NORM_1:
777: *nrm = El::OneNorm(*a->emat);
778: break;
779: case NORM_FROBENIUS:
780: *nrm = El::FrobeniusNorm(*a->emat);
781: break;
782: case NORM_INFINITY:
783: *nrm = El::InfinityNorm(*a->emat);
784: break;
785: default:
786: SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Unsupported norm type");
787: }
788: PetscFunctionReturn(PETSC_SUCCESS);
789: }
791: static PetscErrorCode MatZeroEntries_Elemental(Mat A)
792: {
793: Mat_Elemental *a = (Mat_Elemental *)A->data;
795: PetscFunctionBegin;
796: El::Zero(*a->emat);
797: PetscFunctionReturn(PETSC_SUCCESS);
798: }
800: static PetscErrorCode MatGetOwnershipIS_Elemental(Mat A, IS *rows, IS *cols)
801: {
802: Mat_Elemental *a = (Mat_Elemental *)A->data;
803: PetscInt i, m, shift, stride, *idx;
805: PetscFunctionBegin;
806: if (rows) {
807: m = a->emat->LocalHeight();
808: shift = a->emat->ColShift();
809: stride = a->emat->ColStride();
810: PetscCall(PetscMalloc1(m, &idx));
811: for (i = 0; i < m; i++) {
812: PetscInt rank, offset;
813: E2RO(A, 0, shift + i * stride, &rank, &offset);
814: RO2P(A, 0, rank, offset, &idx[i]);
815: }
816: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, m, idx, PETSC_OWN_POINTER, rows));
817: }
818: if (cols) {
819: m = a->emat->LocalWidth();
820: shift = a->emat->RowShift();
821: stride = a->emat->RowStride();
822: PetscCall(PetscMalloc1(m, &idx));
823: for (i = 0; i < m; i++) {
824: PetscInt rank, offset;
825: E2RO(A, 1, shift + i * stride, &rank, &offset);
826: RO2P(A, 1, rank, offset, &idx[i]);
827: }
828: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, m, idx, PETSC_OWN_POINTER, cols));
829: }
830: PetscFunctionReturn(PETSC_SUCCESS);
831: }
833: static PetscErrorCode MatConvert_Elemental_Dense(Mat A, MatType, MatReuse reuse, Mat *B)
834: {
835: Mat Bmpi;
836: Mat_Elemental *a = (Mat_Elemental *)A->data;
837: MPI_Comm comm;
838: IS isrows, iscols;
839: PetscInt rrank, ridx, crank, cidx, nrows, ncols, i, j, erow, ecol, elrow, elcol;
840: const PetscInt *rows, *cols;
841: PetscElemScalar v;
842: const El::Grid &grid = a->emat->Grid();
844: PetscFunctionBegin;
845: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
847: if (reuse == MAT_REUSE_MATRIX) {
848: Bmpi = *B;
849: } else {
850: PetscCall(MatCreate(comm, &Bmpi));
851: PetscCall(MatSetSizes(Bmpi, A->rmap->n, A->cmap->n, PETSC_DECIDE, PETSC_DECIDE));
852: PetscCall(MatSetType(Bmpi, MATDENSE));
853: PetscCall(MatSetUp(Bmpi));
854: }
856: /* Get local entries of A */
857: PetscCall(MatGetOwnershipIS(A, &isrows, &iscols));
858: PetscCall(ISGetLocalSize(isrows, &nrows));
859: PetscCall(ISGetIndices(isrows, &rows));
860: PetscCall(ISGetLocalSize(iscols, &ncols));
861: PetscCall(ISGetIndices(iscols, &cols));
863: if (a->roworiented) {
864: for (i = 0; i < nrows; i++) {
865: P2RO(A, 0, rows[i], &rrank, &ridx); /* convert indices between PETSc <-> (Rank,Offset) <-> Elemental */
866: RO2E(A, 0, rrank, ridx, &erow);
867: PetscCheck(rrank >= 0 && ridx >= 0 && erow >= 0, comm, PETSC_ERR_PLIB, "Incorrect row translation");
868: for (j = 0; j < ncols; j++) {
869: P2RO(A, 1, cols[j], &crank, &cidx);
870: RO2E(A, 1, crank, cidx, &ecol);
871: PetscCheck(crank >= 0 && cidx >= 0 && ecol >= 0, comm, PETSC_ERR_PLIB, "Incorrect col translation");
873: elrow = erow / grid.MCSize(); /* Elemental local row index */
874: elcol = ecol / grid.MRSize(); /* Elemental local column index */
875: v = a->emat->GetLocal(elrow, elcol);
876: PetscCall(MatSetValues(Bmpi, 1, &rows[i], 1, &cols[j], (PetscScalar *)&v, INSERT_VALUES));
877: }
878: }
879: } else { /* column-oriented */
880: for (j = 0; j < ncols; j++) {
881: P2RO(A, 1, cols[j], &crank, &cidx);
882: RO2E(A, 1, crank, cidx, &ecol);
883: PetscCheck(crank >= 0 && cidx >= 0 && ecol >= 0, comm, PETSC_ERR_PLIB, "Incorrect col translation");
884: for (i = 0; i < nrows; i++) {
885: P2RO(A, 0, rows[i], &rrank, &ridx); /* convert indices between PETSc <-> (Rank,Offset) <-> Elemental */
886: RO2E(A, 0, rrank, ridx, &erow);
887: PetscCheck(rrank >= 0 && ridx >= 0 && erow >= 0, comm, PETSC_ERR_PLIB, "Incorrect row translation");
889: elrow = erow / grid.MCSize(); /* Elemental local row index */
890: elcol = ecol / grid.MRSize(); /* Elemental local column index */
891: v = a->emat->GetLocal(elrow, elcol);
892: PetscCall(MatSetValues(Bmpi, 1, &rows[i], 1, &cols[j], (PetscScalar *)&v, INSERT_VALUES));
893: }
894: }
895: }
896: PetscCall(MatAssemblyBegin(Bmpi, MAT_FINAL_ASSEMBLY));
897: PetscCall(MatAssemblyEnd(Bmpi, MAT_FINAL_ASSEMBLY));
898: if (reuse == MAT_INPLACE_MATRIX) {
899: PetscCall(MatHeaderReplace(A, &Bmpi));
900: } else {
901: *B = Bmpi;
902: }
903: PetscCall(ISDestroy(&isrows));
904: PetscCall(ISDestroy(&iscols));
905: PetscFunctionReturn(PETSC_SUCCESS);
906: }
908: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat A, MatType, MatReuse reuse, Mat *newmat)
909: {
910: Mat mat_elemental;
911: PetscInt M = A->rmap->N, N = A->cmap->N, row, ncols;
912: const PetscInt *cols;
913: const PetscScalar *vals;
915: PetscFunctionBegin;
916: if (reuse == MAT_REUSE_MATRIX) {
917: mat_elemental = *newmat;
918: PetscCall(MatZeroEntries(mat_elemental));
919: } else {
920: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental));
921: PetscCall(MatSetSizes(mat_elemental, PETSC_DECIDE, PETSC_DECIDE, M, N));
922: PetscCall(MatSetType(mat_elemental, MATELEMENTAL));
923: PetscCall(MatSetUp(mat_elemental));
924: }
925: for (row = 0; row < M; row++) {
926: PetscCall(MatGetRow(A, row, &ncols, &cols, &vals));
927: /* PETSc-Elemental interface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
928: PetscCall(MatSetValues(mat_elemental, 1, &row, ncols, cols, vals, ADD_VALUES));
929: PetscCall(MatRestoreRow(A, row, &ncols, &cols, &vals));
930: }
931: PetscCall(MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY));
932: PetscCall(MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY));
934: if (reuse == MAT_INPLACE_MATRIX) {
935: PetscCall(MatHeaderReplace(A, &mat_elemental));
936: } else {
937: *newmat = mat_elemental;
938: }
939: PetscFunctionReturn(PETSC_SUCCESS);
940: }
942: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_Elemental(Mat A, MatType, MatReuse reuse, Mat *newmat)
943: {
944: Mat mat_elemental;
945: PetscInt row, ncols, rstart = A->rmap->rstart, rend = A->rmap->rend, j;
946: const PetscInt *cols;
947: const PetscScalar *vals;
949: PetscFunctionBegin;
950: if (reuse == MAT_REUSE_MATRIX) {
951: mat_elemental = *newmat;
952: PetscCall(MatZeroEntries(mat_elemental));
953: } else {
954: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental));
955: PetscCall(MatSetSizes(mat_elemental, PETSC_DECIDE, PETSC_DECIDE, A->rmap->N, A->cmap->N));
956: PetscCall(MatSetType(mat_elemental, MATELEMENTAL));
957: PetscCall(MatSetUp(mat_elemental));
958: }
959: for (row = rstart; row < rend; row++) {
960: PetscCall(MatGetRow(A, row, &ncols, &cols, &vals));
961: for (j = 0; j < ncols; j++) {
962: /* PETSc-Elemental interface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
963: PetscCall(MatSetValues(mat_elemental, 1, &row, 1, &cols[j], &vals[j], ADD_VALUES));
964: }
965: PetscCall(MatRestoreRow(A, row, &ncols, &cols, &vals));
966: }
967: PetscCall(MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY));
968: PetscCall(MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY));
970: if (reuse == MAT_INPLACE_MATRIX) {
971: PetscCall(MatHeaderReplace(A, &mat_elemental));
972: } else {
973: *newmat = mat_elemental;
974: }
975: PetscFunctionReturn(PETSC_SUCCESS);
976: }
978: PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_Elemental(Mat A, MatType, MatReuse reuse, Mat *newmat)
979: {
980: Mat mat_elemental;
981: PetscInt M = A->rmap->N, N = A->cmap->N, row, ncols, j;
982: const PetscInt *cols;
983: const PetscScalar *vals;
985: PetscFunctionBegin;
986: if (reuse == MAT_REUSE_MATRIX) {
987: mat_elemental = *newmat;
988: PetscCall(MatZeroEntries(mat_elemental));
989: } else {
990: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental));
991: PetscCall(MatSetSizes(mat_elemental, PETSC_DECIDE, PETSC_DECIDE, M, N));
992: PetscCall(MatSetType(mat_elemental, MATELEMENTAL));
993: PetscCall(MatSetUp(mat_elemental));
994: }
995: PetscCall(MatGetRowUpperTriangular(A));
996: for (row = 0; row < M; row++) {
997: PetscCall(MatGetRow(A, row, &ncols, &cols, &vals));
998: /* PETSc-Elemental interface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
999: PetscCall(MatSetValues(mat_elemental, 1, &row, ncols, cols, vals, ADD_VALUES));
1000: for (j = 0; j < ncols; j++) { /* lower triangular part */
1001: PetscScalar v;
1002: if (cols[j] == row) continue;
1003: v = A->hermitian == PETSC_BOOL3_TRUE ? PetscConj(vals[j]) : vals[j];
1004: PetscCall(MatSetValues(mat_elemental, 1, &cols[j], 1, &row, &v, ADD_VALUES));
1005: }
1006: PetscCall(MatRestoreRow(A, row, &ncols, &cols, &vals));
1007: }
1008: PetscCall(MatRestoreRowUpperTriangular(A));
1009: PetscCall(MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY));
1010: PetscCall(MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY));
1012: if (reuse == MAT_INPLACE_MATRIX) {
1013: PetscCall(MatHeaderReplace(A, &mat_elemental));
1014: } else {
1015: *newmat = mat_elemental;
1016: }
1017: PetscFunctionReturn(PETSC_SUCCESS);
1018: }
1020: PETSC_INTERN PetscErrorCode MatConvert_MPISBAIJ_Elemental(Mat A, MatType, MatReuse reuse, Mat *newmat)
1021: {
1022: Mat mat_elemental;
1023: PetscInt M = A->rmap->N, N = A->cmap->N, row, ncols, j, rstart = A->rmap->rstart, rend = A->rmap->rend;
1024: const PetscInt *cols;
1025: const PetscScalar *vals;
1027: PetscFunctionBegin;
1028: if (reuse == MAT_REUSE_MATRIX) {
1029: mat_elemental = *newmat;
1030: PetscCall(MatZeroEntries(mat_elemental));
1031: } else {
1032: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental));
1033: PetscCall(MatSetSizes(mat_elemental, PETSC_DECIDE, PETSC_DECIDE, M, N));
1034: PetscCall(MatSetType(mat_elemental, MATELEMENTAL));
1035: PetscCall(MatSetUp(mat_elemental));
1036: }
1037: PetscCall(MatGetRowUpperTriangular(A));
1038: for (row = rstart; row < rend; row++) {
1039: PetscCall(MatGetRow(A, row, &ncols, &cols, &vals));
1040: /* PETSc-Elemental interface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
1041: PetscCall(MatSetValues(mat_elemental, 1, &row, ncols, cols, vals, ADD_VALUES));
1042: for (j = 0; j < ncols; j++) { /* lower triangular part */
1043: PetscScalar v;
1044: if (cols[j] == row) continue;
1045: v = A->hermitian == PETSC_BOOL3_TRUE ? PetscConj(vals[j]) : vals[j];
1046: PetscCall(MatSetValues(mat_elemental, 1, &cols[j], 1, &row, &v, ADD_VALUES));
1047: }
1048: PetscCall(MatRestoreRow(A, row, &ncols, &cols, &vals));
1049: }
1050: PetscCall(MatRestoreRowUpperTriangular(A));
1051: PetscCall(MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY));
1052: PetscCall(MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY));
1054: if (reuse == MAT_INPLACE_MATRIX) {
1055: PetscCall(MatHeaderReplace(A, &mat_elemental));
1056: } else {
1057: *newmat = mat_elemental;
1058: }
1059: PetscFunctionReturn(PETSC_SUCCESS);
1060: }
1062: static PetscErrorCode MatDestroy_Elemental(Mat A)
1063: {
1064: Mat_Elemental *a = (Mat_Elemental *)A->data;
1065: Mat_Elemental_Grid *commgrid;
1066: PetscMPIInt iflg;
1067: MPI_Comm icomm;
1069: PetscFunctionBegin;
1070: delete a->emat;
1071: delete a->P;
1072: delete a->Q;
1074: El::mpi::Comm cxxcomm(PetscObjectComm((PetscObject)A));
1075: PetscCall(PetscCommDuplicate(cxxcomm.comm, &icomm, nullptr));
1076: PetscCallMPI(MPI_Comm_get_attr(icomm, Petsc_Elemental_keyval, (void **)&commgrid, &iflg));
1077: if (--commgrid->grid_refct == 0) {
1078: delete commgrid->grid;
1079: PetscCall(PetscFree(commgrid));
1080: PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Elemental_keyval));
1081: }
1082: PetscCall(PetscCommDestroy(&icomm));
1083: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatGetOwnershipIS_C", nullptr));
1084: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatFactorGetSolverType_C", nullptr));
1085: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_elemental_mpidense_C", nullptr));
1086: PetscCall(PetscFree(A->data));
1087: PetscFunctionReturn(PETSC_SUCCESS);
1088: }
1090: static PetscErrorCode MatSetUp_Elemental(Mat A)
1091: {
1092: Mat_Elemental *a = (Mat_Elemental *)A->data;
1093: MPI_Comm comm;
1094: PetscMPIInt rsize, csize;
1095: PetscInt n;
1097: PetscFunctionBegin;
1098: PetscCall(PetscLayoutSetUp(A->rmap));
1099: PetscCall(PetscLayoutSetUp(A->cmap));
1101: /* Check if local row and column sizes are equally distributed.
1102: Jed: Elemental uses "element" cyclic ordering so the sizes need to match that
1103: exactly. The strategy in MatElemental is for PETSc to implicitly permute to block ordering (like would be returned by
1104: PetscSplitOwnership(comm,&n,&N), at which point Elemental matrices can act on PETSc vectors without redistributing the vectors. */
1105: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
1106: n = PETSC_DECIDE;
1107: PetscCall(PetscSplitOwnership(comm, &n, &A->rmap->N));
1108: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local row size %" PetscInt_FMT " of ELEMENTAL matrix must be equally distributed", A->rmap->n);
1110: n = PETSC_DECIDE;
1111: PetscCall(PetscSplitOwnership(comm, &n, &A->cmap->N));
1112: PetscCheck(n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local column size %" PetscInt_FMT " of ELEMENTAL matrix must be equally distributed", A->cmap->n);
1114: a->emat->Resize(A->rmap->N, A->cmap->N);
1115: El::Zero(*a->emat);
1117: PetscCallMPI(MPI_Comm_size(A->rmap->comm, &rsize));
1118: PetscCallMPI(MPI_Comm_size(A->cmap->comm, &csize));
1119: PetscCheck(csize == rsize, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Cannot use row and column communicators of different sizes");
1120: a->commsize = rsize;
1121: a->mr[0] = A->rmap->N % rsize;
1122: if (!a->mr[0]) a->mr[0] = rsize;
1123: a->mr[1] = A->cmap->N % csize;
1124: if (!a->mr[1]) a->mr[1] = csize;
1125: a->m[0] = A->rmap->N / rsize + (a->mr[0] != rsize);
1126: a->m[1] = A->cmap->N / csize + (a->mr[1] != csize);
1127: PetscFunctionReturn(PETSC_SUCCESS);
1128: }
1130: static PetscErrorCode MatAssemblyBegin_Elemental(Mat A, MatAssemblyType)
1131: {
1132: Mat_Elemental *a = (Mat_Elemental *)A->data;
1134: PetscFunctionBegin;
1135: /* printf("Calling ProcessQueues\n"); */
1136: a->emat->ProcessQueues();
1137: /* printf("Finished ProcessQueues\n"); */
1138: PetscFunctionReturn(PETSC_SUCCESS);
1139: }
1141: static PetscErrorCode MatAssemblyEnd_Elemental(Mat, MatAssemblyType)
1142: {
1143: PetscFunctionBegin;
1144: /* Currently does nothing */
1145: PetscFunctionReturn(PETSC_SUCCESS);
1146: }
1148: static PetscErrorCode MatLoad_Elemental(Mat newMat, PetscViewer viewer)
1149: {
1150: Mat Adense, Ae;
1151: MPI_Comm comm;
1153: PetscFunctionBegin;
1154: PetscCall(PetscObjectGetComm((PetscObject)newMat, &comm));
1155: PetscCall(MatCreate(comm, &Adense));
1156: PetscCall(MatSetType(Adense, MATDENSE));
1157: PetscCall(MatLoad(Adense, viewer));
1158: PetscCall(MatConvert(Adense, MATELEMENTAL, MAT_INITIAL_MATRIX, &Ae));
1159: PetscCall(MatDestroy(&Adense));
1160: PetscCall(MatHeaderReplace(newMat, &Ae));
1161: PetscFunctionReturn(PETSC_SUCCESS);
1162: }
1164: static struct _MatOps MatOps_Values = {MatSetValues_Elemental,
1165: nullptr,
1166: nullptr,
1167: MatMult_Elemental,
1168: /* 4*/ MatMultAdd_Elemental,
1169: MatMultTranspose_Elemental,
1170: MatMultTransposeAdd_Elemental,
1171: MatSolve_Elemental,
1172: MatSolveAdd_Elemental,
1173: nullptr,
1174: /*10*/ nullptr,
1175: MatLUFactor_Elemental,
1176: MatCholeskyFactor_Elemental,
1177: nullptr,
1178: MatTranspose_Elemental,
1179: /*15*/ MatGetInfo_Elemental,
1180: nullptr,
1181: MatGetDiagonal_Elemental,
1182: MatDiagonalScale_Elemental,
1183: MatNorm_Elemental,
1184: /*20*/ MatAssemblyBegin_Elemental,
1185: MatAssemblyEnd_Elemental,
1186: MatSetOption_Elemental,
1187: MatZeroEntries_Elemental,
1188: /*24*/ nullptr,
1189: MatLUFactorSymbolic_Elemental,
1190: MatLUFactorNumeric_Elemental,
1191: MatCholeskyFactorSymbolic_Elemental,
1192: MatCholeskyFactorNumeric_Elemental,
1193: /*29*/ MatSetUp_Elemental,
1194: nullptr,
1195: nullptr,
1196: nullptr,
1197: nullptr,
1198: /*34*/ MatDuplicate_Elemental,
1199: nullptr,
1200: nullptr,
1201: nullptr,
1202: nullptr,
1203: /*39*/ MatAXPY_Elemental,
1204: nullptr,
1205: nullptr,
1206: nullptr,
1207: MatCopy_Elemental,
1208: /*44*/ nullptr,
1209: MatScale_Elemental,
1210: MatShift_Basic,
1211: nullptr,
1212: nullptr,
1213: /*49*/ nullptr,
1214: nullptr,
1215: nullptr,
1216: nullptr,
1217: nullptr,
1218: /*54*/ nullptr,
1219: nullptr,
1220: nullptr,
1221: nullptr,
1222: nullptr,
1223: /*59*/ nullptr,
1224: MatDestroy_Elemental,
1225: MatView_Elemental,
1226: nullptr,
1227: nullptr,
1228: /*64*/ nullptr,
1229: nullptr,
1230: nullptr,
1231: nullptr,
1232: nullptr,
1233: /*69*/ nullptr,
1234: MatConvert_Elemental_Dense,
1235: nullptr,
1236: nullptr,
1237: nullptr,
1238: /*74*/ nullptr,
1239: nullptr,
1240: nullptr,
1241: nullptr,
1242: MatLoad_Elemental,
1243: /*79*/ nullptr,
1244: nullptr,
1245: nullptr,
1246: nullptr,
1247: nullptr,
1248: /*84*/ nullptr,
1249: MatMatMultNumeric_Elemental,
1250: nullptr,
1251: nullptr,
1252: MatMatTransposeMultNumeric_Elemental,
1253: /*89*/ nullptr,
1254: MatProductSetFromOptions_Elemental,
1255: nullptr,
1256: nullptr,
1257: MatConjugate_Elemental,
1258: /*94*/ nullptr,
1259: nullptr,
1260: nullptr,
1261: nullptr,
1262: nullptr,
1263: /*99*/ nullptr,
1264: MatMatSolve_Elemental,
1265: nullptr,
1266: nullptr,
1267: nullptr,
1268: /*104*/ nullptr,
1269: nullptr,
1270: nullptr,
1271: nullptr,
1272: nullptr,
1273: /*109*/ nullptr,
1274: MatHermitianTranspose_Elemental,
1275: nullptr,
1276: nullptr,
1277: /*114*/ nullptr,
1278: nullptr,
1279: nullptr,
1280: nullptr,
1281: nullptr,
1282: /*119*/ nullptr,
1283: nullptr,
1284: nullptr,
1285: nullptr,
1286: nullptr,
1287: /*124*/ nullptr,
1288: nullptr,
1289: nullptr,
1290: nullptr,
1291: nullptr,
1292: /*129*/ nullptr,
1293: nullptr,
1294: nullptr,
1295: nullptr,
1296: nullptr,
1297: /*134*/ nullptr,
1298: nullptr,
1299: nullptr,
1300: nullptr,
1301: nullptr,
1302: nullptr,
1303: /*140*/ nullptr,
1304: nullptr,
1305: nullptr,
1306: nullptr,
1307: nullptr,
1308: /*144*/ nullptr,
1309: nullptr,
1310: nullptr,
1311: nullptr};
1313: /*MC
1314: MATELEMENTAL = "elemental" - A matrix type for dense matrices using the Elemental package
1316: Use ./configure --download-elemental to install PETSc to use Elemental
1318: Options Database Keys:
1319: + -mat_type elemental - sets the matrix type to "elemental" during a call to MatSetFromOptions()
1320: . -pc_factor_mat_solver_type elemental - to use this direct solver with the option -pc_type lu
1321: - -mat_elemental_grid_height - sets Grid Height for 2D cyclic ordering of internal matrix
1323: Level: beginner
1325: Note:
1326: Note unlike most matrix formats, this format does not store all the matrix entries for a contiguous
1327: range of rows on an MPI process. Use `MatGetOwnershipIS()` to determine what values are stored on
1328: the given rank.
1330: .seealso: `MATDENSE`, `MATSCALAPACK`, `MatGetOwnershipIS()`
1331: M*/
1332: #if defined(__clang__)
1333: #pragma clang diagnostic push
1334: #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
1335: #endif
1336: PETSC_EXTERN PetscErrorCode MatCreate_Elemental(Mat A)
1337: {
1338: Mat_Elemental *a;
1339: PetscBool flg;
1340: PetscMPIInt iflg;
1341: Mat_Elemental_Grid *commgrid;
1342: MPI_Comm icomm;
1343: PetscInt optv1;
1345: PetscFunctionBegin;
1346: A->ops[0] = MatOps_Values;
1347: A->insertmode = NOT_SET_VALUES;
1349: PetscCall(PetscNew(&a));
1350: A->data = (void *)a;
1352: /* Set up the elemental matrix */
1353: El::mpi::Comm cxxcomm(PetscObjectComm((PetscObject)A));
1355: /* Grid needs to be shared between multiple Mats on the same communicator, implement by attribute caching on the MPI_Comm */
1356: if (Petsc_Elemental_keyval == MPI_KEYVAL_INVALID) {
1357: PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Elemental_keyval, nullptr));
1358: PetscCall(PetscCitationsRegister(ElementalCitation, &ElementalCite));
1359: }
1360: PetscCall(PetscCommDuplicate(cxxcomm.comm, &icomm, NULL));
1361: PetscCallMPI(MPI_Comm_get_attr(icomm, Petsc_Elemental_keyval, (void **)&commgrid, &iflg));
1362: if (!iflg) {
1363: PetscCall(PetscNew(&commgrid));
1365: PetscOptionsBegin(PetscObjectComm((PetscObject)A), ((PetscObject)A)->prefix, "Elemental Options", "Mat");
1366: /* displayed default grid sizes (CommSize,1) are set by us arbitrarily until El::Grid() is called */
1367: PetscCall(PetscOptionsInt("-mat_elemental_grid_height", "Grid Height", "None", El::mpi::Size(cxxcomm), &optv1, &flg));
1368: if (flg) {
1369: PetscCheck((El::mpi::Size(cxxcomm) % optv1) == 0, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Grid Height %" PetscInt_FMT " must evenly divide CommSize %" PetscInt_FMT, optv1, (PetscInt)El::mpi::Size(cxxcomm));
1370: commgrid->grid = new El::Grid(cxxcomm, optv1); /* use user-provided grid height */
1371: } else {
1372: commgrid->grid = new El::Grid(cxxcomm); /* use Elemental default grid sizes */
1373: /* printf("new commgrid->grid = %p\n",commgrid->grid); -- memory leak revealed by valgrind? */
1374: }
1375: commgrid->grid_refct = 1;
1376: PetscCallMPI(MPI_Comm_set_attr(icomm, Petsc_Elemental_keyval, (void *)commgrid));
1378: a->pivoting = 1;
1379: PetscCall(PetscOptionsInt("-mat_elemental_pivoting", "Pivoting", "None", a->pivoting, &a->pivoting, NULL));
1381: PetscOptionsEnd();
1382: } else {
1383: commgrid->grid_refct++;
1384: }
1385: PetscCall(PetscCommDestroy(&icomm));
1386: a->grid = commgrid->grid;
1387: a->emat = new El::DistMatrix<PetscElemScalar>(*a->grid);
1388: a->roworiented = PETSC_TRUE;
1390: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatGetOwnershipIS_C", MatGetOwnershipIS_Elemental));
1391: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_elemental_mpidense_C", MatProductSetFromOptions_Elemental_MPIDense));
1392: PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATELEMENTAL));
1393: PetscFunctionReturn(PETSC_SUCCESS);
1394: }
1395: #if defined(__clang__)
1396: #pragma clang diagnostic pop
1397: #endif