Actual source code: mpidense.c
1: /*
2: Basic functions for basic parallel dense matrices.
3: Portions of this code are under:
4: Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved.
5: */
7: #include <../src/mat/impls/dense/mpi/mpidense.h>
8: #include <../src/mat/impls/aij/mpi/mpiaij.h>
9: #include <petscblaslapack.h>
10: #include <petsc/private/vecimpl.h>
11: #include <petsc/private/deviceimpl.h>
12: #include <petsc/private/sfimpl.h>
14: /*@
15: MatDenseGetLocalMatrix - For a `MATMPIDENSE` or `MATSEQDENSE` matrix returns the sequential
16: matrix that represents the operator. For sequential matrices it returns itself.
18: Input Parameter:
19: . A - the sequential or MPI `MATDENSE` matrix
21: Output Parameter:
22: . B - the inner matrix
24: Level: intermediate
26: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATMPIDENSE`, `MATSEQDENSE`
27: @*/
28: PetscErrorCode MatDenseGetLocalMatrix(Mat A, Mat *B)
29: {
30: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
31: PetscBool flg;
33: PetscFunctionBegin;
35: PetscAssertPointer(B, 2);
36: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &flg));
37: if (flg) *B = mat->A;
38: else {
39: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATSEQDENSE, &flg));
40: PetscCheck(flg, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Not for matrix type %s", ((PetscObject)A)->type_name);
41: *B = A;
42: }
43: PetscFunctionReturn(PETSC_SUCCESS);
44: }
46: static PetscErrorCode MatCopy_MPIDense(Mat A, Mat B, MatStructure s)
47: {
48: Mat_MPIDense *Amat = (Mat_MPIDense *)A->data;
49: Mat_MPIDense *Bmat = (Mat_MPIDense *)B->data;
51: PetscFunctionBegin;
52: PetscCall(MatCopy(Amat->A, Bmat->A, s));
53: PetscFunctionReturn(PETSC_SUCCESS);
54: }
56: PetscErrorCode MatShift_MPIDense(Mat A, PetscScalar alpha)
57: {
58: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
59: PetscInt j, lda, rstart = A->rmap->rstart, rend = A->rmap->rend, rend2;
60: PetscScalar *v;
62: PetscFunctionBegin;
63: PetscCall(MatDenseGetArray(mat->A, &v));
64: PetscCall(MatDenseGetLDA(mat->A, &lda));
65: rend2 = PetscMin(rend, A->cmap->N);
66: if (rend2 > rstart) {
67: for (j = rstart; j < rend2; j++) v[j - rstart + j * lda] += alpha;
68: PetscCall(PetscLogFlops(rend2 - rstart));
69: }
70: PetscCall(MatDenseRestoreArray(mat->A, &v));
71: PetscFunctionReturn(PETSC_SUCCESS);
72: }
74: static PetscErrorCode MatGetRow_MPIDense(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
75: {
76: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
77: PetscInt lrow, rstart = A->rmap->rstart, rend = A->rmap->rend;
79: PetscFunctionBegin;
80: PetscCheck(row >= rstart && row < rend, PETSC_COMM_SELF, PETSC_ERR_SUP, "only local rows");
81: lrow = row - rstart;
82: PetscCall(MatGetRow(mat->A, lrow, nz, (const PetscInt **)idx, (const PetscScalar **)v));
83: PetscFunctionReturn(PETSC_SUCCESS);
84: }
86: static PetscErrorCode MatRestoreRow_MPIDense(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
87: {
88: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
89: PetscInt lrow, rstart = A->rmap->rstart, rend = A->rmap->rend;
91: PetscFunctionBegin;
92: PetscCheck(row >= rstart && row < rend, PETSC_COMM_SELF, PETSC_ERR_SUP, "only local rows");
93: lrow = row - rstart;
94: PetscCall(MatRestoreRow(mat->A, lrow, nz, (const PetscInt **)idx, (const PetscScalar **)v));
95: PetscFunctionReturn(PETSC_SUCCESS);
96: }
98: static PetscErrorCode MatGetDiagonalBlock_MPIDense(Mat A, Mat *a)
99: {
100: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
101: PetscInt m = A->rmap->n, rstart = A->rmap->rstart;
102: PetscScalar *array;
103: MPI_Comm comm;
104: PetscBool flg;
105: Mat B;
107: PetscFunctionBegin;
108: PetscCall(MatHasCongruentLayouts(A, &flg));
109: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only square matrices supported.");
110: PetscCall(PetscObjectQuery((PetscObject)A, "DiagonalBlock", (PetscObject *)&B));
111: if (!B) { /* This should use MatDenseGetSubMatrix (not create), but we would need a call like MatRestoreDiagonalBlock */
112: #if PetscDefined(HAVE_CUDA)
113: PetscCall(PetscObjectTypeCompare((PetscObject)mdn->A, MATSEQDENSECUDA, &flg));
114: PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not coded for %s. Send an email to petsc-dev@mcs.anl.gov to request this feature", MATSEQDENSECUDA);
115: #elif PetscDefined(HAVE_HIP)
116: PetscCall(PetscObjectTypeCompare((PetscObject)mdn->A, MATSEQDENSEHIP, &flg));
117: PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not coded for %s. Send an email to petsc-dev@mcs.anl.gov to request this feature", MATSEQDENSEHIP);
118: #endif
119: PetscCall(PetscObjectGetComm((PetscObject)mdn->A, &comm));
120: PetscCall(MatCreate(comm, &B));
121: PetscCall(MatSetSizes(B, m, m, m, m));
122: PetscCall(MatSetType(B, ((PetscObject)mdn->A)->type_name));
123: PetscCall(MatDenseGetArrayRead(mdn->A, (const PetscScalar **)&array));
124: PetscCall(MatSeqDenseSetPreallocation(B, array + m * rstart));
125: PetscCall(MatDenseRestoreArrayRead(mdn->A, (const PetscScalar **)&array));
126: PetscCall(PetscObjectCompose((PetscObject)A, "DiagonalBlock", (PetscObject)B));
127: *a = B;
128: PetscCall(MatDestroy(&B));
129: } else *a = B;
130: PetscFunctionReturn(PETSC_SUCCESS);
131: }
133: static PetscErrorCode MatSetValues_MPIDense(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar v[], InsertMode addv)
134: {
135: Mat_MPIDense *A = (Mat_MPIDense *)mat->data;
136: PetscInt i, j, rstart = mat->rmap->rstart, rend = mat->rmap->rend, row;
137: PetscBool roworiented = A->roworiented;
139: PetscFunctionBegin;
140: for (i = 0; i < m; i++) {
141: if (idxm[i] < 0) continue;
142: PetscCheck(idxm[i] < mat->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large");
143: if (idxm[i] >= rstart && idxm[i] < rend) {
144: row = idxm[i] - rstart;
145: if (roworiented) {
146: PetscCall(MatSetValues(A->A, 1, &row, n, idxn, PetscSafePointerPlusOffset(v, i * n), addv));
147: } else {
148: for (j = 0; j < n; j++) {
149: if (idxn[j] < 0) continue;
150: PetscCheck(idxn[j] < mat->cmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large");
151: PetscCall(MatSetValues(A->A, 1, &row, 1, &idxn[j], PetscSafePointerPlusOffset(v, i + j * m), addv));
152: }
153: }
154: } else if (!A->donotstash) {
155: mat->assembled = PETSC_FALSE;
156: if (roworiented) {
157: PetscCall(MatStashValuesRow_Private(&mat->stash, idxm[i], n, idxn, PetscSafePointerPlusOffset(v, i * n), PETSC_FALSE));
158: } else {
159: PetscCall(MatStashValuesCol_Private(&mat->stash, idxm[i], n, idxn, PetscSafePointerPlusOffset(v, i), m, PETSC_FALSE));
160: }
161: }
162: }
163: PetscFunctionReturn(PETSC_SUCCESS);
164: }
166: static PetscErrorCode MatGetValues_MPIDense(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], PetscScalar v[])
167: {
168: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
169: PetscInt i, j, rstart = mat->rmap->rstart, rend = mat->rmap->rend, row;
171: PetscFunctionBegin;
172: for (i = 0; i < m; i++) {
173: if (idxm[i] < 0) continue; /* negative row */
174: PetscCheck(idxm[i] < mat->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large");
175: PetscCheck(idxm[i] >= rstart && idxm[i] < rend, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only local values currently supported");
176: row = idxm[i] - rstart;
177: for (j = 0; j < n; j++) {
178: if (idxn[j] < 0) continue; /* negative column */
179: PetscCheck(idxn[j] < mat->cmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large");
180: PetscCall(MatGetValues(mdn->A, 1, &row, 1, &idxn[j], v + i * n + j));
181: }
182: }
183: PetscFunctionReturn(PETSC_SUCCESS);
184: }
186: static PetscErrorCode MatDenseGetLDA_MPIDense(Mat A, PetscInt *lda)
187: {
188: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
190: PetscFunctionBegin;
191: PetscCall(MatDenseGetLDA(a->A, lda));
192: PetscFunctionReturn(PETSC_SUCCESS);
193: }
195: static PetscErrorCode MatDenseSetLDA_MPIDense(Mat A, PetscInt lda)
196: {
197: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
198: MatType mtype = MATSEQDENSE;
200: PetscFunctionBegin;
201: if (!a->A) {
202: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
203: PetscCall(PetscLayoutSetUp(A->rmap));
204: PetscCall(PetscLayoutSetUp(A->cmap));
205: PetscCall(MatCreate(PETSC_COMM_SELF, &a->A));
206: PetscCall(MatSetSizes(a->A, A->rmap->n, A->cmap->N, A->rmap->n, A->cmap->N));
207: #if PetscDefined(HAVE_CUDA)
208: PetscBool iscuda;
209: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIDENSECUDA, &iscuda));
210: if (iscuda) mtype = MATSEQDENSECUDA;
211: #elif PetscDefined(HAVE_HIP)
212: PetscBool iship;
213: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIDENSEHIP, &iship));
214: if (iship) mtype = MATSEQDENSEHIP;
215: #endif
216: PetscCall(MatSetType(a->A, mtype));
217: }
218: PetscCall(MatDenseSetLDA(a->A, lda));
219: PetscFunctionReturn(PETSC_SUCCESS);
220: }
222: static PetscErrorCode MatDenseGetArray_MPIDense(Mat A, PetscScalar **array)
223: {
224: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
226: PetscFunctionBegin;
227: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
228: PetscCall(MatDenseGetArray(a->A, array));
229: PetscFunctionReturn(PETSC_SUCCESS);
230: }
232: static PetscErrorCode MatDenseGetArrayRead_MPIDense(Mat A, PetscScalar **array)
233: {
234: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
236: PetscFunctionBegin;
237: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
238: PetscCall(MatDenseGetArrayRead(a->A, (const PetscScalar **)array));
239: PetscFunctionReturn(PETSC_SUCCESS);
240: }
242: static PetscErrorCode MatDenseGetArrayWrite_MPIDense(Mat A, PetscScalar **array)
243: {
244: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
246: PetscFunctionBegin;
247: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
248: PetscCall(MatDenseGetArrayWrite(a->A, array));
249: PetscFunctionReturn(PETSC_SUCCESS);
250: }
252: static PetscErrorCode MatDensePlaceArray_MPIDense(Mat A, const PetscScalar *array)
253: {
254: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
256: PetscFunctionBegin;
257: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
258: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
259: PetscCall(MatDensePlaceArray(a->A, array));
260: PetscFunctionReturn(PETSC_SUCCESS);
261: }
263: static PetscErrorCode MatDenseResetArray_MPIDense(Mat A)
264: {
265: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
267: PetscFunctionBegin;
268: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
269: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
270: PetscCall(MatDenseResetArray(a->A));
271: PetscFunctionReturn(PETSC_SUCCESS);
272: }
274: static PetscErrorCode MatDenseReplaceArray_MPIDense(Mat A, const PetscScalar *array)
275: {
276: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
278: PetscFunctionBegin;
279: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
280: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
281: PetscCall(MatDenseReplaceArray(a->A, array));
282: PetscFunctionReturn(PETSC_SUCCESS);
283: }
285: static PetscErrorCode MatCreateSubMatrix_MPIDense(Mat A, IS isrow, IS iscol, MatReuse scall, Mat *B)
286: {
287: Mat_MPIDense *mat = (Mat_MPIDense *)A->data, *newmatd;
288: PetscInt lda, i, j, rstart, rend, nrows, ncols, Ncols, nlrows, nlcols;
289: const PetscInt *irow, *icol;
290: const PetscScalar *v;
291: PetscScalar *bv;
292: Mat newmat;
293: IS iscol_local;
294: MPI_Comm comm_is, comm_mat;
296: PetscFunctionBegin;
297: PetscCall(PetscObjectGetComm((PetscObject)A, &comm_mat));
298: PetscCall(PetscObjectGetComm((PetscObject)iscol, &comm_is));
299: PetscCheck(comm_mat == comm_is, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "IS communicator must match matrix communicator");
301: PetscCall(ISAllGather(iscol, &iscol_local));
302: PetscCall(ISGetIndices(isrow, &irow));
303: PetscCall(ISGetIndices(iscol_local, &icol));
304: PetscCall(ISGetLocalSize(isrow, &nrows));
305: PetscCall(ISGetLocalSize(iscol, &ncols));
306: PetscCall(ISGetSize(iscol, &Ncols)); /* global number of columns, size of iscol_local */
308: /* No parallel redistribution currently supported! Should really check each index set
309: to confirm that it is OK. ... Currently supports only submatrix same partitioning as
310: original matrix! */
312: PetscCall(MatGetLocalSize(A, &nlrows, &nlcols));
313: PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
315: /* Check submatrix call */
316: if (scall == MAT_REUSE_MATRIX) {
317: /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); */
318: /* Really need to test rows and column sizes! */
319: newmat = *B;
320: } else {
321: /* Create and fill new matrix */
322: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &newmat));
323: PetscCall(MatSetSizes(newmat, nrows, ncols, PETSC_DECIDE, Ncols));
324: PetscCall(MatSetType(newmat, ((PetscObject)A)->type_name));
325: PetscCall(MatMPIDenseSetPreallocation(newmat, NULL));
326: }
328: /* Now extract the data pointers and do the copy, column at a time */
329: newmatd = (Mat_MPIDense *)newmat->data;
330: PetscCall(MatDenseGetArray(newmatd->A, &bv));
331: PetscCall(MatDenseGetArrayRead(mat->A, &v));
332: PetscCall(MatDenseGetLDA(mat->A, &lda));
333: for (i = 0; i < Ncols; i++) {
334: const PetscScalar *av = v + lda * icol[i];
335: for (j = 0; j < nrows; j++) *bv++ = av[irow[j] - rstart];
336: }
337: PetscCall(MatDenseRestoreArrayRead(mat->A, &v));
338: PetscCall(MatDenseRestoreArray(newmatd->A, &bv));
340: /* Assemble the matrices so that the correct flags are set */
341: PetscCall(MatAssemblyBegin(newmat, MAT_FINAL_ASSEMBLY));
342: PetscCall(MatAssemblyEnd(newmat, MAT_FINAL_ASSEMBLY));
344: /* Free work space */
345: PetscCall(ISRestoreIndices(isrow, &irow));
346: PetscCall(ISRestoreIndices(iscol_local, &icol));
347: PetscCall(ISDestroy(&iscol_local));
348: *B = newmat;
349: PetscFunctionReturn(PETSC_SUCCESS);
350: }
352: static PetscErrorCode MatDenseRestoreArray_MPIDense(Mat A, PetscScalar **array)
353: {
354: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
356: PetscFunctionBegin;
357: PetscCall(MatDenseRestoreArray(a->A, array));
358: PetscFunctionReturn(PETSC_SUCCESS);
359: }
361: static PetscErrorCode MatDenseRestoreArrayRead_MPIDense(Mat A, PetscScalar **array)
362: {
363: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
365: PetscFunctionBegin;
366: PetscCall(MatDenseRestoreArrayRead(a->A, (const PetscScalar **)array));
367: PetscFunctionReturn(PETSC_SUCCESS);
368: }
370: static PetscErrorCode MatDenseRestoreArrayWrite_MPIDense(Mat A, PetscScalar **array)
371: {
372: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
374: PetscFunctionBegin;
375: PetscCall(MatDenseRestoreArrayWrite(a->A, array));
376: PetscFunctionReturn(PETSC_SUCCESS);
377: }
379: static PetscErrorCode MatAssemblyBegin_MPIDense(Mat mat, MatAssemblyType mode)
380: {
381: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
382: PetscInt nstash, reallocs;
384: PetscFunctionBegin;
385: if (mdn->donotstash || mat->nooffprocentries) PetscFunctionReturn(PETSC_SUCCESS);
387: PetscCall(MatStashScatterBegin_Private(mat, &mat->stash, mat->rmap->range));
388: PetscCall(MatStashGetInfo_Private(&mat->stash, &nstash, &reallocs));
389: PetscCall(PetscInfo(mdn->A, "Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
390: PetscFunctionReturn(PETSC_SUCCESS);
391: }
393: static PetscErrorCode MatAssemblyEnd_MPIDense(Mat mat, MatAssemblyType mode)
394: {
395: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
396: PetscInt i, *row, *col, flg, j, rstart, ncols;
397: PetscMPIInt n;
398: PetscScalar *val;
400: PetscFunctionBegin;
401: if (!mdn->donotstash && !mat->nooffprocentries) {
402: /* wait on receives */
403: while (1) {
404: PetscCall(MatStashScatterGetMesg_Private(&mat->stash, &n, &row, &col, &val, &flg));
405: if (!flg) break;
407: for (i = 0; i < n;) {
408: /* Now identify the consecutive vals belonging to the same row */
409: for (j = i, rstart = row[j]; j < n; j++) {
410: if (row[j] != rstart) break;
411: }
412: if (j < n) ncols = j - i;
413: else ncols = n - i;
414: /* Now assemble all these values with a single function call */
415: PetscCall(MatSetValues_MPIDense(mat, 1, row + i, ncols, col + i, val + i, mat->insertmode));
416: i = j;
417: }
418: }
419: PetscCall(MatStashScatterEnd_Private(&mat->stash));
420: }
422: PetscCall(MatAssemblyBegin(mdn->A, mode));
423: PetscCall(MatAssemblyEnd(mdn->A, mode));
424: PetscFunctionReturn(PETSC_SUCCESS);
425: }
427: static PetscErrorCode MatZeroEntries_MPIDense(Mat A)
428: {
429: Mat_MPIDense *l = (Mat_MPIDense *)A->data;
431: PetscFunctionBegin;
432: PetscCall(MatZeroEntries(l->A));
433: PetscFunctionReturn(PETSC_SUCCESS);
434: }
436: static PetscErrorCode MatZeroRows_MPIDense(Mat A, PetscInt n, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
437: {
438: Mat_MPIDense *l = (Mat_MPIDense *)A->data;
439: PetscInt i, len, *lrows;
441: PetscFunctionBegin;
442: /* get locally owned rows */
443: PetscCall(PetscLayoutMapLocal(A->rmap, n, rows, &len, &lrows, NULL));
444: /* fix right-hand side if needed */
445: if (x && b) {
446: const PetscScalar *xx;
447: PetscScalar *bb;
449: PetscCall(VecGetArrayRead(x, &xx));
450: PetscCall(VecGetArrayWrite(b, &bb));
451: for (i = 0; i < len; ++i) bb[lrows[i]] = diag * xx[lrows[i]];
452: PetscCall(VecRestoreArrayRead(x, &xx));
453: PetscCall(VecRestoreArrayWrite(b, &bb));
454: }
455: PetscCall(MatZeroRows(l->A, len, lrows, 0.0, NULL, NULL));
456: if (diag != 0.0) {
457: Vec d;
459: PetscCall(MatCreateVecs(A, NULL, &d));
460: PetscCall(VecSet(d, diag));
461: PetscCall(MatDiagonalSet(A, d, INSERT_VALUES));
462: PetscCall(VecDestroy(&d));
463: }
464: PetscCall(PetscFree(lrows));
465: PetscFunctionReturn(PETSC_SUCCESS);
466: }
468: PETSC_INTERN PetscErrorCode MatMult_SeqDense(Mat, Vec, Vec);
469: PETSC_INTERN PetscErrorCode MatMultAdd_SeqDense(Mat, Vec, Vec, Vec);
470: PETSC_INTERN PetscErrorCode MatMultTranspose_SeqDense(Mat, Vec, Vec);
471: PETSC_INTERN PetscErrorCode MatMultTransposeAdd_SeqDense(Mat, Vec, Vec, Vec);
473: static PetscErrorCode MatMultColumnRange_MPIDense(Mat mat, Vec xx, Vec yy, PetscInt c_start, PetscInt c_end)
474: {
475: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
476: const PetscScalar *ax;
477: PetscScalar *ay;
478: PetscMemType axmtype, aymtype;
480: PetscFunctionBegin;
481: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(mat));
482: PetscCall(VecGetArrayReadAndMemType(xx, &ax, &axmtype));
483: PetscCall(VecGetArrayWriteAndMemType(mdn->lvec, &ay, &aymtype));
484: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPI_REPLACE));
485: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ax, ay, MPI_REPLACE));
486: PetscCall(VecRestoreArrayWriteAndMemType(mdn->lvec, &ay));
487: PetscCall(VecRestoreArrayReadAndMemType(xx, &ax));
488: PetscUseMethod(mdn->A, "MatMultColumnRange_C", (Mat, Vec, Vec, PetscInt, PetscInt), (mdn->A, mdn->lvec, yy, c_start, c_end));
489: PetscFunctionReturn(PETSC_SUCCESS);
490: }
492: static PetscErrorCode MatMult_MPIDense(Mat mat, Vec xx, Vec yy)
493: {
494: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
495: const PetscScalar *ax;
496: PetscScalar *ay;
497: PetscMemType axmtype, aymtype;
499: PetscFunctionBegin;
500: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(mat));
501: PetscCall(VecGetArrayReadAndMemType(xx, &ax, &axmtype));
502: PetscCall(VecGetArrayWriteAndMemType(mdn->lvec, &ay, &aymtype));
503: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPI_REPLACE));
504: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ax, ay, MPI_REPLACE));
505: PetscCall(VecRestoreArrayWriteAndMemType(mdn->lvec, &ay));
506: PetscCall(VecRestoreArrayReadAndMemType(xx, &ax));
507: PetscUseTypeMethod(mdn->A, mult, mdn->lvec, yy);
508: PetscFunctionReturn(PETSC_SUCCESS);
509: }
511: static PetscErrorCode MatGetMultPetscSF_MPIDense(Mat A, PetscSF *sf)
512: {
513: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
515: PetscFunctionBegin;
516: *sf = mdn->Mvctx;
517: PetscFunctionReturn(PETSC_SUCCESS);
518: }
520: static PetscErrorCode MatMultAddColumnRange_MPIDense(Mat mat, Vec xx, Vec yy, Vec zz, PetscInt c_start, PetscInt c_end)
521: {
522: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
523: const PetscScalar *ax;
524: PetscScalar *ay;
525: PetscMemType axmtype, aymtype;
527: PetscFunctionBegin;
528: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(mat));
529: PetscCall(VecGetArrayReadAndMemType(xx, &ax, &axmtype));
530: PetscCall(VecGetArrayAndMemType(mdn->lvec, &ay, &aymtype));
531: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPI_REPLACE));
532: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ax, ay, MPI_REPLACE));
533: PetscCall(VecRestoreArrayAndMemType(mdn->lvec, &ay));
534: PetscCall(VecRestoreArrayReadAndMemType(xx, &ax));
535: PetscUseMethod(mdn->A, "MatMultAddColumnRange_C", (Mat, Vec, Vec, Vec, PetscInt, PetscInt), (mdn->A, mdn->lvec, yy, zz, c_start, c_end));
536: PetscFunctionReturn(PETSC_SUCCESS);
537: }
539: static PetscErrorCode MatMultAdd_MPIDense(Mat mat, Vec xx, Vec yy, Vec zz)
540: {
541: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
542: const PetscScalar *ax;
543: PetscScalar *ay;
544: PetscMemType axmtype, aymtype;
546: PetscFunctionBegin;
547: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(mat));
548: PetscCall(VecGetArrayReadAndMemType(xx, &ax, &axmtype));
549: PetscCall(VecGetArrayAndMemType(mdn->lvec, &ay, &aymtype));
550: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPI_REPLACE));
551: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ax, ay, MPI_REPLACE));
552: PetscCall(VecRestoreArrayAndMemType(mdn->lvec, &ay));
553: PetscCall(VecRestoreArrayReadAndMemType(xx, &ax));
554: PetscUseTypeMethod(mdn->A, multadd, mdn->lvec, yy, zz);
555: PetscFunctionReturn(PETSC_SUCCESS);
556: }
558: static PetscErrorCode MatMultHermitianTransposeColumnRange_MPIDense(Mat A, Vec xx, Vec yy, PetscInt c_start, PetscInt c_end)
559: {
560: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
561: const PetscScalar *ax;
562: PetscScalar *ay;
563: PetscMemType axmtype, aymtype;
564: PetscInt r_start, r_end;
565: PetscInt c_start_local, c_end_local;
567: PetscFunctionBegin;
568: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
569: PetscCall(VecZeroEntries(a->lvec));
570: PetscCall(VecGetOwnershipRange(yy, &r_start, &r_end));
571: c_start_local = PetscMax(c_start, r_start);
572: c_end_local = PetscMin(c_end, r_end);
573: PetscCall(VecGetArrayAndMemType(yy, &ay, &aymtype));
574: if (c_end_local > c_start_local) {
575: if (PetscMemTypeHost(aymtype)) {
576: PetscCall(PetscArrayzero(&ay[c_start_local], (size_t)(c_end_local - c_start_local)));
577: } else {
578: PetscCall(PetscDeviceRegisterMemory(ay, aymtype, sizeof(*ay) * ((size_t)(r_end - r_start))));
579: PetscCall(PetscDeviceArrayZero(NULL, &ay[c_start_local], (size_t)(c_end_local - c_start_local)));
580: }
581: }
582: PetscUseMethod(a->A, "MatMultHermitianTransposeColumnRange_C", (Mat, Vec, Vec, PetscInt, PetscInt), (a->A, xx, a->lvec, c_start, c_end));
583: PetscCall(VecGetArrayReadAndMemType(a->lvec, &ax, &axmtype));
584: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPIU_SUM));
585: PetscCall(PetscSFReduceEnd(a->Mvctx, MPIU_SCALAR, ax, ay, MPIU_SUM));
586: PetscCall(VecRestoreArrayReadAndMemType(a->lvec, &ax));
587: PetscCall(VecRestoreArrayAndMemType(yy, &ay));
588: PetscFunctionReturn(PETSC_SUCCESS);
589: }
591: static PetscErrorCode MatMultTransposeKernel_MPIDense(Mat A, Vec xx, Vec yy, PetscBool herm)
592: {
593: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
594: const PetscScalar *ax;
595: PetscScalar *ay;
596: PetscMemType axmtype, aymtype;
598: PetscFunctionBegin;
599: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
600: PetscCall(VecSet(yy, 0.0));
601: if (herm) PetscUseTypeMethod(a->A, multhermitiantranspose, xx, a->lvec);
602: else PetscUseTypeMethod(a->A, multtranspose, xx, a->lvec);
603: PetscCall(VecGetArrayReadAndMemType(a->lvec, &ax, &axmtype));
604: PetscCall(VecGetArrayAndMemType(yy, &ay, &aymtype));
605: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPIU_SUM));
606: PetscCall(PetscSFReduceEnd(a->Mvctx, MPIU_SCALAR, ax, ay, MPIU_SUM));
607: PetscCall(VecRestoreArrayReadAndMemType(a->lvec, &ax));
608: PetscCall(VecRestoreArrayAndMemType(yy, &ay));
609: PetscFunctionReturn(PETSC_SUCCESS);
610: }
612: static PetscErrorCode MatMultHermitianTransposeAddColumnRange_MPIDense(Mat A, Vec xx, Vec yy, Vec zz, PetscInt c_start, PetscInt c_end)
613: {
614: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
615: const PetscScalar *ax;
616: PetscScalar *ay;
617: PetscMemType axmtype, aymtype;
619: PetscFunctionBegin;
620: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
621: PetscCall(VecCopy(yy, zz));
622: PetscCall(VecZeroEntries(a->lvec));
623: PetscUseMethod(a->A, "MatMultHermitianTransposeColumnRange_C", (Mat, Vec, Vec, PetscInt, PetscInt), (a->A, xx, a->lvec, c_start, c_end));
624: PetscCall(VecGetArrayReadAndMemType(a->lvec, &ax, &axmtype));
625: PetscCall(VecGetArrayAndMemType(zz, &ay, &aymtype));
626: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPIU_SUM));
627: PetscCall(PetscSFReduceEnd(a->Mvctx, MPIU_SCALAR, ax, ay, MPIU_SUM));
628: PetscCall(VecRestoreArrayReadAndMemType(a->lvec, &ax));
629: PetscCall(VecRestoreArrayAndMemType(zz, &ay));
630: PetscFunctionReturn(PETSC_SUCCESS);
631: }
633: static PetscErrorCode MatMultTransposeAddKernel_MPIDense(Mat A, Vec xx, Vec yy, Vec zz, PetscBool herm)
634: {
635: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
636: const PetscScalar *ax;
637: PetscScalar *ay;
638: PetscMemType axmtype, aymtype;
640: PetscFunctionBegin;
641: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
642: PetscCall(VecCopy(yy, zz));
643: if (herm) PetscUseTypeMethod(a->A, multhermitiantranspose, xx, a->lvec);
644: else PetscUseTypeMethod(a->A, multtranspose, xx, a->lvec);
645: PetscCall(VecGetArrayReadAndMemType(a->lvec, &ax, &axmtype));
646: PetscCall(VecGetArrayAndMemType(zz, &ay, &aymtype));
647: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPIU_SUM));
648: PetscCall(PetscSFReduceEnd(a->Mvctx, MPIU_SCALAR, ax, ay, MPIU_SUM));
649: PetscCall(VecRestoreArrayReadAndMemType(a->lvec, &ax));
650: PetscCall(VecRestoreArrayAndMemType(zz, &ay));
651: PetscFunctionReturn(PETSC_SUCCESS);
652: }
654: static PetscErrorCode MatMultTranspose_MPIDense(Mat A, Vec xx, Vec yy)
655: {
656: PetscFunctionBegin;
657: PetscCall(MatMultTransposeKernel_MPIDense(A, xx, yy, PETSC_FALSE));
658: PetscFunctionReturn(PETSC_SUCCESS);
659: }
661: static PetscErrorCode MatMultTransposeAdd_MPIDense(Mat A, Vec xx, Vec yy, Vec zz)
662: {
663: PetscFunctionBegin;
664: PetscCall(MatMultTransposeAddKernel_MPIDense(A, xx, yy, zz, PETSC_FALSE));
665: PetscFunctionReturn(PETSC_SUCCESS);
666: }
668: static PetscErrorCode MatMultHermitianTranspose_MPIDense(Mat A, Vec xx, Vec yy)
669: {
670: PetscFunctionBegin;
671: PetscCall(MatMultTransposeKernel_MPIDense(A, xx, yy, PETSC_TRUE));
672: PetscFunctionReturn(PETSC_SUCCESS);
673: }
675: static PetscErrorCode MatMultHermitianTransposeAdd_MPIDense(Mat A, Vec xx, Vec yy, Vec zz)
676: {
677: PetscFunctionBegin;
678: PetscCall(MatMultTransposeAddKernel_MPIDense(A, xx, yy, zz, PETSC_TRUE));
679: PetscFunctionReturn(PETSC_SUCCESS);
680: }
682: PetscErrorCode MatGetDiagonal_MPIDense(Mat A, Vec v)
683: {
684: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
685: PetscInt lda, len, i, nl, ng, m = A->rmap->n, radd;
686: PetscScalar *x;
687: const PetscScalar *av;
689: PetscFunctionBegin;
690: PetscCall(VecGetArray(v, &x));
691: PetscCall(VecGetSize(v, &ng));
692: PetscCheck(ng == A->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming mat and vec");
693: PetscCall(VecGetLocalSize(v, &nl));
694: len = PetscMin(a->A->rmap->n, a->A->cmap->n);
695: radd = A->rmap->rstart * m;
696: PetscCall(MatDenseGetArrayRead(a->A, &av));
697: PetscCall(MatDenseGetLDA(a->A, &lda));
698: for (i = 0; i < len; i++) x[i] = av[radd + i * lda + i];
699: PetscCall(MatDenseRestoreArrayRead(a->A, &av));
700: if (nl - i > 0) PetscCall(PetscArrayzero(x + i, nl - i));
701: PetscCall(VecRestoreArray(v, &x));
702: PetscFunctionReturn(PETSC_SUCCESS);
703: }
705: static PetscErrorCode MatDestroy_MPIDense(Mat mat)
706: {
707: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
709: PetscFunctionBegin;
710: PetscCall(PetscLogObjectState((PetscObject)mat, "Rows=%" PetscInt_FMT ", Cols=%" PetscInt_FMT, mat->rmap->N, mat->cmap->N));
711: PetscCall(MatStashDestroy_Private(&mat->stash));
712: PetscCheck(!mdn->vecinuse, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
713: PetscCheck(!mdn->matinuse, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
714: PetscCall(MatDestroy(&mdn->A));
715: PetscCall(VecDestroy(&mdn->lvec));
716: PetscCall(PetscSFDestroy(&mdn->Mvctx));
717: PetscCall(VecDestroy(&mdn->cvec));
718: PetscCall(MatDestroy(&mdn->cmat));
720: PetscCall(PetscFree(mat->data));
721: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, NULL));
723: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetLDA_C", NULL));
724: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseSetLDA_C", NULL));
725: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArray_C", NULL));
726: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArray_C", NULL));
727: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArrayRead_C", NULL));
728: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArrayRead_C", NULL));
729: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArrayWrite_C", NULL));
730: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArrayWrite_C", NULL));
731: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDensePlaceArray_C", NULL));
732: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseResetArray_C", NULL));
733: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseReplaceArray_C", NULL));
734: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpiaij_mpidense_C", NULL));
735: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpiaij_C", NULL));
736: #if defined(PETSC_HAVE_ELEMENTAL)
737: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_elemental_C", NULL));
738: #endif
739: #if defined(PETSC_HAVE_SCALAPACK) && (defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL_DOUBLE))
740: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_scalapack_C", NULL));
741: #endif
742: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPIDenseSetPreallocation_C", NULL));
743: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaij_mpidense_C", NULL));
744: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaij_C", NULL));
745: #if defined(PETSC_HAVE_CUDA)
746: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijcusparse_mpidense_C", NULL));
747: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaijcusparse_C", NULL));
748: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpidensecuda_C", NULL));
749: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidensecuda_mpidense_C", NULL));
750: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaij_mpidensecuda_C", NULL));
751: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijcusparse_mpidensecuda_C", NULL));
752: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidensecuda_mpiaij_C", NULL));
753: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidensecuda_mpiaijcusparse_C", NULL));
754: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAGetArray_C", NULL));
755: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAGetArrayRead_C", NULL));
756: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAGetArrayWrite_C", NULL));
757: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDARestoreArray_C", NULL));
758: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDARestoreArrayRead_C", NULL));
759: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDARestoreArrayWrite_C", NULL));
760: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAPlaceArray_C", NULL));
761: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAResetArray_C", NULL));
762: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAReplaceArray_C", NULL));
763: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDASetPreallocation_C", NULL));
764: #endif
765: #if defined(PETSC_HAVE_HIP)
766: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijhipsparse_mpidense_C", NULL));
767: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaijhipsparse_C", NULL));
768: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpidensehip_C", NULL));
769: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidensehip_mpidense_C", NULL));
770: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaij_mpidensehip_C", NULL));
771: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijhipsparse_mpidensehip_C", NULL));
772: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidensehip_mpiaij_C", NULL));
773: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidensehip_mpiaijhipsparse_C", NULL));
774: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPGetArray_C", NULL));
775: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPGetArrayRead_C", NULL));
776: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPGetArrayWrite_C", NULL));
777: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPRestoreArray_C", NULL));
778: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPRestoreArrayRead_C", NULL));
779: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPRestoreArrayWrite_C", NULL));
780: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPPlaceArray_C", NULL));
781: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPResetArray_C", NULL));
782: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPReplaceArray_C", NULL));
783: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPSetPreallocation_C", NULL));
784: #endif
785: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumn_C", NULL));
786: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumn_C", NULL));
787: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVec_C", NULL));
788: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVec_C", NULL));
789: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVecRead_C", NULL));
790: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVecRead_C", NULL));
791: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVecWrite_C", NULL));
792: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVecWrite_C", NULL));
793: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetSubMatrix_C", NULL));
794: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreSubMatrix_C", NULL));
795: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultColumnRange_C", NULL));
796: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultAddColumnRange_C", NULL));
797: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultHermitianTransposeColumnRange_C", NULL));
798: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultHermitianTransposeAddColumnRange_C", NULL));
799: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatGetMultPetscSF_C", NULL));
801: PetscCall(PetscObjectCompose((PetscObject)mat, "DiagonalBlock", NULL));
802: PetscFunctionReturn(PETSC_SUCCESS);
803: }
805: #include <petscdraw.h>
806: static PetscErrorCode MatView_MPIDense_ASCIIorDraworSocket(Mat mat, PetscViewer viewer)
807: {
808: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
809: PetscMPIInt rank;
810: PetscViewerType vtype;
811: PetscBool isascii, isdraw;
812: PetscViewer sviewer;
813: PetscViewerFormat format;
815: PetscFunctionBegin;
816: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)mat), &rank));
817: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
818: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
819: if (isascii) {
820: PetscCall(PetscViewerGetType(viewer, &vtype));
821: PetscCall(PetscViewerGetFormat(viewer, &format));
822: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
823: MatInfo info;
824: PetscCall(MatGetInfo(mat, MAT_LOCAL, &info));
825: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
826: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " [%d] local rows %" PetscInt_FMT " nz %" PetscInt_FMT " nz alloced %" PetscInt_FMT " mem %" PetscInt_FMT " \n", rank, mat->rmap->n, (PetscInt)info.nz_used, (PetscInt)info.nz_allocated,
827: (PetscInt)info.memory));
828: PetscCall(PetscViewerFlush(viewer));
829: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
830: if (mdn->Mvctx) PetscCall(PetscSFView(mdn->Mvctx, viewer));
831: PetscFunctionReturn(PETSC_SUCCESS);
832: } else if (format == PETSC_VIEWER_ASCII_INFO) {
833: PetscFunctionReturn(PETSC_SUCCESS);
834: }
835: } else if (isdraw) {
836: PetscDraw draw;
837: PetscBool isnull;
839: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
840: PetscCall(PetscDrawIsNull(draw, &isnull));
841: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
842: }
844: {
845: /* assemble the entire matrix onto first processor. */
846: Mat A;
847: PetscInt M = mat->rmap->N, N = mat->cmap->N, m, row, i, nz;
848: PetscInt *cols;
849: PetscScalar *vals;
851: PetscCall(MatCreate(PetscObjectComm((PetscObject)mat), &A));
852: if (rank == 0) {
853: PetscCall(MatSetSizes(A, M, N, M, N));
854: } else {
855: PetscCall(MatSetSizes(A, 0, 0, M, N));
856: }
857: /* Since this is a temporary matrix, MATMPIDENSE instead of ((PetscObject)A)->type_name here is probably acceptable. */
858: PetscCall(MatSetType(A, MATMPIDENSE));
859: PetscCall(MatMPIDenseSetPreallocation(A, NULL));
861: /* Copy the matrix ... This isn't the most efficient means,
862: but it's quick for now */
863: A->insertmode = INSERT_VALUES;
865: row = mat->rmap->rstart;
866: m = mdn->A->rmap->n;
867: for (i = 0; i < m; i++) {
868: PetscCall(MatGetRow_MPIDense(mat, row, &nz, &cols, &vals));
869: PetscCall(MatSetValues_MPIDense(A, 1, &row, nz, cols, vals, INSERT_VALUES));
870: PetscCall(MatRestoreRow_MPIDense(mat, row, &nz, &cols, &vals));
871: row++;
872: }
874: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
875: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
876: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
877: if (rank == 0) {
878: PetscCall(PetscObjectSetName((PetscObject)((Mat_MPIDense *)A->data)->A, ((PetscObject)mat)->name));
879: PetscCall(MatView_SeqDense(((Mat_MPIDense *)A->data)->A, sviewer));
880: }
881: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
882: PetscCall(MatDestroy(&A));
883: }
884: PetscFunctionReturn(PETSC_SUCCESS);
885: }
887: static PetscErrorCode MatView_MPIDense(Mat mat, PetscViewer viewer)
888: {
889: PetscBool isascii, isbinary, isdraw, issocket;
891: PetscFunctionBegin;
892: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
893: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
894: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSOCKET, &issocket));
895: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
897: if (isascii || issocket || isdraw) PetscCall(MatView_MPIDense_ASCIIorDraworSocket(mat, viewer));
898: else if (isbinary) PetscCall(MatView_Dense_Binary(mat, viewer));
899: PetscFunctionReturn(PETSC_SUCCESS);
900: }
902: static PetscErrorCode MatGetInfo_MPIDense(Mat A, MatInfoType flag, MatInfo *info)
903: {
904: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
905: Mat mdn = mat->A;
906: PetscLogDouble isend[5], irecv[5];
908: PetscFunctionBegin;
909: info->block_size = 1.0;
911: PetscCall(MatGetInfo(mdn, MAT_LOCAL, info));
913: isend[0] = info->nz_used;
914: isend[1] = info->nz_allocated;
915: isend[2] = info->nz_unneeded;
916: isend[3] = info->memory;
917: isend[4] = info->mallocs;
918: if (flag == MAT_LOCAL) {
919: info->nz_used = isend[0];
920: info->nz_allocated = isend[1];
921: info->nz_unneeded = isend[2];
922: info->memory = isend[3];
923: info->mallocs = isend[4];
924: } else if (flag == MAT_GLOBAL_MAX) {
925: PetscCallMPI(MPIU_Allreduce(isend, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_MAX, PetscObjectComm((PetscObject)A)));
927: info->nz_used = irecv[0];
928: info->nz_allocated = irecv[1];
929: info->nz_unneeded = irecv[2];
930: info->memory = irecv[3];
931: info->mallocs = irecv[4];
932: } else if (flag == MAT_GLOBAL_SUM) {
933: PetscCallMPI(MPIU_Allreduce(isend, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_SUM, PetscObjectComm((PetscObject)A)));
935: info->nz_used = irecv[0];
936: info->nz_allocated = irecv[1];
937: info->nz_unneeded = irecv[2];
938: info->memory = irecv[3];
939: info->mallocs = irecv[4];
940: }
941: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
942: info->fill_ratio_needed = 0;
943: info->factor_mallocs = 0;
944: PetscFunctionReturn(PETSC_SUCCESS);
945: }
947: static PetscErrorCode MatSetOption_MPIDense(Mat A, MatOption op, PetscBool flg)
948: {
949: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
951: PetscFunctionBegin;
952: switch (op) {
953: case MAT_NEW_NONZERO_LOCATIONS:
954: case MAT_NEW_NONZERO_LOCATION_ERR:
955: case MAT_NEW_NONZERO_ALLOCATION_ERR:
956: MatCheckPreallocated(A, 1);
957: PetscCall(MatSetOption(a->A, op, flg));
958: break;
959: case MAT_ROW_ORIENTED:
960: MatCheckPreallocated(A, 1);
961: a->roworiented = flg;
962: PetscCall(MatSetOption(a->A, op, flg));
963: break;
964: case MAT_IGNORE_OFF_PROC_ENTRIES:
965: a->donotstash = flg;
966: break;
967: case MAT_SYMMETRIC:
968: case MAT_STRUCTURALLY_SYMMETRIC:
969: case MAT_HERMITIAN:
970: case MAT_SYMMETRY_ETERNAL:
971: case MAT_STRUCTURAL_SYMMETRY_ETERNAL:
972: case MAT_SPD:
973: case MAT_SPD_ETERNAL:
974: /* if the diagonal matrix is square it inherits some of the properties above */
975: if (a->A && A->rmap->n == A->cmap->n) PetscCall(MatSetOption(a->A, op, flg));
976: break;
977: default:
978: break;
979: }
980: PetscFunctionReturn(PETSC_SUCCESS);
981: }
983: static PetscErrorCode MatDiagonalScale_MPIDense(Mat A, Vec ll, Vec rr)
984: {
985: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
986: const PetscScalar *l;
987: PetscScalar x, *v, *vv, *r;
988: PetscInt i, j, s2a, s3a, s2, s3, m = mdn->A->rmap->n, n = mdn->A->cmap->n, lda;
990: PetscFunctionBegin;
991: PetscCall(MatDenseGetArray(mdn->A, &vv));
992: PetscCall(MatDenseGetLDA(mdn->A, &lda));
993: PetscCall(MatGetLocalSize(A, &s2, &s3));
994: if (ll) {
995: PetscCall(VecGetLocalSize(ll, &s2a));
996: PetscCheck(s2a == s2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left scaling vector non-conforming local size, %" PetscInt_FMT " != %" PetscInt_FMT, s2a, s2);
997: PetscCall(VecGetArrayRead(ll, &l));
998: for (i = 0; i < m; i++) {
999: x = l[i];
1000: v = vv + i;
1001: for (j = 0; j < n; j++) {
1002: (*v) *= x;
1003: v += lda;
1004: }
1005: }
1006: PetscCall(VecRestoreArrayRead(ll, &l));
1007: PetscCall(PetscLogFlops(1.0 * n * m));
1008: }
1009: if (rr) {
1010: const PetscScalar *ar;
1012: PetscCall(VecGetLocalSize(rr, &s3a));
1013: PetscCheck(s3a == s3, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Right scaling vec non-conforming local size, %" PetscInt_FMT " != %" PetscInt_FMT ".", s3a, s3);
1014: PetscCall(VecGetArrayRead(rr, &ar));
1015: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
1016: PetscCall(VecGetArray(mdn->lvec, &r));
1017: PetscCall(PetscSFBcastBegin(mdn->Mvctx, MPIU_SCALAR, ar, r, MPI_REPLACE));
1018: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ar, r, MPI_REPLACE));
1019: PetscCall(VecRestoreArrayRead(rr, &ar));
1020: for (i = 0; i < n; i++) {
1021: x = r[i];
1022: v = vv + i * lda;
1023: for (j = 0; j < m; j++) (*v++) *= x;
1024: }
1025: PetscCall(VecRestoreArray(mdn->lvec, &r));
1026: PetscCall(PetscLogFlops(1.0 * n * m));
1027: }
1028: PetscCall(MatDenseRestoreArray(mdn->A, &vv));
1029: PetscFunctionReturn(PETSC_SUCCESS);
1030: }
1032: static PetscErrorCode MatNorm_MPIDense(Mat A, NormType type, PetscReal *nrm)
1033: {
1034: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
1035: PetscInt i, j;
1036: PetscMPIInt size;
1037: PetscReal sum = 0.0;
1038: const PetscScalar *av, *v;
1040: PetscFunctionBegin;
1041: PetscCall(MatDenseGetArrayRead(mdn->A, &av));
1042: v = av;
1043: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
1044: if (size == 1) {
1045: PetscCall(MatNorm(mdn->A, type, nrm));
1046: } else {
1047: if (type == NORM_FROBENIUS) {
1048: for (i = 0; i < mdn->A->cmap->n * mdn->A->rmap->n; i++) {
1049: sum += PetscRealPart(PetscConj(*v) * (*v));
1050: v++;
1051: }
1052: PetscCallMPI(MPIU_Allreduce(&sum, nrm, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)A)));
1053: *nrm = PetscSqrtReal(*nrm);
1054: PetscCall(PetscLogFlops(2.0 * mdn->A->cmap->n * mdn->A->rmap->n));
1055: } else if (type == NORM_1) {
1056: PetscReal *tmp;
1058: PetscCall(PetscCalloc1(A->cmap->N, &tmp));
1059: *nrm = 0.0;
1060: v = av;
1061: for (j = 0; j < mdn->A->cmap->n; j++) {
1062: for (i = 0; i < mdn->A->rmap->n; i++) {
1063: tmp[j] += PetscAbsScalar(*v);
1064: v++;
1065: }
1066: }
1067: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, tmp, A->cmap->N, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)A)));
1068: for (j = 0; j < A->cmap->N; j++) {
1069: if (tmp[j] > *nrm) *nrm = tmp[j];
1070: }
1071: PetscCall(PetscFree(tmp));
1072: PetscCall(PetscLogFlops(A->cmap->n * A->rmap->n));
1073: } else if (type == NORM_INFINITY) { /* max row norm */
1074: PetscCall(MatNorm(mdn->A, type, nrm));
1075: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, nrm, 1, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)A)));
1076: } else SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Unsupported norm type %s", NormTypes[type]);
1077: }
1078: PetscCall(MatDenseRestoreArrayRead(mdn->A, &av));
1079: PetscFunctionReturn(PETSC_SUCCESS);
1080: }
1082: static PetscErrorCode MatTranspose_MPIDense(Mat A, MatReuse reuse, Mat *matout)
1083: {
1084: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1085: Mat B;
1086: PetscInt M = A->rmap->N, N = A->cmap->N, m, n, *rwork, rstart = A->rmap->rstart;
1087: PetscInt j, i, lda;
1088: PetscScalar *v;
1090: PetscFunctionBegin;
1091: if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *matout));
1092: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
1093: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
1094: PetscCall(MatSetSizes(B, A->cmap->n, A->rmap->n, N, M));
1095: PetscCall(MatSetType(B, ((PetscObject)A)->type_name));
1096: PetscCall(MatMPIDenseSetPreallocation(B, NULL));
1097: } else B = *matout;
1099: m = a->A->rmap->n;
1100: n = a->A->cmap->n;
1101: PetscCall(MatDenseGetArrayRead(a->A, (const PetscScalar **)&v));
1102: PetscCall(MatDenseGetLDA(a->A, &lda));
1103: PetscCall(PetscMalloc1(m, &rwork));
1104: for (i = 0; i < m; i++) rwork[i] = rstart + i;
1105: for (j = 0; j < n; j++) {
1106: PetscCall(MatSetValues(B, 1, &j, m, rwork, v, INSERT_VALUES));
1107: v = PetscSafePointerPlusOffset(v, lda);
1108: }
1109: PetscCall(MatDenseRestoreArrayRead(a->A, (const PetscScalar **)&v));
1110: PetscCall(PetscFree(rwork));
1111: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
1112: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
1113: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
1114: *matout = B;
1115: } else {
1116: PetscCall(MatHeaderMerge(A, &B));
1117: }
1118: PetscFunctionReturn(PETSC_SUCCESS);
1119: }
1121: static PetscErrorCode MatDuplicate_MPIDense(Mat, MatDuplicateOption, Mat *);
1122: PETSC_INTERN PetscErrorCode MatScale_MPIDense(Mat, PetscScalar);
1124: static PetscErrorCode MatSetUp_MPIDense(Mat A)
1125: {
1126: PetscFunctionBegin;
1127: PetscCall(PetscLayoutSetUp(A->rmap));
1128: PetscCall(PetscLayoutSetUp(A->cmap));
1129: if (!A->preallocated) PetscCall(MatMPIDenseSetPreallocation(A, NULL));
1130: PetscFunctionReturn(PETSC_SUCCESS);
1131: }
1133: static PetscErrorCode MatAXPY_MPIDense(Mat Y, PetscScalar alpha, Mat X, MatStructure str)
1134: {
1135: Mat_MPIDense *A = (Mat_MPIDense *)Y->data, *B = (Mat_MPIDense *)X->data;
1137: PetscFunctionBegin;
1138: PetscCall(MatAXPY(A->A, alpha, B->A, str));
1139: PetscFunctionReturn(PETSC_SUCCESS);
1140: }
1142: static PetscErrorCode MatConjugate_MPIDense(Mat mat)
1143: {
1144: Mat_MPIDense *a = (Mat_MPIDense *)mat->data;
1146: PetscFunctionBegin;
1147: PetscCall(MatConjugate(a->A));
1148: PetscFunctionReturn(PETSC_SUCCESS);
1149: }
1151: static PetscErrorCode MatRealPart_MPIDense(Mat A)
1152: {
1153: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1155: PetscFunctionBegin;
1156: PetscCall(MatRealPart(a->A));
1157: PetscFunctionReturn(PETSC_SUCCESS);
1158: }
1160: static PetscErrorCode MatImaginaryPart_MPIDense(Mat A)
1161: {
1162: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1164: PetscFunctionBegin;
1165: PetscCall(MatImaginaryPart(a->A));
1166: PetscFunctionReturn(PETSC_SUCCESS);
1167: }
1169: static PetscErrorCode MatGetColumnVector_MPIDense(Mat A, Vec v, PetscInt col)
1170: {
1171: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1173: PetscFunctionBegin;
1174: PetscCheck(a->A, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Missing local matrix");
1175: PetscCheck(a->A->ops->getcolumnvector, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Missing get column operation");
1176: PetscUseTypeMethod(a->A, getcolumnvector, v, col);
1177: PetscFunctionReturn(PETSC_SUCCESS);
1178: }
1180: PETSC_INTERN PetscErrorCode MatGetColumnReductions_SeqDense(Mat, PetscInt, PetscReal *);
1182: static PetscErrorCode MatGetColumnReductions_MPIDense(Mat A, PetscInt type, PetscReal *reductions)
1183: {
1184: PetscInt i, m, n;
1185: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1187: PetscFunctionBegin;
1188: PetscCall(MatGetSize(A, &m, &n));
1189: if (type == REDUCTION_MEAN_REALPART) {
1190: PetscCall(MatGetColumnReductions_SeqDense(a->A, (PetscInt)REDUCTION_SUM_REALPART, reductions));
1191: } else if (type == REDUCTION_MEAN_IMAGINARYPART) {
1192: PetscCall(MatGetColumnReductions_SeqDense(a->A, (PetscInt)REDUCTION_SUM_IMAGINARYPART, reductions));
1193: } else {
1194: PetscCall(MatGetColumnReductions_SeqDense(a->A, type, reductions));
1195: }
1196: if (type == NORM_2) {
1197: for (i = 0; i < n; i++) reductions[i] *= reductions[i];
1198: }
1199: if (type == NORM_INFINITY) {
1200: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, reductions, n, MPIU_REAL, MPIU_MAX, A->hdr.comm));
1201: } else {
1202: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, reductions, n, MPIU_REAL, MPIU_SUM, A->hdr.comm));
1203: }
1204: if (type == NORM_2) {
1205: for (i = 0; i < n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
1206: } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
1207: for (i = 0; i < n; i++) reductions[i] /= m;
1208: }
1209: PetscFunctionReturn(PETSC_SUCCESS);
1210: }
1212: static PetscErrorCode MatSetRandom_MPIDense(Mat x, PetscRandom rctx)
1213: {
1214: Mat_MPIDense *d = (Mat_MPIDense *)x->data;
1216: PetscFunctionBegin;
1217: PetscCall(MatSetRandom(d->A, rctx));
1218: #if defined(PETSC_HAVE_DEVICE)
1219: x->offloadmask = d->A->offloadmask;
1220: #endif
1221: PetscFunctionReturn(PETSC_SUCCESS);
1222: }
1224: static PetscErrorCode MatMatTransposeMultSymbolic_MPIDense_MPIDense(Mat, Mat, PetscReal, Mat);
1225: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense(Mat, Mat, Mat);
1226: static PetscErrorCode MatTransposeMatMultSymbolic_MPIDense_MPIDense(Mat, Mat, PetscReal, Mat);
1227: static PetscErrorCode MatTransposeMatMultNumeric_MPIDense_MPIDense(Mat, Mat, Mat);
1228: static PetscErrorCode MatEqual_MPIDense(Mat, Mat, PetscBool *);
1229: static PetscErrorCode MatLoad_MPIDense(Mat, PetscViewer);
1230: static PetscErrorCode MatProductSetFromOptions_MPIDense(Mat);
1232: static struct _MatOps MatOps_Values = {MatSetValues_MPIDense,
1233: MatGetRow_MPIDense,
1234: MatRestoreRow_MPIDense,
1235: MatMult_MPIDense,
1236: /* 4*/ MatMultAdd_MPIDense,
1237: MatMultTranspose_MPIDense,
1238: MatMultTransposeAdd_MPIDense,
1239: NULL,
1240: NULL,
1241: NULL,
1242: /* 10*/ NULL,
1243: NULL,
1244: NULL,
1245: NULL,
1246: MatTranspose_MPIDense,
1247: /* 15*/ MatGetInfo_MPIDense,
1248: MatEqual_MPIDense,
1249: MatGetDiagonal_MPIDense,
1250: MatDiagonalScale_MPIDense,
1251: MatNorm_MPIDense,
1252: /* 20*/ MatAssemblyBegin_MPIDense,
1253: MatAssemblyEnd_MPIDense,
1254: MatSetOption_MPIDense,
1255: MatZeroEntries_MPIDense,
1256: /* 24*/ MatZeroRows_MPIDense,
1257: NULL,
1258: NULL,
1259: NULL,
1260: NULL,
1261: /* 29*/ MatSetUp_MPIDense,
1262: NULL,
1263: NULL,
1264: MatGetDiagonalBlock_MPIDense,
1265: NULL,
1266: /* 34*/ MatDuplicate_MPIDense,
1267: NULL,
1268: NULL,
1269: NULL,
1270: NULL,
1271: /* 39*/ MatAXPY_MPIDense,
1272: MatCreateSubMatrices_MPIDense,
1273: NULL,
1274: MatGetValues_MPIDense,
1275: MatCopy_MPIDense,
1276: /* 44*/ NULL,
1277: MatScale_MPIDense,
1278: MatShift_MPIDense,
1279: NULL,
1280: NULL,
1281: /* 49*/ MatSetRandom_MPIDense,
1282: NULL,
1283: NULL,
1284: NULL,
1285: NULL,
1286: /* 54*/ NULL,
1287: NULL,
1288: NULL,
1289: NULL,
1290: NULL,
1291: /* 59*/ MatCreateSubMatrix_MPIDense,
1292: MatDestroy_MPIDense,
1293: MatView_MPIDense,
1294: NULL,
1295: NULL,
1296: /* 64*/ NULL,
1297: NULL,
1298: NULL,
1299: NULL,
1300: NULL,
1301: /* 69*/ NULL,
1302: NULL,
1303: NULL,
1304: NULL,
1305: NULL,
1306: /* 74*/ NULL,
1307: NULL,
1308: NULL,
1309: NULL,
1310: MatLoad_MPIDense,
1311: /* 79*/ NULL,
1312: NULL,
1313: NULL,
1314: NULL,
1315: /* 83*/ NULL,
1316: NULL,
1317: NULL,
1318: NULL,
1319: MatMatTransposeMultSymbolic_MPIDense_MPIDense,
1320: MatMatTransposeMultNumeric_MPIDense_MPIDense,
1321: /* 89*/ NULL,
1322: MatProductSetFromOptions_MPIDense,
1323: NULL,
1324: NULL,
1325: MatConjugate_MPIDense,
1326: /* 94*/ NULL,
1327: NULL,
1328: MatRealPart_MPIDense,
1329: MatImaginaryPart_MPIDense,
1330: NULL,
1331: /*99*/ NULL,
1332: NULL,
1333: NULL,
1334: NULL,
1335: MatGetColumnVector_MPIDense,
1336: /*104*/ NULL,
1337: NULL,
1338: NULL,
1339: NULL,
1340: NULL,
1341: /*109*/ NULL,
1342: NULL,
1343: MatMultHermitianTranspose_MPIDense,
1344: MatMultHermitianTransposeAdd_MPIDense,
1345: NULL,
1346: /*114*/ NULL,
1347: MatGetColumnReductions_MPIDense,
1348: NULL,
1349: NULL,
1350: NULL,
1351: /*120*/ MatTransposeMatMultSymbolic_MPIDense_MPIDense,
1352: MatTransposeMatMultNumeric_MPIDense_MPIDense,
1353: NULL,
1354: NULL,
1355: /*124*/ NULL,
1356: NULL,
1357: NULL,
1358: NULL,
1359: NULL,
1360: /*129*/ NULL,
1361: MatCreateMPIMatConcatenateSeqMat_MPIDense,
1362: NULL,
1363: NULL,
1364: NULL,
1365: /*134*/ NULL,
1366: NULL,
1367: NULL,
1368: NULL,
1369: NULL,
1370: /*139*/ NULL,
1371: NULL,
1372: NULL,
1373: NULL,
1374: NULL,
1375: MatADot_Default,
1376: /*144*/ MatANorm_Default,
1377: NULL,
1378: NULL,
1379: NULL};
1381: static PetscErrorCode MatMPIDenseSetPreallocation_MPIDense(Mat mat, PetscScalar *data)
1382: {
1383: Mat_MPIDense *a = (Mat_MPIDense *)mat->data;
1384: MatType mtype = MATSEQDENSE;
1386: PetscFunctionBegin;
1387: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1388: PetscCall(PetscLayoutSetUp(mat->rmap));
1389: PetscCall(PetscLayoutSetUp(mat->cmap));
1390: if (!a->A) {
1391: PetscCall(MatCreate(PETSC_COMM_SELF, &a->A));
1392: PetscCall(MatSetSizes(a->A, mat->rmap->n, mat->cmap->N, mat->rmap->n, mat->cmap->N));
1393: }
1394: #if defined(PETSC_HAVE_CUDA)
1395: PetscBool iscuda;
1396: PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATMPIDENSECUDA, &iscuda));
1397: if (iscuda) mtype = MATSEQDENSECUDA;
1398: #endif
1399: #if defined(PETSC_HAVE_HIP)
1400: PetscBool iship;
1401: PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATMPIDENSEHIP, &iship));
1402: if (iship) mtype = MATSEQDENSEHIP;
1403: #endif
1404: PetscCall(MatSetType(a->A, mtype));
1405: PetscCall(MatSeqDenseSetPreallocation(a->A, data));
1406: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP)
1407: mat->offloadmask = a->A->offloadmask;
1408: #endif
1409: mat->preallocated = PETSC_TRUE;
1410: mat->assembled = PETSC_TRUE;
1411: PetscFunctionReturn(PETSC_SUCCESS);
1412: }
1414: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIDense(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1415: {
1416: Mat B, C;
1418: PetscFunctionBegin;
1419: PetscCall(MatMPIAIJGetLocalMat(A, MAT_INITIAL_MATRIX, &C));
1420: PetscCall(MatConvert_SeqAIJ_SeqDense(C, MATSEQDENSE, MAT_INITIAL_MATRIX, &B));
1421: PetscCall(MatDestroy(&C));
1422: if (reuse == MAT_REUSE_MATRIX) {
1423: C = *newmat;
1424: } else C = NULL;
1425: PetscCall(MatCreateMPIMatConcatenateSeqMat(PetscObjectComm((PetscObject)A), B, A->cmap->n, !C ? MAT_INITIAL_MATRIX : MAT_REUSE_MATRIX, &C));
1426: PetscCall(MatDestroy(&B));
1427: if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &C));
1428: else if (reuse == MAT_INITIAL_MATRIX) *newmat = C;
1429: PetscFunctionReturn(PETSC_SUCCESS);
1430: }
1432: static PetscErrorCode MatConvert_MPIDense_MPIAIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1433: {
1434: Mat B, C;
1436: PetscFunctionBegin;
1437: PetscCall(MatDenseGetLocalMatrix(A, &C));
1438: PetscCall(MatConvert_SeqDense_SeqAIJ(C, MATSEQAIJ, MAT_INITIAL_MATRIX, &B));
1439: if (reuse == MAT_REUSE_MATRIX) {
1440: C = *newmat;
1441: } else C = NULL;
1442: PetscCall(MatCreateMPIMatConcatenateSeqMat(PetscObjectComm((PetscObject)A), B, A->cmap->n, !C ? MAT_INITIAL_MATRIX : MAT_REUSE_MATRIX, &C));
1443: PetscCall(MatDestroy(&B));
1444: if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &C));
1445: else if (reuse == MAT_INITIAL_MATRIX) *newmat = C;
1446: PetscFunctionReturn(PETSC_SUCCESS);
1447: }
1449: #if defined(PETSC_HAVE_ELEMENTAL)
1450: PETSC_INTERN PetscErrorCode MatConvert_MPIDense_Elemental(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1451: {
1452: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1453: Mat mat_elemental;
1454: PetscScalar *v;
1455: PetscInt m = A->rmap->n, N = A->cmap->N, rstart = A->rmap->rstart, i, *rows, *cols, lda;
1457: PetscFunctionBegin;
1458: if (reuse == MAT_REUSE_MATRIX) {
1459: mat_elemental = *newmat;
1460: PetscCall(MatZeroEntries(*newmat));
1461: } else {
1462: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental));
1463: PetscCall(MatSetSizes(mat_elemental, PETSC_DECIDE, PETSC_DECIDE, A->rmap->N, A->cmap->N));
1464: PetscCall(MatSetType(mat_elemental, MATELEMENTAL));
1465: PetscCall(MatSetUp(mat_elemental));
1466: PetscCall(MatSetOption(mat_elemental, MAT_ROW_ORIENTED, PETSC_FALSE));
1467: }
1469: PetscCall(PetscMalloc2(m, &rows, N, &cols));
1470: for (i = 0; i < N; i++) cols[i] = i;
1471: for (i = 0; i < m; i++) rows[i] = rstart + i;
1473: /* PETSc-Elemental interface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
1474: PetscCall(MatDenseGetArray(A, &v));
1475: PetscCall(MatDenseGetLDA(a->A, &lda));
1476: if (lda == m) PetscCall(MatSetValues(mat_elemental, m, rows, N, cols, v, ADD_VALUES));
1477: else {
1478: for (i = 0; i < N; i++) PetscCall(MatSetValues(mat_elemental, m, rows, 1, &i, v + lda * i, ADD_VALUES));
1479: }
1480: PetscCall(MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY));
1481: PetscCall(MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY));
1482: PetscCall(MatDenseRestoreArray(A, &v));
1483: PetscCall(PetscFree2(rows, cols));
1485: if (reuse == MAT_INPLACE_MATRIX) {
1486: PetscCall(MatHeaderReplace(A, &mat_elemental));
1487: } else {
1488: *newmat = mat_elemental;
1489: }
1490: PetscFunctionReturn(PETSC_SUCCESS);
1491: }
1492: #endif
1494: static PetscErrorCode MatDenseGetColumn_MPIDense(Mat A, PetscInt col, PetscScalar **vals)
1495: {
1496: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
1498: PetscFunctionBegin;
1499: PetscCall(MatDenseGetColumn(mat->A, col, vals));
1500: PetscFunctionReturn(PETSC_SUCCESS);
1501: }
1503: static PetscErrorCode MatDenseRestoreColumn_MPIDense(Mat A, PetscScalar **vals)
1504: {
1505: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
1507: PetscFunctionBegin;
1508: PetscCall(MatDenseRestoreColumn(mat->A, vals));
1509: PetscFunctionReturn(PETSC_SUCCESS);
1510: }
1512: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIDense(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
1513: {
1514: Mat_MPIDense *mat;
1515: PetscInt m, nloc, N;
1517: PetscFunctionBegin;
1518: PetscCall(MatGetSize(inmat, &m, &N));
1519: PetscCall(MatGetLocalSize(inmat, NULL, &nloc));
1520: if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
1521: PetscInt sum;
1523: if (n == PETSC_DECIDE) PetscCall(PetscSplitOwnership(comm, &n, &N));
1524: /* Check sum(n) = N */
1525: PetscCallMPI(MPIU_Allreduce(&n, &sum, 1, MPIU_INT, MPI_SUM, comm));
1526: PetscCheck(sum == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Sum of local columns %" PetscInt_FMT " != global columns %" PetscInt_FMT, sum, N);
1528: PetscCall(MatCreateDense(comm, m, n, PETSC_DETERMINE, N, NULL, outmat));
1529: PetscCall(MatSetOption(*outmat, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
1530: }
1532: /* numeric phase */
1533: mat = (Mat_MPIDense *)(*outmat)->data;
1534: PetscCall(MatCopy(inmat, mat->A, SAME_NONZERO_PATTERN));
1535: PetscFunctionReturn(PETSC_SUCCESS);
1536: }
1538: PetscErrorCode MatDenseGetColumnVec_MPIDense(Mat A, PetscInt col, Vec *v)
1539: {
1540: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1541: PetscInt lda;
1543: PetscFunctionBegin;
1544: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1545: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1546: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
1547: a->vecinuse = col + 1;
1548: PetscCall(MatDenseGetLDA(a->A, &lda));
1549: PetscCall(MatDenseGetArray(a->A, (PetscScalar **)&a->ptrinuse));
1550: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)lda)));
1551: *v = a->cvec;
1552: PetscFunctionReturn(PETSC_SUCCESS);
1553: }
1555: PetscErrorCode MatDenseRestoreColumnVec_MPIDense(Mat A, PetscInt col, Vec *v)
1556: {
1557: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1559: PetscFunctionBegin;
1560: PetscCheck(a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
1561: PetscCheck(a->cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector");
1562: VecCheckAssembled(a->cvec);
1563: a->vecinuse = 0;
1564: PetscCall(MatDenseRestoreArray(a->A, (PetscScalar **)&a->ptrinuse));
1565: PetscCall(VecResetArray(a->cvec));
1566: if (v) *v = NULL;
1567: PetscFunctionReturn(PETSC_SUCCESS);
1568: }
1570: PetscErrorCode MatDenseGetColumnVecRead_MPIDense(Mat A, PetscInt col, Vec *v)
1571: {
1572: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1573: PetscInt lda;
1575: PetscFunctionBegin;
1576: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1577: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1578: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
1579: a->vecinuse = col + 1;
1580: PetscCall(MatDenseGetLDA(a->A, &lda));
1581: PetscCall(MatDenseGetArrayRead(a->A, &a->ptrinuse));
1582: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)lda)));
1583: PetscCall(VecLockReadPush(a->cvec));
1584: *v = a->cvec;
1585: PetscFunctionReturn(PETSC_SUCCESS);
1586: }
1588: PetscErrorCode MatDenseRestoreColumnVecRead_MPIDense(Mat A, PetscInt col, Vec *v)
1589: {
1590: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1592: PetscFunctionBegin;
1593: PetscCheck(a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
1594: PetscCheck(a->cvec, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Missing internal column vector");
1595: VecCheckAssembled(a->cvec);
1596: a->vecinuse = 0;
1597: PetscCall(MatDenseRestoreArrayRead(a->A, &a->ptrinuse));
1598: PetscCall(VecLockReadPop(a->cvec));
1599: PetscCall(VecResetArray(a->cvec));
1600: if (v) *v = NULL;
1601: PetscFunctionReturn(PETSC_SUCCESS);
1602: }
1604: PetscErrorCode MatDenseGetColumnVecWrite_MPIDense(Mat A, PetscInt col, Vec *v)
1605: {
1606: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1607: PetscInt lda;
1609: PetscFunctionBegin;
1610: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1611: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1612: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
1613: a->vecinuse = col + 1;
1614: PetscCall(MatDenseGetLDA(a->A, &lda));
1615: PetscCall(MatDenseGetArrayWrite(a->A, (PetscScalar **)&a->ptrinuse));
1616: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)lda)));
1617: *v = a->cvec;
1618: PetscFunctionReturn(PETSC_SUCCESS);
1619: }
1621: PetscErrorCode MatDenseRestoreColumnVecWrite_MPIDense(Mat A, PetscInt col, Vec *v)
1622: {
1623: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1625: PetscFunctionBegin;
1626: PetscCheck(a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
1627: PetscCheck(a->cvec, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Missing internal column vector");
1628: VecCheckAssembled(a->cvec);
1629: a->vecinuse = 0;
1630: PetscCall(MatDenseRestoreArrayWrite(a->A, (PetscScalar **)&a->ptrinuse));
1631: PetscCall(VecResetArray(a->cvec));
1632: if (v) *v = NULL;
1633: PetscFunctionReturn(PETSC_SUCCESS);
1634: }
1636: static PetscErrorCode MatDenseGetSubMatrix_MPIDense(Mat A, PetscInt rbegin, PetscInt rend, PetscInt cbegin, PetscInt cend, Mat *v)
1637: {
1638: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1639: Mat_MPIDense *c;
1640: MPI_Comm comm;
1641: PetscInt prbegin, prend, pcbegin, pcend;
1643: PetscFunctionBegin;
1644: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
1645: PetscCheck(!a->vecinuse, comm, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1646: PetscCheck(!a->matinuse, comm, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1647: prbegin = PetscMax(0, PetscMin(A->rmap->rend, rbegin) - A->rmap->rstart);
1648: prend = PetscMin(A->rmap->n, PetscMax(0, rend - A->rmap->rstart));
1649: pcbegin = PetscMax(0, PetscMin(A->cmap->rend, cbegin) - A->cmap->rstart);
1650: pcend = PetscMin(A->cmap->n, PetscMax(0, cend - A->cmap->rstart));
1651: if (!a->cmat) {
1652: PetscCall(MatCreate(comm, &a->cmat));
1653: PetscCall(MatSetType(a->cmat, ((PetscObject)A)->type_name));
1654: if (rend - rbegin == A->rmap->N) PetscCall(PetscLayoutReference(A->rmap, &a->cmat->rmap));
1655: else {
1656: PetscCall(PetscLayoutSetLocalSize(a->cmat->rmap, prend - prbegin));
1657: PetscCall(PetscLayoutSetSize(a->cmat->rmap, rend - rbegin));
1658: PetscCall(PetscLayoutSetUp(a->cmat->rmap));
1659: }
1660: if (cend - cbegin == A->cmap->N) PetscCall(PetscLayoutReference(A->cmap, &a->cmat->cmap));
1661: else {
1662: PetscCall(PetscLayoutSetLocalSize(a->cmat->cmap, pcend - pcbegin));
1663: PetscCall(PetscLayoutSetSize(a->cmat->cmap, cend - cbegin));
1664: PetscCall(PetscLayoutSetUp(a->cmat->cmap));
1665: }
1666: c = (Mat_MPIDense *)a->cmat->data;
1667: c->sub_rbegin = rbegin;
1668: c->sub_rend = rend;
1669: c->sub_cbegin = cbegin;
1670: c->sub_cend = cend;
1671: }
1672: c = (Mat_MPIDense *)a->cmat->data;
1673: if (c->sub_rbegin != rbegin || c->sub_rend != rend) {
1674: PetscCall(PetscLayoutDestroy(&a->cmat->rmap));
1675: PetscCall(PetscLayoutCreate(comm, &a->cmat->rmap));
1676: PetscCall(PetscLayoutSetLocalSize(a->cmat->rmap, prend - prbegin));
1677: PetscCall(PetscLayoutSetSize(a->cmat->rmap, rend - rbegin));
1678: PetscCall(PetscLayoutSetUp(a->cmat->rmap));
1679: c->sub_rbegin = rbegin;
1680: c->sub_rend = rend;
1681: }
1682: if (c->sub_cbegin != cbegin || c->sub_cend != cend) {
1683: // special optimization: check if all columns are owned by rank 0, in which case no communication is necessary
1684: if ((cend - cbegin != a->cmat->cmap->N) || (A->cmap->range[1] != A->cmap->N)) {
1685: PetscCall(PetscLayoutDestroy(&a->cmat->cmap));
1686: PetscCall(PetscLayoutCreate(comm, &a->cmat->cmap));
1687: PetscCall(PetscLayoutSetLocalSize(a->cmat->cmap, pcend - pcbegin));
1688: PetscCall(PetscLayoutSetSize(a->cmat->cmap, cend - cbegin));
1689: PetscCall(PetscLayoutSetUp(a->cmat->cmap));
1690: PetscCall(VecDestroy(&c->lvec));
1691: PetscCall(PetscSFDestroy(&c->Mvctx));
1692: }
1693: c->sub_cbegin = cbegin;
1694: c->sub_cend = cend;
1695: }
1696: PetscCheck(!c->A, comm, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1697: PetscCall(MatDenseGetSubMatrix(a->A, prbegin, prend, cbegin, cend, &c->A));
1699: a->cmat->preallocated = PETSC_TRUE;
1700: a->cmat->assembled = PETSC_TRUE;
1701: #if defined(PETSC_HAVE_DEVICE)
1702: a->cmat->offloadmask = c->A->offloadmask;
1703: #endif
1704: a->matinuse = cbegin + 1;
1705: *v = a->cmat;
1706: PetscFunctionReturn(PETSC_SUCCESS);
1707: }
1709: static PetscErrorCode MatDenseRestoreSubMatrix_MPIDense(Mat A, Mat *v)
1710: {
1711: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1712: Mat_MPIDense *c;
1714: PetscFunctionBegin;
1715: PetscCheck(a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseGetSubMatrix() first");
1716: PetscCheck(a->cmat, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Missing internal matrix");
1717: PetscCheck(*v == a->cmat, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not the matrix obtained from MatDenseGetSubMatrix()");
1718: a->matinuse = 0;
1719: c = (Mat_MPIDense *)a->cmat->data;
1720: PetscCall(MatDenseRestoreSubMatrix(a->A, &c->A));
1721: *v = NULL;
1722: #if defined(PETSC_HAVE_DEVICE)
1723: A->offloadmask = a->A->offloadmask;
1724: #endif
1725: PetscFunctionReturn(PETSC_SUCCESS);
1726: }
1728: /*MC
1729: MATMPIDENSE - MATMPIDENSE = "mpidense" - A matrix type to be used for distributed dense matrices.
1731: Options Database Key:
1732: . -mat_type mpidense - sets the matrix type to `MATMPIDENSE` during a call to `MatSetFromOptions()`
1734: Level: beginner
1736: .seealso: [](ch_matrices), `Mat`, `MatCreateDense()`, `MATSEQDENSE`, `MATDENSE`
1737: M*/
1738: PetscErrorCode MatCreate_MPIDense(Mat mat)
1739: {
1740: Mat_MPIDense *a;
1742: PetscFunctionBegin;
1743: PetscCall(PetscNew(&a));
1744: mat->data = (void *)a;
1745: mat->ops[0] = MatOps_Values;
1747: mat->insertmode = NOT_SET_VALUES;
1749: /* build cache for off array entries formed */
1750: a->donotstash = PETSC_FALSE;
1752: PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)mat), 1, &mat->stash));
1754: /* stuff used for matrix vector multiply */
1755: a->lvec = NULL;
1756: a->Mvctx = NULL;
1757: a->roworiented = PETSC_TRUE;
1759: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetLDA_C", MatDenseGetLDA_MPIDense));
1760: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseSetLDA_C", MatDenseSetLDA_MPIDense));
1761: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArray_C", MatDenseGetArray_MPIDense));
1762: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArray_C", MatDenseRestoreArray_MPIDense));
1763: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArrayRead_C", MatDenseGetArrayRead_MPIDense));
1764: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArrayRead_C", MatDenseRestoreArrayRead_MPIDense));
1765: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArrayWrite_C", MatDenseGetArrayWrite_MPIDense));
1766: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArrayWrite_C", MatDenseRestoreArrayWrite_MPIDense));
1767: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDensePlaceArray_C", MatDensePlaceArray_MPIDense));
1768: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseResetArray_C", MatDenseResetArray_MPIDense));
1769: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseReplaceArray_C", MatDenseReplaceArray_MPIDense));
1770: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVec_C", MatDenseGetColumnVec_MPIDense));
1771: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVec_C", MatDenseRestoreColumnVec_MPIDense));
1772: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVecRead_C", MatDenseGetColumnVecRead_MPIDense));
1773: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVecRead_C", MatDenseRestoreColumnVecRead_MPIDense));
1774: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVecWrite_C", MatDenseGetColumnVecWrite_MPIDense));
1775: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVecWrite_C", MatDenseRestoreColumnVecWrite_MPIDense));
1776: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetSubMatrix_C", MatDenseGetSubMatrix_MPIDense));
1777: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreSubMatrix_C", MatDenseRestoreSubMatrix_MPIDense));
1778: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpiaij_mpidense_C", MatConvert_MPIAIJ_MPIDense));
1779: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpiaij_C", MatConvert_MPIDense_MPIAIJ));
1780: #if defined(PETSC_HAVE_ELEMENTAL)
1781: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_elemental_C", MatConvert_MPIDense_Elemental));
1782: #endif
1783: #if defined(PETSC_HAVE_SCALAPACK) && (defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL_DOUBLE))
1784: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_scalapack_C", MatConvert_Dense_ScaLAPACK));
1785: #endif
1786: #if defined(PETSC_HAVE_CUDA)
1787: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpidensecuda_C", MatConvert_MPIDense_MPIDenseCUDA));
1788: #endif
1789: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPIDenseSetPreallocation_C", MatMPIDenseSetPreallocation_MPIDense));
1790: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaij_mpidense_C", MatProductSetFromOptions_MPIAIJ_MPIDense));
1791: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaij_C", MatProductSetFromOptions_MPIDense_MPIAIJ));
1792: #if defined(PETSC_HAVE_CUDA)
1793: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijcusparse_mpidense_C", MatProductSetFromOptions_MPIAIJ_MPIDense));
1794: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaijcusparse_C", MatProductSetFromOptions_MPIDense_MPIAIJ));
1795: #endif
1796: #if defined(PETSC_HAVE_HIP)
1797: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpidensehip_C", MatConvert_MPIDense_MPIDenseHIP));
1798: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijhipsparse_mpidense_C", MatProductSetFromOptions_MPIAIJ_MPIDense));
1799: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaijhipsparse_C", MatProductSetFromOptions_MPIDense_MPIAIJ));
1800: #endif
1801: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumn_C", MatDenseGetColumn_MPIDense));
1802: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumn_C", MatDenseRestoreColumn_MPIDense));
1803: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultColumnRange_C", MatMultColumnRange_MPIDense));
1804: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultAddColumnRange_C", MatMultAddColumnRange_MPIDense));
1805: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultHermitianTransposeColumnRange_C", MatMultHermitianTransposeColumnRange_MPIDense));
1806: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultHermitianTransposeAddColumnRange_C", MatMultHermitianTransposeAddColumnRange_MPIDense));
1807: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatGetMultPetscSF_C", MatGetMultPetscSF_MPIDense));
1808: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, MATMPIDENSE));
1809: PetscFunctionReturn(PETSC_SUCCESS);
1810: }
1812: /*MC
1813: MATDENSE - MATDENSE = "dense" - A matrix type to be used for dense matrices.
1815: This matrix type is identical to `MATSEQDENSE` when constructed with a single process communicator,
1816: and `MATMPIDENSE` otherwise.
1818: Options Database Key:
1819: . -mat_type dense - sets the matrix type to `MATDENSE` during a call to `MatSetFromOptions()`
1821: Level: beginner
1823: .seealso: [](ch_matrices), `Mat`, `MATSEQDENSE`, `MATMPIDENSE`, `MATDENSECUDA`, `MATDENSEHIP`
1824: M*/
1826: /*@
1827: MatMPIDenseSetPreallocation - Sets the array used to store the matrix entries
1829: Collective
1831: Input Parameters:
1832: + B - the matrix
1833: - data - optional location of matrix data. Set to `NULL` for PETSc
1834: to control all matrix memory allocation.
1836: Level: intermediate
1838: Notes:
1839: The dense format is fully compatible with standard Fortran
1840: storage by columns.
1842: The data input variable is intended primarily for Fortran programmers
1843: who wish to allocate their own matrix memory space. Most users should
1844: set `data` to `NULL`.
1846: .seealso: [](ch_matrices), `Mat`, `MATMPIDENSE`, `MatCreate()`, `MatCreateSeqDense()`, `MatSetValues()`
1847: @*/
1848: PetscErrorCode MatMPIDenseSetPreallocation(Mat B, PetscScalar *data)
1849: {
1850: PetscFunctionBegin;
1852: PetscTryMethod(B, "MatMPIDenseSetPreallocation_C", (Mat, PetscScalar *), (B, data));
1853: PetscFunctionReturn(PETSC_SUCCESS);
1854: }
1856: /*@
1857: MatDensePlaceArray - Allows one to replace the array in a `MATDENSE` matrix with an
1858: array provided by the user. This is useful to avoid copying an array
1859: into a matrix
1861: Not Collective
1863: Input Parameters:
1864: + mat - the matrix
1865: - array - the array in column major order
1867: Level: developer
1869: Note:
1870: Adding `const` to `array` was an oversight, see notes in `VecPlaceArray()`.
1872: You can return to the original array with a call to `MatDenseResetArray()`. The user is responsible for freeing this array; it will not be
1873: freed when the matrix is destroyed.
1875: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArray()`, `MatDenseResetArray()`, `VecPlaceArray()`, `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecResetArray()`,
1876: `MatDenseReplaceArray()`
1877: @*/
1878: PetscErrorCode MatDensePlaceArray(Mat mat, const PetscScalar *array)
1879: {
1880: PetscFunctionBegin;
1882: PetscUseMethod(mat, "MatDensePlaceArray_C", (Mat, const PetscScalar *), (mat, array));
1883: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
1884: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP)
1885: mat->offloadmask = PETSC_OFFLOAD_CPU;
1886: #endif
1887: PetscFunctionReturn(PETSC_SUCCESS);
1888: }
1890: /*@
1891: MatDenseResetArray - Resets the matrix array to that it previously had before the call to `MatDensePlaceArray()`
1893: Not Collective
1895: Input Parameter:
1896: . mat - the matrix
1898: Level: developer
1900: Note:
1901: You can only call this after a call to `MatDensePlaceArray()`
1903: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArray()`, `MatDensePlaceArray()`, `VecPlaceArray()`, `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecResetArray()`
1904: @*/
1905: PetscErrorCode MatDenseResetArray(Mat mat)
1906: {
1907: PetscFunctionBegin;
1909: PetscUseMethod(mat, "MatDenseResetArray_C", (Mat), (mat));
1910: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
1911: PetscFunctionReturn(PETSC_SUCCESS);
1912: }
1914: /*@
1915: MatDenseReplaceArray - Allows one to replace the array in a dense matrix with an
1916: array provided by the user. This is useful to avoid copying an array
1917: into a matrix
1919: Not Collective
1921: Input Parameters:
1922: + mat - the matrix
1923: - array - the array in column major order
1925: Level: developer
1927: Note:
1928: Adding `const` to `array` was an oversight, see notes in `VecPlaceArray()`.
1930: The memory passed in MUST be obtained with `PetscMalloc()` and CANNOT be
1931: freed by the user. It will be freed when the matrix is destroyed.
1933: .seealso: [](ch_matrices), `Mat`, `MatDensePlaceArray()`, `MatDenseGetArray()`, `VecReplaceArray()`
1934: @*/
1935: PetscErrorCode MatDenseReplaceArray(Mat mat, const PetscScalar *array)
1936: {
1937: PetscFunctionBegin;
1939: PetscUseMethod(mat, "MatDenseReplaceArray_C", (Mat, const PetscScalar *), (mat, array));
1940: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
1941: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP)
1942: mat->offloadmask = PETSC_OFFLOAD_CPU;
1943: #endif
1944: PetscFunctionReturn(PETSC_SUCCESS);
1945: }
1947: /*@
1948: MatCreateDense - Creates a matrix in `MATDENSE` format.
1950: Collective
1952: Input Parameters:
1953: + comm - MPI communicator
1954: . m - number of local rows (or `PETSC_DECIDE` to have calculated if `M` is given)
1955: . n - number of local columns (or `PETSC_DECIDE` to have calculated if `N` is given)
1956: . M - number of global rows (or `PETSC_DECIDE` to have calculated if `m` is given)
1957: . N - number of global columns (or `PETSC_DECIDE` to have calculated if `n` is given)
1958: - data - optional location of matrix data. Set data to `NULL` (`PETSC_NULL_SCALAR_ARRAY` for Fortran users) for PETSc
1959: to control all matrix memory allocation.
1961: Output Parameter:
1962: . A - the matrix
1964: Level: intermediate
1966: Notes:
1967: The dense format is fully compatible with standard Fortran
1968: storage by columns.
1970: Although local portions of the matrix are stored in column-major
1971: order, the matrix is partitioned across MPI ranks by row.
1973: The data input variable is intended primarily for Fortran programmers
1974: who wish to allocate their own matrix memory space. Most users should
1975: set `data` to `NULL` (`PETSC_NULL_SCALAR_ARRAY` for Fortran users).
1977: The user MUST specify either the local or global matrix dimensions
1978: (possibly both).
1980: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatCreate()`, `MatCreateSeqDense()`, `MatSetValues()`
1981: @*/
1982: PetscErrorCode MatCreateDense(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscScalar data[], Mat *A)
1983: {
1984: PetscFunctionBegin;
1985: PetscCall(MatCreate(comm, A));
1986: PetscCall(MatSetSizes(*A, m, n, M, N));
1987: PetscCall(MatSetType(*A, MATDENSE));
1988: PetscCall(MatSeqDenseSetPreallocation(*A, data));
1989: PetscCall(MatMPIDenseSetPreallocation(*A, data));
1990: PetscFunctionReturn(PETSC_SUCCESS);
1991: }
1993: static PetscErrorCode MatDuplicate_MPIDense(Mat A, MatDuplicateOption cpvalues, Mat *newmat)
1994: {
1995: Mat mat;
1996: Mat_MPIDense *a, *oldmat = (Mat_MPIDense *)A->data;
1998: PetscFunctionBegin;
1999: *newmat = NULL;
2000: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat));
2001: PetscCall(MatSetSizes(mat, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
2002: PetscCall(MatSetType(mat, ((PetscObject)A)->type_name));
2003: a = (Mat_MPIDense *)mat->data;
2005: mat->factortype = A->factortype;
2006: mat->assembled = PETSC_TRUE;
2007: mat->preallocated = PETSC_TRUE;
2009: mat->insertmode = NOT_SET_VALUES;
2010: a->donotstash = oldmat->donotstash;
2012: PetscCall(PetscLayoutReference(A->rmap, &mat->rmap));
2013: PetscCall(PetscLayoutReference(A->cmap, &mat->cmap));
2015: PetscCall(MatDuplicate(oldmat->A, cpvalues, &a->A));
2017: *newmat = mat;
2018: PetscFunctionReturn(PETSC_SUCCESS);
2019: }
2021: static PetscErrorCode MatLoad_MPIDense(Mat newMat, PetscViewer viewer)
2022: {
2023: PetscBool isbinary;
2024: #if defined(PETSC_HAVE_HDF5)
2025: PetscBool ishdf5;
2026: #endif
2028: PetscFunctionBegin;
2031: /* force binary viewer to load .info file if it has not yet done so */
2032: PetscCall(PetscViewerSetUp(viewer));
2033: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
2034: #if defined(PETSC_HAVE_HDF5)
2035: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2036: #endif
2037: if (isbinary) {
2038: PetscCall(MatLoad_Dense_Binary(newMat, viewer));
2039: #if defined(PETSC_HAVE_HDF5)
2040: } else if (ishdf5) {
2041: PetscCall(MatLoad_Dense_HDF5(newMat, viewer));
2042: #endif
2043: } else SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "Viewer type %s not yet supported for reading %s matrices", ((PetscObject)viewer)->type_name, ((PetscObject)newMat)->type_name);
2044: PetscFunctionReturn(PETSC_SUCCESS);
2045: }
2047: static PetscErrorCode MatEqual_MPIDense(Mat A, Mat B, PetscBool *flag)
2048: {
2049: Mat_MPIDense *matB = (Mat_MPIDense *)B->data, *matA = (Mat_MPIDense *)A->data;
2050: Mat a, b;
2052: PetscFunctionBegin;
2053: a = matA->A;
2054: b = matB->A;
2055: PetscCall(MatEqual(a, b, flag));
2056: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flag, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)A)));
2057: PetscFunctionReturn(PETSC_SUCCESS);
2058: }
2060: static PetscErrorCode MatProductCtxDestroy_MatTransMatMult_MPIDense_MPIDense(PetscCtxRt data)
2061: {
2062: MatProductCtx_TransMatMultDense *atb = *(MatProductCtx_TransMatMultDense **)data;
2064: PetscFunctionBegin;
2065: PetscCall(PetscFree2(atb->sendbuf, atb->recvcounts));
2066: PetscCall(MatDestroy(&atb->atb));
2067: PetscCall(PetscFree(atb));
2068: PetscFunctionReturn(PETSC_SUCCESS);
2069: }
2071: static PetscErrorCode MatProductCtxDestroy_MatMatTransMult_MPIDense_MPIDense(PetscCtxRt data)
2072: {
2073: MatProductCtx_MatTransMultDense *abt = *(MatProductCtx_MatTransMultDense **)data;
2075: PetscFunctionBegin;
2076: PetscCall(PetscFree2(abt->buf[0], abt->buf[1]));
2077: PetscCall(PetscFree2(abt->recvcounts, abt->recvdispls));
2078: PetscCall(PetscFree(abt));
2079: PetscFunctionReturn(PETSC_SUCCESS);
2080: }
2082: static PetscErrorCode MatTransposeMatMultNumeric_MPIDense_MPIDense(Mat A, Mat B, Mat C)
2083: {
2084: Mat_MPIDense *a = (Mat_MPIDense *)A->data, *b = (Mat_MPIDense *)B->data, *c = (Mat_MPIDense *)C->data;
2085: MatProductCtx_TransMatMultDense *atb;
2086: MPI_Comm comm;
2087: PetscMPIInt size, *recvcounts;
2088: PetscScalar *carray, *sendbuf;
2089: const PetscScalar *atbarray;
2090: PetscInt i, cN = C->cmap->N, proc, k, j, lda;
2091: const PetscInt *ranges;
2093: PetscFunctionBegin;
2094: MatCheckProduct(C, 3);
2095: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2096: atb = (MatProductCtx_TransMatMultDense *)C->product->data;
2097: recvcounts = atb->recvcounts;
2098: sendbuf = atb->sendbuf;
2100: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
2101: PetscCallMPI(MPI_Comm_size(comm, &size));
2103: /* compute atbarray = aseq^T * bseq */
2104: PetscCall(MatTransposeMatMult(a->A, b->A, atb->atb ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX, PETSC_DETERMINE, &atb->atb));
2106: PetscCall(MatGetOwnershipRanges(C, &ranges));
2108: if (ranges[1] == C->rmap->N) {
2109: /* all of the values are being reduced to rank 0: optimize this case to use MPI_Reduce and GPU aware MPI if available */
2110: PetscInt atb_lda, c_lda;
2111: Mat atb_local = atb->atb;
2112: Mat atb_alloc = NULL;
2113: Mat c_local = c->A;
2114: Mat c_alloc = NULL;
2115: PetscMemType atb_memtype, c_memtype;
2116: const PetscScalar *atb_array = NULL;
2117: MPI_Datatype vector_type;
2118: PetscScalar *c_array = NULL;
2119: PetscMPIInt rank;
2121: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2123: PetscCall(MatDenseGetLDA(atb_local, &atb_lda));
2124: if (atb_lda != C->rmap->N) {
2125: // copy atb to a matrix that will have lda == the number of rows
2126: PetscCall(MatDuplicate(atb_local, MAT_DO_NOT_COPY_VALUES, &atb_alloc));
2127: PetscCall(MatCopy(atb_local, atb_alloc, DIFFERENT_NONZERO_PATTERN));
2128: atb_local = atb_alloc;
2129: }
2131: if (rank == 0) {
2132: PetscCall(MatDenseGetLDA(c_local, &c_lda));
2133: if (c_lda != C->rmap->N) {
2134: // copy c to a matrix that will have lda == the number of rows
2135: PetscCall(MatDuplicate(c_local, MAT_DO_NOT_COPY_VALUES, &c_alloc));
2136: c_local = c_alloc;
2137: }
2138: PetscCall(MatZeroEntries(c_local));
2139: }
2140: /* atb_local and c_local have nrows = lda = A->cmap->N and ncols =
2141: * B->cmap->N: use the a->Mvctx to use the best reduction method */
2142: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
2143: vector_type = MPIU_SCALAR;
2144: if (B->cmap->N > 1) {
2145: PetscMPIInt mpi_N;
2147: PetscCall(PetscMPIIntCast(B->cmap->N, &mpi_N));
2148: PetscCallMPI(MPI_Type_contiguous(mpi_N, MPIU_SCALAR, &vector_type));
2149: PetscCallMPI(MPI_Type_commit(&vector_type));
2150: }
2151: PetscCall(MatDenseGetArrayReadAndMemType(atb_local, &atb_array, &atb_memtype));
2152: PetscCall(MatDenseGetArrayWriteAndMemType(c_local, &c_array, &c_memtype));
2153: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, vector_type, atb_memtype, atb_array, c_memtype, c_array, MPIU_SUM));
2154: PetscCall(PetscSFReduceEnd(a->Mvctx, vector_type, atb_array, c_array, MPIU_SUM));
2155: PetscCall(MatDenseRestoreArrayWriteAndMemType(c_local, &c_array));
2156: PetscCall(MatDenseRestoreArrayReadAndMemType(atb_local, &atb_array));
2157: if (rank == 0 && c_local != c->A) PetscCall(MatCopy(c_local, c->A, DIFFERENT_NONZERO_PATTERN));
2158: if (B->cmap->N > 1) PetscCallMPI(MPI_Type_free(&vector_type));
2159: PetscCall(MatDestroy(&atb_alloc));
2160: PetscCall(MatDestroy(&c_alloc));
2161: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
2162: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2163: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
2164: PetscFunctionReturn(PETSC_SUCCESS);
2165: }
2167: /* arrange atbarray into sendbuf */
2168: PetscCall(MatDenseGetArrayRead(atb->atb, &atbarray));
2169: PetscCall(MatDenseGetLDA(atb->atb, &lda));
2170: for (proc = 0, k = 0; proc < size; proc++) {
2171: for (j = 0; j < cN; j++) {
2172: for (i = ranges[proc]; i < ranges[proc + 1]; i++) sendbuf[k++] = atbarray[i + j * lda];
2173: }
2174: }
2175: PetscCall(MatDenseRestoreArrayRead(atb->atb, &atbarray));
2177: /* sum all atbarray to local values of C */
2178: PetscCall(MatDenseGetArrayWrite(c->A, &carray));
2179: PetscCallMPI(MPI_Reduce_scatter(sendbuf, carray, recvcounts, MPIU_SCALAR, MPIU_SUM, comm));
2180: PetscCall(MatDenseRestoreArrayWrite(c->A, &carray));
2181: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
2182: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2183: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
2184: PetscFunctionReturn(PETSC_SUCCESS);
2185: }
2187: static PetscErrorCode MatTransposeMatMultSymbolic_MPIDense_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2188: {
2189: MPI_Comm comm;
2190: PetscMPIInt size;
2191: PetscInt cm = A->cmap->n, cM, cN = B->cmap->N;
2192: MatProductCtx_TransMatMultDense *atb;
2193: PetscBool cisdense = PETSC_FALSE;
2194: const PetscInt *ranges;
2196: PetscFunctionBegin;
2197: MatCheckProduct(C, 4);
2198: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
2199: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
2200: PetscCheck(A->rmap->rstart == B->rmap->rstart && A->rmap->rend == B->rmap->rend, comm, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, A (%" PetscInt_FMT ", %" PetscInt_FMT ") != B (%" PetscInt_FMT ",%" PetscInt_FMT ")", A->rmap->rstart,
2201: A->rmap->rend, B->rmap->rstart, B->rmap->rend);
2203: /* create matrix product C */
2204: PetscCall(MatSetSizes(C, cm, B->cmap->n, A->cmap->N, B->cmap->N));
2205: #if defined(PETSC_HAVE_CUDA)
2206: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATMPIDENSE, MATMPIDENSECUDA, ""));
2207: #endif
2208: #if defined(PETSC_HAVE_HIP)
2209: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATMPIDENSE, MATMPIDENSEHIP, ""));
2210: #endif
2211: if (!cisdense) PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2212: PetscCall(MatSetUp(C));
2214: /* create data structure for reuse C */
2215: PetscCallMPI(MPI_Comm_size(comm, &size));
2216: PetscCall(PetscNew(&atb));
2217: cM = C->rmap->N;
2218: PetscCall(PetscMalloc2(cM * cN, &atb->sendbuf, size, &atb->recvcounts));
2219: PetscCall(MatGetOwnershipRanges(C, &ranges));
2220: for (PetscMPIInt i = 0; i < size; i++) PetscCall(PetscMPIIntCast((ranges[i + 1] - ranges[i]) * cN, &atb->recvcounts[i]));
2221: C->product->data = atb;
2222: C->product->destroy = MatProductCtxDestroy_MatTransMatMult_MPIDense_MPIDense;
2223: PetscFunctionReturn(PETSC_SUCCESS);
2224: }
2226: static PetscErrorCode MatMatTransposeMultSymbolic_MPIDense_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2227: {
2228: MPI_Comm comm;
2229: PetscMPIInt i, size;
2230: PetscInt maxRows, bufsiz;
2231: PetscMPIInt tag;
2232: PetscInt alg;
2233: MatProductCtx_MatTransMultDense *abt;
2234: Mat_Product *product = C->product;
2235: PetscBool flg;
2237: PetscFunctionBegin;
2238: MatCheckProduct(C, 4);
2239: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
2240: /* check local size of A and B */
2241: PetscCheck(A->cmap->n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrix local column dimensions are incompatible, A (%" PetscInt_FMT ") != B (%" PetscInt_FMT ")", A->cmap->n, B->cmap->n);
2243: PetscCall(PetscStrcmp(product->alg, "allgatherv", &flg));
2244: alg = flg ? 0 : 1;
2246: /* setup matrix product C */
2247: PetscCall(MatSetSizes(C, A->rmap->n, B->rmap->n, A->rmap->N, B->rmap->N));
2248: PetscCall(MatSetType(C, MATMPIDENSE));
2249: PetscCall(MatSetUp(C));
2250: PetscCall(PetscObjectGetNewTag((PetscObject)C, &tag));
2252: /* create data structure for reuse C */
2253: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
2254: PetscCallMPI(MPI_Comm_size(comm, &size));
2255: PetscCall(PetscNew(&abt));
2256: abt->tag = tag;
2257: abt->alg = alg;
2258: switch (alg) {
2259: case 1: /* alg: "cyclic" */
2260: for (maxRows = 0, i = 0; i < size; i++) maxRows = PetscMax(maxRows, B->rmap->range[i + 1] - B->rmap->range[i]);
2261: bufsiz = A->cmap->N * maxRows;
2262: PetscCall(PetscMalloc2(bufsiz, &abt->buf[0], bufsiz, &abt->buf[1]));
2263: break;
2264: default: /* alg: "allgatherv" */
2265: PetscCall(PetscMalloc2(B->rmap->n * B->cmap->N, &abt->buf[0], B->rmap->N * B->cmap->N, &abt->buf[1]));
2266: PetscCall(PetscMalloc2(size, &abt->recvcounts, size + 1, &abt->recvdispls));
2267: for (i = 0; i <= size; i++) PetscCall(PetscMPIIntCast(B->rmap->range[i] * A->cmap->N, &abt->recvdispls[i]));
2268: for (i = 0; i < size; i++) PetscCall(PetscMPIIntCast(abt->recvdispls[i + 1] - abt->recvdispls[i], &abt->recvcounts[i]));
2269: break;
2270: }
2272: C->product->data = abt;
2273: C->product->destroy = MatProductCtxDestroy_MatMatTransMult_MPIDense_MPIDense;
2274: C->ops->mattransposemultnumeric = MatMatTransposeMultNumeric_MPIDense_MPIDense;
2275: PetscFunctionReturn(PETSC_SUCCESS);
2276: }
2278: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense_Cyclic(Mat A, Mat B, Mat C)
2279: {
2280: Mat_MPIDense *a = (Mat_MPIDense *)A->data, *b = (Mat_MPIDense *)B->data, *c = (Mat_MPIDense *)C->data;
2281: MatProductCtx_MatTransMultDense *abt;
2282: MPI_Comm comm;
2283: PetscMPIInt rank, size, sendto, recvfrom, recvisfrom;
2284: PetscScalar *sendbuf, *recvbuf = NULL, *cv;
2285: PetscInt i, cK = A->cmap->N, sendsiz, recvsiz, k, j, bn;
2286: PetscScalar _DOne = 1.0, _DZero = 0.0;
2287: const PetscScalar *av, *bv;
2288: PetscBLASInt cm, cn, ck, alda, blda = 0, clda;
2289: MPI_Request reqs[2];
2290: const PetscInt *ranges;
2292: PetscFunctionBegin;
2293: MatCheckProduct(C, 3);
2294: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2295: abt = (MatProductCtx_MatTransMultDense *)C->product->data;
2296: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
2297: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2298: PetscCallMPI(MPI_Comm_size(comm, &size));
2299: PetscCall(MatDenseGetArrayRead(a->A, &av));
2300: PetscCall(MatDenseGetArrayRead(b->A, &bv));
2301: PetscCall(MatDenseGetArrayWrite(c->A, &cv));
2302: PetscCall(MatDenseGetLDA(a->A, &i));
2303: PetscCall(PetscBLASIntCast(i, &alda));
2304: PetscCall(MatDenseGetLDA(b->A, &i));
2305: PetscCall(PetscBLASIntCast(i, &blda));
2306: PetscCall(MatDenseGetLDA(c->A, &i));
2307: PetscCall(PetscBLASIntCast(i, &clda));
2308: PetscCall(MatGetOwnershipRanges(B, &ranges));
2309: bn = B->rmap->n;
2310: if (blda == bn) {
2311: sendbuf = (PetscScalar *)bv;
2312: } else {
2313: sendbuf = abt->buf[0];
2314: for (k = 0, i = 0; i < cK; i++) {
2315: for (j = 0; j < bn; j++, k++) sendbuf[k] = bv[i * blda + j];
2316: }
2317: }
2318: if (size > 1) {
2319: sendto = (rank + size - 1) % size;
2320: recvfrom = (rank + size + 1) % size;
2321: } else {
2322: sendto = recvfrom = 0;
2323: }
2324: PetscCall(PetscBLASIntCast(cK, &ck));
2325: PetscCall(PetscBLASIntCast(c->A->rmap->n, &cm));
2326: recvisfrom = rank;
2327: for (i = 0; i < size; i++) {
2328: /* we have finished receiving in sending, bufs can be read/modified */
2329: PetscMPIInt nextrecvisfrom = (recvisfrom + 1) % size; /* which process the next recvbuf will originate on */
2330: PetscInt nextbn = ranges[nextrecvisfrom + 1] - ranges[nextrecvisfrom];
2332: if (nextrecvisfrom != rank) {
2333: /* start the cyclic sends from sendbuf, to recvbuf (which will switch to sendbuf) */
2334: sendsiz = cK * bn;
2335: recvsiz = cK * nextbn;
2336: recvbuf = (i & 1) ? abt->buf[0] : abt->buf[1];
2337: PetscCallMPI(MPIU_Isend(sendbuf, sendsiz, MPIU_SCALAR, sendto, abt->tag, comm, &reqs[0]));
2338: PetscCallMPI(MPIU_Irecv(recvbuf, recvsiz, MPIU_SCALAR, recvfrom, abt->tag, comm, &reqs[1]));
2339: }
2341: /* local aseq * sendbuf^T */
2342: PetscCall(PetscBLASIntCast(ranges[recvisfrom + 1] - ranges[recvisfrom], &cn));
2343: if (cm && cn && ck) PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &cm, &cn, &ck, &_DOne, av, &alda, sendbuf, &cn, &_DZero, cv + clda * ranges[recvisfrom], &clda));
2345: if (nextrecvisfrom != rank) {
2346: /* wait for the sends and receives to complete, swap sendbuf and recvbuf */
2347: PetscCallMPI(MPI_Waitall(2, reqs, MPI_STATUSES_IGNORE));
2348: }
2349: bn = nextbn;
2350: recvisfrom = nextrecvisfrom;
2351: sendbuf = recvbuf;
2352: }
2353: PetscCall(MatDenseRestoreArrayRead(a->A, &av));
2354: PetscCall(MatDenseRestoreArrayRead(b->A, &bv));
2355: PetscCall(MatDenseRestoreArrayWrite(c->A, &cv));
2356: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
2357: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2358: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
2359: PetscFunctionReturn(PETSC_SUCCESS);
2360: }
2362: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense_Allgatherv(Mat A, Mat B, Mat C)
2363: {
2364: Mat_MPIDense *a = (Mat_MPIDense *)A->data, *b = (Mat_MPIDense *)B->data, *c = (Mat_MPIDense *)C->data;
2365: MatProductCtx_MatTransMultDense *abt;
2366: MPI_Comm comm;
2367: PetscMPIInt size, ibn;
2368: PetscScalar *cv, *sendbuf, *recvbuf;
2369: const PetscScalar *av, *bv;
2370: PetscInt blda, i, cK = A->cmap->N, k, j, bn;
2371: PetscScalar _DOne = 1.0, _DZero = 0.0;
2372: PetscBLASInt cm, cn, ck, alda, clda;
2374: PetscFunctionBegin;
2375: MatCheckProduct(C, 3);
2376: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2377: abt = (MatProductCtx_MatTransMultDense *)C->product->data;
2378: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
2379: PetscCallMPI(MPI_Comm_size(comm, &size));
2380: PetscCall(MatDenseGetArrayRead(a->A, &av));
2381: PetscCall(MatDenseGetArrayRead(b->A, &bv));
2382: PetscCall(MatDenseGetArrayWrite(c->A, &cv));
2383: PetscCall(MatDenseGetLDA(a->A, &i));
2384: PetscCall(PetscBLASIntCast(i, &alda));
2385: PetscCall(MatDenseGetLDA(b->A, &blda));
2386: PetscCall(MatDenseGetLDA(c->A, &i));
2387: PetscCall(PetscBLASIntCast(i, &clda));
2388: /* copy transpose of B into buf[0] */
2389: bn = B->rmap->n;
2390: sendbuf = abt->buf[0];
2391: recvbuf = abt->buf[1];
2392: for (k = 0, j = 0; j < bn; j++) {
2393: for (i = 0; i < cK; i++, k++) sendbuf[k] = bv[i * blda + j];
2394: }
2395: PetscCall(MatDenseRestoreArrayRead(b->A, &bv));
2396: PetscCall(PetscMPIIntCast(bn * cK, &ibn));
2397: PetscCallMPI(MPI_Allgatherv(sendbuf, ibn, MPIU_SCALAR, recvbuf, abt->recvcounts, abt->recvdispls, MPIU_SCALAR, comm));
2398: PetscCall(PetscBLASIntCast(cK, &ck));
2399: PetscCall(PetscBLASIntCast(c->A->rmap->n, &cm));
2400: PetscCall(PetscBLASIntCast(c->A->cmap->n, &cn));
2401: if (cm && cn && ck) PetscCallBLAS("BLASgemm", BLASgemm_("N", "N", &cm, &cn, &ck, &_DOne, av, &alda, recvbuf, &ck, &_DZero, cv, &clda));
2402: PetscCall(MatDenseRestoreArrayRead(a->A, &av));
2403: PetscCall(MatDenseRestoreArrayRead(b->A, &bv));
2404: PetscCall(MatDenseRestoreArrayWrite(c->A, &cv));
2405: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
2406: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2407: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
2408: PetscFunctionReturn(PETSC_SUCCESS);
2409: }
2411: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense(Mat A, Mat B, Mat C)
2412: {
2413: MatProductCtx_MatTransMultDense *abt;
2415: PetscFunctionBegin;
2416: MatCheckProduct(C, 3);
2417: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2418: abt = (MatProductCtx_MatTransMultDense *)C->product->data;
2419: switch (abt->alg) {
2420: case 1:
2421: PetscCall(MatMatTransposeMultNumeric_MPIDense_MPIDense_Cyclic(A, B, C));
2422: break;
2423: default:
2424: PetscCall(MatMatTransposeMultNumeric_MPIDense_MPIDense_Allgatherv(A, B, C));
2425: break;
2426: }
2427: PetscFunctionReturn(PETSC_SUCCESS);
2428: }
2430: static PetscErrorCode MatProductCtxDestroy_MatMatMult_MPIDense_MPIDense(PetscCtxRt data)
2431: {
2432: MatProductCtx_MatMultDense *ab = *(MatProductCtx_MatMultDense **)data;
2434: PetscFunctionBegin;
2435: PetscCall(MatDestroy(&ab->Ce));
2436: PetscCall(MatDestroy(&ab->Ae));
2437: PetscCall(MatDestroy(&ab->Be));
2438: PetscCall(PetscFree(ab));
2439: PetscFunctionReturn(PETSC_SUCCESS);
2440: }
2442: static PetscErrorCode MatMatMultNumeric_MPIDense_MPIDense(Mat A, Mat B, Mat C)
2443: {
2444: MatProductCtx_MatMultDense *ab;
2445: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
2446: Mat_MPIDense *b = (Mat_MPIDense *)B->data;
2448: PetscFunctionBegin;
2449: MatCheckProduct(C, 3);
2450: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Missing product data");
2451: ab = (MatProductCtx_MatMultDense *)C->product->data;
2452: if (ab->Ae && ab->Ce) {
2453: #if PetscDefined(HAVE_ELEMENTAL)
2454: PetscCall(MatConvert_MPIDense_Elemental(A, MATELEMENTAL, MAT_REUSE_MATRIX, &ab->Ae));
2455: PetscCall(MatConvert_MPIDense_Elemental(B, MATELEMENTAL, MAT_REUSE_MATRIX, &ab->Be));
2456: PetscCall(MatMatMultNumeric_Elemental(ab->Ae, ab->Be, ab->Ce));
2457: PetscCall(MatConvert(ab->Ce, MATMPIDENSE, MAT_REUSE_MATRIX, &C));
2458: #else
2459: SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "PETSC_HAVE_ELEMENTAL not defined");
2460: #endif
2461: } else {
2462: MPI_Comm comm;
2463: const PetscScalar *read;
2464: PetscScalar *write;
2465: PetscInt lda;
2466: const PetscInt *ranges;
2467: PetscMPIInt size;
2469: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A)); /* cannot be done during the symbolic phase because of possible calls to MatProductReplaceMats() */
2470: comm = PetscObjectComm((PetscObject)B);
2471: PetscCallMPI(MPI_Comm_size(comm, &size));
2472: PetscCall(PetscLayoutGetRanges(B->rmap, &ranges));
2473: if (ranges[1] == ranges[size]) {
2474: // optimize for the case where the B matrix is broadcast from rank 0
2475: PetscInt b_lda, be_lda;
2476: Mat b_local = b->A;
2477: Mat b_alloc = NULL;
2478: Mat be_local = ab->Be;
2479: Mat be_alloc = NULL;
2480: PetscMemType b_memtype, be_memtype;
2481: const PetscScalar *b_array = NULL;
2482: MPI_Datatype vector_type;
2483: PetscScalar *be_array = NULL;
2484: PetscMPIInt rank;
2486: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2487: PetscCall(MatDenseGetLDA(be_local, &be_lda));
2488: if (be_lda != B->rmap->N) {
2489: PetscCall(MatDuplicate(be_local, MAT_DO_NOT_COPY_VALUES, &be_alloc));
2490: be_local = be_alloc;
2491: }
2493: if (rank == 0) {
2494: PetscCall(MatDenseGetLDA(b_local, &b_lda));
2495: if (b_lda != B->rmap->N) {
2496: PetscCall(MatDuplicate(b_local, MAT_DO_NOT_COPY_VALUES, &b_alloc));
2497: PetscCall(MatCopy(b_local, b_alloc, DIFFERENT_NONZERO_PATTERN));
2498: b_local = b_alloc;
2499: }
2500: }
2501: vector_type = MPIU_SCALAR;
2502: if (B->cmap->N > 1) {
2503: PetscMPIInt mpi_N;
2505: PetscCall(PetscMPIIntCast(B->cmap->N, &mpi_N));
2506: PetscCallMPI(MPI_Type_contiguous(mpi_N, MPIU_SCALAR, &vector_type));
2507: PetscCallMPI(MPI_Type_commit(&vector_type));
2508: }
2509: PetscCall(MatDenseGetArrayReadAndMemType(b_local, &b_array, &b_memtype));
2510: PetscCall(MatDenseGetArrayWriteAndMemType(be_local, &be_array, &be_memtype));
2511: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, vector_type, b_memtype, b_array, be_memtype, be_array, MPI_REPLACE));
2512: PetscCall(PetscSFBcastEnd(mdn->Mvctx, vector_type, b_array, be_array, MPI_REPLACE));
2513: PetscCall(MatDenseRestoreArrayWriteAndMemType(be_local, &be_array));
2514: PetscCall(MatDenseRestoreArrayReadAndMemType(b_local, &b_array));
2515: if (be_local != ab->Be) PetscCall(MatCopy(be_local, ab->Be, DIFFERENT_NONZERO_PATTERN));
2516: if (B->cmap->N > 1) PetscCallMPI(MPI_Type_free(&vector_type));
2517: PetscCall(MatDestroy(&be_alloc));
2518: PetscCall(MatDestroy(&b_alloc));
2519: } else {
2520: PetscCall(MatDenseGetLDA(B, &lda));
2521: PetscCall(MatDenseGetArrayRead(B, &read));
2522: PetscCall(MatDenseGetArrayWrite(ab->Be, &write));
2523: for (PetscInt i = 0; i < C->cmap->N; ++i) {
2524: PetscCall(PetscSFBcastBegin(mdn->Mvctx, MPIU_SCALAR, read + i * lda, write + i * ab->Be->rmap->n, MPI_REPLACE));
2525: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, read + i * lda, write + i * ab->Be->rmap->n, MPI_REPLACE));
2526: }
2527: PetscCall(MatDenseRestoreArrayWrite(ab->Be, &write));
2528: PetscCall(MatDenseRestoreArrayRead(B, &read));
2529: }
2530: PetscCall(MatMatMultNumeric_SeqDense_SeqDense(((Mat_MPIDense *)A->data)->A, ab->Be, ((Mat_MPIDense *)C->data)->A));
2531: }
2532: PetscFunctionReturn(PETSC_SUCCESS);
2533: }
2535: static PetscErrorCode MatMatMultSymbolic_MPIDense_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2536: {
2537: Mat_Product *product = C->product;
2538: PetscInt alg;
2539: MatProductCtx_MatMultDense *ab;
2540: PetscBool flg;
2542: PetscFunctionBegin;
2543: MatCheckProduct(C, 4);
2544: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
2545: /* check local size of A and B */
2546: PetscCheck(A->cmap->rstart == B->rmap->rstart && A->cmap->rend == B->rmap->rend, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, A (%" PetscInt_FMT ", %" PetscInt_FMT ") != B (%" PetscInt_FMT ", %" PetscInt_FMT ")",
2547: A->rmap->rstart, A->rmap->rend, B->rmap->rstart, B->rmap->rend);
2549: PetscCall(PetscStrcmp(product->alg, "petsc", &flg));
2550: alg = flg ? 0 : 1;
2552: /* setup C */
2553: PetscCall(MatSetSizes(C, A->rmap->n, B->cmap->n, A->rmap->N, B->cmap->N));
2554: PetscCall(MatSetType(C, MATMPIDENSE));
2555: PetscCall(MatSetUp(C));
2557: /* create data structure for reuse Cdense */
2558: PetscCall(PetscNew(&ab));
2560: switch (alg) {
2561: case 1: /* alg: "elemental" */
2562: #if PetscDefined(HAVE_ELEMENTAL)
2563: /* create elemental matrices Ae and Be */
2564: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &ab->Ae));
2565: PetscCall(MatSetSizes(ab->Ae, PETSC_DECIDE, PETSC_DECIDE, A->rmap->N, A->cmap->N));
2566: PetscCall(MatSetType(ab->Ae, MATELEMENTAL));
2567: PetscCall(MatSetUp(ab->Ae));
2568: PetscCall(MatSetOption(ab->Ae, MAT_ROW_ORIENTED, PETSC_FALSE));
2570: PetscCall(MatCreate(PetscObjectComm((PetscObject)B), &ab->Be));
2571: PetscCall(MatSetSizes(ab->Be, PETSC_DECIDE, PETSC_DECIDE, B->rmap->N, B->cmap->N));
2572: PetscCall(MatSetType(ab->Be, MATELEMENTAL));
2573: PetscCall(MatSetUp(ab->Be));
2574: PetscCall(MatSetOption(ab->Be, MAT_ROW_ORIENTED, PETSC_FALSE));
2576: /* compute symbolic Ce = Ae*Be */
2577: PetscCall(MatCreate(PetscObjectComm((PetscObject)C), &ab->Ce));
2578: PetscCall(MatMatMultSymbolic_Elemental(ab->Ae, ab->Be, fill, ab->Ce));
2579: #else
2580: SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "PETSC_HAVE_ELEMENTAL not defined");
2581: #endif
2582: break;
2583: default: /* alg: "petsc" */
2584: ab->Ae = NULL;
2585: PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, A->cmap->N, B->cmap->N, NULL, &ab->Be));
2586: ab->Ce = NULL;
2587: break;
2588: }
2590: C->product->data = ab;
2591: C->product->destroy = MatProductCtxDestroy_MatMatMult_MPIDense_MPIDense;
2592: C->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIDense;
2593: PetscFunctionReturn(PETSC_SUCCESS);
2594: }
2596: static PetscErrorCode MatProductSetFromOptions_MPIDense_AB(Mat C)
2597: {
2598: Mat_Product *product = C->product;
2599: const char *algTypes[2] = {"petsc", "elemental"};
2600: PetscInt alg, nalg = PetscDefined(HAVE_ELEMENTAL) ? 2 : 1;
2601: PetscBool flg = PETSC_FALSE;
2603: PetscFunctionBegin;
2604: /* Set default algorithm */
2605: alg = 0; /* default is PETSc */
2606: PetscCall(PetscStrcmp(product->alg, "default", &flg));
2607: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2609: /* Get runtime option */
2610: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_AB", "Mat");
2611: PetscCall(PetscOptionsEList("-mat_product_algorithm", "Algorithmic approach", "MatProduct_AB", algTypes, nalg, algTypes[alg], &alg, &flg));
2612: PetscOptionsEnd();
2613: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2615: C->ops->matmultsymbolic = MatMatMultSymbolic_MPIDense_MPIDense;
2616: C->ops->productsymbolic = MatProductSymbolic_AB;
2617: PetscFunctionReturn(PETSC_SUCCESS);
2618: }
2620: static PetscErrorCode MatProductSetFromOptions_MPIDense_AtB(Mat C)
2621: {
2622: Mat_Product *product = C->product;
2623: Mat A = product->A, B = product->B;
2625: PetscFunctionBegin;
2626: PetscCheck(A->rmap->rstart == B->rmap->rstart && A->rmap->rend == B->rmap->rend, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrix local dimensions are incompatible, (%" PetscInt_FMT ", %" PetscInt_FMT ") != (%" PetscInt_FMT ",%" PetscInt_FMT ")",
2627: A->rmap->rstart, A->rmap->rend, B->rmap->rstart, B->rmap->rend);
2628: C->ops->transposematmultsymbolic = MatTransposeMatMultSymbolic_MPIDense_MPIDense;
2629: C->ops->productsymbolic = MatProductSymbolic_AtB;
2630: PetscFunctionReturn(PETSC_SUCCESS);
2631: }
2633: static PetscErrorCode MatProductSetFromOptions_MPIDense_ABt(Mat C)
2634: {
2635: Mat_Product *product = C->product;
2636: const char *algTypes[2] = {"allgatherv", "cyclic"};
2637: PetscInt alg, nalg = 2;
2638: PetscBool flg = PETSC_FALSE;
2640: PetscFunctionBegin;
2641: /* Set default algorithm */
2642: alg = 0; /* default is allgatherv */
2643: PetscCall(PetscStrcmp(product->alg, "default", &flg));
2644: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2646: /* Get runtime option */
2647: if (product->api_user) {
2648: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatMatTransposeMult", "Mat");
2649: PetscCall(PetscOptionsEList("-matmattransmult_mpidense_mpidense_via", "Algorithmic approach", "MatMatTransposeMult", algTypes, nalg, algTypes[alg], &alg, &flg));
2650: PetscOptionsEnd();
2651: } else {
2652: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_ABt", "Mat");
2653: PetscCall(PetscOptionsEList("-mat_product_algorithm", "Algorithmic approach", "MatProduct_ABt", algTypes, nalg, algTypes[alg], &alg, &flg));
2654: PetscOptionsEnd();
2655: }
2656: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2658: C->ops->mattransposemultsymbolic = MatMatTransposeMultSymbolic_MPIDense_MPIDense;
2659: C->ops->productsymbolic = MatProductSymbolic_ABt;
2660: PetscFunctionReturn(PETSC_SUCCESS);
2661: }
2663: static PetscErrorCode MatProductSetFromOptions_MPIDense(Mat C)
2664: {
2665: Mat_Product *product = C->product;
2667: PetscFunctionBegin;
2668: switch (product->type) {
2669: case MATPRODUCT_AB:
2670: PetscCall(MatProductSetFromOptions_MPIDense_AB(C));
2671: break;
2672: case MATPRODUCT_AtB:
2673: PetscCall(MatProductSetFromOptions_MPIDense_AtB(C));
2674: break;
2675: case MATPRODUCT_ABt:
2676: PetscCall(MatProductSetFromOptions_MPIDense_ABt(C));
2677: break;
2678: default:
2679: break;
2680: }
2681: PetscFunctionReturn(PETSC_SUCCESS);
2682: }
2684: PetscErrorCode MatDenseScatter_Private(PetscSF sf, Mat X, Mat Y, InsertMode mode, ScatterMode smode)
2685: {
2686: const PetscScalar *in;
2687: PetscScalar *out;
2688: PetscSF vsf;
2689: PetscInt N, ny, rld, lld;
2690: PetscMemType mtype[2];
2691: MPI_Op op = MPI_OP_NULL;
2693: PetscFunctionBegin;
2697: if (mode == INSERT_VALUES) op = MPI_REPLACE;
2698: else if (mode == ADD_VALUES) op = MPIU_SUM;
2699: else if (mode == MAX_VALUES) op = MPIU_MAX;
2700: else if (mode == MIN_VALUES) op = MPIU_MIN;
2701: PetscCheck(op != MPI_OP_NULL, PetscObjectComm((PetscObject)sf), PETSC_ERR_SUP, "Unsupported InsertMode %d in MatDenseScatter_Private()", mode);
2702: PetscCheck(smode == SCATTER_FORWARD || smode == SCATTER_REVERSE, PetscObjectComm((PetscObject)sf), PETSC_ERR_SUP, "Unsupported ScatterMode %d in MatDenseScatter_Private()", smode);
2703: PetscCall(MatGetSize(X, NULL, &N));
2704: PetscCall(MatGetSize(Y, NULL, &ny));
2705: PetscCheck(N == ny, PetscObjectComm((PetscObject)sf), PETSC_ERR_ARG_SIZ, "Matrix column sizes must match: %" PetscInt_FMT " != %" PetscInt_FMT, N, ny);
2706: PetscCall(MatDenseGetLDA(X, &rld));
2707: PetscCall(MatDenseGetLDA(Y, &lld));
2708: /* get cached or create new strided PetscSF when the number of columns is greater than one */
2709: if (N > 1) {
2710: PetscCall(PetscObjectQuery((PetscObject)sf, "_MatDenseScatter_StridedSF", (PetscObject *)&vsf));
2711: if (vsf) {
2712: PetscInt nr[2], nl[2];
2714: PetscCall(PetscSFGetGraph(sf, nr, nl, NULL, NULL));
2715: PetscCall(PetscSFGetGraph(vsf, nr + 1, nl + 1, NULL, NULL));
2716: if (N * nr[0] != nr[1] || N * nl[0] != nl[1]) vsf = NULL;
2717: }
2718: if (!vsf) {
2719: PetscCall(PetscSFCreateStridedSF(sf, N, rld, lld, &vsf));
2720: PetscCall(PetscObjectCompose((PetscObject)sf, "_MatDenseScatter_StridedSF", (PetscObject)vsf));
2721: PetscCall(PetscObjectDereference((PetscObject)vsf));
2722: }
2723: } else vsf = sf;
2724: /* the output array is accessed in read and write mode,
2725: but write-only in the INSERT_VALUES case could be worth exploring */
2726: PetscCall(MatDenseGetArrayReadAndMemType(X, &in, &mtype[0]));
2727: PetscCall(MatDenseGetArrayAndMemType(Y, &out, &mtype[1]));
2728: if (smode == SCATTER_FORWARD) {
2729: PetscCall(PetscSFBcastWithMemTypeBegin(vsf, vsf->vscat.unit, mtype[0], in, mtype[1], out, op));
2730: PetscCall(PetscSFBcastEnd(vsf, vsf->vscat.unit, in, out, op));
2731: } else {
2732: PetscCall(PetscSFReduceWithMemTypeBegin(vsf, vsf->vscat.unit, mtype[0], in, mtype[1], out, op));
2733: PetscCall(PetscSFReduceEnd(vsf, vsf->vscat.unit, in, out, op));
2734: }
2735: PetscCall(MatDenseRestoreArrayAndMemType(Y, &out));
2736: PetscCall(MatDenseRestoreArrayReadAndMemType(X, &in));
2737: PetscFunctionReturn(PETSC_SUCCESS);
2738: }