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: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
53: if (A->ops->copy != B->ops->copy) {
54: PetscCall(MatCopy_Basic(A, B, s));
55: PetscFunctionReturn(PETSC_SUCCESS);
56: }
57: PetscCall(MatCopy(Amat->A, Bmat->A, s));
58: PetscFunctionReturn(PETSC_SUCCESS);
59: }
61: PetscErrorCode MatShift_MPIDense(Mat A, PetscScalar alpha)
62: {
63: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
64: PetscInt j, lda, rstart = A->rmap->rstart, rend = A->rmap->rend, rend2;
65: PetscScalar *v;
67: PetscFunctionBegin;
68: PetscCall(MatDenseGetArray(mat->A, &v));
69: PetscCall(MatDenseGetLDA(mat->A, &lda));
70: rend2 = PetscMin(rend, A->cmap->N);
71: if (rend2 > rstart) {
72: for (j = rstart; j < rend2; j++) v[j - rstart + j * lda] += alpha;
73: PetscCall(PetscLogFlops(rend2 - rstart));
74: }
75: PetscCall(MatDenseRestoreArray(mat->A, &v));
76: PetscFunctionReturn(PETSC_SUCCESS);
77: }
79: static PetscErrorCode MatGetRow_MPIDense(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
80: {
81: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
82: PetscInt lrow, rstart = A->rmap->rstart, rend = A->rmap->rend;
84: PetscFunctionBegin;
85: PetscCheck(row >= rstart && row < rend, PETSC_COMM_SELF, PETSC_ERR_SUP, "only local rows");
86: lrow = row - rstart;
87: PetscCall(MatGetRow(mat->A, lrow, nz, (const PetscInt **)idx, (const PetscScalar **)v));
88: PetscFunctionReturn(PETSC_SUCCESS);
89: }
91: static PetscErrorCode MatRestoreRow_MPIDense(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
92: {
93: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
94: PetscInt lrow, rstart = A->rmap->rstart, rend = A->rmap->rend;
96: PetscFunctionBegin;
97: PetscCheck(row >= rstart && row < rend, PETSC_COMM_SELF, PETSC_ERR_SUP, "only local rows");
98: lrow = row - rstart;
99: PetscCall(MatRestoreRow(mat->A, lrow, nz, (const PetscInt **)idx, (const PetscScalar **)v));
100: PetscFunctionReturn(PETSC_SUCCESS);
101: }
103: static PetscErrorCode MatGetDiagonalBlock_MPIDense(Mat A, Mat *a)
104: {
105: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
106: PetscInt m = A->rmap->n, rstart = A->rmap->rstart;
107: PetscScalar *array;
108: MPI_Comm comm;
109: PetscBool flg;
110: Mat B;
112: PetscFunctionBegin;
113: PetscCall(MatHasCongruentLayouts(A, &flg));
114: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only square matrices supported.");
115: PetscCall(PetscObjectQuery((PetscObject)A, "DiagonalBlock", (PetscObject *)&B));
116: if (!B) { /* This should use MatDenseGetSubMatrix (not create), but we would need a call like MatRestoreDiagonalBlock */
117: #if PetscDefined(HAVE_CUDA)
118: PetscCall(PetscObjectTypeCompare((PetscObject)mdn->A, MATSEQDENSECUDA, &flg));
119: 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);
120: #elif PetscDefined(HAVE_HIP)
121: PetscCall(PetscObjectTypeCompare((PetscObject)mdn->A, MATSEQDENSEHIP, &flg));
122: 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);
123: #endif
124: PetscCall(PetscObjectGetComm((PetscObject)mdn->A, &comm));
125: PetscCall(MatCreate(comm, &B));
126: PetscCall(MatSetSizes(B, m, m, m, m));
127: PetscCall(MatSetType(B, ((PetscObject)mdn->A)->type_name));
128: PetscCall(MatDenseGetArrayRead(mdn->A, (const PetscScalar **)&array));
129: PetscCall(MatSeqDenseSetPreallocation(B, PetscSafePointerPlusOffset(array, m * rstart)));
130: PetscCall(MatDenseRestoreArrayRead(mdn->A, (const PetscScalar **)&array));
131: PetscCall(PetscObjectCompose((PetscObject)A, "DiagonalBlock", (PetscObject)B));
132: *a = B;
133: PetscCall(MatDestroy(&B));
134: } else *a = B;
135: PetscFunctionReturn(PETSC_SUCCESS);
136: }
138: static PetscErrorCode MatSetValues_MPIDense(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar v[], InsertMode addv)
139: {
140: Mat_MPIDense *A = (Mat_MPIDense *)mat->data;
141: PetscInt i, j, rstart = mat->rmap->rstart, rend = mat->rmap->rend, row;
142: PetscBool roworiented = A->roworiented;
144: PetscFunctionBegin;
145: for (i = 0; i < m; i++) {
146: if (idxm[i] < 0) continue;
147: PetscCheck(idxm[i] < mat->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large");
148: if (idxm[i] >= rstart && idxm[i] < rend) {
149: row = idxm[i] - rstart;
150: if (roworiented) {
151: PetscCall(MatSetValues(A->A, 1, &row, n, idxn, PetscSafePointerPlusOffset(v, i * n), addv));
152: } else {
153: for (j = 0; j < n; j++) {
154: if (idxn[j] < 0) continue;
155: PetscCheck(idxn[j] < mat->cmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large");
156: PetscCall(MatSetValues(A->A, 1, &row, 1, &idxn[j], PetscSafePointerPlusOffset(v, i + j * m), addv));
157: }
158: }
159: } else if (!A->donotstash) {
160: mat->assembled = PETSC_FALSE;
161: if (roworiented) {
162: PetscCall(MatStashValuesRow_Private(&mat->stash, idxm[i], n, idxn, PetscSafePointerPlusOffset(v, i * n), PETSC_FALSE));
163: } else {
164: PetscCall(MatStashValuesCol_Private(&mat->stash, idxm[i], n, idxn, PetscSafePointerPlusOffset(v, i), m, PETSC_FALSE));
165: }
166: }
167: }
168: PetscFunctionReturn(PETSC_SUCCESS);
169: }
171: static PetscErrorCode MatGetValues_MPIDense(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], PetscScalar v[])
172: {
173: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
174: PetscInt i, j, rstart = mat->rmap->rstart, rend = mat->rmap->rend, row;
175: PetscBool roworiented = mdn->roworiented;
176: PetscScalar *value;
178: PetscFunctionBegin;
179: for (i = 0; i < m; i++) {
180: if (idxm[i] < 0) continue; /* negative row */
181: PetscCheck(idxm[i] < mat->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large");
182: PetscCheck(idxm[i] >= rstart && idxm[i] < rend, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only local values currently supported");
183: row = idxm[i] - rstart;
184: for (j = 0; j < n; j++) {
185: if (idxn[j] < 0) continue; /* negative column */
186: PetscCheck(idxn[j] < mat->cmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large");
187: value = roworiented ? &v[j + i * n] : &v[i + j * m];
188: PetscCall(MatGetValues(mdn->A, 1, &row, 1, &idxn[j], value));
189: }
190: }
191: PetscFunctionReturn(PETSC_SUCCESS);
192: }
194: static PetscErrorCode MatDenseGetLDA_MPIDense(Mat A, PetscInt *lda)
195: {
196: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
198: PetscFunctionBegin;
199: PetscCall(MatDenseGetLDA(a->A, lda));
200: PetscFunctionReturn(PETSC_SUCCESS);
201: }
203: static PetscErrorCode MatDenseSetLDA_MPIDense(Mat A, PetscInt lda)
204: {
205: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
206: MatType mtype = MATSEQDENSE;
208: PetscFunctionBegin;
209: if (!a->A) {
210: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
211: PetscCall(PetscLayoutSetUp(A->rmap));
212: PetscCall(PetscLayoutSetUp(A->cmap));
213: PetscCall(MatCreate(PETSC_COMM_SELF, &a->A));
214: PetscCall(MatSetSizes(a->A, A->rmap->n, A->cmap->N, A->rmap->n, A->cmap->N));
215: #if PetscDefined(HAVE_CUDA)
216: PetscBool iscuda;
217: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIDENSECUDA, &iscuda));
218: if (iscuda) mtype = MATSEQDENSECUDA;
219: #elif PetscDefined(HAVE_HIP)
220: PetscBool iship;
221: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIDENSEHIP, &iship));
222: if (iship) mtype = MATSEQDENSEHIP;
223: #endif
224: PetscCall(MatSetType(a->A, mtype));
225: }
226: PetscCall(MatDenseSetLDA(a->A, lda));
227: PetscFunctionReturn(PETSC_SUCCESS);
228: }
230: static PetscErrorCode MatDenseGetArray_MPIDense(Mat A, PetscScalar **array)
231: {
232: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
234: PetscFunctionBegin;
235: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
236: PetscCall(MatDenseGetArray(a->A, array));
237: PetscFunctionReturn(PETSC_SUCCESS);
238: }
240: static PetscErrorCode MatDenseGetArrayRead_MPIDense(Mat A, PetscScalar **array)
241: {
242: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
244: PetscFunctionBegin;
245: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
246: PetscCall(MatDenseGetArrayRead(a->A, (const PetscScalar **)array));
247: PetscFunctionReturn(PETSC_SUCCESS);
248: }
250: static PetscErrorCode MatDenseGetArrayWrite_MPIDense(Mat A, PetscScalar **array)
251: {
252: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
254: PetscFunctionBegin;
255: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
256: PetscCall(MatDenseGetArrayWrite(a->A, array));
257: PetscFunctionReturn(PETSC_SUCCESS);
258: }
260: static PetscErrorCode MatDensePlaceArray_MPIDense(Mat A, const PetscScalar *array)
261: {
262: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
264: PetscFunctionBegin;
265: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
266: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
267: PetscCall(MatDensePlaceArray(a->A, array));
268: PetscFunctionReturn(PETSC_SUCCESS);
269: }
271: static PetscErrorCode MatDenseResetArray_MPIDense(Mat A)
272: {
273: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
275: PetscFunctionBegin;
276: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
277: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
278: PetscCall(MatDenseResetArray(a->A));
279: PetscFunctionReturn(PETSC_SUCCESS);
280: }
282: static PetscErrorCode MatDenseReplaceArray_MPIDense(Mat A, const PetscScalar *array)
283: {
284: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
286: PetscFunctionBegin;
287: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
288: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
289: PetscCall(MatDenseReplaceArray(a->A, array));
290: PetscFunctionReturn(PETSC_SUCCESS);
291: }
293: static PetscErrorCode MatCreateSubMatrix_MPIDense(Mat A, IS isrow, IS iscol, MatReuse scall, Mat *B)
294: {
295: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
296: PetscInt lda, i, j, rstart, rend, nrows, ncols, Ncols, nlrows, nlcols;
297: const PetscInt *irow, *icol;
298: const PetscScalar *v;
299: PetscScalar *bv;
300: Mat newmat;
301: IS iscol_local;
302: MPI_Comm comm_is, comm_mat;
304: PetscFunctionBegin;
305: PetscCall(PetscObjectGetComm((PetscObject)A, &comm_mat));
306: PetscCall(PetscObjectGetComm((PetscObject)iscol, &comm_is));
307: PetscCheck(comm_mat == comm_is, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "IS communicator must match matrix communicator");
309: PetscCall(ISAllGather(iscol, &iscol_local));
310: PetscCall(ISGetIndices(isrow, &irow));
311: PetscCall(ISGetIndices(iscol_local, &icol));
312: PetscCall(ISGetLocalSize(isrow, &nrows));
313: PetscCall(ISGetLocalSize(iscol, &ncols));
314: PetscCall(ISGetSize(iscol, &Ncols)); /* global number of columns, size of iscol_local */
316: /* No parallel redistribution currently supported! Should really check each index set
317: to confirm that it is OK. ... Currently supports only submatrix same partitioning as
318: original matrix! */
320: PetscCall(MatGetLocalSize(A, &nlrows, &nlcols));
321: PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
323: /* Check submatrix call */
324: if (scall == MAT_REUSE_MATRIX) {
325: /* SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); */
326: /* Really need to test rows and column sizes! */
327: newmat = *B;
328: } else {
329: /* Create and fill new matrix */
330: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &newmat));
331: PetscCall(MatSetSizes(newmat, nrows, ncols, PETSC_DECIDE, Ncols));
332: PetscCall(MatSetType(newmat, ((PetscObject)A)->type_name));
333: PetscCall(MatMPIDenseSetPreallocation(newmat, NULL));
334: }
336: /* Now extract the data pointers and do the copy, column at a time */
337: PetscCall(MatDenseGetArray(newmat, &bv));
338: PetscCall(MatDenseGetArrayRead(mat->A, &v));
339: PetscCall(MatDenseGetLDA(mat->A, &lda));
340: for (i = 0; i < Ncols; i++) {
341: const PetscScalar *av = v + lda * icol[i];
342: for (j = 0; j < nrows; j++) *bv++ = av[irow[j] - rstart];
343: }
344: PetscCall(MatDenseRestoreArrayRead(mat->A, &v));
345: PetscCall(MatDenseRestoreArray(newmat, &bv));
347: /* Free work space */
348: PetscCall(ISRestoreIndices(isrow, &irow));
349: PetscCall(ISRestoreIndices(iscol_local, &icol));
350: PetscCall(ISDestroy(&iscol_local));
351: *B = newmat;
352: PetscFunctionReturn(PETSC_SUCCESS);
353: }
355: static PetscErrorCode MatDenseRestoreArray_MPIDense(Mat A, PetscScalar **array)
356: {
357: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
359: PetscFunctionBegin;
360: PetscCall(MatDenseRestoreArray(a->A, array));
361: PetscFunctionReturn(PETSC_SUCCESS);
362: }
364: static PetscErrorCode MatDenseRestoreArrayRead_MPIDense(Mat A, PetscScalar **array)
365: {
366: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
368: PetscFunctionBegin;
369: PetscCall(MatDenseRestoreArrayRead(a->A, (const PetscScalar **)array));
370: PetscFunctionReturn(PETSC_SUCCESS);
371: }
373: static PetscErrorCode MatDenseRestoreArrayWrite_MPIDense(Mat A, PetscScalar **array)
374: {
375: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
377: PetscFunctionBegin;
378: PetscCall(MatDenseRestoreArrayWrite(a->A, array));
379: PetscFunctionReturn(PETSC_SUCCESS);
380: }
382: static PetscErrorCode MatAssemblyBegin_MPIDense(Mat mat, MatAssemblyType mode)
383: {
384: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
385: PetscInt nstash, reallocs;
387: PetscFunctionBegin;
388: if (mdn->donotstash || mat->nooffprocentries) PetscFunctionReturn(PETSC_SUCCESS);
390: PetscCall(MatStashScatterBegin_Private(mat, &mat->stash, mat->rmap->range));
391: PetscCall(MatStashGetInfo_Private(&mat->stash, &nstash, &reallocs));
392: PetscCall(PetscInfo(mdn->A, "Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
393: PetscFunctionReturn(PETSC_SUCCESS);
394: }
396: static PetscErrorCode MatAssemblyEnd_MPIDense(Mat mat, MatAssemblyType mode)
397: {
398: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
399: PetscInt i, *row, *col, flg, j, rstart, ncols;
400: PetscMPIInt n;
401: PetscScalar *val;
403: PetscFunctionBegin;
404: if (!mdn->donotstash && !mat->nooffprocentries) {
405: /* wait on receives */
406: while (1) {
407: PetscCall(MatStashScatterGetMesg_Private(&mat->stash, &n, &row, &col, &val, &flg));
408: if (!flg) break;
410: for (i = 0; i < n;) {
411: /* Now identify the consecutive vals belonging to the same row */
412: for (j = i, rstart = row[j]; j < n; j++) {
413: if (row[j] != rstart) break;
414: }
415: if (j < n) ncols = j - i;
416: else ncols = n - i;
417: /* Now assemble all these values with a single function call */
418: PetscCall(MatSetValues_MPIDense(mat, 1, row + i, ncols, col + i, val + i, mat->insertmode));
419: i = j;
420: }
421: }
422: PetscCall(MatStashScatterEnd_Private(&mat->stash));
423: }
425: PetscCall(MatAssemblyBegin(mdn->A, mode));
426: PetscCall(MatAssemblyEnd(mdn->A, mode));
427: PetscFunctionReturn(PETSC_SUCCESS);
428: }
430: static PetscErrorCode MatZeroEntries_MPIDense(Mat A)
431: {
432: Mat_MPIDense *l = (Mat_MPIDense *)A->data;
434: PetscFunctionBegin;
435: PetscCall(MatZeroEntries(l->A));
436: PetscFunctionReturn(PETSC_SUCCESS);
437: }
439: static PetscErrorCode MatSetInf_MPIDense(Mat A)
440: {
441: Mat_MPIDense *l = (Mat_MPIDense *)A->data;
443: PetscFunctionBegin;
444: PetscCall(MatFlag(l->A, 1));
445: PetscFunctionReturn(PETSC_SUCCESS);
446: }
448: static PetscErrorCode MatZeroRows_MPIDense(Mat A, PetscInt n, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
449: {
450: Mat_MPIDense *l = (Mat_MPIDense *)A->data;
451: PetscInt i, len, *lrows;
453: PetscFunctionBegin;
454: /* get locally owned rows */
455: PetscCall(PetscLayoutMapLocal(A->rmap, n, rows, &len, &lrows, NULL));
456: /* fix right-hand side if needed */
457: if (x && b) {
458: const PetscScalar *xx;
459: PetscScalar *bb;
461: PetscCall(VecGetArrayRead(x, &xx));
462: PetscCall(VecGetArrayWrite(b, &bb));
463: for (i = 0; i < len; ++i) bb[lrows[i]] = diag * xx[lrows[i]];
464: PetscCall(VecRestoreArrayRead(x, &xx));
465: PetscCall(VecRestoreArrayWrite(b, &bb));
466: }
467: PetscCall(MatZeroRows(l->A, len, lrows, 0.0, NULL, NULL));
468: if (diag != 0.0) {
469: Vec d;
471: PetscCall(MatCreateVecs(A, NULL, &d));
472: PetscCall(VecSet(d, diag));
473: PetscCall(MatDiagonalSet(A, d, INSERT_VALUES));
474: PetscCall(VecDestroy(&d));
475: }
476: PetscCall(PetscFree(lrows));
477: PetscFunctionReturn(PETSC_SUCCESS);
478: }
480: PETSC_INTERN PetscErrorCode MatMult_SeqDense(Mat, Vec, Vec);
481: PETSC_INTERN PetscErrorCode MatMultAdd_SeqDense(Mat, Vec, Vec, Vec);
482: PETSC_INTERN PetscErrorCode MatMultTranspose_SeqDense(Mat, Vec, Vec);
483: PETSC_INTERN PetscErrorCode MatMultTransposeAdd_SeqDense(Mat, Vec, Vec, Vec);
485: static PetscErrorCode MatMultColumnRange_MPIDense(Mat mat, Vec xx, Vec yy, PetscInt c_start, PetscInt c_end)
486: {
487: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
488: const PetscScalar *ax;
489: PetscScalar *ay;
490: PetscMemType axmtype, aymtype;
492: PetscFunctionBegin;
493: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(mat));
494: PetscCall(VecGetArrayReadAndMemType(xx, &ax, &axmtype));
495: PetscCall(VecGetArrayWriteAndMemType(mdn->lvec, &ay, &aymtype));
496: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPI_REPLACE));
497: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ax, ay, MPI_REPLACE));
498: PetscCall(VecRestoreArrayWriteAndMemType(mdn->lvec, &ay));
499: PetscCall(VecRestoreArrayReadAndMemType(xx, &ax));
500: PetscUseMethod(mdn->A, "MatMultColumnRange_C", (Mat, Vec, Vec, PetscInt, PetscInt), (mdn->A, mdn->lvec, yy, c_start, c_end));
501: PetscFunctionReturn(PETSC_SUCCESS);
502: }
504: static PetscErrorCode MatMult_MPIDense(Mat mat, Vec xx, Vec yy)
505: {
506: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
507: const PetscScalar *ax;
508: PetscScalar *ay;
509: PetscMemType axmtype, aymtype;
511: PetscFunctionBegin;
512: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(mat));
513: PetscCall(VecGetArrayReadAndMemType(xx, &ax, &axmtype));
514: PetscCall(VecGetArrayWriteAndMemType(mdn->lvec, &ay, &aymtype));
515: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPI_REPLACE));
516: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ax, ay, MPI_REPLACE));
517: PetscCall(VecRestoreArrayWriteAndMemType(mdn->lvec, &ay));
518: PetscCall(VecRestoreArrayReadAndMemType(xx, &ax));
519: PetscUseTypeMethod(mdn->A, mult, mdn->lvec, yy);
520: PetscFunctionReturn(PETSC_SUCCESS);
521: }
523: static PetscErrorCode MatGetMultPetscSF_MPIDense(Mat A, PetscSF *sf)
524: {
525: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
527: PetscFunctionBegin;
528: *sf = mdn->Mvctx;
529: PetscFunctionReturn(PETSC_SUCCESS);
530: }
532: static PetscErrorCode MatMultAddColumnRange_MPIDense(Mat mat, Vec xx, Vec yy, Vec zz, PetscInt c_start, PetscInt c_end)
533: {
534: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
535: const PetscScalar *ax;
536: PetscScalar *ay;
537: PetscMemType axmtype, aymtype;
539: PetscFunctionBegin;
540: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(mat));
541: PetscCall(VecGetArrayReadAndMemType(xx, &ax, &axmtype));
542: PetscCall(VecGetArrayAndMemType(mdn->lvec, &ay, &aymtype));
543: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPI_REPLACE));
544: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ax, ay, MPI_REPLACE));
545: PetscCall(VecRestoreArrayAndMemType(mdn->lvec, &ay));
546: PetscCall(VecRestoreArrayReadAndMemType(xx, &ax));
547: PetscUseMethod(mdn->A, "MatMultAddColumnRange_C", (Mat, Vec, Vec, Vec, PetscInt, PetscInt), (mdn->A, mdn->lvec, yy, zz, c_start, c_end));
548: PetscFunctionReturn(PETSC_SUCCESS);
549: }
551: static PetscErrorCode MatMultAdd_MPIDense(Mat mat, Vec xx, Vec yy, Vec zz)
552: {
553: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
554: const PetscScalar *ax;
555: PetscScalar *ay;
556: PetscMemType axmtype, aymtype;
558: PetscFunctionBegin;
559: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(mat));
560: PetscCall(VecGetArrayReadAndMemType(xx, &ax, &axmtype));
561: PetscCall(VecGetArrayAndMemType(mdn->lvec, &ay, &aymtype));
562: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPI_REPLACE));
563: PetscCall(PetscSFBcastEnd(mdn->Mvctx, MPIU_SCALAR, ax, ay, MPI_REPLACE));
564: PetscCall(VecRestoreArrayAndMemType(mdn->lvec, &ay));
565: PetscCall(VecRestoreArrayReadAndMemType(xx, &ax));
566: PetscUseTypeMethod(mdn->A, multadd, mdn->lvec, yy, zz);
567: PetscFunctionReturn(PETSC_SUCCESS);
568: }
570: static PetscErrorCode MatMultHermitianTransposeColumnRange_MPIDense(Mat A, Vec xx, Vec yy, PetscInt c_start, PetscInt c_end)
571: {
572: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
573: const PetscScalar *ax;
574: PetscScalar *ay;
575: PetscMemType axmtype, aymtype;
576: PetscInt r_start, r_end;
577: PetscInt c_start_local, c_end_local;
579: PetscFunctionBegin;
580: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
581: PetscCall(VecZeroEntries(a->lvec));
582: PetscCall(VecGetOwnershipRange(yy, &r_start, &r_end));
583: c_start_local = PetscMax(c_start, r_start);
584: c_end_local = PetscMin(c_end, r_end);
585: PetscCall(VecGetArrayAndMemType(yy, &ay, &aymtype));
586: if (c_end_local > c_start_local) {
587: if (PetscMemTypeHost(aymtype)) {
588: PetscCall(PetscArrayzero(&ay[c_start_local], (size_t)(c_end_local - c_start_local)));
589: } else {
590: PetscDeviceContext dctx;
592: PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
593: PetscCall(PetscDeviceRegisterMemory(ay, aymtype, sizeof(*ay) * ((size_t)(r_end - r_start))));
594: PetscCall(PetscDeviceArrayZero(dctx, &ay[c_start_local], (size_t)(c_end_local - c_start_local)));
595: }
596: }
597: PetscUseMethod(a->A, "MatMultHermitianTransposeColumnRange_C", (Mat, Vec, Vec, PetscInt, PetscInt), (a->A, xx, a->lvec, c_start, c_end));
598: PetscCall(VecGetArrayReadAndMemType(a->lvec, &ax, &axmtype));
599: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPIU_SUM));
600: PetscCall(PetscSFReduceEnd(a->Mvctx, MPIU_SCALAR, ax, ay, MPIU_SUM));
601: PetscCall(VecRestoreArrayReadAndMemType(a->lvec, &ax));
602: PetscCall(VecRestoreArrayAndMemType(yy, &ay));
603: PetscFunctionReturn(PETSC_SUCCESS);
604: }
606: static PetscErrorCode MatMultTransposeKernel_MPIDense(Mat A, Vec xx, Vec yy, PetscBool herm)
607: {
608: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
609: const PetscScalar *ax;
610: PetscScalar *ay;
611: PetscMemType axmtype, aymtype;
613: PetscFunctionBegin;
614: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
615: PetscCall(VecSet(yy, 0.0));
616: if (herm) PetscUseTypeMethod(a->A, multhermitiantranspose, xx, a->lvec);
617: else PetscUseTypeMethod(a->A, multtranspose, xx, a->lvec);
618: PetscCall(VecGetArrayReadAndMemType(a->lvec, &ax, &axmtype));
619: PetscCall(VecGetArrayAndMemType(yy, &ay, &aymtype));
620: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPIU_SUM));
621: PetscCall(PetscSFReduceEnd(a->Mvctx, MPIU_SCALAR, ax, ay, MPIU_SUM));
622: PetscCall(VecRestoreArrayReadAndMemType(a->lvec, &ax));
623: PetscCall(VecRestoreArrayAndMemType(yy, &ay));
624: PetscFunctionReturn(PETSC_SUCCESS);
625: }
627: static PetscErrorCode MatMultHermitianTransposeAddColumnRange_MPIDense(Mat A, Vec xx, Vec yy, Vec zz, PetscInt c_start, PetscInt c_end)
628: {
629: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
630: const PetscScalar *ax;
631: PetscScalar *ay;
632: PetscMemType axmtype, aymtype;
634: PetscFunctionBegin;
635: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
636: PetscCall(VecCopy(yy, zz));
637: PetscCall(VecZeroEntries(a->lvec));
638: PetscUseMethod(a->A, "MatMultHermitianTransposeColumnRange_C", (Mat, Vec, Vec, PetscInt, PetscInt), (a->A, xx, a->lvec, c_start, c_end));
639: PetscCall(VecGetArrayReadAndMemType(a->lvec, &ax, &axmtype));
640: PetscCall(VecGetArrayAndMemType(zz, &ay, &aymtype));
641: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPIU_SUM));
642: PetscCall(PetscSFReduceEnd(a->Mvctx, MPIU_SCALAR, ax, ay, MPIU_SUM));
643: PetscCall(VecRestoreArrayReadAndMemType(a->lvec, &ax));
644: PetscCall(VecRestoreArrayAndMemType(zz, &ay));
645: PetscFunctionReturn(PETSC_SUCCESS);
646: }
648: static PetscErrorCode MatMultTransposeAddKernel_MPIDense(Mat A, Vec xx, Vec yy, Vec zz, PetscBool herm)
649: {
650: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
651: const PetscScalar *ax;
652: PetscScalar *ay;
653: PetscMemType axmtype, aymtype;
655: PetscFunctionBegin;
656: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
657: PetscCall(VecCopy(yy, zz));
658: if (herm) PetscUseTypeMethod(a->A, multhermitiantranspose, xx, a->lvec);
659: else PetscUseTypeMethod(a->A, multtranspose, xx, a->lvec);
660: PetscCall(VecGetArrayReadAndMemType(a->lvec, &ax, &axmtype));
661: PetscCall(VecGetArrayAndMemType(zz, &ay, &aymtype));
662: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, MPIU_SCALAR, axmtype, ax, aymtype, ay, MPIU_SUM));
663: PetscCall(PetscSFReduceEnd(a->Mvctx, MPIU_SCALAR, ax, ay, MPIU_SUM));
664: PetscCall(VecRestoreArrayReadAndMemType(a->lvec, &ax));
665: PetscCall(VecRestoreArrayAndMemType(zz, &ay));
666: PetscFunctionReturn(PETSC_SUCCESS);
667: }
669: static PetscErrorCode MatMultTranspose_MPIDense(Mat A, Vec xx, Vec yy)
670: {
671: PetscFunctionBegin;
672: PetscCall(MatMultTransposeKernel_MPIDense(A, xx, yy, PETSC_FALSE));
673: PetscFunctionReturn(PETSC_SUCCESS);
674: }
676: static PetscErrorCode MatMultTransposeAdd_MPIDense(Mat A, Vec xx, Vec yy, Vec zz)
677: {
678: PetscFunctionBegin;
679: PetscCall(MatMultTransposeAddKernel_MPIDense(A, xx, yy, zz, PETSC_FALSE));
680: PetscFunctionReturn(PETSC_SUCCESS);
681: }
683: static PetscErrorCode MatMultHermitianTranspose_MPIDense(Mat A, Vec xx, Vec yy)
684: {
685: PetscFunctionBegin;
686: PetscCall(MatMultTransposeKernel_MPIDense(A, xx, yy, PETSC_TRUE));
687: PetscFunctionReturn(PETSC_SUCCESS);
688: }
690: static PetscErrorCode MatMultHermitianTransposeAdd_MPIDense(Mat A, Vec xx, Vec yy, Vec zz)
691: {
692: PetscFunctionBegin;
693: PetscCall(MatMultTransposeAddKernel_MPIDense(A, xx, yy, zz, PETSC_TRUE));
694: PetscFunctionReturn(PETSC_SUCCESS);
695: }
697: PetscErrorCode MatGetDiagonal_MPIDense(Mat A, Vec v)
698: {
699: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
700: PetscInt lda, len, i, nl, ng, m = A->rmap->n, radd;
701: PetscScalar *x;
702: const PetscScalar *av;
704: PetscFunctionBegin;
705: PetscCall(VecGetArray(v, &x));
706: PetscCall(VecGetSize(v, &ng));
707: PetscCheck(ng == A->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming mat and vec");
708: PetscCall(VecGetLocalSize(v, &nl));
709: len = PetscMin(a->A->rmap->n, a->A->cmap->n);
710: radd = A->rmap->rstart * m;
711: PetscCall(MatDenseGetArrayRead(a->A, &av));
712: PetscCall(MatDenseGetLDA(a->A, &lda));
713: for (i = 0; i < len; i++) x[i] = av[radd + i * lda + i];
714: PetscCall(MatDenseRestoreArrayRead(a->A, &av));
715: if (nl - i > 0) PetscCall(PetscArrayzero(x + i, nl - i));
716: PetscCall(VecRestoreArray(v, &x));
717: PetscFunctionReturn(PETSC_SUCCESS);
718: }
720: static PetscErrorCode MatDestroy_MPIDense(Mat mat)
721: {
722: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
724: PetscFunctionBegin;
725: PetscCall(PetscLogObjectState((PetscObject)mat, "Rows=%" PetscInt_FMT ", Cols=%" PetscInt_FMT, mat->rmap->N, mat->cmap->N));
726: PetscCall(MatStashDestroy_Private(&mat->stash));
727: PetscCheck(!mdn->vecinuse, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
728: PetscCheck(!mdn->matinuse, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
729: PetscCall(MatDestroy(&mdn->A));
730: PetscCall(VecDestroy(&mdn->lvec));
731: PetscCall(PetscSFDestroy(&mdn->Mvctx));
732: PetscCall(VecDestroy(&mdn->cvec));
733: PetscCall(MatDestroy(&mdn->cmat));
735: PetscCall(PetscFree(mat->data));
736: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, NULL));
738: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetLDA_C", NULL));
739: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseSetLDA_C", NULL));
740: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArray_C", NULL));
741: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArray_C", NULL));
742: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArrayRead_C", NULL));
743: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArrayRead_C", NULL));
744: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArrayWrite_C", NULL));
745: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArrayWrite_C", NULL));
746: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDensePlaceArray_C", NULL));
747: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseResetArray_C", NULL));
748: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseReplaceArray_C", NULL));
749: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpiaij_mpidense_C", NULL));
750: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpiaij_C", NULL));
751: #if PetscDefined(HAVE_ELEMENTAL)
752: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_elemental_C", NULL));
753: #endif
754: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
755: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_scalapack_C", NULL));
756: #endif
757: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPIDenseSetPreallocation_C", NULL));
758: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaij_mpidense_C", NULL));
759: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaij_C", NULL));
760: #if PetscDefined(HAVE_CUDA)
761: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijcusparse_mpidense_C", NULL));
762: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaijcusparse_C", NULL));
763: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpidensecuda_C", NULL));
764: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidensecuda_mpidense_C", NULL));
765: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaij_mpidensecuda_C", NULL));
766: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijcusparse_mpidensecuda_C", NULL));
767: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidensecuda_mpiaij_C", NULL));
768: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidensecuda_mpiaijcusparse_C", NULL));
769: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAGetArray_C", NULL));
770: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAGetArrayRead_C", NULL));
771: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAGetArrayWrite_C", NULL));
772: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDARestoreArray_C", NULL));
773: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDARestoreArrayRead_C", NULL));
774: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDARestoreArrayWrite_C", NULL));
775: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAPlaceArray_C", NULL));
776: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAResetArray_C", NULL));
777: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDAReplaceArray_C", NULL));
778: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseCUDASetPreallocation_C", NULL));
779: #endif
780: #if PetscDefined(HAVE_HIP)
781: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijhipsparse_mpidense_C", NULL));
782: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidense_mpiaijhipsparse_C", NULL));
783: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_mpidensehip_C", NULL));
784: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidensehip_mpidense_C", NULL));
785: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaij_mpidensehip_C", NULL));
786: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpiaijhipsparse_mpidensehip_C", NULL));
787: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidensehip_mpiaij_C", NULL));
788: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpidensehip_mpiaijhipsparse_C", NULL));
789: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPGetArray_C", NULL));
790: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPGetArrayRead_C", NULL));
791: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPGetArrayWrite_C", NULL));
792: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPRestoreArray_C", NULL));
793: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPRestoreArrayRead_C", NULL));
794: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPRestoreArrayWrite_C", NULL));
795: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPPlaceArray_C", NULL));
796: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPResetArray_C", NULL));
797: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPReplaceArray_C", NULL));
798: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseHIPSetPreallocation_C", NULL));
799: #endif
800: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumn_C", NULL));
801: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumn_C", NULL));
802: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVec_C", NULL));
803: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVec_C", NULL));
804: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVecRead_C", NULL));
805: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVecRead_C", NULL));
806: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVecWrite_C", NULL));
807: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVecWrite_C", NULL));
808: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetSubMatrix_C", NULL));
809: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreSubMatrix_C", NULL));
810: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultColumnRange_C", NULL));
811: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultAddColumnRange_C", NULL));
812: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultHermitianTransposeColumnRange_C", NULL));
813: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultHermitianTransposeAddColumnRange_C", NULL));
814: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatGetMultPetscSF_C", NULL));
815: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseUpdateColumnLayout_C", NULL));
817: PetscCall(PetscObjectCompose((PetscObject)mat, "DiagonalBlock", NULL));
818: PetscFunctionReturn(PETSC_SUCCESS);
819: }
821: #include <petscdraw.h>
822: static PetscErrorCode MatView_MPIDense_ASCIIorDraworSocket(Mat mat, PetscViewer viewer)
823: {
824: Mat_MPIDense *mdn = (Mat_MPIDense *)mat->data;
825: PetscMPIInt rank;
826: PetscViewerType vtype;
827: PetscBool isascii, isdraw;
828: PetscViewer sviewer;
829: PetscViewerFormat format;
831: PetscFunctionBegin;
832: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)mat), &rank));
833: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
834: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
835: if (isascii) {
836: PetscCall(PetscViewerGetType(viewer, &vtype));
837: PetscCall(PetscViewerGetFormat(viewer, &format));
838: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
839: MatInfo info;
840: PetscCall(MatGetInfo(mat, MAT_LOCAL, &info));
841: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
842: 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,
843: (PetscInt)info.memory));
844: PetscCall(PetscViewerFlush(viewer));
845: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
846: if (mdn->Mvctx) PetscCall(PetscSFView(mdn->Mvctx, viewer));
847: PetscFunctionReturn(PETSC_SUCCESS);
848: } else if (format == PETSC_VIEWER_ASCII_INFO) {
849: PetscFunctionReturn(PETSC_SUCCESS);
850: }
851: } else if (isdraw) {
852: PetscDraw draw;
853: PetscBool isnull;
855: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
856: PetscCall(PetscDrawIsNull(draw, &isnull));
857: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
858: }
860: {
861: /* assemble the entire matrix onto first processor. */
862: Mat A;
863: PetscInt M = mat->rmap->N, N = mat->cmap->N, m, row, i, nz;
864: PetscInt *cols;
865: PetscScalar *vals;
867: PetscCall(MatCreate(PetscObjectComm((PetscObject)mat), &A));
868: if (rank == 0) {
869: PetscCall(MatSetSizes(A, M, N, M, N));
870: } else {
871: PetscCall(MatSetSizes(A, 0, 0, M, N));
872: }
873: /* Since this is a temporary matrix, MATMPIDENSE instead of ((PetscObject)A)->type_name here is probably acceptable. */
874: PetscCall(MatSetType(A, MATMPIDENSE));
875: PetscCall(MatMPIDenseSetPreallocation(A, NULL));
877: /* Copy the matrix ... This isn't the most efficient means,
878: but it's quick for now */
879: A->insertmode = INSERT_VALUES;
881: row = mat->rmap->rstart;
882: m = mdn->A->rmap->n;
883: for (i = 0; i < m; i++) {
884: PetscCall(MatGetRow_MPIDense(mat, row, &nz, &cols, &vals));
885: PetscCall(MatSetValues_MPIDense(A, 1, &row, nz, cols, vals, INSERT_VALUES));
886: PetscCall(MatRestoreRow_MPIDense(mat, row, &nz, &cols, &vals));
887: row++;
888: }
890: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
891: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
892: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
893: if (rank == 0) {
894: PetscCall(PetscObjectSetName((PetscObject)((Mat_MPIDense *)A->data)->A, ((PetscObject)mat)->name));
895: PetscCall(MatView_SeqDense(((Mat_MPIDense *)A->data)->A, sviewer));
896: }
897: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
898: PetscCall(MatDestroy(&A));
899: }
900: PetscFunctionReturn(PETSC_SUCCESS);
901: }
903: static PetscErrorCode MatView_MPIDense(Mat mat, PetscViewer viewer)
904: {
905: PetscBool isascii, isbinary, isdraw, issocket;
907: PetscFunctionBegin;
908: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
909: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
910: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSOCKET, &issocket));
911: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
913: if (isascii || issocket || isdraw) PetscCall(MatView_MPIDense_ASCIIorDraworSocket(mat, viewer));
914: else if (isbinary) PetscCall(MatView_Dense_Binary(mat, viewer));
915: PetscFunctionReturn(PETSC_SUCCESS);
916: }
918: static PetscErrorCode MatGetInfo_MPIDense(Mat A, MatInfoType flag, MatInfo *info)
919: {
920: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
921: Mat mdn = mat->A;
922: PetscLogDouble irecv[5];
924: PetscFunctionBegin;
925: info->block_size = 1.0;
927: PetscCall(MatGetInfo(mdn, MAT_LOCAL, info));
929: irecv[0] = info->nz_used;
930: irecv[1] = info->nz_allocated;
931: irecv[2] = info->nz_unneeded;
932: irecv[3] = info->memory;
933: irecv[4] = info->mallocs;
934: if (flag == MAT_LOCAL) {
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: } else if (flag == MAT_GLOBAL_MAX) {
941: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_MAX, PetscObjectComm((PetscObject)A)));
943: info->nz_used = irecv[0];
944: info->nz_allocated = irecv[1];
945: info->nz_unneeded = irecv[2];
946: info->memory = irecv[3];
947: info->mallocs = irecv[4];
948: } else if (flag == MAT_GLOBAL_SUM) {
949: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_SUM, PetscObjectComm((PetscObject)A)));
951: info->nz_used = irecv[0];
952: info->nz_allocated = irecv[1];
953: info->nz_unneeded = irecv[2];
954: info->memory = irecv[3];
955: info->mallocs = irecv[4];
956: }
957: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
958: info->fill_ratio_needed = 0;
959: info->factor_mallocs = 0;
960: PetscFunctionReturn(PETSC_SUCCESS);
961: }
963: static PetscErrorCode MatSetOption_MPIDense(Mat A, MatOption op, PetscBool flg)
964: {
965: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
967: PetscFunctionBegin;
968: switch (op) {
969: case MAT_NEW_NONZERO_LOCATIONS:
970: case MAT_NEW_NONZERO_LOCATION_ERR:
971: case MAT_NEW_NONZERO_ALLOCATION_ERR:
972: MatCheckPreallocated(A, 1);
973: PetscCall(MatSetOption(a->A, op, flg));
974: break;
975: case MAT_ROW_ORIENTED:
976: MatCheckPreallocated(A, 1);
977: a->roworiented = flg;
978: PetscCall(MatSetOption(a->A, op, flg));
979: break;
980: case MAT_IGNORE_OFF_PROC_ENTRIES:
981: a->donotstash = flg;
982: break;
983: case MAT_SYMMETRIC:
984: case MAT_STRUCTURALLY_SYMMETRIC:
985: case MAT_HERMITIAN:
986: case MAT_SYMMETRY_ETERNAL:
987: case MAT_STRUCTURAL_SYMMETRY_ETERNAL:
988: case MAT_SPD:
989: case MAT_SPD_ETERNAL:
990: /* if the diagonal matrix is square it inherits some of the properties above */
991: if (a->A && A->rmap->n == A->cmap->n) PetscCall(MatSetOption(a->A, op, flg));
992: break;
993: default:
994: break;
995: }
996: PetscFunctionReturn(PETSC_SUCCESS);
997: }
999: static PetscErrorCode MatDiagonalScale_MPIDense(Mat A, Vec ll, Vec rr)
1000: {
1001: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
1002: PetscInt s1, s2, s3;
1004: PetscFunctionBegin;
1005: PetscCall(MatGetLocalSize(A, &s2, &s3));
1006: if (ll) {
1007: PetscCall(VecGetLocalSize(ll, &s1));
1008: PetscCheck(s1 == s2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left scaling vector non-conforming local size, %" PetscInt_FMT " != %" PetscInt_FMT, s1, s2);
1009: }
1010: if (rr) {
1011: PetscCall(VecGetLocalSize(rr, &s1));
1012: PetscCheck(s1 == s3, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Right scaling vector non-conforming local size, %" PetscInt_FMT " != %" PetscInt_FMT, s1, s3);
1013: /* gather the right scaling into the local column layout, staying on the device when the Vecs live there */
1014: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
1015: PetscCall(VecScatterBegin(mdn->Mvctx, rr, mdn->lvec, INSERT_VALUES, SCATTER_FORWARD));
1016: PetscCall(VecScatterEnd(mdn->Mvctx, rr, mdn->lvec, INSERT_VALUES, SCATTER_FORWARD));
1017: }
1018: /* the local matrix holds exactly the local rows and all the columns, so the local part of ll applies to it directly;
1019: the operation is called rather than MatDiagonalScale() because ll is the parallel Vec and the interface checks that
1020: it shares the communicator of the sequential matrix, as in MatDiagonalScale_MPIAIJ() */
1021: PetscUseTypeMethod(mdn->A, diagonalscale, ll, rr ? mdn->lvec : NULL);
1022: PetscCall(PetscObjectStateIncrease((PetscObject)mdn->A));
1023: PetscFunctionReturn(PETSC_SUCCESS);
1024: }
1026: static PetscErrorCode MatNorm_MPIDense(Mat A, NormType type, PetscReal *nrm)
1027: {
1028: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
1029: PetscInt i, j, lda;
1030: PetscMPIInt size;
1031: const PetscScalar *av;
1033: PetscFunctionBegin;
1034: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
1035: if (size == 1) {
1036: PetscCall(MatNorm(mdn->A, type, nrm));
1037: } else {
1038: if (type == NORM_FROBENIUS) {
1039: PetscCall(MatNorm(mdn->A, NORM_FROBENIUS, nrm));
1040: *nrm *= *nrm;
1041: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, nrm, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)A)));
1042: *nrm = PetscSqrtReal(*nrm);
1043: } else if (type == NORM_1) {
1044: PetscReal *tmp;
1046: PetscCall(PetscCalloc1(A->cmap->N, &tmp));
1047: *nrm = 0.0;
1048: PetscCall(MatDenseGetArrayRead(mdn->A, &av));
1049: PetscCall(MatDenseGetLDA(mdn->A, &lda));
1050: for (j = 0; j < mdn->A->cmap->n; j++) {
1051: for (i = 0; i < mdn->A->rmap->n; i++) tmp[j] += PetscAbsScalar(av[i + j * lda]);
1052: }
1053: PetscCall(MatDenseRestoreArrayRead(mdn->A, &av));
1054: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, tmp, A->cmap->N, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)A)));
1055: for (j = 0; j < A->cmap->N; j++) {
1056: if (tmp[j] > *nrm) *nrm = tmp[j];
1057: }
1058: PetscCall(PetscFree(tmp));
1059: PetscCall(PetscLogFlops(A->cmap->n * A->rmap->n));
1060: } else if (type == NORM_INFINITY) { /* max row norm */
1061: PetscCall(MatNorm(mdn->A, type, nrm));
1062: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, nrm, 1, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)A)));
1063: } else SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Unsupported norm type %s", NormTypes[type]);
1064: }
1065: PetscFunctionReturn(PETSC_SUCCESS);
1066: }
1068: static PetscErrorCode MatTranspose_MPIDense(Mat A, MatReuse reuse, Mat *matout)
1069: {
1070: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1071: Mat B;
1072: PetscInt M = A->rmap->N, N = A->cmap->N, m, n, *rwork, rstart = A->rmap->rstart;
1073: PetscInt j, i, lda;
1074: PetscScalar *v;
1076: PetscFunctionBegin;
1077: if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *matout));
1078: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
1079: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
1080: PetscCall(MatSetSizes(B, A->cmap->n, A->rmap->n, N, M));
1081: PetscCall(MatSetType(B, ((PetscObject)A)->type_name));
1082: PetscCall(MatMPIDenseSetPreallocation(B, NULL));
1083: } else B = *matout;
1085: m = a->A->rmap->n;
1086: n = a->A->cmap->n;
1087: PetscCall(MatDenseGetArrayRead(a->A, (const PetscScalar **)&v));
1088: PetscCall(MatDenseGetLDA(a->A, &lda));
1089: PetscCall(PetscMalloc1(m, &rwork));
1090: for (i = 0; i < m; i++) rwork[i] = rstart + i;
1091: for (j = 0; j < n; j++) {
1092: PetscCall(MatSetValues(B, 1, &j, m, rwork, v, INSERT_VALUES));
1093: v = PetscSafePointerPlusOffset(v, lda);
1094: }
1095: PetscCall(MatDenseRestoreArrayRead(a->A, (const PetscScalar **)&v));
1096: PetscCall(PetscFree(rwork));
1097: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
1098: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
1099: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
1100: *matout = B;
1101: } else {
1102: PetscCall(MatHeaderMerge(A, &B));
1103: }
1104: PetscFunctionReturn(PETSC_SUCCESS);
1105: }
1107: static PetscErrorCode MatDuplicate_MPIDense(Mat, MatDuplicateOption, Mat *);
1108: PETSC_INTERN PetscErrorCode MatScale_MPIDense(Mat, PetscScalar);
1110: static PetscErrorCode MatSetUp_MPIDense(Mat A)
1111: {
1112: PetscFunctionBegin;
1113: PetscCall(PetscLayoutSetUp(A->rmap));
1114: PetscCall(PetscLayoutSetUp(A->cmap));
1115: if (!A->preallocated) PetscCall(MatMPIDenseSetPreallocation(A, NULL));
1116: PetscFunctionReturn(PETSC_SUCCESS);
1117: }
1119: static PetscErrorCode MatAXPY_MPIDense(Mat Y, PetscScalar alpha, Mat X, MatStructure str)
1120: {
1121: Mat_MPIDense *A = (Mat_MPIDense *)Y->data, *B = (Mat_MPIDense *)X->data;
1123: PetscFunctionBegin;
1124: PetscCall(MatAXPY(A->A, alpha, B->A, str));
1125: PetscFunctionReturn(PETSC_SUCCESS);
1126: }
1128: static PetscErrorCode MatConjugate_MPIDense(Mat mat)
1129: {
1130: Mat_MPIDense *a = (Mat_MPIDense *)mat->data;
1132: PetscFunctionBegin;
1133: PetscCall(MatConjugate(a->A));
1134: PetscFunctionReturn(PETSC_SUCCESS);
1135: }
1137: static PetscErrorCode MatRealPart_MPIDense(Mat A)
1138: {
1139: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1141: PetscFunctionBegin;
1142: PetscCall(MatRealPart(a->A));
1143: PetscFunctionReturn(PETSC_SUCCESS);
1144: }
1146: static PetscErrorCode MatImaginaryPart_MPIDense(Mat A)
1147: {
1148: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1150: PetscFunctionBegin;
1151: PetscCall(MatImaginaryPart(a->A));
1152: PetscFunctionReturn(PETSC_SUCCESS);
1153: }
1155: static PetscErrorCode MatGetColumnVector_MPIDense(Mat A, Vec v, PetscInt col)
1156: {
1157: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1159: PetscFunctionBegin;
1160: PetscCheck(a->A, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Missing local matrix");
1161: PetscCheck(a->A->ops->getcolumnvector, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Missing get column operation");
1162: PetscUseTypeMethod(a->A, getcolumnvector, v, col);
1163: PetscFunctionReturn(PETSC_SUCCESS);
1164: }
1166: PETSC_INTERN PetscErrorCode MatGetColumnReductions_SeqDense(Mat, PetscInt, PetscReal *);
1168: static PetscErrorCode MatGetColumnReductions_MPIDense(Mat A, PetscInt type, PetscReal *reductions)
1169: {
1170: PetscInt i, m, n;
1171: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1173: PetscFunctionBegin;
1174: PetscCall(MatGetSize(A, &m, &n));
1175: if (type == REDUCTION_MEAN_REALPART) {
1176: PetscCall(MatGetColumnReductions_SeqDense(a->A, (PetscInt)REDUCTION_SUM_REALPART, reductions));
1177: } else if (type == REDUCTION_MEAN_IMAGINARYPART) {
1178: PetscCall(MatGetColumnReductions_SeqDense(a->A, (PetscInt)REDUCTION_SUM_IMAGINARYPART, reductions));
1179: } else {
1180: PetscCall(MatGetColumnReductions_SeqDense(a->A, type, reductions));
1181: }
1182: if (type == NORM_2) {
1183: for (i = 0; i < n; i++) reductions[i] *= reductions[i];
1184: }
1185: if (type == NORM_INFINITY) {
1186: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, reductions, n, MPIU_REAL, MPIU_MAX, A->hdr.comm));
1187: } else {
1188: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, reductions, n, MPIU_REAL, MPIU_SUM, A->hdr.comm));
1189: }
1190: if (type == NORM_2) {
1191: for (i = 0; i < n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
1192: } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
1193: for (i = 0; i < n; i++) reductions[i] /= m;
1194: }
1195: PetscFunctionReturn(PETSC_SUCCESS);
1196: }
1198: static PetscErrorCode MatSetRandom_MPIDense(Mat x, PetscRandom rctx)
1199: {
1200: Mat_MPIDense *d = (Mat_MPIDense *)x->data;
1202: PetscFunctionBegin;
1203: PetscCall(MatSetRandom(d->A, rctx));
1204: #if PetscDefined(HAVE_DEVICE)
1205: x->offloadmask = d->A->offloadmask;
1206: #endif
1207: PetscCall(PetscObjectStateIncrease((PetscObject)x));
1208: PetscFunctionReturn(PETSC_SUCCESS);
1209: }
1211: static PetscErrorCode MatMatTransposeMultSymbolic_MPIDense_MPIDense(Mat, Mat, PetscReal, Mat);
1212: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense(Mat, Mat, Mat);
1213: static PetscErrorCode MatTransposeMatMultSymbolic_MPIDense_MPIDense(Mat, Mat, PetscReal, Mat);
1214: static PetscErrorCode MatTransposeMatMultNumeric_MPIDense_MPIDense(Mat, Mat, Mat);
1215: static PetscErrorCode MatEqual_MPIDense(Mat, Mat, PetscBool *);
1216: static PetscErrorCode MatLoad_MPIDense(Mat, PetscViewer);
1217: static PetscErrorCode MatProductSetFromOptions_MPIDense(Mat);
1219: static struct _MatOps MatOps_Values = {MatSetValues_MPIDense,
1220: MatGetRow_MPIDense,
1221: MatRestoreRow_MPIDense,
1222: MatMult_MPIDense,
1223: /* 4*/ MatMultAdd_MPIDense,
1224: MatMultTranspose_MPIDense,
1225: MatMultTransposeAdd_MPIDense,
1226: NULL,
1227: NULL,
1228: NULL,
1229: /* 10*/ NULL,
1230: NULL,
1231: NULL,
1232: NULL,
1233: MatTranspose_MPIDense,
1234: /* 15*/ MatGetInfo_MPIDense,
1235: MatEqual_MPIDense,
1236: MatGetDiagonal_MPIDense,
1237: MatDiagonalScale_MPIDense,
1238: MatNorm_MPIDense,
1239: /* 20*/ MatAssemblyBegin_MPIDense,
1240: MatAssemblyEnd_MPIDense,
1241: MatSetOption_MPIDense,
1242: MatZeroEntries_MPIDense,
1243: /* 24*/ MatZeroRows_MPIDense,
1244: NULL,
1245: NULL,
1246: NULL,
1247: NULL,
1248: /* 29*/ MatSetUp_MPIDense,
1249: NULL,
1250: NULL,
1251: MatGetDiagonalBlock_MPIDense,
1252: MatSetInf_MPIDense,
1253: /* 34*/ MatDuplicate_MPIDense,
1254: NULL,
1255: NULL,
1256: NULL,
1257: NULL,
1258: /* 39*/ MatAXPY_MPIDense,
1259: MatCreateSubMatrices_MPIDense,
1260: NULL,
1261: MatGetValues_MPIDense,
1262: MatCopy_MPIDense,
1263: /* 44*/ NULL,
1264: MatScale_MPIDense,
1265: MatShift_MPIDense,
1266: NULL,
1267: NULL,
1268: /* 49*/ MatSetRandom_MPIDense,
1269: NULL,
1270: NULL,
1271: NULL,
1272: NULL,
1273: /* 54*/ NULL,
1274: NULL,
1275: NULL,
1276: NULL,
1277: NULL,
1278: /* 59*/ MatCreateSubMatrix_MPIDense,
1279: MatDestroy_MPIDense,
1280: MatView_MPIDense,
1281: NULL,
1282: NULL,
1283: /* 64*/ NULL,
1284: NULL,
1285: NULL,
1286: NULL,
1287: NULL,
1288: /* 69*/ NULL,
1289: NULL,
1290: NULL,
1291: NULL,
1292: NULL,
1293: /* 74*/ NULL,
1294: NULL,
1295: NULL,
1296: NULL,
1297: MatLoad_MPIDense,
1298: /* 79*/ NULL,
1299: NULL,
1300: NULL,
1301: NULL,
1302: /* 83*/ NULL,
1303: NULL,
1304: NULL,
1305: NULL,
1306: MatMatTransposeMultSymbolic_MPIDense_MPIDense,
1307: MatMatTransposeMultNumeric_MPIDense_MPIDense,
1308: /* 89*/ NULL,
1309: MatProductSetFromOptions_MPIDense,
1310: NULL,
1311: NULL,
1312: MatConjugate_MPIDense,
1313: /* 94*/ NULL,
1314: NULL,
1315: MatRealPart_MPIDense,
1316: MatImaginaryPart_MPIDense,
1317: NULL,
1318: /*99*/ NULL,
1319: NULL,
1320: NULL,
1321: NULL,
1322: MatGetColumnVector_MPIDense,
1323: /*104*/ NULL,
1324: NULL,
1325: NULL,
1326: NULL,
1327: NULL,
1328: /*109*/ NULL,
1329: NULL,
1330: MatMultHermitianTranspose_MPIDense,
1331: MatMultHermitianTransposeAdd_MPIDense,
1332: NULL,
1333: /*114*/ NULL,
1334: MatGetColumnReductions_MPIDense,
1335: NULL,
1336: NULL,
1337: NULL,
1338: /*120*/ MatTransposeMatMultSymbolic_MPIDense_MPIDense,
1339: MatTransposeMatMultNumeric_MPIDense_MPIDense,
1340: NULL,
1341: NULL,
1342: /*124*/ NULL,
1343: NULL,
1344: NULL,
1345: NULL,
1346: NULL,
1347: /*129*/ NULL,
1348: MatCreateMPIMatConcatenateSeqMat_MPIDense,
1349: NULL,
1350: NULL,
1351: NULL,
1352: /*134*/ NULL,
1353: NULL,
1354: NULL,
1355: NULL,
1356: NULL,
1357: /*139*/ NULL,
1358: NULL,
1359: NULL,
1360: NULL,
1361: NULL,
1362: NULL,
1363: /*144*/ NULL,
1364: NULL,
1365: NULL,
1366: NULL};
1368: static PetscErrorCode MatMPIDenseSetPreallocation_MPIDense(Mat mat, PetscScalar *data)
1369: {
1370: Mat_MPIDense *a = (Mat_MPIDense *)mat->data;
1371: MatType mtype = MATSEQDENSE;
1373: PetscFunctionBegin;
1374: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)mat), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1375: PetscCall(PetscLayoutSetUp(mat->rmap));
1376: PetscCall(PetscLayoutSetUp(mat->cmap));
1377: if (!a->A) {
1378: PetscCall(MatCreate(PETSC_COMM_SELF, &a->A));
1379: PetscCall(MatSetSizes(a->A, mat->rmap->n, mat->cmap->N, mat->rmap->n, mat->cmap->N));
1380: }
1381: #if PetscDefined(HAVE_CUDA)
1382: PetscBool iscuda;
1383: PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATMPIDENSECUDA, &iscuda));
1384: if (iscuda) mtype = MATSEQDENSECUDA;
1385: #endif
1386: #if PetscDefined(HAVE_HIP)
1387: PetscBool iship;
1388: PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATMPIDENSEHIP, &iship));
1389: if (iship) mtype = MATSEQDENSEHIP;
1390: #endif
1391: PetscCall(MatSetType(a->A, mtype));
1392: PetscCall(MatSeqDenseSetPreallocation(a->A, data));
1393: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1394: mat->offloadmask = a->A->offloadmask;
1395: #endif
1396: mat->preallocated = PETSC_TRUE;
1397: mat->assembled = PETSC_TRUE;
1398: PetscFunctionReturn(PETSC_SUCCESS);
1399: }
1401: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIDense(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1402: {
1403: Mat B, C;
1405: PetscFunctionBegin;
1406: PetscCall(MatMPIAIJGetLocalMat(A, MAT_INITIAL_MATRIX, &C));
1407: PetscCall(MatConvert_SeqAIJ_SeqDense(C, MATSEQDENSE, MAT_INITIAL_MATRIX, &B));
1408: PetscCall(MatDestroy(&C));
1409: if (reuse == MAT_REUSE_MATRIX) C = *newmat;
1410: PetscCall(MatCreateMPIMatConcatenateSeqMat(PetscObjectComm((PetscObject)A), B, A->cmap->n, !C ? MAT_INITIAL_MATRIX : MAT_REUSE_MATRIX, &C));
1411: PetscCall(MatDestroy(&B));
1412: if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &C));
1413: else if (reuse == MAT_INITIAL_MATRIX) *newmat = C;
1414: PetscFunctionReturn(PETSC_SUCCESS);
1415: }
1417: static PetscErrorCode MatConvert_MPIDense_MPIAIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1418: {
1419: Mat B, C;
1421: PetscFunctionBegin;
1422: PetscCall(MatDenseGetLocalMatrix(A, &C));
1423: PetscCall(MatConvert_SeqDense_SeqAIJ(C, MATSEQAIJ, MAT_INITIAL_MATRIX, &B));
1424: if (reuse == MAT_REUSE_MATRIX) {
1425: C = *newmat;
1426: } else C = NULL;
1427: PetscCall(MatCreateMPIMatConcatenateSeqMat(PetscObjectComm((PetscObject)A), B, A->cmap->n, !C ? MAT_INITIAL_MATRIX : MAT_REUSE_MATRIX, &C));
1428: PetscCall(MatDestroy(&B));
1429: if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &C));
1430: else if (reuse == MAT_INITIAL_MATRIX) *newmat = C;
1431: PetscFunctionReturn(PETSC_SUCCESS);
1432: }
1434: #if PetscDefined(HAVE_ELEMENTAL)
1435: PETSC_INTERN PetscErrorCode MatConvert_MPIDense_Elemental(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1436: {
1437: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1438: Mat mat_elemental;
1439: PetscScalar *v;
1440: PetscInt m = A->rmap->n, N = A->cmap->N, rstart = A->rmap->rstart, i, *rows, *cols, lda;
1442: PetscFunctionBegin;
1443: if (reuse == MAT_REUSE_MATRIX) {
1444: mat_elemental = *newmat;
1445: PetscCall(MatZeroEntries(*newmat));
1446: } else {
1447: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental));
1448: PetscCall(MatSetSizes(mat_elemental, PETSC_DECIDE, PETSC_DECIDE, A->rmap->N, A->cmap->N));
1449: PetscCall(MatSetType(mat_elemental, MATELEMENTAL));
1450: PetscCall(MatSetUp(mat_elemental));
1451: PetscCall(MatSetOption(mat_elemental, MAT_ROW_ORIENTED, PETSC_FALSE));
1452: }
1454: PetscCall(PetscMalloc2(m, &rows, N, &cols));
1455: for (i = 0; i < N; i++) cols[i] = i;
1456: for (i = 0; i < m; i++) rows[i] = rstart + i;
1458: /* PETSc-Elemental interface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
1459: PetscCall(MatDenseGetArray(A, &v));
1460: PetscCall(MatDenseGetLDA(a->A, &lda));
1461: if (lda == m) PetscCall(MatSetValues(mat_elemental, m, rows, N, cols, v, ADD_VALUES));
1462: else {
1463: for (i = 0; i < N; i++) PetscCall(MatSetValues(mat_elemental, m, rows, 1, &i, v + lda * i, ADD_VALUES));
1464: }
1465: PetscCall(MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY));
1466: PetscCall(MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY));
1467: PetscCall(MatDenseRestoreArray(A, &v));
1468: PetscCall(PetscFree2(rows, cols));
1470: if (reuse == MAT_INPLACE_MATRIX) {
1471: PetscCall(MatHeaderReplace(A, &mat_elemental));
1472: } else {
1473: *newmat = mat_elemental;
1474: }
1475: PetscFunctionReturn(PETSC_SUCCESS);
1476: }
1477: #endif
1479: static PetscErrorCode MatDenseGetColumn_MPIDense(Mat A, PetscInt col, PetscScalar **vals)
1480: {
1481: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
1483: PetscFunctionBegin;
1484: PetscCall(MatDenseGetColumn(mat->A, col, vals));
1485: PetscFunctionReturn(PETSC_SUCCESS);
1486: }
1488: static PetscErrorCode MatDenseRestoreColumn_MPIDense(Mat A, PetscScalar **vals)
1489: {
1490: Mat_MPIDense *mat = (Mat_MPIDense *)A->data;
1492: PetscFunctionBegin;
1493: PetscCall(MatDenseRestoreColumn(mat->A, vals));
1494: PetscFunctionReturn(PETSC_SUCCESS);
1495: }
1497: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIDense(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
1498: {
1499: Mat_MPIDense *mat;
1500: PetscInt m, nloc, N;
1502: PetscFunctionBegin;
1503: PetscCall(MatGetSize(inmat, &m, &N));
1504: PetscCall(MatGetLocalSize(inmat, NULL, &nloc));
1505: if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
1506: PetscInt sum;
1508: if (n == PETSC_DECIDE) PetscCall(PetscSplitOwnership(comm, &n, &N));
1509: /* Check sum(n) = N */
1510: PetscCallMPI(MPIU_Allreduce(&n, &sum, 1, MPIU_INT, MPI_SUM, comm));
1511: PetscCheck(sum == N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Sum of local columns %" PetscInt_FMT " != global columns %" PetscInt_FMT, sum, N);
1513: PetscCall(MatCreateDense(comm, m, n, PETSC_DETERMINE, N, NULL, outmat));
1514: PetscCall(MatSetOption(*outmat, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
1515: }
1517: /* numeric phase */
1518: mat = (Mat_MPIDense *)(*outmat)->data;
1519: PetscCall(MatCopy(inmat, mat->A, SAME_NONZERO_PATTERN));
1520: PetscFunctionReturn(PETSC_SUCCESS);
1521: }
1523: PetscErrorCode MatDenseGetColumnVec_MPIDense(Mat A, PetscInt col, Vec *v)
1524: {
1525: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1526: PetscInt lda;
1528: PetscFunctionBegin;
1529: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1530: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1531: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
1532: a->vecinuse = col + 1;
1533: PetscCall(MatDenseGetLDA(a->A, &lda));
1534: PetscCall(MatDenseGetArray(a->A, (PetscScalar **)&a->ptrinuse));
1535: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)lda)));
1536: *v = a->cvec;
1537: PetscFunctionReturn(PETSC_SUCCESS);
1538: }
1540: PetscErrorCode MatDenseRestoreColumnVec_MPIDense(Mat A, PetscInt col, Vec *v)
1541: {
1542: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1544: PetscFunctionBegin;
1545: PetscCheck(a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
1546: PetscCheck(a->cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector");
1547: VecCheckAssembled(a->cvec);
1548: a->vecinuse = 0;
1549: PetscCall(MatDenseRestoreArray(a->A, (PetscScalar **)&a->ptrinuse));
1550: PetscCall(VecResetArray(a->cvec));
1551: if (v) *v = NULL;
1552: PetscFunctionReturn(PETSC_SUCCESS);
1553: }
1555: PetscErrorCode MatDenseGetColumnVecRead_MPIDense(Mat A, PetscInt col, Vec *v)
1556: {
1557: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1558: PetscInt lda;
1560: PetscFunctionBegin;
1561: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1562: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1563: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
1564: a->vecinuse = col + 1;
1565: PetscCall(MatDenseGetLDA(a->A, &lda));
1566: PetscCall(MatDenseGetArrayRead(a->A, &a->ptrinuse));
1567: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)lda)));
1568: PetscCall(VecLockReadPush(a->cvec));
1569: *v = a->cvec;
1570: PetscFunctionReturn(PETSC_SUCCESS);
1571: }
1573: PetscErrorCode MatDenseRestoreColumnVecRead_MPIDense(Mat A, PetscInt col, Vec *v)
1574: {
1575: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1577: PetscFunctionBegin;
1578: PetscCheck(a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
1579: PetscCheck(a->cvec, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Missing internal column vector");
1580: VecCheckAssembled(a->cvec);
1581: a->vecinuse = 0;
1582: PetscCall(MatDenseRestoreArrayRead(a->A, &a->ptrinuse));
1583: PetscCall(VecLockReadPop(a->cvec));
1584: PetscCall(VecResetArray(a->cvec));
1585: if (v) *v = NULL;
1586: PetscFunctionReturn(PETSC_SUCCESS);
1587: }
1589: PetscErrorCode MatDenseGetColumnVecWrite_MPIDense(Mat A, PetscInt col, Vec *v)
1590: {
1591: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1592: PetscInt lda;
1594: PetscFunctionBegin;
1595: PetscCheck(!a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1596: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1597: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
1598: a->vecinuse = col + 1;
1599: PetscCall(MatDenseGetLDA(a->A, &lda));
1600: PetscCall(MatDenseGetArrayWrite(a->A, (PetscScalar **)&a->ptrinuse));
1601: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)lda)));
1602: *v = a->cvec;
1603: PetscFunctionReturn(PETSC_SUCCESS);
1604: }
1606: PetscErrorCode MatDenseRestoreColumnVecWrite_MPIDense(Mat A, PetscInt col, Vec *v)
1607: {
1608: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1610: PetscFunctionBegin;
1611: PetscCheck(a->vecinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
1612: PetscCheck(a->cvec, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Missing internal column vector");
1613: VecCheckAssembled(a->cvec);
1614: a->vecinuse = 0;
1615: PetscCall(MatDenseRestoreArrayWrite(a->A, (PetscScalar **)&a->ptrinuse));
1616: PetscCall(VecResetArray(a->cvec));
1617: if (v) *v = NULL;
1618: PetscFunctionReturn(PETSC_SUCCESS);
1619: }
1621: static PetscErrorCode MatDenseSetUpSubLayout_Private(MPI_Comm comm, PetscLayout parent, PetscInt begin, PetscInt end, PetscLayout *sub)
1622: {
1623: PetscInt *range;
1624: PetscLayout layout;
1626: PetscFunctionBegin;
1627: if (parent->range == NULL) PetscCall(PetscLayoutSetUp(parent));
1628: PetscCheck(begin == end || (parent->range[0] <= begin && end <= parent->range[parent->size]), parent->comm, PETSC_ERR_ARG_SIZ, "Submatrix interval [%" PetscInt_FMT ",%" PetscInt_FMT ") is outside the parent ownership range [%" PetscInt_FMT ",%" PetscInt_FMT ")", begin, end,
1629: parent->range[0], parent->range[parent->size]);
1630: PetscCall(PetscMalloc1(parent->size + 1, &range));
1631: for (PetscInt i = 0; i <= parent->size; ++i) range[i] = PetscClipInterval(parent->range[i], begin, end) - begin;
1632: PetscCall(PetscLayoutCreateFromRanges(comm, range, PETSC_OWN_POINTER, 1, &layout));
1633: PetscCall(PetscLayoutDestroy(sub));
1634: *sub = layout;
1635: PetscFunctionReturn(PETSC_SUCCESS);
1636: }
1638: static PetscErrorCode MatDenseGetSubMatrix_MPIDense(Mat A, PetscInt rbegin, PetscInt rend, PetscInt cbegin, PetscInt cend, Mat *v)
1639: {
1640: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1641: Mat_MPIDense *c;
1642: MPI_Comm comm;
1643: PetscInt prbegin, prend;
1645: PetscFunctionBegin;
1646: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
1647: PetscCheck(!a->vecinuse, comm, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1648: PetscCheck(!a->matinuse, comm, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1649: if (A->rmap->range == NULL) PetscCall(PetscLayoutSetUp(A->rmap));
1650: prbegin = PetscClipInterval(rbegin - A->rmap->rstart, 0, A->rmap->n);
1651: prend = PetscClipInterval(rend - A->rmap->rstart, 0, A->rmap->n);
1652: if (!a->cmat) {
1653: PetscCall(MatCreate(comm, &a->cmat));
1654: PetscCall(MatSetType(a->cmat, ((PetscObject)A)->type_name));
1655: PetscCall(MatSetVecType(a->cmat, A->defaultvectype));
1656: if (rend - rbegin == A->rmap->N) PetscCall(PetscLayoutReference(A->rmap, &a->cmat->rmap));
1657: else PetscCall(MatDenseSetUpSubLayout_Private(comm, A->rmap, rbegin, rend, &a->cmat->rmap));
1658: if (cend - cbegin == A->cmap->N) PetscCall(PetscLayoutReference(A->cmap, &a->cmat->cmap));
1659: else PetscCall(MatDenseSetUpSubLayout_Private(comm, A->cmap, cbegin, cend, &a->cmat->cmap));
1660: c = (Mat_MPIDense *)a->cmat->data;
1661: c->sub_rbegin = rbegin;
1662: c->sub_rend = rend;
1663: c->sub_cbegin = cbegin;
1664: c->sub_cend = cend;
1665: }
1666: c = (Mat_MPIDense *)a->cmat->data;
1667: if (c->sub_rbegin != rbegin || c->sub_rend != rend) {
1668: PetscCall(MatDenseSetUpSubLayout_Private(comm, A->rmap, rbegin, rend, &a->cmat->rmap));
1669: PetscCall(VecDestroy(&c->cvec));
1670: c->sub_rbegin = rbegin;
1671: c->sub_rend = rend;
1672: }
1673: if (c->sub_cbegin != cbegin || c->sub_cend != cend) {
1674: // equal-width slices have the same layout when the first process owns every column
1675: if (cend - cbegin != a->cmat->cmap->N || A->cmap->range[0] > 0 || A->cmap->range[1] != A->cmap->N) {
1676: PetscCall(MatDenseSetUpSubLayout_Private(comm, A->cmap, cbegin, cend, &a->cmat->cmap));
1677: PetscCall(VecDestroy(&c->lvec));
1678: PetscCall(PetscSFDestroy(&c->Mvctx));
1679: }
1680: c->sub_cbegin = cbegin;
1681: c->sub_cend = cend;
1682: }
1683: PetscCheck(!c->A, comm, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1684: PetscCall(MatDenseGetSubMatrix(a->A, prbegin, prend, cbegin, cend, &c->A));
1686: a->cmat->preallocated = PETSC_TRUE;
1687: a->cmat->assembled = PETSC_TRUE;
1688: #if PetscDefined(HAVE_DEVICE)
1689: a->cmat->offloadmask = c->A->offloadmask;
1690: #endif
1691: a->matinuse = cbegin + 1;
1692: *v = a->cmat;
1693: PetscFunctionReturn(PETSC_SUCCESS);
1694: }
1696: static PetscErrorCode MatDenseRestoreSubMatrix_MPIDense(Mat A, Mat *v)
1697: {
1698: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1699: Mat_MPIDense *c;
1701: PetscFunctionBegin;
1702: PetscCheck(a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseGetSubMatrix() first");
1703: PetscCheck(a->cmat, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Missing internal matrix");
1704: PetscCheck(*v == a->cmat, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not the matrix obtained from MatDenseGetSubMatrix()");
1705: a->matinuse = 0;
1706: c = (Mat_MPIDense *)a->cmat->data;
1707: PetscCall(MatDenseRestoreSubMatrix(a->A, &c->A));
1708: *v = NULL;
1709: #if PetscDefined(HAVE_DEVICE)
1710: A->offloadmask = a->A->offloadmask;
1711: #endif
1712: PetscFunctionReturn(PETSC_SUCCESS);
1713: }
1715: static PetscErrorCode MatDenseUpdateColumnLayout_MPIDense(Mat A, PetscLayout clayout)
1716: {
1717: Mat_MPIDense *a = (Mat_MPIDense *)A->data;
1719: PetscFunctionBegin;
1720: if (A->cmap == clayout) PetscFunctionReturn(PETSC_SUCCESS);
1721: PetscCheck(!a->matinuse, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1722: PetscCall(PetscLayoutReference(clayout, &A->cmap));
1723: PetscCall(MatDestroy(&a->cmat));
1724: PetscCall(PetscSFDestroy(&a->Mvctx));
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 PetscDefined(HAVE_ELEMENTAL)
1781: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_elemental_C", MatConvert_MPIDense_Elemental));
1782: #endif
1783: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
1784: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpidense_scalapack_C", MatConvert_Dense_ScaLAPACK));
1785: #endif
1786: #if PetscDefined(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 PetscDefined(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 PetscDefined(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(PetscObjectComposeFunction((PetscObject)mat, "MatDenseUpdateColumnLayout_C", MatDenseUpdateColumnLayout_MPIDense));
1809: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, MATMPIDENSE));
1810: PetscFunctionReturn(PETSC_SUCCESS);
1811: }
1813: /*MC
1814: MATDENSE - MATDENSE = "dense" - A matrix type to be used for dense matrices.
1816: This matrix type is identical to `MATSEQDENSE` when constructed with a single process communicator,
1817: and `MATMPIDENSE` otherwise.
1819: Options Database Key:
1820: . -mat_type dense - sets the matrix type to `MATDENSE` during a call to `MatSetFromOptions()`
1822: Level: beginner
1824: .seealso: [](ch_matrices), `Mat`, `MATSEQDENSE`, `MATMPIDENSE`, `MATDENSECUDA`, `MATDENSEHIP`
1825: M*/
1827: /*@
1828: MatMPIDenseSetPreallocation - Sets the array used to store the matrix entries
1830: Collective
1832: Input Parameters:
1833: + B - the matrix
1834: - data - optional location of matrix data. Set to `NULL` for PETSc
1835: to control all matrix memory allocation.
1837: Level: intermediate
1839: Notes:
1840: The dense format is fully compatible with standard Fortran
1841: storage by columns.
1843: The data input variable is intended primarily for Fortran programmers
1844: who wish to allocate their own matrix memory space. Most users should
1845: set `data` to `NULL`.
1847: .seealso: [](ch_matrices), `Mat`, `MATMPIDENSE`, `MatCreate()`, `MatCreateSeqDense()`, `MatSetValues()`
1848: @*/
1849: PetscErrorCode MatMPIDenseSetPreallocation(Mat B, PetscScalar *data)
1850: {
1851: PetscFunctionBegin;
1853: PetscTryMethod(B, "MatMPIDenseSetPreallocation_C", (Mat, PetscScalar *), (B, data));
1854: PetscFunctionReturn(PETSC_SUCCESS);
1855: }
1857: /*@
1858: MatDensePlaceArray - Allows one to replace the array in a `MATDENSE` matrix with an
1859: array provided by the user. This is useful to avoid copying an array
1860: into a matrix
1862: Not Collective
1864: Input Parameters:
1865: + mat - the matrix
1866: - array - the array in column major order
1868: Level: developer
1870: Note:
1871: Adding `const` to `array` was an oversight, see notes in `VecPlaceArray()`.
1873: You can return to the original array with a call to `MatDenseResetArray()`. The user is responsible for freeing this array; it will not be
1874: freed when the matrix is destroyed.
1876: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArray()`, `MatDenseResetArray()`, `VecPlaceArray()`, `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecResetArray()`,
1877: `MatDensePlaceArrayWithMemType()`, `MatDenseReplaceArray()`
1878: @*/
1879: PetscErrorCode MatDensePlaceArray(Mat mat, const PetscScalar *array)
1880: {
1881: PetscFunctionBegin;
1883: PetscUseMethod(mat, "MatDensePlaceArray_C", (Mat, const PetscScalar *), (mat, array));
1884: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
1885: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1886: mat->offloadmask = PETSC_OFFLOAD_CPU;
1887: #endif
1888: PetscFunctionReturn(PETSC_SUCCESS);
1889: }
1891: /*@
1892: MatDenseResetArray - Resets the matrix array to that it previously had before the call to `MatDensePlaceArray()`
1894: Not Collective
1896: Input Parameter:
1897: . mat - the matrix
1899: Level: developer
1901: Note:
1902: You can only call this after a call to `MatDensePlaceArray()`
1904: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArray()`, `MatDensePlaceArray()`, `MatDenseResetArrayWithMemType()`, `VecPlaceArray()`, `VecGetArray()`, `VecRestoreArray()`, `VecReplaceArray()`, `VecResetArray()`
1905: @*/
1906: PetscErrorCode MatDenseResetArray(Mat mat)
1907: {
1908: PetscFunctionBegin;
1910: PetscUseMethod(mat, "MatDenseResetArray_C", (Mat), (mat));
1911: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
1912: PetscFunctionReturn(PETSC_SUCCESS);
1913: }
1915: /*@
1916: MatDenseReplaceArray - Allows one to replace the array in a dense matrix with an
1917: array provided by the user. This is useful to avoid copying an array
1918: into a matrix
1920: Not Collective
1922: Input Parameters:
1923: + mat - the matrix
1924: - array - the array in column major order
1926: Level: developer
1928: Note:
1929: Adding `const` to `array` was an oversight, see notes in `VecPlaceArray()`.
1931: The memory passed in MUST be obtained with `PetscMalloc()` and CANNOT be
1932: freed by the user. It will be freed when the matrix is destroyed.
1934: .seealso: [](ch_matrices), `Mat`, `MatDensePlaceArray()`, `MatDenseGetArray()`, `VecReplaceArray()`
1935: @*/
1936: PetscErrorCode MatDenseReplaceArray(Mat mat, const PetscScalar *array)
1937: {
1938: PetscFunctionBegin;
1940: PetscUseMethod(mat, "MatDenseReplaceArray_C", (Mat, const PetscScalar *), (mat, array));
1941: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
1942: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1943: mat->offloadmask = PETSC_OFFLOAD_CPU;
1944: #endif
1945: PetscFunctionReturn(PETSC_SUCCESS);
1946: }
1948: /*@
1949: MatCreateDense - Creates a matrix in `MATDENSE` format.
1951: Collective
1953: Input Parameters:
1954: + comm - MPI communicator
1955: . m - number of local rows (or `PETSC_DECIDE` to have calculated if `M` is given)
1956: . n - number of local columns (or `PETSC_DECIDE` to have calculated if `N` is given)
1957: . M - number of global rows (or `PETSC_DECIDE` to have calculated if `m` is given)
1958: . N - number of global columns (or `PETSC_DECIDE` to have calculated if `n` is given)
1959: - data - optional location of matrix data. Set data to `NULL` (`PETSC_NULL_SCALAR_ARRAY` for Fortran users) for PETSc
1960: to control all matrix memory allocation.
1962: Output Parameter:
1963: . A - the matrix
1965: Level: intermediate
1967: Notes:
1968: The dense format is fully compatible with standard Fortran
1969: storage by columns.
1971: Although local portions of the matrix are stored in column-major
1972: order, the matrix is partitioned across MPI processes by row.
1974: The data input variable is intended primarily for Fortran programmers
1975: who wish to allocate their own matrix memory space. Most users should
1976: set `data` to `NULL` (`PETSC_NULL_SCALAR_ARRAY` for Fortran users).
1978: The user MUST specify either the local or global matrix dimensions
1979: (possibly both).
1981: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatCreate()`, `MatCreateSeqDense()`, `MatSetValues()`
1982: @*/
1983: PetscErrorCode MatCreateDense(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscScalar data[], Mat *A)
1984: {
1985: PetscFunctionBegin;
1986: PetscCall(MatCreate(comm, A));
1987: PetscCall(MatSetSizes(*A, m, n, M, N));
1988: PetscCall(MatSetType(*A, MATDENSE));
1989: PetscCall(MatSeqDenseSetPreallocation(*A, data));
1990: PetscCall(MatMPIDenseSetPreallocation(*A, data));
1991: PetscFunctionReturn(PETSC_SUCCESS);
1992: }
1994: static PetscErrorCode MatDuplicate_MPIDense(Mat A, MatDuplicateOption cpvalues, Mat *newmat)
1995: {
1996: Mat mat;
1997: Mat_MPIDense *a, *oldmat = (Mat_MPIDense *)A->data;
1999: PetscFunctionBegin;
2000: *newmat = NULL;
2001: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat));
2002: PetscCall(MatSetSizes(mat, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
2003: PetscCall(MatSetType(mat, ((PetscObject)A)->type_name));
2004: a = (Mat_MPIDense *)mat->data;
2006: mat->factortype = A->factortype;
2007: mat->assembled = PETSC_TRUE;
2008: mat->preallocated = PETSC_TRUE;
2010: mat->insertmode = NOT_SET_VALUES;
2011: a->donotstash = oldmat->donotstash;
2013: PetscCall(PetscLayoutReference(A->rmap, &mat->rmap));
2014: PetscCall(PetscLayoutReference(A->cmap, &mat->cmap));
2016: PetscCall(MatDuplicate(oldmat->A, cpvalues, &a->A));
2018: *newmat = mat;
2019: PetscFunctionReturn(PETSC_SUCCESS);
2020: }
2022: static PetscErrorCode MatLoad_MPIDense(Mat newMat, PetscViewer viewer)
2023: {
2024: PetscBool isbinary;
2025: #if PetscDefined(HAVE_HDF5)
2026: PetscBool ishdf5;
2027: #endif
2029: PetscFunctionBegin;
2032: /* force binary viewer to load .info file if it has not yet done so */
2033: PetscCall(PetscViewerSetUp(viewer));
2034: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
2035: #if PetscDefined(HAVE_HDF5)
2036: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2037: #endif
2038: if (isbinary) {
2039: PetscCall(MatLoad_Dense_Binary(newMat, viewer));
2040: #if PetscDefined(HAVE_HDF5)
2041: } else if (ishdf5) {
2042: PetscCall(MatLoad_Dense_HDF5(newMat, viewer));
2043: #endif
2044: } 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);
2045: PetscFunctionReturn(PETSC_SUCCESS);
2046: }
2048: static PetscErrorCode MatEqual_MPIDense(Mat A, Mat B, PetscBool *flag)
2049: {
2050: Mat_MPIDense *matB = (Mat_MPIDense *)B->data, *matA = (Mat_MPIDense *)A->data;
2051: Mat a, b;
2053: PetscFunctionBegin;
2054: a = matA->A;
2055: b = matB->A;
2056: PetscCall(MatEqual(a, b, flag));
2057: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flag, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)A)));
2058: PetscFunctionReturn(PETSC_SUCCESS);
2059: }
2061: static PetscErrorCode MatProductCtxDestroy_MatTransMatMult_MPIDense_MPIDense(PetscCtxRt data)
2062: {
2063: MatProductCtx_TransMatMultDense *atb = *(MatProductCtx_TransMatMultDense **)data;
2065: PetscFunctionBegin;
2066: PetscCall(PetscFree2(atb->sendbuf, atb->recvcounts));
2067: PetscCall(MatDestroy(&atb->atb));
2068: PetscCall(PetscFree(atb));
2069: PetscFunctionReturn(PETSC_SUCCESS);
2070: }
2072: static PetscErrorCode MatProductCtxDestroy_MatMatTransMult_MPIDense_MPIDense(PetscCtxRt data)
2073: {
2074: MatProductCtx_MatTransMultDense *abt = *(MatProductCtx_MatTransMultDense **)data;
2076: PetscFunctionBegin;
2077: PetscCall(MatDestroy(&abt->Bfull));
2078: PetscCall(PetscFree2(abt->buf[0], abt->buf[1]));
2079: PetscCall(PetscFree2(abt->recvcounts, abt->recvdispls));
2080: PetscCall(PetscFree(abt));
2081: PetscFunctionReturn(PETSC_SUCCESS);
2082: }
2084: static PetscErrorCode MatTransposeMatMultNumeric_MPIDense_MPIDense(Mat A, Mat B, Mat C)
2085: {
2086: Mat_MPIDense *a = (Mat_MPIDense *)A->data, *b = (Mat_MPIDense *)B->data, *c = (Mat_MPIDense *)C->data;
2087: MatProductCtx_TransMatMultDense *atb;
2088: MPI_Comm comm;
2089: PetscMPIInt size;
2090: PetscScalar *carray, *sendbuf;
2091: const PetscScalar *atbarray;
2092: PetscInt i, cN = C->cmap->N, proc, k, j, lda;
2093: const PetscInt *ranges;
2095: PetscFunctionBegin;
2096: MatCheckProduct(C, 3);
2097: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2098: if (!C->rmap->N || !C->cmap->N) PetscFunctionReturn(PETSC_SUCCESS);
2099: atb = (MatProductCtx_TransMatMultDense *)C->product->data;
2101: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
2102: PetscCallMPI(MPI_Comm_size(comm, &size));
2104: /* compute atbarray = aseq^T * bseq */
2105: PetscCall(MatTransposeMatMult(a->A, b->A, atb->atb ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX, PETSC_DETERMINE, &atb->atb));
2107: if (atb->usebackend == PETSC_TRUE) {
2108: if (a->Mvctx == NULL) PetscCall(MatSetUpMultiply_MPIDense(A));
2109: PetscCall(MatZeroEntries(c->A));
2110: PetscCall(MatDenseScatter_Private(a->Mvctx, atb->atb, c->A, ADD_VALUES, SCATTER_REVERSE));
2111: PetscFunctionReturn(PETSC_SUCCESS);
2112: }
2114: PetscCall(MatGetOwnershipRanges(C, &ranges));
2116: if (ranges[1] == C->rmap->N) {
2117: /* all of the values are being reduced to rank 0: optimize this case to use MPI_Reduce and GPU aware MPI if available */
2118: PetscInt atb_lda, c_lda;
2119: Mat atb_local = atb->atb;
2120: Mat atb_alloc = NULL;
2121: Mat c_local = c->A;
2122: Mat c_alloc = NULL;
2123: PetscMemType atb_memtype, c_memtype;
2124: const PetscScalar *atb_array = NULL;
2125: MPI_Datatype vector_type;
2126: PetscScalar *c_array = NULL;
2127: PetscMPIInt rank;
2129: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2131: PetscCall(MatDenseGetLDA(atb_local, &atb_lda));
2132: if (atb_lda != C->rmap->N) {
2133: // copy atb to a matrix that will have lda == the number of rows
2134: PetscCall(MatDuplicate(atb_local, MAT_DO_NOT_COPY_VALUES, &atb_alloc));
2135: PetscCall(MatCopy(atb_local, atb_alloc, DIFFERENT_NONZERO_PATTERN));
2136: atb_local = atb_alloc;
2137: }
2139: if (rank == 0) {
2140: PetscCall(MatDenseGetLDA(c_local, &c_lda));
2141: if (c_lda != C->rmap->N) {
2142: // copy c to a matrix that will have lda == the number of rows
2143: PetscCall(MatDuplicate(c_local, MAT_DO_NOT_COPY_VALUES, &c_alloc));
2144: c_local = c_alloc;
2145: }
2146: PetscCall(MatZeroEntries(c_local));
2147: }
2148: /* atb_local and c_local have nrows = lda = A->cmap->N and ncols =
2149: * B->cmap->N: use the a->Mvctx to use the best reduction method */
2150: if (!a->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A));
2151: vector_type = MPIU_SCALAR;
2152: if (B->cmap->N > 1) {
2153: PetscMPIInt mpi_N;
2155: PetscCall(PetscMPIIntCast(B->cmap->N, &mpi_N));
2156: PetscCallMPI(MPI_Type_contiguous(mpi_N, MPIU_SCALAR, &vector_type));
2157: PetscCallMPI(MPI_Type_commit(&vector_type));
2158: }
2159: PetscCall(MatDenseGetArrayReadAndMemType(atb_local, &atb_array, &atb_memtype));
2160: PetscCall(MatDenseGetArrayWriteAndMemType(c_local, &c_array, &c_memtype));
2161: PetscCall(PetscSFReduceWithMemTypeBegin(a->Mvctx, vector_type, atb_memtype, atb_array, c_memtype, c_array, MPIU_SUM));
2162: PetscCall(PetscSFReduceEnd(a->Mvctx, vector_type, atb_array, c_array, MPIU_SUM));
2163: PetscCall(MatDenseRestoreArrayWriteAndMemType(c_local, &c_array));
2164: PetscCall(MatDenseRestoreArrayReadAndMemType(atb_local, &atb_array));
2165: if (rank == 0 && c_local != c->A) PetscCall(MatCopy(c_local, c->A, DIFFERENT_NONZERO_PATTERN));
2166: if (B->cmap->N > 1) PetscCallMPI(MPI_Type_free(&vector_type));
2167: PetscCall(MatDestroy(&atb_alloc));
2168: PetscCall(MatDestroy(&c_alloc));
2169: PetscFunctionReturn(PETSC_SUCCESS);
2170: }
2172: sendbuf = atb->sendbuf;
2173: /* arrange atbarray into sendbuf */
2174: PetscCall(MatDenseGetArrayRead(atb->atb, &atbarray));
2175: PetscCall(MatDenseGetLDA(atb->atb, &lda));
2176: for (proc = 0, k = 0; proc < size; proc++) {
2177: for (j = 0; j < cN; j++) {
2178: for (i = ranges[proc]; i < ranges[proc + 1]; i++) sendbuf[k++] = atbarray[i + j * lda];
2179: }
2180: }
2181: PetscCall(MatDenseRestoreArrayRead(atb->atb, &atbarray));
2183: /* sum all atbarray to local values of C */
2184: PetscCall(MatDenseGetLDA(c->A, &lda));
2185: PetscCall(MatDenseGetArrayWrite(c->A, &carray));
2186: if (lda > C->rmap->n && cN > 1 && C->rmap->n) {
2187: PetscScalar *recvbuf;
2189: /* the local product is no longer needed after packing sendbuf; reuse its storage for the contiguous reduction */
2190: PetscCall(MatDenseGetArrayWrite(atb->atb, &recvbuf));
2191: PetscCallMPI(MPI_Reduce_scatter(sendbuf, recvbuf, atb->recvcounts, MPIU_SCALAR, MPIU_SUM, comm));
2192: for (j = 0; j < cN; j++) PetscCall(PetscArraycpy(carray + (size_t)j * lda, recvbuf + (size_t)j * C->rmap->n, C->rmap->n));
2193: PetscCall(MatDenseRestoreArrayWrite(atb->atb, &recvbuf));
2194: } else PetscCallMPI(MPI_Reduce_scatter(sendbuf, carray, atb->recvcounts, MPIU_SCALAR, MPIU_SUM, comm));
2195: PetscCall(MatDenseRestoreArrayWrite(c->A, &carray));
2196: PetscFunctionReturn(PETSC_SUCCESS);
2197: }
2199: static PetscErrorCode MatTransposeMatMultSymbolic_MPIDense_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2200: {
2201: MPI_Comm comm;
2202: MatProductCtx_TransMatMultDense *atb;
2204: PetscFunctionBegin;
2205: MatCheckProduct(C, 4);
2206: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
2207: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
2208: 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,
2209: A->rmap->rend, B->rmap->rstart, B->rmap->rend);
2210: PetscCall(MatDenseSetUpProduct_Private(C, A->cmap, B->cmap, A, NULL));
2211: /* create data structure for reuse C */
2212: PetscCall(PetscNew(&atb));
2213: PetscCall(PetscStrcmp(C->product->alg, MATPRODUCTALGORITHMBACKEND, &atb->usebackend));
2214: if (atb->usebackend == PETSC_FALSE) {
2215: const PetscInt *ranges;
2216: PetscMPIInt size;
2218: PetscCallMPI(MPI_Comm_size(comm, &size));
2219: PetscCall(MatGetOwnershipRanges(C, &ranges));
2220: if (ranges[1] != C->rmap->N) {
2221: PetscCall(PetscMalloc2((size_t)C->rmap->N * C->cmap->N, &atb->sendbuf, size, &atb->recvcounts));
2222: for (PetscMPIInt p = 0; p < size; ++p) PetscCall(PetscMPIIntCast((PetscCount)(ranges[p + 1] - ranges[p]) * C->cmap->N, &atb->recvcounts[p]));
2223: }
2224: }
2225: C->product->data = atb;
2226: C->product->destroy = MatProductCtxDestroy_MatTransMatMult_MPIDense_MPIDense;
2227: PetscFunctionReturn(PETSC_SUCCESS);
2228: }
2230: static PetscErrorCode MatMatTransposeMultSymbolic_MPIDense_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2231: {
2232: MPI_Comm comm;
2233: PetscMPIInt i, size;
2234: PetscInt maxRows, bufsiz;
2235: PetscMPIInt tag;
2236: PetscInt alg;
2237: MatProductCtx_MatTransMultDense *abt;
2238: Mat_Product *product = C->product;
2239: PetscMemType amtype, bmtype, cmtype;
2240: PetscBool cyclic, backend;
2242: PetscFunctionBegin;
2243: MatCheckProduct(C, 4);
2244: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
2245: /* check local size of A and B */
2246: 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);
2248: PetscCall(PetscStrcmp(product->alg, "cyclic", &cyclic));
2249: if (cyclic == PETSC_FALSE) {
2250: PetscCall(PetscStrcmp(product->alg, MATPRODUCTALGORITHMBACKEND, &backend));
2251: alg = 0;
2252: } else {
2253: backend = PETSC_FALSE;
2254: alg = 1;
2255: }
2257: PetscCall(MatGetCurrentMemType(A, &amtype));
2258: PetscCall(MatGetCurrentMemType(B, &bmtype));
2259: PetscCall(MatDenseSetUpProduct_Private(C, A->rmap, B->rmap, A, backend == PETSC_TRUE && PetscMemTypeDevice(amtype) == PETSC_TRUE && amtype == bmtype ? NULL : MATMPIDENSE));
2260: PetscCall(MatGetCurrentMemType(C, &cmtype));
2261: PetscCall(PetscObjectGetNewTag((PetscObject)C, &tag));
2263: /* create data structure for reuse C */
2264: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
2265: PetscCallMPI(MPI_Comm_size(comm, &size));
2266: PetscCall(PetscNew(&abt));
2267: abt->tag = tag;
2268: abt->alg = alg;
2269: switch (alg) {
2270: case 1: /* alg: "cyclic" */
2271: for (maxRows = 0, i = 0; i < size; i++) maxRows = PetscMax(maxRows, B->rmap->range[i + 1] - B->rmap->range[i]);
2272: bufsiz = A->cmap->N * maxRows;
2273: PetscCall(PetscMalloc2(bufsiz, &abt->buf[0], bufsiz, &abt->buf[1]));
2274: break;
2275: default: /* alg: "allgatherv" */
2276: if (backend == PETSC_TRUE && PetscMemTypeDevice(cmtype) == PETSC_TRUE && amtype == cmtype && bmtype == cmtype) {
2277: PetscCall(MatCreateDenseWithMemType(PETSC_COMM_SELF, cmtype, B->rmap->N, B->cmap->N, B->rmap->N, B->cmap->N, PETSC_DECIDE, NULL, &abt->Bfull));
2278: break;
2279: }
2280: PetscCall(PetscMalloc2(B->rmap->n * B->cmap->N, &abt->buf[0], B->rmap->N * B->cmap->N, &abt->buf[1]));
2281: PetscCall(PetscMalloc2(size, &abt->recvcounts, size + 1, &abt->recvdispls));
2282: for (i = 0; i <= size; i++) PetscCall(PetscMPIIntCast(B->rmap->range[i] * A->cmap->N, &abt->recvdispls[i]));
2283: for (i = 0; i < size; i++) PetscCall(PetscMPIIntCast(abt->recvdispls[i + 1] - abt->recvdispls[i], &abt->recvcounts[i]));
2284: break;
2285: }
2287: C->product->data = abt;
2288: C->product->destroy = MatProductCtxDestroy_MatMatTransMult_MPIDense_MPIDense;
2289: C->ops->mattransposemultnumeric = MatMatTransposeMultNumeric_MPIDense_MPIDense;
2290: PetscFunctionReturn(PETSC_SUCCESS);
2291: }
2293: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense_Cyclic(Mat A, Mat B, Mat C)
2294: {
2295: Mat_MPIDense *a = (Mat_MPIDense *)A->data, *b = (Mat_MPIDense *)B->data, *c = (Mat_MPIDense *)C->data;
2296: MatProductCtx_MatTransMultDense *abt;
2297: MPI_Comm comm;
2298: PetscMPIInt rank, size, sendto, recvfrom, recvisfrom;
2299: PetscScalar *sendbuf, *recvbuf = NULL, *cv;
2300: PetscInt i, cK = A->cmap->N, sendsiz, recvsiz, k, j, bn;
2301: PetscScalar _DOne = 1.0, _DZero = 0.0;
2302: const PetscScalar *av, *bv;
2303: PetscBLASInt cm, cn, ck, alda, blda = 0, clda;
2304: MPI_Request reqs[2];
2305: const PetscInt *ranges;
2307: PetscFunctionBegin;
2308: MatCheckProduct(C, 3);
2309: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2310: abt = (MatProductCtx_MatTransMultDense *)C->product->data;
2311: PetscCall(PetscObjectGetComm((PetscObject)C, &comm));
2312: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2313: PetscCallMPI(MPI_Comm_size(comm, &size));
2314: PetscCall(MatDenseGetArrayRead(a->A, &av));
2315: PetscCall(MatDenseGetArrayRead(b->A, &bv));
2316: PetscCall(MatDenseGetArrayWrite(c->A, &cv));
2317: PetscCall(MatDenseGetLDA(a->A, &i));
2318: PetscCall(PetscBLASIntCast(i, &alda));
2319: PetscCall(MatDenseGetLDA(b->A, &i));
2320: PetscCall(PetscBLASIntCast(i, &blda));
2321: PetscCall(MatDenseGetLDA(c->A, &i));
2322: PetscCall(PetscBLASIntCast(i, &clda));
2323: PetscCall(MatGetOwnershipRanges(B, &ranges));
2324: bn = B->rmap->n;
2325: if (blda == bn) {
2326: sendbuf = (PetscScalar *)bv;
2327: } else {
2328: sendbuf = abt->buf[0];
2329: for (k = 0, i = 0; i < cK; i++) {
2330: for (j = 0; j < bn; j++, k++) sendbuf[k] = bv[i * blda + j];
2331: }
2332: }
2333: if (size > 1) {
2334: sendto = (rank + size - 1) % size;
2335: recvfrom = (rank + size + 1) % size;
2336: } else {
2337: sendto = recvfrom = 0;
2338: }
2339: PetscCall(PetscBLASIntCast(cK, &ck));
2340: PetscCall(PetscBLASIntCast(c->A->rmap->n, &cm));
2341: recvisfrom = rank;
2342: for (i = 0; i < size; i++) {
2343: /* we have finished receiving in sending, bufs can be read/modified */
2344: PetscMPIInt nextrecvisfrom = (recvisfrom + 1) % size; /* which process the next recvbuf will originate on */
2345: PetscInt nextbn = ranges[nextrecvisfrom + 1] - ranges[nextrecvisfrom];
2347: if (nextrecvisfrom != rank) {
2348: /* start the cyclic sends from sendbuf, to recvbuf (which will switch to sendbuf) */
2349: sendsiz = cK * bn;
2350: recvsiz = cK * nextbn;
2351: recvbuf = (i & 1) ? abt->buf[0] : abt->buf[1];
2352: PetscCallMPI(MPIU_Isend(sendbuf, sendsiz, MPIU_SCALAR, sendto, abt->tag, comm, &reqs[0]));
2353: PetscCallMPI(MPIU_Irecv(recvbuf, recvsiz, MPIU_SCALAR, recvfrom, abt->tag, comm, &reqs[1]));
2354: }
2356: /* local aseq * sendbuf^T */
2357: PetscCall(PetscBLASIntCast(ranges[recvisfrom + 1] - ranges[recvisfrom], &cn));
2358: if (cm && cn && ck) PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &cm, &cn, &ck, &_DOne, av, &alda, sendbuf, &cn, &_DZero, cv + clda * ranges[recvisfrom], &clda));
2360: if (nextrecvisfrom != rank) {
2361: /* wait for the sends and receives to complete, swap sendbuf and recvbuf */
2362: PetscCallMPI(MPI_Waitall(2, reqs, MPI_STATUSES_IGNORE));
2363: }
2364: bn = nextbn;
2365: recvisfrom = nextrecvisfrom;
2366: sendbuf = recvbuf;
2367: }
2368: PetscCall(MatDenseRestoreArrayRead(a->A, &av));
2369: PetscCall(MatDenseRestoreArrayRead(b->A, &bv));
2370: PetscCall(MatDenseRestoreArrayWrite(c->A, &cv));
2371: PetscFunctionReturn(PETSC_SUCCESS);
2372: }
2374: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense_Allgatherv(Mat A, Mat B, Mat C)
2375: {
2376: Mat_MPIDense *a = (Mat_MPIDense *)A->data, *b = (Mat_MPIDense *)B->data, *c = (Mat_MPIDense *)C->data;
2377: MatProductCtx_MatTransMultDense *abt;
2378: MPI_Comm comm;
2379: PetscMPIInt size, ibn;
2380: PetscScalar *cv, *sendbuf, *recvbuf;
2381: const PetscScalar *av, *bv;
2382: PetscInt blda, i, cK = A->cmap->N, k, j, bn;
2383: PetscScalar _DOne = 1.0, _DZero = 0.0;
2384: PetscBLASInt cm, cn, ck, alda, clda;
2386: PetscFunctionBegin;
2387: MatCheckProduct(C, 3);
2388: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2389: abt = (MatProductCtx_MatTransMultDense *)C->product->data;
2390: if (abt->Bfull != NULL) {
2391: if (c->Mvctx == NULL) PetscCall(MatSetUpMultiply_MPIDense(C));
2392: PetscCall(MatDenseScatter_Private(c->Mvctx, B, abt->Bfull, INSERT_VALUES, SCATTER_FORWARD));
2393: PetscCall((*c->A->ops->mattransposemultnumeric)(a->A, abt->Bfull, c->A));
2394: PetscFunctionReturn(PETSC_SUCCESS);
2395: }
2396: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
2397: PetscCallMPI(MPI_Comm_size(comm, &size));
2398: PetscCall(MatDenseGetArrayRead(a->A, &av));
2399: PetscCall(MatDenseGetArrayRead(b->A, &bv));
2400: PetscCall(MatDenseGetArrayWrite(c->A, &cv));
2401: PetscCall(MatDenseGetLDA(a->A, &i));
2402: PetscCall(PetscBLASIntCast(i, &alda));
2403: PetscCall(MatDenseGetLDA(b->A, &blda));
2404: PetscCall(MatDenseGetLDA(c->A, &i));
2405: PetscCall(PetscBLASIntCast(i, &clda));
2406: /* copy transpose of B into buf[0] */
2407: bn = B->rmap->n;
2408: sendbuf = abt->buf[0];
2409: recvbuf = abt->buf[1];
2410: for (k = 0, j = 0; j < bn; j++) {
2411: for (i = 0; i < cK; i++, k++) sendbuf[k] = bv[i * blda + j];
2412: }
2413: PetscCall(MatDenseRestoreArrayRead(b->A, &bv));
2414: PetscCall(PetscMPIIntCast(bn * cK, &ibn));
2415: PetscCallMPI(MPI_Allgatherv(sendbuf, ibn, MPIU_SCALAR, recvbuf, abt->recvcounts, abt->recvdispls, MPIU_SCALAR, comm));
2416: PetscCall(PetscBLASIntCast(cK, &ck));
2417: PetscCall(PetscBLASIntCast(c->A->rmap->n, &cm));
2418: PetscCall(PetscBLASIntCast(c->A->cmap->n, &cn));
2419: if (cm && cn && ck) PetscCallBLAS("BLASgemm", BLASgemm_("N", "N", &cm, &cn, &ck, &_DOne, av, &alda, recvbuf, &ck, &_DZero, cv, &clda));
2420: PetscCall(MatDenseRestoreArrayRead(a->A, &av));
2421: PetscCall(MatDenseRestoreArrayWrite(c->A, &cv));
2422: PetscFunctionReturn(PETSC_SUCCESS);
2423: }
2425: static PetscErrorCode MatMatTransposeMultNumeric_MPIDense_MPIDense(Mat A, Mat B, Mat C)
2426: {
2427: MatProductCtx_MatTransMultDense *abt;
2429: PetscFunctionBegin;
2430: MatCheckProduct(C, 3);
2431: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2432: abt = (MatProductCtx_MatTransMultDense *)C->product->data;
2433: switch (abt->alg) {
2434: case 1:
2435: PetscCall(MatMatTransposeMultNumeric_MPIDense_MPIDense_Cyclic(A, B, C));
2436: break;
2437: default:
2438: PetscCall(MatMatTransposeMultNumeric_MPIDense_MPIDense_Allgatherv(A, B, C));
2439: break;
2440: }
2441: PetscFunctionReturn(PETSC_SUCCESS);
2442: }
2444: static PetscErrorCode MatProductCtxDestroy_MatMatMult_MPIDense_MPIDense(PetscCtxRt data)
2445: {
2446: MatProductCtx_MatMultDense *ab = *(MatProductCtx_MatMultDense **)data;
2448: PetscFunctionBegin;
2449: PetscCall(MatDestroy(&ab->Ce));
2450: PetscCall(MatDestroy(&ab->Ae));
2451: PetscCall(MatDestroy(&ab->Be));
2452: PetscCall(PetscFree(ab));
2453: PetscFunctionReturn(PETSC_SUCCESS);
2454: }
2456: static PetscErrorCode MatMatMultNumeric_MPIDense_MPIDense(Mat A, Mat B, Mat C)
2457: {
2458: MatProductCtx_MatMultDense *ab;
2459: Mat_MPIDense *mdn = (Mat_MPIDense *)A->data;
2460: Mat_MPIDense *b = (Mat_MPIDense *)B->data;
2462: PetscFunctionBegin;
2463: MatCheckProduct(C, 3);
2464: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Missing product data");
2465: if (!C->rmap->N || !C->cmap->N) PetscFunctionReturn(PETSC_SUCCESS);
2466: ab = (MatProductCtx_MatMultDense *)C->product->data;
2467: if (ab->Ae && ab->Ce) {
2468: #if PetscDefined(HAVE_ELEMENTAL)
2469: PetscCall(MatConvert_MPIDense_Elemental(A, MATELEMENTAL, MAT_REUSE_MATRIX, &ab->Ae));
2470: PetscCall(MatConvert_MPIDense_Elemental(B, MATELEMENTAL, MAT_REUSE_MATRIX, &ab->Be));
2471: PetscCall(MatMatMultNumeric_Elemental(ab->Ae, ab->Be, ab->Ce));
2472: PetscCall(MatConvert(ab->Ce, MATMPIDENSE, MAT_REUSE_MATRIX, &C));
2473: #else
2474: SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "PETSC_HAVE_ELEMENTAL not defined");
2475: #endif
2476: } else {
2477: MPI_Comm comm;
2478: Mat clocal;
2479: const PetscInt *ranges;
2480: PetscMPIInt size;
2482: if (!mdn->Mvctx) PetscCall(MatSetUpMultiply_MPIDense(A)); /* cannot be done during the symbolic phase because of possible calls to MatProductReplaceMats() */
2483: comm = PetscObjectComm((PetscObject)B);
2484: PetscCallMPI(MPI_Comm_size(comm, &size));
2485: PetscCall(PetscLayoutGetRanges(B->rmap, &ranges));
2486: if (ranges[1] == ranges[size]) {
2487: // optimize for the case where the B matrix is broadcast from rank 0
2488: PetscInt b_lda, be_lda;
2489: Mat b_local = b->A;
2490: Mat b_alloc = NULL;
2491: Mat be_local = ab->Be;
2492: Mat be_alloc = NULL;
2493: PetscMemType b_memtype, be_memtype;
2494: const PetscScalar *b_array = NULL;
2495: MPI_Datatype vector_type;
2496: PetscScalar *be_array = NULL;
2497: PetscMPIInt rank;
2499: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2500: PetscCall(MatDenseGetLDA(be_local, &be_lda));
2501: if (be_lda != B->rmap->N) {
2502: PetscCall(MatDuplicate(be_local, MAT_DO_NOT_COPY_VALUES, &be_alloc));
2503: be_local = be_alloc;
2504: }
2506: if (rank == 0) {
2507: PetscCall(MatDenseGetLDA(b_local, &b_lda));
2508: if (b_lda != B->rmap->N) {
2509: PetscCall(MatDuplicate(b_local, MAT_DO_NOT_COPY_VALUES, &b_alloc));
2510: PetscCall(MatCopy(b_local, b_alloc, DIFFERENT_NONZERO_PATTERN));
2511: b_local = b_alloc;
2512: }
2513: }
2514: vector_type = MPIU_SCALAR;
2515: if (B->cmap->N > 1) {
2516: PetscMPIInt mpi_N;
2518: PetscCall(PetscMPIIntCast(B->cmap->N, &mpi_N));
2519: PetscCallMPI(MPI_Type_contiguous(mpi_N, MPIU_SCALAR, &vector_type));
2520: PetscCallMPI(MPI_Type_commit(&vector_type));
2521: }
2522: PetscCall(MatDenseGetArrayReadAndMemType(b_local, &b_array, &b_memtype));
2523: PetscCall(MatDenseGetArrayWriteAndMemType(be_local, &be_array, &be_memtype));
2524: PetscCall(PetscSFBcastWithMemTypeBegin(mdn->Mvctx, vector_type, b_memtype, b_array, be_memtype, be_array, MPI_REPLACE));
2525: PetscCall(PetscSFBcastEnd(mdn->Mvctx, vector_type, b_array, be_array, MPI_REPLACE));
2526: PetscCall(MatDenseRestoreArrayWriteAndMemType(be_local, &be_array));
2527: PetscCall(MatDenseRestoreArrayReadAndMemType(b_local, &b_array));
2528: if (be_local != ab->Be) PetscCall(MatCopy(be_local, ab->Be, DIFFERENT_NONZERO_PATTERN));
2529: if (B->cmap->N > 1) PetscCallMPI(MPI_Type_free(&vector_type));
2530: PetscCall(MatDestroy(&be_alloc));
2531: PetscCall(MatDestroy(&b_alloc));
2532: } else PetscCall(MatDenseScatter_Private(mdn->Mvctx, B, ab->Be, INSERT_VALUES, SCATTER_FORWARD));
2533: PetscCall(MatDenseGetLocalMatrix(C, &clocal));
2534: PetscCall((*clocal->ops->matmultnumeric)(mdn->A, ab->Be, clocal));
2535: }
2536: PetscFunctionReturn(PETSC_SUCCESS);
2537: }
2539: static PetscErrorCode MatMatMultSymbolic_MPIDense_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2540: {
2541: Mat_Product *product = C->product;
2542: PetscInt alg;
2543: MatProductCtx_MatMultDense *ab;
2544: PetscBool flg;
2545: VecType vtype;
2547: PetscFunctionBegin;
2548: MatCheckProduct(C, 4);
2549: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
2550: /* check local size of A and B */
2551: 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 ")",
2552: A->rmap->rstart, A->rmap->rend, B->rmap->rstart, B->rmap->rend);
2554: PetscCall(PetscStrcmp(product->alg, "petsc", &flg));
2555: alg = flg ? 0 : 1;
2557: /* the local product workspace uses the VecType of the output Mat */
2558: PetscCall(PetscObjectObjectTypeCompare((PetscObject)A, (PetscObject)C, &flg));
2559: if (flg == PETSC_TRUE) PetscCall(MatSetVecType(C, A->defaultvectype));
2560: PetscCall(PetscObjectObjectTypeCompare((PetscObject)A, (PetscObject)B, &flg));
2561: PetscCall(MatDenseSetUpProduct_Private(C, A->rmap, B->cmap, A, flg == PETSC_TRUE ? NULL : MATMPIDENSE));
2563: /* create data structure for reuse Cdense */
2564: PetscCall(PetscNew(&ab));
2566: switch (alg) {
2567: case 1: /* alg: "elemental" */
2568: #if PetscDefined(HAVE_ELEMENTAL)
2569: /* create elemental matrices Ae and Be */
2570: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &ab->Ae));
2571: PetscCall(MatSetSizes(ab->Ae, PETSC_DECIDE, PETSC_DECIDE, A->rmap->N, A->cmap->N));
2572: PetscCall(MatSetType(ab->Ae, MATELEMENTAL));
2573: PetscCall(MatSetUp(ab->Ae));
2574: PetscCall(MatSetOption(ab->Ae, MAT_ROW_ORIENTED, PETSC_FALSE));
2576: PetscCall(MatCreate(PetscObjectComm((PetscObject)B), &ab->Be));
2577: PetscCall(MatSetSizes(ab->Be, PETSC_DECIDE, PETSC_DECIDE, B->rmap->N, B->cmap->N));
2578: PetscCall(MatSetType(ab->Be, MATELEMENTAL));
2579: PetscCall(MatSetUp(ab->Be));
2580: PetscCall(MatSetOption(ab->Be, MAT_ROW_ORIENTED, PETSC_FALSE));
2582: /* compute symbolic Ce = Ae*Be */
2583: PetscCall(MatCreate(PetscObjectComm((PetscObject)C), &ab->Ce));
2584: PetscCall(MatMatMultSymbolic_Elemental(ab->Ae, ab->Be, fill, ab->Ce));
2585: #else
2586: SETERRQ(PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "PETSC_HAVE_ELEMENTAL not defined");
2587: #endif
2588: break;
2589: default: /* alg: "petsc" */
2590: ab->Ae = NULL;
2591: PetscCall(MatGetVecType(C, &vtype));
2592: PetscCall(MatCreateDenseFromVecType(PETSC_COMM_SELF, vtype, A->cmap->N, B->cmap->N, A->cmap->N, B->cmap->N, PETSC_DECIDE, NULL, &ab->Be));
2593: ab->Ce = NULL;
2594: break;
2595: }
2597: C->product->data = ab;
2598: C->product->destroy = MatProductCtxDestroy_MatMatMult_MPIDense_MPIDense;
2599: C->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIDense;
2600: PetscFunctionReturn(PETSC_SUCCESS);
2601: }
2603: static PetscErrorCode MatProductSetFromOptions_MPIDense_AB(Mat C)
2604: {
2605: Mat_Product *product = C->product;
2606: const char *algTypes[2] = {"petsc", "elemental"};
2607: PetscInt alg, nalg = PetscDefined(HAVE_ELEMENTAL) ? 2 : 1;
2608: PetscBool flg = PETSC_FALSE;
2610: PetscFunctionBegin;
2611: /* Set default algorithm */
2612: alg = 0; /* default is PETSc */
2613: PetscCall(PetscStrcmp(product->alg, "default", &flg));
2614: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2616: /* Get runtime option */
2617: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_AB", "Mat");
2618: PetscCall(PetscOptionsEList("-mat_product_algorithm", "Algorithmic approach", "MatProduct_AB", algTypes, nalg, algTypes[alg], &alg, &flg));
2619: PetscOptionsEnd();
2620: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2622: C->ops->matmultsymbolic = MatMatMultSymbolic_MPIDense_MPIDense;
2623: C->ops->productsymbolic = MatProductSymbolic_AB;
2624: PetscFunctionReturn(PETSC_SUCCESS);
2625: }
2627: static PetscErrorCode MatProductSetFromOptions_MPIDense_AtB(Mat C)
2628: {
2629: Mat_Product *product = C->product;
2630: Mat A = product->A, B = product->B;
2631: const char *algTypes[] = {MATPRODUCTALGORITHMDEFAULT, MATPRODUCTALGORITHMBACKEND};
2632: PetscInt alg;
2633: PetscBool set;
2635: PetscFunctionBegin;
2636: 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 ")",
2637: A->rmap->rstart, A->rmap->rend, B->rmap->rstart, B->rmap->rend);
2638: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_AtB", "Mat");
2639: PetscCall(PetscOptionsEList(product->api_user == PETSC_TRUE ? "-mattransmatmult_mpidense_mpidense_via" : "-mat_product_algorithm", "Algorithmic approach", "MatTransposeMatMult", algTypes, PETSC_STATIC_ARRAY_LENGTH(algTypes), algTypes[0], &alg, &set));
2640: PetscOptionsEnd();
2641: if (set == PETSC_TRUE) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2642: C->ops->transposematmultsymbolic = MatTransposeMatMultSymbolic_MPIDense_MPIDense;
2643: C->ops->productsymbolic = MatProductSymbolic_AtB;
2644: PetscFunctionReturn(PETSC_SUCCESS);
2645: }
2647: static PetscErrorCode MatProductSetFromOptions_MPIDense_ABt(Mat C)
2648: {
2649: Mat_Product *product = C->product;
2650: const char *algTypes[] = {"allgatherv", "cyclic", MATPRODUCTALGORITHMBACKEND};
2651: PetscInt alg, nalg = PETSC_STATIC_ARRAY_LENGTH(algTypes);
2652: PetscBool flg = PETSC_FALSE;
2654: PetscFunctionBegin;
2655: /* Set default algorithm */
2656: alg = 0; /* default is allgatherv */
2657: PetscCall(PetscStrcmp(product->alg, "default", &flg));
2658: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2660: /* Get runtime option */
2661: if (product->api_user) {
2662: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatMatTransposeMult", "Mat");
2663: PetscCall(PetscOptionsEList("-matmattransmult_mpidense_mpidense_via", "Algorithmic approach", "MatMatTransposeMult", algTypes, nalg, algTypes[alg], &alg, &flg));
2664: PetscOptionsEnd();
2665: } else {
2666: PetscOptionsBegin(PetscObjectComm((PetscObject)C), ((PetscObject)C)->prefix, "MatProduct_ABt", "Mat");
2667: PetscCall(PetscOptionsEList("-mat_product_algorithm", "Algorithmic approach", "MatProduct_ABt", algTypes, nalg, algTypes[alg], &alg, &flg));
2668: PetscOptionsEnd();
2669: }
2670: if (flg) PetscCall(MatProductSetAlgorithm(C, algTypes[alg]));
2672: C->ops->mattransposemultsymbolic = MatMatTransposeMultSymbolic_MPIDense_MPIDense;
2673: C->ops->productsymbolic = MatProductSymbolic_ABt;
2674: PetscFunctionReturn(PETSC_SUCCESS);
2675: }
2677: static PetscErrorCode MatProductSetFromOptions_MPIDense(Mat C)
2678: {
2679: Mat_Product *product = C->product;
2681: PetscFunctionBegin;
2682: switch (product->type) {
2683: case MATPRODUCT_AB:
2684: PetscCall(MatProductSetFromOptions_MPIDense_AB(C));
2685: break;
2686: case MATPRODUCT_AtB:
2687: PetscCall(MatProductSetFromOptions_MPIDense_AtB(C));
2688: break;
2689: case MATPRODUCT_ABt:
2690: PetscCall(MatProductSetFromOptions_MPIDense_ABt(C));
2691: break;
2692: default:
2693: break;
2694: }
2695: PetscFunctionReturn(PETSC_SUCCESS);
2696: }
2698: typedef struct {
2699: PetscSF sf;
2700: PetscInt N, rld, lld, nr, nl;
2701: } MatDenseScatterCtx;
2703: static PetscErrorCode MatDenseScatterCtxDestroy_Private(PetscCtxRt ptr)
2704: {
2705: MatDenseScatterCtx **ctx = (MatDenseScatterCtx **)ptr;
2707: PetscFunctionBegin;
2708: PetscCall(PetscSFDestroy(&(*ctx)->sf));
2709: PetscCall(PetscFree(*ctx));
2710: PetscFunctionReturn(PETSC_SUCCESS);
2711: }
2713: // The communication of MatDenseScatter_Private(), for a caller that already has a PetscSF for the layouts of X and Y
2714: PetscErrorCode MatDenseScatterWithSF_Private(PetscSF vsf, Mat X, Mat Y, MPI_Op op, ScatterMode smode, PetscBool writeonly)
2715: {
2716: const PetscScalar *in;
2717: PetscScalar *out;
2718: PetscMemType mtype[2];
2720: PetscFunctionBegin;
2721: PetscCall(MatDenseGetArrayReadAndMemType(X, &in, &mtype[0]));
2722: if (writeonly == PETSC_TRUE) PetscCall(MatDenseGetArrayWriteAndMemType(Y, &out, &mtype[1]));
2723: else PetscCall(MatDenseGetArrayAndMemType(Y, &out, &mtype[1]));
2724: if (smode == SCATTER_FORWARD) {
2725: PetscCall(PetscSFBcastWithMemTypeBegin(vsf, vsf->vscat.unit, mtype[0], in, mtype[1], out, op));
2726: PetscCall(PetscSFBcastEnd(vsf, vsf->vscat.unit, in, out, op));
2727: } else {
2728: PetscCall(PetscSFReduceWithMemTypeBegin(vsf, vsf->vscat.unit, mtype[0], in, mtype[1], out, op));
2729: PetscCall(PetscSFReduceEnd(vsf, vsf->vscat.unit, in, out, op));
2730: }
2731: if (writeonly == PETSC_TRUE) PetscCall(MatDenseRestoreArrayWriteAndMemType(Y, &out));
2732: else PetscCall(MatDenseRestoreArrayAndMemType(Y, &out));
2733: PetscCall(MatDenseRestoreArrayReadAndMemType(X, &in));
2734: PetscFunctionReturn(PETSC_SUCCESS);
2735: }
2737: // Copies the local rows of the dense matrix A to a new sequential dense matrix B with leading dimension lda
2738: PetscErrorCode MatDenseDuplicateLocalWithLDA_Private(Mat A, PetscInt lda, Mat *B)
2739: {
2740: Mat Al;
2741: MatType type;
2742: PetscInt m, n;
2744: PetscFunctionBegin;
2745: PetscCall(MatDenseGetLocalMatrix(A, &Al));
2746: PetscCall(MatGetSize(Al, &m, &n));
2747: PetscCall(MatGetType(Al, &type));
2748: PetscCall(MatCreate(PETSC_COMM_SELF, B));
2749: PetscCall(MatSetSizes(*B, m, n, m, n));
2750: PetscCall(MatSetType(*B, type));
2751: PetscCall(MatDenseSetLDA(*B, lda));
2752: PetscCall(MatSetUp(*B));
2753: PetscCall(MatCopy(Al, *B, SAME_NONZERO_PATTERN));
2754: PetscFunctionReturn(PETSC_SUCCESS);
2755: }
2757: /* MatDenseScatter_Private - Scatters the columns of the dense matrix X into the dense matrix Y
2759: Collective
2761: Input Parameters:
2762: + sf - the PetscSF for one column
2763: . X - the matrix to read
2764: . Y - the matrix to update
2765: . mode - INSERT_VALUES, ADD_VALUES, MAX_VALUES, or MIN_VALUES
2766: - smode - SCATTER_FORWARD for a broadcast from X to Y, or SCATTER_REVERSE for a reduction from X to Y
2768: Level: developer
2770: Note:
2771: With more than one column, a strided PetscSF is cached on sf. It is rebuilt if the number of columns changes.
2772: A matrix with another leading dimension is copied to a local work matrix with the cached one, because only all
2773: processes together can rebuild the cached PetscSF.
2775: .seealso: `MatDenseScatterWithSF_Private()`, `PetscSFCreateStridedSF()`
2776: */
2777: PetscErrorCode MatDenseScatter_Private(PetscSF sf, Mat X, Mat Y, InsertMode mode, ScatterMode smode)
2778: {
2779: PetscSF vsf;
2780: Mat Xw = NULL, Yw = NULL;
2781: PetscInt N, ny, rld, lld;
2782: MPI_Op op = MPI_OP_NULL;
2783: PetscBool writeonly = PETSC_FALSE;
2785: PetscFunctionBegin;
2789: if (mode == INSERT_VALUES) op = MPI_REPLACE;
2790: else if (mode == ADD_VALUES) op = MPIU_SUM;
2791: else if (mode == MAX_VALUES) op = MPIU_MAX;
2792: else if (mode == MIN_VALUES) op = MPIU_MIN;
2793: PetscCheck(op != MPI_OP_NULL, PetscObjectComm((PetscObject)sf), PETSC_ERR_SUP, "Unsupported InsertMode %d in MatDenseScatter_Private()", mode);
2794: PetscCheck(smode == SCATTER_FORWARD || smode == SCATTER_REVERSE, PetscObjectComm((PetscObject)sf), PETSC_ERR_SUP, "Unsupported ScatterMode %d in MatDenseScatter_Private()", smode);
2795: PetscCall(MatGetSize(X, NULL, &N));
2796: PetscCall(MatGetSize(Y, NULL, &ny));
2797: PetscCheck(N == ny, PetscObjectComm((PetscObject)sf), PETSC_ERR_ARG_SIZ, "Matrix column sizes must match: %" PetscInt_FMT " != %" PetscInt_FMT, N, ny);
2798: if (!N) PetscFunctionReturn(PETSC_SUCCESS);
2799: PetscCall(MatDenseGetLDA(smode == SCATTER_FORWARD ? X : Y, &rld));
2800: PetscCall(MatDenseGetLDA(smode == SCATTER_FORWARD ? Y : X, &lld));
2801: /* get cached or create new strided PetscSF when the number of columns is greater than one */
2802: if (N > 1) {
2803: MatDenseScatterCtx *ctx;
2804: PetscInt nr, nl;
2806: PetscCall(PetscSFGetGraph(sf, &nr, &nl, NULL, NULL));
2807: PetscCall(PetscObjectContainerQuery((PetscObject)sf, "_MatDenseScatter_StridedSF", &ctx));
2808: if (ctx && ctx->N != N) {
2809: PetscCall(PetscObjectCompose((PetscObject)sf, "_MatDenseScatter_StridedSF", NULL));
2810: ctx = NULL;
2811: }
2812: PetscCheck(ctx == NULL || (ctx->nr == nr && ctx->nl == nl), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "The cached strided SF has a different graph; the calling function must destroy it first on all processes with PetscObjectCompose((PetscObject)sf, \"_MatDenseScatter_StridedSF\", NULL)");
2813: if (ctx != NULL) {
2814: if (rld != ctx->rld) PetscCall(MatDenseDuplicateLocalWithLDA_Private(smode == SCATTER_FORWARD ? X : Y, ctx->rld, smode == SCATTER_FORWARD ? &Xw : &Yw));
2815: if (lld != ctx->lld) PetscCall(MatDenseDuplicateLocalWithLDA_Private(smode == SCATTER_FORWARD ? Y : X, ctx->lld, smode == SCATTER_FORWARD ? &Yw : &Xw));
2816: lld = ctx->lld;
2817: } else {
2818: PetscCall(PetscNew(&ctx));
2819: PetscCall(PetscSFCreateStridedSF(sf, N, rld, lld, &ctx->sf));
2820: ctx->N = N;
2821: ctx->rld = rld;
2822: ctx->lld = lld;
2823: ctx->nr = nr;
2824: ctx->nl = nl;
2825: PetscCall(PetscObjectContainerCompose((PetscObject)sf, "_MatDenseScatter_StridedSF", ctx, MatDenseScatterCtxDestroy_Private));
2826: }
2827: vsf = ctx->sf;
2828: } else vsf = sf;
2829: if (mode == INSERT_VALUES && smode == SCATTER_FORWARD) {
2830: const PetscInt *ilocal;
2831: PetscInt nl;
2833: PetscCall(PetscSFGetGraph(sf, NULL, &nl, &ilocal, NULL));
2834: PetscCall(MatGetLocalSize(Y, &ny, NULL));
2835: // preserve untouched rows and padding; contiguous leaves cover every row only in this case
2836: writeonly = (PetscBool)(ilocal == NULL && (PetscCount)nl * PetscMax(sf->vscat.bs, 1) == ny && lld == ny);
2837: }
2838: PetscCall(MatDenseScatterWithSF_Private(vsf, Xw ? Xw : X, Yw ? Yw : Y, op, smode, writeonly));
2839: if (Yw) {
2840: Mat Yl;
2842: PetscCall(MatDenseGetLocalMatrix(Y, &Yl));
2843: PetscCall(MatCopy(Yw, Yl, SAME_NONZERO_PATTERN));
2844: PetscCall(PetscObjectStateIncrease((PetscObject)Y));
2845: }
2846: PetscCall(MatDestroy(&Xw));
2847: PetscCall(MatDestroy(&Yw));
2848: PetscFunctionReturn(PETSC_SUCCESS);
2849: }