Actual source code: dense.c
1: /*
2: Defines the basic matrix operations for sequential dense.
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/seq/dense.h>
8: #include <../src/mat/impls/dense/mpi/mpidense.h>
9: #include <petscblaslapack.h>
10: #include <../src/mat/impls/aij/seq/aij.h>
11: #include <petsc/private/vecimpl.h>
13: PetscErrorCode MatSeqDenseSymmetrize_Private(Mat A, PetscBool hermitian)
14: {
15: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
16: PetscInt j, k, n = A->rmap->n;
17: PetscScalar *v;
19: PetscFunctionBegin;
20: PetscCheck(A->rmap->n == A->cmap->n, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Cannot symmetrize a rectangular matrix");
21: PetscCall(MatDenseGetArray(A, &v));
22: if (!hermitian) {
23: for (k = 0; k < n; k++) {
24: for (j = k; j < n; j++) v[j * mat->lda + k] = v[k * mat->lda + j];
25: }
26: } else {
27: for (k = 0; k < n; k++) {
28: for (j = k; j < n; j++) v[j * mat->lda + k] = PetscConj(v[k * mat->lda + j]);
29: }
30: }
31: PetscCall(MatDenseRestoreArray(A, &v));
32: PetscFunctionReturn(PETSC_SUCCESS);
33: }
35: PetscErrorCode MatSeqDenseInvertFactors_Private(Mat A)
36: {
37: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
38: PetscBLASInt info, n;
40: PetscFunctionBegin;
41: if (!A->rmap->n || !A->cmap->n) PetscFunctionReturn(PETSC_SUCCESS);
42: PetscCall(PetscBLASIntCast(A->cmap->n, &n));
43: if (A->factortype == MAT_FACTOR_LU) {
44: PetscCheck(mat->pivots, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Pivots not present");
45: if (!mat->fwork) {
46: mat->lfwork = n;
47: PetscCall(PetscMalloc1(mat->lfwork, &mat->fwork));
48: }
49: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
50: PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&n, mat->v, &mat->lda, mat->pivots, mat->fwork, &mat->lfwork, &info));
51: PetscCall(PetscFPTrapPop());
52: PetscCall(PetscLogFlops((1.0 * A->cmap->n * A->cmap->n * A->cmap->n) / 3.0));
53: } else if (A->factortype == MAT_FACTOR_CHOLESKY) {
54: if (A->spd == PETSC_BOOL3_TRUE) {
55: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
56: PetscCallBLAS("LAPACKpotri", LAPACKpotri_("L", &n, mat->v, &mat->lda, &info));
57: PetscCall(PetscFPTrapPop());
58: PetscCall(MatSeqDenseSymmetrize_Private(A, PETSC_TRUE));
59: #if PetscDefined(USE_COMPLEX)
60: } else if (A->hermitian == PETSC_BOOL3_TRUE) {
61: PetscCheck(mat->pivots, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Pivots not present");
62: PetscCheck(mat->fwork, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Fwork not present");
63: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
64: PetscCallBLAS("LAPACKhetri", LAPACKhetri_("L", &n, mat->v, &mat->lda, mat->pivots, mat->fwork, &info));
65: PetscCall(PetscFPTrapPop());
66: PetscCall(MatSeqDenseSymmetrize_Private(A, PETSC_TRUE));
67: #endif
68: } else { /* symmetric case */
69: PetscCheck(mat->pivots, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Pivots not present");
70: PetscCheck(mat->fwork, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Fwork not present");
71: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
72: PetscCallBLAS("LAPACKsytri", LAPACKsytri_("L", &n, mat->v, &mat->lda, mat->pivots, mat->fwork, &info));
73: PetscCall(PetscFPTrapPop());
74: PetscCall(MatSeqDenseSymmetrize_Private(A, PETSC_FALSE));
75: }
76: PetscCheck(info >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error in LAPACK argument %" PetscBLASInt_FMT, -info);
77: PetscCheck(info <= 0, PETSC_COMM_SELF, PETSC_ERR_MAT_CH_ZRPVT, "Bad Inversion: zero pivot in row %" PetscBLASInt_FMT, info - 1);
78: PetscCall(PetscLogFlops((1.0 * A->cmap->n * A->cmap->n * A->cmap->n) / 3.0));
79: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Matrix must be factored to solve");
81: A->ops->solve = NULL;
82: A->ops->matsolve = NULL;
83: A->ops->solvetranspose = NULL;
84: A->ops->matsolvetranspose = NULL;
85: A->ops->solveadd = NULL;
86: A->ops->solvetransposeadd = NULL;
87: A->factortype = MAT_FACTOR_NONE;
88: PetscCall(PetscFree(A->solvertype));
89: PetscFunctionReturn(PETSC_SUCCESS);
90: }
92: static PetscErrorCode MatZeroRowsColumns_SeqDense(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
93: {
94: Mat_SeqDense *l = (Mat_SeqDense *)A->data;
95: PetscInt m = l->lda, n = A->cmap->n, r = A->rmap->n, i, j;
96: PetscScalar *slot, *bb, *v;
97: const PetscScalar *xx;
99: PetscFunctionBegin;
100: if (PetscDefined(USE_DEBUG)) {
101: for (i = 0; i < N; i++) {
102: PetscCheck(rows[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative row requested to be zeroed");
103: PetscCheck(rows[i] < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %" PetscInt_FMT " requested to be zeroed greater than or equal number of rows %" PetscInt_FMT, rows[i], A->rmap->n);
104: PetscCheck(rows[i] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Col %" PetscInt_FMT " requested to be zeroed greater than or equal number of cols %" PetscInt_FMT, rows[i], A->cmap->n);
105: }
106: }
107: if (!N) PetscFunctionReturn(PETSC_SUCCESS);
109: /* fix right-hand side if needed */
110: if (x && b) {
111: Vec xt;
113: PetscCheck(A->rmap->n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only coded for square matrices");
114: PetscCall(VecDuplicate(x, &xt));
115: PetscCall(VecCopy(x, xt));
116: PetscCall(VecScale(xt, -1.0));
117: PetscCall(MatMultAdd(A, xt, b, b));
118: PetscCall(VecDestroy(&xt));
119: PetscCall(VecGetArrayRead(x, &xx));
120: PetscCall(VecGetArray(b, &bb));
121: for (i = 0; i < N; i++) bb[rows[i]] = diag * xx[rows[i]];
122: PetscCall(VecRestoreArrayRead(x, &xx));
123: PetscCall(VecRestoreArray(b, &bb));
124: }
126: PetscCall(MatDenseGetArray(A, &v));
127: for (i = 0; i < N; i++) {
128: slot = v + rows[i] * m;
129: PetscCall(PetscArrayzero(slot, r));
130: }
131: for (i = 0; i < N; i++) {
132: slot = v + rows[i];
133: for (j = 0; j < n; j++) {
134: *slot = 0.0;
135: slot += m;
136: }
137: }
138: if (diag != 0.0) {
139: PetscCheck(A->rmap->n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only coded for square matrices");
140: for (i = 0; i < N; i++) {
141: slot = v + (m + 1) * rows[i];
142: *slot = diag;
143: }
144: }
145: PetscCall(MatDenseRestoreArray(A, &v));
146: PetscFunctionReturn(PETSC_SUCCESS);
147: }
149: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
150: {
151: Mat B = NULL;
152: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
153: Mat_SeqDense *b;
154: PetscInt *ai = a->i, *aj = a->j, m = A->rmap->N, n = A->cmap->N, i;
155: const MatScalar *av;
156: PetscBool isseqdense;
158: PetscFunctionBegin;
159: if (reuse == MAT_REUSE_MATRIX) {
160: PetscCall(PetscObjectTypeCompare((PetscObject)*newmat, MATSEQDENSE, &isseqdense));
161: PetscCheck(isseqdense, PetscObjectComm((PetscObject)*newmat), PETSC_ERR_USER, "Cannot reuse matrix of type %s", ((PetscObject)*newmat)->type_name);
162: }
163: if (reuse != MAT_REUSE_MATRIX) {
164: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
165: PetscCall(MatSetSizes(B, m, n, m, n));
166: PetscCall(MatSetType(B, MATSEQDENSE));
167: PetscCall(MatSeqDenseSetPreallocation(B, NULL));
168: b = (Mat_SeqDense *)B->data;
169: } else {
170: b = (Mat_SeqDense *)((*newmat)->data);
171: for (i = 0; i < n; i++) PetscCall(PetscArrayzero(b->v + i * b->lda, m));
172: }
173: PetscCall(MatSeqAIJGetArrayRead(A, &av));
174: for (i = 0; i < m; i++) {
175: PetscInt j;
176: for (j = 0; j < ai[1] - ai[0]; j++) {
177: b->v[*aj * b->lda + i] = *av;
178: aj++;
179: av++;
180: }
181: ai++;
182: }
183: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
185: if (reuse == MAT_INPLACE_MATRIX) {
186: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
187: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
188: PetscCall(MatHeaderReplace(A, &B));
189: } else {
190: if (B) *newmat = B;
191: PetscCall(MatAssemblyBegin(*newmat, MAT_FINAL_ASSEMBLY));
192: PetscCall(MatAssemblyEnd(*newmat, MAT_FINAL_ASSEMBLY));
193: }
194: PetscFunctionReturn(PETSC_SUCCESS);
195: }
197: PETSC_INTERN PetscErrorCode MatConvert_SeqDense_SeqAIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
198: {
199: Mat B = NULL;
200: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
201: PetscInt i, j;
202: PetscInt *rows, *nnz;
203: MatScalar *aa = a->v, *vals;
205: PetscFunctionBegin;
206: PetscCall(PetscCalloc3(A->rmap->n, &rows, A->rmap->n, &nnz, A->rmap->n, &vals));
207: if (reuse != MAT_REUSE_MATRIX) {
208: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
209: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
210: PetscCall(MatSetType(B, MATSEQAIJ));
211: for (j = 0; j < A->cmap->n; j++) {
212: for (i = 0; i < A->rmap->n; i++)
213: if (aa[i] != 0.0 || (i == j && A->cmap->n == A->rmap->n)) ++nnz[i];
214: aa += a->lda;
215: }
216: PetscCall(MatSeqAIJSetPreallocation(B, PETSC_DETERMINE, nnz));
217: } else B = *newmat;
218: aa = a->v;
219: for (j = 0; j < A->cmap->n; j++) {
220: PetscInt numRows = 0;
221: for (i = 0; i < A->rmap->n; i++)
222: if (aa[i] != 0.0 || (i == j && A->cmap->n == A->rmap->n)) {
223: rows[numRows] = i;
224: vals[numRows++] = aa[i];
225: }
226: PetscCall(MatSetValues(B, numRows, rows, 1, &j, vals, INSERT_VALUES));
227: aa += a->lda;
228: }
229: PetscCall(PetscFree3(rows, nnz, vals));
230: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
231: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
233: if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &B));
234: else if (reuse != MAT_REUSE_MATRIX) *newmat = B;
235: PetscFunctionReturn(PETSC_SUCCESS);
236: }
238: PetscErrorCode MatAXPY_SeqDense(Mat Y, PetscScalar alpha, Mat X, MatStructure str)
239: {
240: Mat_SeqDense *x = (Mat_SeqDense *)X->data, *y = (Mat_SeqDense *)Y->data;
241: const PetscScalar *xv;
242: PetscScalar *yv;
243: PetscBLASInt N, m, ldax = 0, lday = 0, one = 1;
245: PetscFunctionBegin;
246: PetscCall(MatDenseGetArrayRead(X, &xv));
247: PetscCall(MatDenseGetArray(Y, &yv));
248: PetscCall(PetscBLASIntCast(X->rmap->n * X->cmap->n, &N));
249: PetscCall(PetscBLASIntCast(X->rmap->n, &m));
250: PetscCall(PetscBLASIntCast(x->lda, &ldax));
251: PetscCall(PetscBLASIntCast(y->lda, &lday));
252: if (ldax > m || lday > m) {
253: for (PetscInt j = 0; j < X->cmap->n; j++) PetscCallBLAS("BLASaxpy", BLASaxpy_(&m, &alpha, PetscSafePointerPlusOffset(xv, j * ldax), &one, PetscSafePointerPlusOffset(yv, j * lday), &one));
254: } else {
255: PetscCallBLAS("BLASaxpy", BLASaxpy_(&N, &alpha, xv, &one, yv, &one));
256: }
257: PetscCall(MatDenseRestoreArrayRead(X, &xv));
258: PetscCall(MatDenseRestoreArray(Y, &yv));
259: PetscCall(PetscLogFlops(PetscMax(2.0 * N - 1, 0)));
260: PetscFunctionReturn(PETSC_SUCCESS);
261: }
263: static PetscErrorCode MatGetInfo_SeqDense(Mat A, MatInfoType flag, MatInfo *info)
264: {
265: PetscLogDouble N = A->rmap->n * A->cmap->n;
267: PetscFunctionBegin;
268: info->block_size = 1.0;
269: info->nz_allocated = N;
270: info->nz_used = N;
271: info->nz_unneeded = 0;
272: info->assemblies = A->num_ass;
273: info->mallocs = 0;
274: info->memory = 0; /* REVIEW ME */
275: info->fill_ratio_given = 0;
276: info->fill_ratio_needed = 0;
277: info->factor_mallocs = 0;
278: PetscFunctionReturn(PETSC_SUCCESS);
279: }
281: PetscErrorCode MatScale_SeqDense(Mat A, PetscScalar alpha)
282: {
283: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
284: PetscScalar *v;
285: PetscBLASInt one = 1, j, nz, lda = 0;
287: PetscFunctionBegin;
288: PetscCall(MatDenseGetArray(A, &v));
289: PetscCall(PetscBLASIntCast(a->lda, &lda));
290: if (lda > A->rmap->n) {
291: PetscCall(PetscBLASIntCast(A->rmap->n, &nz));
292: for (j = 0; j < A->cmap->n; j++) PetscCallBLAS("BLASscal", BLASscal_(&nz, &alpha, v + j * lda, &one));
293: } else {
294: PetscCall(PetscBLASIntCast(A->rmap->n * A->cmap->n, &nz));
295: PetscCallBLAS("BLASscal", BLASscal_(&nz, &alpha, v, &one));
296: }
297: PetscCall(PetscLogFlops(A->rmap->n * A->cmap->n));
298: PetscCall(MatDenseRestoreArray(A, &v));
299: PetscFunctionReturn(PETSC_SUCCESS);
300: }
302: PetscErrorCode MatShift_SeqDense(Mat A, PetscScalar alpha)
303: {
304: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
305: PetscScalar *v;
306: PetscInt j, k;
308: PetscFunctionBegin;
309: PetscCall(MatDenseGetArray(A, &v));
310: k = PetscMin(A->rmap->n, A->cmap->n);
311: for (j = 0; j < k; j++) v[j + j * a->lda] += alpha;
312: PetscCall(PetscLogFlops(k));
313: PetscCall(MatDenseRestoreArray(A, &v));
314: PetscFunctionReturn(PETSC_SUCCESS);
315: }
317: static PetscErrorCode MatIsHermitian_SeqDense(Mat A, PetscReal rtol, PetscBool *fl)
318: {
319: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
320: PetscInt i, j, m = A->rmap->n, N = a->lda;
321: const PetscScalar *v;
323: PetscFunctionBegin;
324: *fl = PETSC_FALSE;
325: if (A->rmap->n != A->cmap->n) PetscFunctionReturn(PETSC_SUCCESS);
326: PetscCall(MatDenseGetArrayRead(A, &v));
327: for (i = 0; i < m; i++) {
328: for (j = i; j < m; j++) {
329: if (PetscAbsScalar(v[i + j * N] - PetscConj(v[j + i * N])) > rtol) goto restore;
330: }
331: }
332: *fl = PETSC_TRUE;
333: restore:
334: PetscCall(MatDenseRestoreArrayRead(A, &v));
335: PetscFunctionReturn(PETSC_SUCCESS);
336: }
338: static PetscErrorCode MatIsSymmetric_SeqDense(Mat A, PetscReal rtol, PetscBool *fl)
339: {
340: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
341: PetscInt i, j, m = A->rmap->n, N = a->lda;
342: const PetscScalar *v;
344: PetscFunctionBegin;
345: *fl = PETSC_FALSE;
346: if (A->rmap->n != A->cmap->n) PetscFunctionReturn(PETSC_SUCCESS);
347: PetscCall(MatDenseGetArrayRead(A, &v));
348: for (i = 0; i < m; i++) {
349: for (j = i; j < m; j++) {
350: if (PetscAbsScalar(v[i + j * N] - v[j + i * N]) > rtol) goto restore;
351: }
352: }
353: *fl = PETSC_TRUE;
354: restore:
355: PetscCall(MatDenseRestoreArrayRead(A, &v));
356: PetscFunctionReturn(PETSC_SUCCESS);
357: }
359: PetscErrorCode MatDuplicateNoCreate_SeqDense(Mat newi, Mat A, MatDuplicateOption cpvalues)
360: {
361: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
362: PetscInt lda = mat->lda, j, m, nlda = lda;
363: PetscBool isdensecpu;
365: PetscFunctionBegin;
366: PetscCall(PetscLayoutReference(A->rmap, &newi->rmap));
367: PetscCall(PetscLayoutReference(A->cmap, &newi->cmap));
368: if (cpvalues == MAT_SHARE_NONZERO_PATTERN) { /* propagate LDA */
369: PetscCall(MatDenseSetLDA(newi, lda));
370: }
371: PetscCall(PetscObjectTypeCompare((PetscObject)newi, MATSEQDENSE, &isdensecpu));
372: if (isdensecpu) PetscCall(MatSeqDenseSetPreallocation(newi, NULL));
373: if (cpvalues == MAT_COPY_VALUES) {
374: const PetscScalar *av;
375: PetscScalar *v;
377: PetscCall(MatDenseGetArrayRead(A, &av));
378: PetscCall(MatDenseGetArrayWrite(newi, &v));
379: PetscCall(MatDenseGetLDA(newi, &nlda));
380: m = A->rmap->n;
381: if (lda > m || nlda > m) {
382: for (j = 0; j < A->cmap->n; j++) PetscCall(PetscArraycpy(PetscSafePointerPlusOffset(v, j * nlda), PetscSafePointerPlusOffset(av, j * lda), m));
383: } else {
384: PetscCall(PetscArraycpy(v, av, A->rmap->n * A->cmap->n));
385: }
386: PetscCall(MatDenseRestoreArrayWrite(newi, &v));
387: PetscCall(MatDenseRestoreArrayRead(A, &av));
388: PetscCall(MatPropagateSymmetryOptions(A, newi));
389: }
390: PetscFunctionReturn(PETSC_SUCCESS);
391: }
393: PetscErrorCode MatDuplicate_SeqDense(Mat A, MatDuplicateOption cpvalues, Mat *newmat)
394: {
395: PetscFunctionBegin;
396: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), newmat));
397: PetscCall(MatSetSizes(*newmat, A->rmap->n, A->cmap->n, A->rmap->n, A->cmap->n));
398: PetscCall(MatSetType(*newmat, ((PetscObject)A)->type_name));
399: PetscCall(MatDuplicateNoCreate_SeqDense(*newmat, A, cpvalues));
400: PetscFunctionReturn(PETSC_SUCCESS);
401: }
403: static PetscErrorCode MatSolve_SeqDense_Internal_LU(Mat A, PetscScalar *x, PetscBLASInt ldx, PetscBLASInt m, PetscBLASInt nrhs, PetscBLASInt k, PetscBool T)
404: {
405: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
407: PetscFunctionBegin;
408: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
409: PetscCallLAPACKInfo("LAPACKgetrs", LAPACKgetrs_(T ? "T" : "N", &m, &nrhs, mat->v, &mat->lda, mat->pivots, x, &m, &info));
410: PetscCall(PetscFPTrapPop());
411: PetscCall(PetscLogFlops(nrhs * (2.0 * m * m - m)));
412: PetscFunctionReturn(PETSC_SUCCESS);
413: }
415: static PetscErrorCode MatSolve_SeqDense_Internal_Cholesky(Mat A, PetscScalar *x, PetscBLASInt ldx, PetscBLASInt m, PetscBLASInt nrhs, PetscBLASInt k, PetscBool T)
416: {
417: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
419: PetscFunctionBegin;
420: if (A->spd == PETSC_BOOL3_TRUE) {
421: if (PetscDefined(USE_COMPLEX) && T) PetscCall(MatConjugate_SeqDense(A));
422: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
423: PetscCallLAPACKInfo("LAPACKpotrs", LAPACKpotrs_("L", &m, &nrhs, mat->v, &mat->lda, x, &m, &info));
424: PetscCall(PetscFPTrapPop());
425: if (PetscDefined(USE_COMPLEX) && T) PetscCall(MatConjugate_SeqDense(A));
426: #if PetscDefined(USE_COMPLEX)
427: } else if (A->hermitian == PETSC_BOOL3_TRUE) {
428: if (T) PetscCall(MatConjugate_SeqDense(A));
429: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
430: PetscCallLAPACKInfo("LAPACKhetrs", LAPACKhetrs_("L", &m, &nrhs, mat->v, &mat->lda, mat->pivots, x, &m, &info));
431: PetscCall(PetscFPTrapPop());
432: if (T) PetscCall(MatConjugate_SeqDense(A));
433: #endif
434: } else { /* symmetric case */
435: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
436: PetscCallLAPACKInfo("LAPACKsytrs", LAPACKsytrs_("L", &m, &nrhs, mat->v, &mat->lda, mat->pivots, x, &m, &info));
437: PetscCall(PetscFPTrapPop());
438: }
439: PetscCall(PetscLogFlops(nrhs * (2.0 * m * m - m)));
440: PetscFunctionReturn(PETSC_SUCCESS);
441: }
443: static PetscErrorCode MatSolve_SeqDense_Internal_QR(Mat A, PetscScalar *x, PetscBLASInt ldx, PetscBLASInt m, PetscBLASInt nrhs, PetscBLASInt k)
444: {
445: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
446: char trans;
448: PetscFunctionBegin;
449: if (PetscDefined(USE_COMPLEX)) {
450: trans = 'C';
451: } else {
452: trans = 'T';
453: }
454: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
455: { /* lwork depends on the number of right-hand sides */
456: PetscBLASInt nlfwork, lfwork = -1;
457: PetscScalar fwork;
459: PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("L", &trans, &m, &nrhs, &mat->rank, mat->v, &mat->lda, mat->tau, x, &ldx, &fwork, &lfwork, &info));
460: nlfwork = (PetscBLASInt)PetscRealPart(fwork);
461: if (nlfwork > mat->lfwork) {
462: mat->lfwork = nlfwork;
463: PetscCall(PetscFree(mat->fwork));
464: PetscCall(PetscMalloc1(mat->lfwork, &mat->fwork));
465: }
466: }
467: PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("L", &trans, &m, &nrhs, &mat->rank, mat->v, &mat->lda, mat->tau, x, &ldx, mat->fwork, &mat->lfwork, &info));
468: PetscCall(PetscFPTrapPop());
469: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
470: PetscCallLAPACKInfo("LAPACKtrtrs", LAPACKtrtrs_("U", "N", "N", &mat->rank, &nrhs, mat->v, &mat->lda, x, &ldx, &info));
471: PetscCall(PetscFPTrapPop());
472: for (PetscInt j = 0; j < nrhs; j++) {
473: for (PetscInt i = mat->rank; i < k; i++) x[j * ldx + i] = 0.;
474: }
475: PetscCall(PetscLogFlops(nrhs * (4.0 * m * mat->rank - PetscSqr(mat->rank))));
476: PetscFunctionReturn(PETSC_SUCCESS);
477: }
479: static PetscErrorCode MatSolveTranspose_SeqDense_Internal_QR(Mat A, PetscScalar *x, PetscBLASInt ldx, PetscBLASInt m, PetscBLASInt nrhs, PetscBLASInt k)
480: {
481: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
483: PetscFunctionBegin;
484: if (A->rmap->n == A->cmap->n && mat->rank == A->rmap->n) {
485: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
486: PetscCallLAPACKInfo("LAPACKtrtrs", LAPACKtrtrs_("U", "T", "N", &m, &nrhs, mat->v, &mat->lda, x, &ldx, &info));
487: PetscCall(PetscFPTrapPop());
488: if (PetscDefined(USE_COMPLEX)) PetscCall(MatConjugate_SeqDense(A));
489: { /* lwork depends on the number of right-hand sides */
490: PetscBLASInt nlfwork, lfwork = -1;
491: PetscScalar fwork;
493: PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("L", "N", &m, &nrhs, &mat->rank, mat->v, &mat->lda, mat->tau, x, &ldx, &fwork, &lfwork, &info));
494: nlfwork = (PetscBLASInt)PetscRealPart(fwork);
495: if (nlfwork > mat->lfwork) {
496: mat->lfwork = nlfwork;
497: PetscCall(PetscFree(mat->fwork));
498: PetscCall(PetscMalloc1(mat->lfwork, &mat->fwork));
499: }
500: }
501: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
502: PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("L", "N", &m, &nrhs, &mat->rank, mat->v, &mat->lda, mat->tau, x, &ldx, mat->fwork, &mat->lfwork, &info));
503: PetscCall(PetscFPTrapPop());
504: if (PetscDefined(USE_COMPLEX)) PetscCall(MatConjugate_SeqDense(A));
505: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "QR factored matrix cannot be used for transpose solve");
506: PetscCall(PetscLogFlops(nrhs * (4.0 * m * mat->rank - PetscSqr(mat->rank))));
507: PetscFunctionReturn(PETSC_SUCCESS);
508: }
510: static PetscErrorCode MatSolve_SeqDense_SetUp(Mat A, Vec xx, Vec yy, PetscScalar **_y, PetscBLASInt *_m, PetscBLASInt *_k)
511: {
512: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
513: PetscScalar *y;
514: PetscBLASInt m = 0, k = 0;
516: PetscFunctionBegin;
517: PetscCall(PetscBLASIntCast(A->rmap->n, &m));
518: PetscCall(PetscBLASIntCast(A->cmap->n, &k));
519: if (k < m) {
520: PetscCall(VecCopy(xx, mat->qrrhs));
521: PetscCall(VecGetArray(mat->qrrhs, &y));
522: } else {
523: PetscCall(VecCopy(xx, yy));
524: PetscCall(VecGetArray(yy, &y));
525: }
526: *_y = y;
527: *_k = k;
528: *_m = m;
529: PetscFunctionReturn(PETSC_SUCCESS);
530: }
532: static PetscErrorCode MatSolve_SeqDense_TearDown(Mat A, Vec xx, Vec yy, PetscScalar **_y, PetscBLASInt *_m, PetscBLASInt *_k)
533: {
534: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
535: PetscScalar *y = NULL;
536: PetscBLASInt m, k;
538: PetscFunctionBegin;
539: y = *_y;
540: *_y = NULL;
541: k = *_k;
542: m = *_m;
543: if (k < m) {
544: PetscScalar *yv;
545: PetscCall(VecGetArray(yy, &yv));
546: PetscCall(PetscArraycpy(yv, y, k));
547: PetscCall(VecRestoreArray(yy, &yv));
548: PetscCall(VecRestoreArray(mat->qrrhs, &y));
549: } else {
550: PetscCall(VecRestoreArray(yy, &y));
551: }
552: PetscFunctionReturn(PETSC_SUCCESS);
553: }
555: static PetscErrorCode MatSolve_SeqDense_LU(Mat A, Vec xx, Vec yy)
556: {
557: PetscScalar *y = NULL;
558: PetscBLASInt m = 0, k = 0;
560: PetscFunctionBegin;
561: PetscCall(MatSolve_SeqDense_SetUp(A, xx, yy, &y, &m, &k));
562: PetscCall(MatSolve_SeqDense_Internal_LU(A, y, m, m, 1, k, PETSC_FALSE));
563: PetscCall(MatSolve_SeqDense_TearDown(A, xx, yy, &y, &m, &k));
564: PetscFunctionReturn(PETSC_SUCCESS);
565: }
567: static PetscErrorCode MatSolveTranspose_SeqDense_LU(Mat A, Vec xx, Vec yy)
568: {
569: PetscScalar *y = NULL;
570: PetscBLASInt m = 0, k = 0;
572: PetscFunctionBegin;
573: PetscCall(MatSolve_SeqDense_SetUp(A, xx, yy, &y, &m, &k));
574: PetscCall(MatSolve_SeqDense_Internal_LU(A, y, m, m, 1, k, PETSC_TRUE));
575: PetscCall(MatSolve_SeqDense_TearDown(A, xx, yy, &y, &m, &k));
576: PetscFunctionReturn(PETSC_SUCCESS);
577: }
579: static PetscErrorCode MatSolve_SeqDense_Cholesky(Mat A, Vec xx, Vec yy)
580: {
581: PetscScalar *y = NULL;
582: PetscBLASInt m = 0, k = 0;
584: PetscFunctionBegin;
585: PetscCall(MatSolve_SeqDense_SetUp(A, xx, yy, &y, &m, &k));
586: PetscCall(MatSolve_SeqDense_Internal_Cholesky(A, y, m, m, 1, k, PETSC_FALSE));
587: PetscCall(MatSolve_SeqDense_TearDown(A, xx, yy, &y, &m, &k));
588: PetscFunctionReturn(PETSC_SUCCESS);
589: }
591: static PetscErrorCode MatSolveTranspose_SeqDense_Cholesky(Mat A, Vec xx, Vec yy)
592: {
593: PetscScalar *y = NULL;
594: PetscBLASInt m = 0, k = 0;
596: PetscFunctionBegin;
597: PetscCall(MatSolve_SeqDense_SetUp(A, xx, yy, &y, &m, &k));
598: PetscCall(MatSolve_SeqDense_Internal_Cholesky(A, y, m, m, 1, k, PETSC_TRUE));
599: PetscCall(MatSolve_SeqDense_TearDown(A, xx, yy, &y, &m, &k));
600: PetscFunctionReturn(PETSC_SUCCESS);
601: }
603: static PetscErrorCode MatSolve_SeqDense_QR(Mat A, Vec xx, Vec yy)
604: {
605: PetscScalar *y = NULL;
606: PetscBLASInt m = 0, k = 0;
608: PetscFunctionBegin;
609: PetscCall(MatSolve_SeqDense_SetUp(A, xx, yy, &y, &m, &k));
610: PetscCall(MatSolve_SeqDense_Internal_QR(A, y, PetscMax(m, k), m, 1, k));
611: PetscCall(MatSolve_SeqDense_TearDown(A, xx, yy, &y, &m, &k));
612: PetscFunctionReturn(PETSC_SUCCESS);
613: }
615: static PetscErrorCode MatSolveTranspose_SeqDense_QR(Mat A, Vec xx, Vec yy)
616: {
617: PetscScalar *y = NULL;
618: PetscBLASInt m = 0, k = 0;
620: PetscFunctionBegin;
621: PetscCall(MatSolve_SeqDense_SetUp(A, xx, yy, &y, &m, &k));
622: PetscCall(MatSolveTranspose_SeqDense_Internal_QR(A, y, PetscMax(m, k), m, 1, k));
623: PetscCall(MatSolve_SeqDense_TearDown(A, xx, yy, &y, &m, &k));
624: PetscFunctionReturn(PETSC_SUCCESS);
625: }
627: static PetscErrorCode MatMatSolve_SeqDense_SetUp(Mat A, Mat B, Mat X, PetscScalar **_y, PetscBLASInt *_ldy, PetscBLASInt *_m, PetscBLASInt *_nrhs, PetscBLASInt *_k)
628: {
629: const PetscScalar *b;
630: PetscScalar *y;
631: PetscInt n, _ldb, _ldx;
632: PetscBLASInt nrhs = 0, m = 0, k = 0, ldb = 0, ldx = 0, ldy = 0;
634: PetscFunctionBegin;
635: *_ldy = 0;
636: *_m = 0;
637: *_nrhs = 0;
638: *_k = 0;
639: *_y = NULL;
640: PetscCall(PetscBLASIntCast(A->rmap->n, &m));
641: PetscCall(PetscBLASIntCast(A->cmap->n, &k));
642: PetscCall(MatGetSize(B, NULL, &n));
643: PetscCall(PetscBLASIntCast(n, &nrhs));
644: PetscCall(MatDenseGetLDA(B, &_ldb));
645: PetscCall(PetscBLASIntCast(_ldb, &ldb));
646: PetscCall(MatDenseGetLDA(X, &_ldx));
647: PetscCall(PetscBLASIntCast(_ldx, &ldx));
648: if (ldx < m) {
649: PetscCall(MatDenseGetArrayRead(B, &b));
650: PetscCall(PetscMalloc1(nrhs * m, &y));
651: if (ldb == m) {
652: PetscCall(PetscArraycpy(y, b, ldb * nrhs));
653: } else {
654: for (PetscInt j = 0; j < nrhs; j++) PetscCall(PetscArraycpy(&y[j * m], &b[j * ldb], m));
655: }
656: ldy = m;
657: PetscCall(MatDenseRestoreArrayRead(B, &b));
658: } else {
659: if (ldb == ldx) {
660: PetscCall(MatCopy(B, X, SAME_NONZERO_PATTERN));
661: PetscCall(MatDenseGetArray(X, &y));
662: } else {
663: PetscCall(MatDenseGetArray(X, &y));
664: PetscCall(MatDenseGetArrayRead(B, &b));
665: for (PetscInt j = 0; j < nrhs; j++) PetscCall(PetscArraycpy(&y[j * ldx], &b[j * ldb], m));
666: PetscCall(MatDenseRestoreArrayRead(B, &b));
667: }
668: ldy = ldx;
669: }
670: *_y = y;
671: *_ldy = ldy;
672: *_k = k;
673: *_m = m;
674: *_nrhs = nrhs;
675: PetscFunctionReturn(PETSC_SUCCESS);
676: }
678: static PetscErrorCode MatMatSolve_SeqDense_TearDown(Mat A, Mat B, Mat X, PetscScalar **_y, PetscBLASInt *_ldy, PetscBLASInt *_m, PetscBLASInt *_nrhs, PetscBLASInt *_k)
679: {
680: PetscScalar *y;
681: PetscInt _ldx;
682: PetscBLASInt k, ldy, nrhs, ldx = 0;
684: PetscFunctionBegin;
685: y = *_y;
686: *_y = NULL;
687: k = *_k;
688: ldy = *_ldy;
689: nrhs = *_nrhs;
690: PetscCall(MatDenseGetLDA(X, &_ldx));
691: PetscCall(PetscBLASIntCast(_ldx, &ldx));
692: if (ldx != ldy) {
693: PetscScalar *xv;
694: PetscCall(MatDenseGetArray(X, &xv));
695: for (PetscInt j = 0; j < nrhs; j++) PetscCall(PetscArraycpy(&xv[j * ldx], &y[j * ldy], k));
696: PetscCall(MatDenseRestoreArray(X, &xv));
697: PetscCall(PetscFree(y));
698: } else {
699: PetscCall(MatDenseRestoreArray(X, &y));
700: }
701: PetscFunctionReturn(PETSC_SUCCESS);
702: }
704: static PetscErrorCode MatMatSolve_SeqDense_LU(Mat A, Mat B, Mat X)
705: {
706: PetscScalar *y;
707: PetscBLASInt m, k, ldy, nrhs;
709: PetscFunctionBegin;
710: PetscCall(MatMatSolve_SeqDense_SetUp(A, B, X, &y, &ldy, &m, &nrhs, &k));
711: PetscCall(MatSolve_SeqDense_Internal_LU(A, y, ldy, m, nrhs, k, PETSC_FALSE));
712: PetscCall(MatMatSolve_SeqDense_TearDown(A, B, X, &y, &ldy, &m, &nrhs, &k));
713: PetscFunctionReturn(PETSC_SUCCESS);
714: }
716: static PetscErrorCode MatMatSolveTranspose_SeqDense_LU(Mat A, Mat B, Mat X)
717: {
718: PetscScalar *y;
719: PetscBLASInt m, k, ldy, nrhs;
721: PetscFunctionBegin;
722: PetscCall(MatMatSolve_SeqDense_SetUp(A, B, X, &y, &ldy, &m, &nrhs, &k));
723: PetscCall(MatSolve_SeqDense_Internal_LU(A, y, ldy, m, nrhs, k, PETSC_TRUE));
724: PetscCall(MatMatSolve_SeqDense_TearDown(A, B, X, &y, &ldy, &m, &nrhs, &k));
725: PetscFunctionReturn(PETSC_SUCCESS);
726: }
728: static PetscErrorCode MatMatSolve_SeqDense_Cholesky(Mat A, Mat B, Mat X)
729: {
730: PetscScalar *y;
731: PetscBLASInt m, k, ldy, nrhs;
733: PetscFunctionBegin;
734: PetscCall(MatMatSolve_SeqDense_SetUp(A, B, X, &y, &ldy, &m, &nrhs, &k));
735: PetscCall(MatSolve_SeqDense_Internal_Cholesky(A, y, ldy, m, nrhs, k, PETSC_FALSE));
736: PetscCall(MatMatSolve_SeqDense_TearDown(A, B, X, &y, &ldy, &m, &nrhs, &k));
737: PetscFunctionReturn(PETSC_SUCCESS);
738: }
740: static PetscErrorCode MatMatSolveTranspose_SeqDense_Cholesky(Mat A, Mat B, Mat X)
741: {
742: PetscScalar *y;
743: PetscBLASInt m, k, ldy, nrhs;
745: PetscFunctionBegin;
746: PetscCall(MatMatSolve_SeqDense_SetUp(A, B, X, &y, &ldy, &m, &nrhs, &k));
747: PetscCall(MatSolve_SeqDense_Internal_Cholesky(A, y, ldy, m, nrhs, k, PETSC_TRUE));
748: PetscCall(MatMatSolve_SeqDense_TearDown(A, B, X, &y, &ldy, &m, &nrhs, &k));
749: PetscFunctionReturn(PETSC_SUCCESS);
750: }
752: static PetscErrorCode MatMatSolve_SeqDense_QR(Mat A, Mat B, Mat X)
753: {
754: PetscScalar *y;
755: PetscBLASInt m, k, ldy, nrhs;
757: PetscFunctionBegin;
758: PetscCall(MatMatSolve_SeqDense_SetUp(A, B, X, &y, &ldy, &m, &nrhs, &k));
759: PetscCall(MatSolve_SeqDense_Internal_QR(A, y, ldy, m, nrhs, k));
760: PetscCall(MatMatSolve_SeqDense_TearDown(A, B, X, &y, &ldy, &m, &nrhs, &k));
761: PetscFunctionReturn(PETSC_SUCCESS);
762: }
764: static PetscErrorCode MatMatSolveTranspose_SeqDense_QR(Mat A, Mat B, Mat X)
765: {
766: PetscScalar *y;
767: PetscBLASInt m, k, ldy, nrhs;
769: PetscFunctionBegin;
770: PetscCall(MatMatSolve_SeqDense_SetUp(A, B, X, &y, &ldy, &m, &nrhs, &k));
771: PetscCall(MatSolveTranspose_SeqDense_Internal_QR(A, y, ldy, m, nrhs, k));
772: PetscCall(MatMatSolve_SeqDense_TearDown(A, B, X, &y, &ldy, &m, &nrhs, &k));
773: PetscFunctionReturn(PETSC_SUCCESS);
774: }
776: /* COMMENT: I have chosen to hide row permutation in the pivots,
777: rather than put it in the Mat->row slot.*/
778: PetscErrorCode MatLUFactor_SeqDense(Mat A, IS row, IS col, PETSC_UNUSED const MatFactorInfo *minfo)
779: {
780: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
781: PetscBLASInt n, m, info;
783: PetscFunctionBegin;
784: PetscCall(PetscBLASIntCast(A->cmap->n, &n));
785: PetscCall(PetscBLASIntCast(A->rmap->n, &m));
786: if (!mat->pivots) PetscCall(PetscMalloc1(A->rmap->n, &mat->pivots));
787: if (!A->rmap->n || !A->cmap->n) PetscFunctionReturn(PETSC_SUCCESS);
788: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
789: PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&m, &n, mat->v, &mat->lda, mat->pivots, &info));
790: PetscCheck(info >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error in LAPACK argument %" PetscBLASInt_FMT, -info);
791: PetscCheck(info <= 0, PETSC_COMM_SELF, PETSC_ERR_MAT_LU_ZRPVT, "Bad factorization: zero pivot in row %" PetscBLASInt_FMT, info - 1);
792: PetscCall(PetscFPTrapPop());
794: A->ops->solve = MatSolve_SeqDense_LU;
795: A->ops->matsolve = MatMatSolve_SeqDense_LU;
796: A->ops->solvetranspose = MatSolveTranspose_SeqDense_LU;
797: A->ops->matsolvetranspose = MatMatSolveTranspose_SeqDense_LU;
798: A->factortype = MAT_FACTOR_LU;
800: PetscCall(PetscFree(A->solvertype));
801: PetscCall(PetscStrallocpy(MATSOLVERPETSC, &A->solvertype));
803: PetscCall(PetscLogFlops((2.0 * A->cmap->n * A->cmap->n * A->cmap->n) / 3));
804: PetscFunctionReturn(PETSC_SUCCESS);
805: }
807: static PetscErrorCode MatLUFactorNumeric_SeqDense(Mat fact, Mat A, const MatFactorInfo *info)
808: {
809: PetscFunctionBegin;
810: PetscCall(MatDuplicateNoCreate_SeqDense(fact, A, MAT_COPY_VALUES));
811: PetscUseTypeMethod(fact, lufactor, NULL, NULL, info);
812: PetscFunctionReturn(PETSC_SUCCESS);
813: }
815: PetscErrorCode MatLUFactorSymbolic_SeqDense(Mat fact, Mat A, IS row, IS col, PETSC_UNUSED const MatFactorInfo *info)
816: {
817: PetscFunctionBegin;
818: fact->preallocated = PETSC_TRUE;
819: fact->assembled = PETSC_TRUE;
820: fact->ops->lufactornumeric = MatLUFactorNumeric_SeqDense;
821: PetscFunctionReturn(PETSC_SUCCESS);
822: }
824: /* Cholesky as L*L^T or L*D*L^T and the symmetric/hermitian complex variants */
825: PetscErrorCode MatCholeskyFactor_SeqDense(Mat A, IS perm, PETSC_UNUSED const MatFactorInfo *minfo)
826: {
827: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
828: PetscBLASInt info, n;
830: PetscFunctionBegin;
831: PetscCall(PetscBLASIntCast(A->cmap->n, &n));
832: if (!A->rmap->n || !A->cmap->n) PetscFunctionReturn(PETSC_SUCCESS);
833: if (A->spd == PETSC_BOOL3_TRUE) {
834: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
835: PetscCallBLAS("LAPACKpotrf", LAPACKpotrf_("L", &n, mat->v, &mat->lda, &info));
836: PetscCall(PetscFPTrapPop());
837: #if PetscDefined(USE_COMPLEX)
838: } else if (A->hermitian == PETSC_BOOL3_TRUE) {
839: if (!mat->pivots) PetscCall(PetscMalloc1(A->rmap->n, &mat->pivots));
840: if (!mat->fwork) {
841: PetscScalar dummy;
843: mat->lfwork = -1;
844: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
845: PetscCallBLAS("LAPACKhetrf", LAPACKhetrf_("L", &n, mat->v, &mat->lda, mat->pivots, &dummy, &mat->lfwork, &info));
846: PetscCall(PetscFPTrapPop());
847: PetscCall(PetscBLASIntCast((PetscCount)(PetscRealPart(dummy)), &mat->lfwork));
848: PetscCall(PetscMalloc1(mat->lfwork, &mat->fwork));
849: }
850: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
851: PetscCallBLAS("LAPACKhetrf", LAPACKhetrf_("L", &n, mat->v, &mat->lda, mat->pivots, mat->fwork, &mat->lfwork, &info));
852: PetscCall(PetscFPTrapPop());
853: #endif
854: } else { /* symmetric case */
855: if (!mat->pivots) PetscCall(PetscMalloc1(A->rmap->n, &mat->pivots));
856: if (!mat->fwork) {
857: PetscScalar dummy;
859: mat->lfwork = -1;
860: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
861: PetscCallBLAS("LAPACKsytrf", LAPACKsytrf_("L", &n, mat->v, &mat->lda, mat->pivots, &dummy, &mat->lfwork, &info));
862: PetscCall(PetscFPTrapPop());
863: PetscCall(PetscBLASIntCast((PetscCount)(PetscRealPart(dummy)), &mat->lfwork));
864: PetscCall(PetscMalloc1(mat->lfwork, &mat->fwork));
865: }
866: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
867: PetscCallBLAS("LAPACKsytrf", LAPACKsytrf_("L", &n, mat->v, &mat->lda, mat->pivots, mat->fwork, &mat->lfwork, &info));
868: PetscCall(PetscFPTrapPop());
869: }
870: PetscCheck(info >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error in LAPACK argument %" PetscBLASInt_FMT, -info);
871: PetscCheck(info <= 0, PETSC_COMM_SELF, PETSC_ERR_MAT_CH_ZRPVT, "Bad factorization: zero pivot in row %" PetscBLASInt_FMT, info - 1);
873: A->ops->solve = MatSolve_SeqDense_Cholesky;
874: A->ops->matsolve = MatMatSolve_SeqDense_Cholesky;
875: A->ops->solvetranspose = MatSolveTranspose_SeqDense_Cholesky;
876: A->ops->matsolvetranspose = MatMatSolveTranspose_SeqDense_Cholesky;
877: A->factortype = MAT_FACTOR_CHOLESKY;
879: PetscCall(PetscFree(A->solvertype));
880: PetscCall(PetscStrallocpy(MATSOLVERPETSC, &A->solvertype));
882: PetscCall(PetscLogFlops((1.0 * A->cmap->n * A->cmap->n * A->cmap->n) / 3.0));
883: PetscFunctionReturn(PETSC_SUCCESS);
884: }
886: static PetscErrorCode MatCholeskyFactorNumeric_SeqDense(Mat fact, Mat A, const MatFactorInfo *info)
887: {
888: PetscFunctionBegin;
889: PetscCall(MatDuplicateNoCreate_SeqDense(fact, A, MAT_COPY_VALUES));
890: PetscUseTypeMethod(fact, choleskyfactor, NULL, info);
891: PetscFunctionReturn(PETSC_SUCCESS);
892: }
894: PetscErrorCode MatCholeskyFactorSymbolic_SeqDense(Mat fact, Mat A, IS row, const MatFactorInfo *info)
895: {
896: PetscFunctionBegin;
897: fact->assembled = PETSC_TRUE;
898: fact->preallocated = PETSC_TRUE;
899: fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqDense;
900: PetscFunctionReturn(PETSC_SUCCESS);
901: }
903: PetscErrorCode MatQRFactor_SeqDense(Mat A, IS col, PETSC_UNUSED const MatFactorInfo *minfo)
904: {
905: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
906: PetscBLASInt n, m, min, max;
908: PetscFunctionBegin;
909: PetscCall(PetscBLASIntCast(A->cmap->n, &n));
910: PetscCall(PetscBLASIntCast(A->rmap->n, &m));
911: max = PetscMax(m, n);
912: min = PetscMin(m, n);
913: if (!mat->tau) PetscCall(PetscMalloc1(min, &mat->tau));
914: if (!mat->pivots) PetscCall(PetscMalloc1(n, &mat->pivots));
915: if (!mat->qrrhs) PetscCall(MatCreateVecs(A, NULL, &mat->qrrhs));
916: if (!A->rmap->n || !A->cmap->n) PetscFunctionReturn(PETSC_SUCCESS);
917: if (!mat->fwork) {
918: PetscScalar dummy;
920: mat->lfwork = -1;
921: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
922: PetscCallLAPACKInfo("LAPACKgeqrf", LAPACKgeqrf_(&m, &n, mat->v, &mat->lda, mat->tau, &dummy, &mat->lfwork, &info));
923: PetscCall(PetscFPTrapPop());
924: PetscCall(PetscBLASIntCast((PetscCount)(PetscRealPart(dummy)), &mat->lfwork));
925: PetscCall(PetscMalloc1(mat->lfwork, &mat->fwork));
926: }
927: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
928: PetscCallLAPACKInfo("LAPACKgeqrf", LAPACKgeqrf_(&m, &n, mat->v, &mat->lda, mat->tau, mat->fwork, &mat->lfwork, &info));
929: PetscCall(PetscFPTrapPop());
930: // TODO: try to estimate rank or test for and use geqp3 for rank revealing QR. For now just say rank is min of m and n
931: mat->rank = min;
933: A->ops->solve = MatSolve_SeqDense_QR;
934: A->ops->matsolve = MatMatSolve_SeqDense_QR;
935: A->factortype = MAT_FACTOR_QR;
936: if (m == n) {
937: A->ops->solvetranspose = MatSolveTranspose_SeqDense_QR;
938: A->ops->matsolvetranspose = MatMatSolveTranspose_SeqDense_QR;
939: }
941: PetscCall(PetscFree(A->solvertype));
942: PetscCall(PetscStrallocpy(MATSOLVERPETSC, &A->solvertype));
944: PetscCall(PetscLogFlops(2.0 * min * min * (max - min / 3.0)));
945: PetscFunctionReturn(PETSC_SUCCESS);
946: }
948: static PetscErrorCode MatQRFactorNumeric_SeqDense(Mat fact, Mat A, const MatFactorInfo *info)
949: {
950: PetscFunctionBegin;
951: PetscCall(MatDuplicateNoCreate_SeqDense(fact, A, MAT_COPY_VALUES));
952: PetscUseMethod(fact, "MatQRFactor_C", (Mat, IS, const MatFactorInfo *), (fact, NULL, info));
953: PetscFunctionReturn(PETSC_SUCCESS);
954: }
956: PetscErrorCode MatQRFactorSymbolic_SeqDense(Mat fact, Mat A, IS row, const MatFactorInfo *info)
957: {
958: PetscFunctionBegin;
959: fact->assembled = PETSC_TRUE;
960: fact->preallocated = PETSC_TRUE;
961: PetscCall(PetscObjectComposeFunction((PetscObject)fact, "MatQRFactorNumeric_C", MatQRFactorNumeric_SeqDense));
962: PetscFunctionReturn(PETSC_SUCCESS);
963: }
965: /* uses LAPACK */
966: PETSC_INTERN PetscErrorCode MatGetFactor_seqdense_petsc(Mat A, MatFactorType ftype, Mat *fact)
967: {
968: PetscFunctionBegin;
969: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), fact));
970: PetscCall(MatSetSizes(*fact, A->rmap->n, A->cmap->n, A->rmap->n, A->cmap->n));
971: PetscCall(MatSetType(*fact, MATDENSE));
972: (*fact)->trivialsymbolic = PETSC_TRUE;
973: if (ftype == MAT_FACTOR_LU || ftype == MAT_FACTOR_ILU) {
974: (*fact)->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqDense;
975: (*fact)->ops->ilufactorsymbolic = MatLUFactorSymbolic_SeqDense;
976: } else if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
977: (*fact)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqDense;
978: } else if (ftype == MAT_FACTOR_QR) {
979: PetscCall(PetscObjectComposeFunction((PetscObject)*fact, "MatQRFactorSymbolic_C", MatQRFactorSymbolic_SeqDense));
980: }
981: (*fact)->factortype = ftype;
983: PetscCall(PetscFree((*fact)->solvertype));
984: PetscCall(PetscStrallocpy(MATSOLVERPETSC, &(*fact)->solvertype));
985: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&(*fact)->preferredordering[MAT_FACTOR_LU]));
986: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&(*fact)->preferredordering[MAT_FACTOR_ILU]));
987: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&(*fact)->preferredordering[MAT_FACTOR_CHOLESKY]));
988: PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, (char **)&(*fact)->preferredordering[MAT_FACTOR_ICC]));
989: PetscFunctionReturn(PETSC_SUCCESS);
990: }
992: static PetscErrorCode MatSOR_SeqDense(Mat A, Vec bb, PetscReal omega, MatSORType flag, PetscReal shift, PetscInt its, PetscInt lits, Vec xx)
993: {
994: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
995: PetscScalar *x, *v = mat->v, zero = 0.0, xt;
996: const PetscScalar *b;
997: PetscInt m = A->rmap->n, i;
998: PetscBLASInt o = 1, bm = 0;
1000: PetscFunctionBegin;
1001: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1002: PetscCheck(A->offloadmask != PETSC_OFFLOAD_GPU, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented");
1003: #endif
1004: if (shift == -1) shift = 0.0; /* negative shift indicates do not error on zero diagonal; this code never zeros on zero diagonal */
1005: PetscCall(PetscBLASIntCast(m, &bm));
1006: if (flag & SOR_ZERO_INITIAL_GUESS) {
1007: /* this is a hack fix, should have another version without the second BLASdotu */
1008: PetscCall(VecSet(xx, zero));
1009: }
1010: PetscCall(VecGetArray(xx, &x));
1011: PetscCall(VecGetArrayRead(bb, &b));
1012: its = its * lits;
1013: PetscCheck(its > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Relaxation requires global its %" PetscInt_FMT " and local its %" PetscInt_FMT " both positive", its, lits);
1014: while (its--) {
1015: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1016: for (i = 0; i < m; i++) {
1017: PetscCallBLAS("BLASdotu", xt = b[i] - BLASdotu_(&bm, v + i, &bm, x, &o));
1018: x[i] = (1. - omega) * x[i] + (xt + v[i + i * m] * x[i]) * omega / (v[i + i * m] + shift);
1019: }
1020: }
1021: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1022: for (i = m - 1; i >= 0; i--) {
1023: PetscCallBLAS("BLASdotu", xt = b[i] - BLASdotu_(&bm, v + i, &bm, x, &o));
1024: x[i] = (1. - omega) * x[i] + (xt + v[i + i * m] * x[i]) * omega / (v[i + i * m] + shift);
1025: }
1026: }
1027: }
1028: PetscCall(VecRestoreArrayRead(bb, &b));
1029: PetscCall(VecRestoreArray(xx, &x));
1030: PetscFunctionReturn(PETSC_SUCCESS);
1031: }
1033: PETSC_INTERN PetscErrorCode MatMultColumnRangeKernel_SeqDense(Mat A, Vec xx, Vec yy, PetscInt c_start, PetscInt c_end, PetscBool trans, PetscBool herm)
1034: {
1035: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1036: PetscScalar *y, _DOne = 1.0, _DZero = 0.0;
1037: PetscBLASInt m, n, _One = 1;
1038: const PetscScalar *v = mat->v, *x;
1040: PetscFunctionBegin;
1041: PetscCall(PetscBLASIntCast(A->rmap->n, &m));
1042: PetscCall(PetscBLASIntCast(c_end - c_start, &n));
1043: PetscCall(VecGetArrayRead(xx, &x));
1044: PetscCall(VecGetArrayWrite(yy, &y));
1045: if (!m || !n) {
1046: PetscBLASInt i;
1047: if (trans)
1048: for (i = 0; i < n; i++) y[i] = 0.0;
1049: else
1050: for (i = 0; i < m; i++) y[i] = 0.0;
1051: } else {
1052: if (trans) {
1053: if (herm) PetscCallBLAS("BLASgemv", BLASgemv_("C", &m, &n, &_DOne, v + c_start * mat->lda, &mat->lda, x, &_One, &_DZero, y + c_start, &_One));
1054: else PetscCallBLAS("BLASgemv", BLASgemv_("T", &m, &n, &_DOne, v + c_start * mat->lda, &mat->lda, x, &_One, &_DZero, y + c_start, &_One));
1055: } else {
1056: PetscCallBLAS("BLASgemv", BLASgemv_("N", &m, &n, &_DOne, v + c_start * mat->lda, &mat->lda, x + c_start, &_One, &_DZero, y, &_One));
1057: }
1058: PetscCall(PetscLogFlops(2.0 * m * n - n));
1059: }
1060: PetscCall(VecRestoreArrayRead(xx, &x));
1061: PetscCall(VecRestoreArrayWrite(yy, &y));
1062: PetscFunctionReturn(PETSC_SUCCESS);
1063: }
1065: PetscErrorCode MatMultHermitianTransposeColumnRange_SeqDense(Mat A, Vec xx, Vec yy, PetscInt c_start, PetscInt c_end)
1066: {
1067: PetscFunctionBegin;
1068: PetscCall(MatMultColumnRangeKernel_SeqDense(A, xx, yy, c_start, c_end, PETSC_TRUE, PETSC_TRUE));
1069: PetscFunctionReturn(PETSC_SUCCESS);
1070: }
1072: PetscErrorCode MatMult_SeqDense(Mat A, Vec xx, Vec yy)
1073: {
1074: PetscFunctionBegin;
1075: PetscCall(MatMultColumnRangeKernel_SeqDense(A, xx, yy, 0, A->cmap->n, PETSC_FALSE, PETSC_FALSE));
1076: PetscFunctionReturn(PETSC_SUCCESS);
1077: }
1079: PetscErrorCode MatMultTranspose_SeqDense(Mat A, Vec xx, Vec yy)
1080: {
1081: PetscFunctionBegin;
1082: PetscCall(MatMultColumnRangeKernel_SeqDense(A, xx, yy, 0, A->cmap->n, PETSC_TRUE, PETSC_FALSE));
1083: PetscFunctionReturn(PETSC_SUCCESS);
1084: }
1086: PetscErrorCode MatMultHermitianTranspose_SeqDense(Mat A, Vec xx, Vec yy)
1087: {
1088: PetscFunctionBegin;
1089: PetscCall(MatMultColumnRangeKernel_SeqDense(A, xx, yy, 0, A->cmap->n, PETSC_TRUE, PETSC_TRUE));
1090: PetscFunctionReturn(PETSC_SUCCESS);
1091: }
1093: PETSC_INTERN PetscErrorCode MatMultAddColumnRangeKernel_SeqDense(Mat A, Vec xx, Vec zz, Vec yy, PetscInt c_start, PetscInt c_end, PetscBool trans, PetscBool herm)
1094: {
1095: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1096: const PetscScalar *v = mat->v, *x;
1097: PetscScalar *y, _DOne = 1.0;
1098: PetscBLASInt m, n, _One = 1;
1100: PetscFunctionBegin;
1101: PetscCall(PetscBLASIntCast(A->rmap->n, &m));
1102: PetscCall(PetscBLASIntCast(c_end - c_start, &n));
1103: PetscCall(VecCopy(zz, yy));
1104: if (!m || !n) PetscFunctionReturn(PETSC_SUCCESS);
1105: PetscCall(VecGetArray(yy, &y));
1106: PetscCall(VecGetArrayRead(xx, &x));
1107: if (trans) {
1108: if (herm) PetscCallBLAS("BLASgemv", BLASgemv_("C", &m, &n, &_DOne, v + c_start * mat->lda, &mat->lda, x, &_One, &_DOne, y + c_start, &_One));
1109: else PetscCallBLAS("BLASgemv", BLASgemv_("T", &m, &n, &_DOne, v + c_start * mat->lda, &mat->lda, x, &_One, &_DOne, y + c_start, &_One));
1110: } else {
1111: PetscCallBLAS("BLASgemv", BLASgemv_("N", &m, &n, &_DOne, v + c_start * mat->lda, &mat->lda, x + c_start, &_One, &_DOne, y, &_One));
1112: }
1113: PetscCall(VecRestoreArrayRead(xx, &x));
1114: PetscCall(VecRestoreArray(yy, &y));
1115: PetscCall(PetscLogFlops(2.0 * m * n));
1116: PetscFunctionReturn(PETSC_SUCCESS);
1117: }
1119: PetscErrorCode MatMultColumnRange_SeqDense(Mat A, Vec xx, Vec yy, PetscInt c_start, PetscInt c_end)
1120: {
1121: PetscFunctionBegin;
1122: PetscCall(MatMultColumnRangeKernel_SeqDense(A, xx, yy, c_start, c_end, PETSC_FALSE, PETSC_FALSE));
1123: PetscFunctionReturn(PETSC_SUCCESS);
1124: }
1126: PetscErrorCode MatMultAddColumnRange_SeqDense(Mat A, Vec xx, Vec zz, Vec yy, PetscInt c_start, PetscInt c_end)
1127: {
1128: PetscFunctionBegin;
1129: PetscCall(MatMultAddColumnRangeKernel_SeqDense(A, xx, zz, yy, c_start, c_end, PETSC_FALSE, PETSC_FALSE));
1130: PetscFunctionReturn(PETSC_SUCCESS);
1131: }
1133: PetscErrorCode MatMultHermitianTransposeAddColumnRange_SeqDense(Mat A, Vec xx, Vec zz, Vec yy, PetscInt c_start, PetscInt c_end)
1134: {
1135: PetscFunctionBegin;
1136: PetscMPIInt rank;
1137: PetscCallMPI(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
1138: PetscCall(MatMultAddColumnRangeKernel_SeqDense(A, xx, zz, yy, c_start, c_end, PETSC_TRUE, PETSC_TRUE));
1139: PetscFunctionReturn(PETSC_SUCCESS);
1140: }
1142: PetscErrorCode MatMultAdd_SeqDense(Mat A, Vec xx, Vec zz, Vec yy)
1143: {
1144: PetscFunctionBegin;
1145: PetscCall(MatMultAddColumnRangeKernel_SeqDense(A, xx, zz, yy, 0, A->cmap->n, PETSC_FALSE, PETSC_FALSE));
1146: PetscFunctionReturn(PETSC_SUCCESS);
1147: }
1149: PetscErrorCode MatMultTransposeAdd_SeqDense(Mat A, Vec xx, Vec zz, Vec yy)
1150: {
1151: PetscFunctionBegin;
1152: PetscCall(MatMultAddColumnRangeKernel_SeqDense(A, xx, zz, yy, 0, A->cmap->n, PETSC_TRUE, PETSC_FALSE));
1153: PetscFunctionReturn(PETSC_SUCCESS);
1154: }
1156: PetscErrorCode MatMultHermitianTransposeAdd_SeqDense(Mat A, Vec xx, Vec zz, Vec yy)
1157: {
1158: PetscFunctionBegin;
1159: PetscCall(MatMultAddColumnRangeKernel_SeqDense(A, xx, zz, yy, 0, A->cmap->n, PETSC_TRUE, PETSC_TRUE));
1160: PetscFunctionReturn(PETSC_SUCCESS);
1161: }
1163: static PetscErrorCode MatGetRow_SeqDense(Mat A, PetscInt row, PetscInt *ncols, PetscInt **cols, PetscScalar **vals)
1164: {
1165: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1166: PetscInt i;
1168: PetscFunctionBegin;
1169: if (ncols) *ncols = A->cmap->n;
1170: if (cols) {
1171: PetscCall(PetscMalloc1(A->cmap->n, cols));
1172: for (i = 0; i < A->cmap->n; i++) (*cols)[i] = i;
1173: }
1174: if (vals) {
1175: const PetscScalar *v;
1177: PetscCall(MatDenseGetArrayRead(A, &v));
1178: PetscCall(PetscMalloc1(A->cmap->n, vals));
1179: v += row;
1180: for (i = 0; i < A->cmap->n; i++) {
1181: (*vals)[i] = *v;
1182: v += mat->lda;
1183: }
1184: PetscCall(MatDenseRestoreArrayRead(A, &v));
1185: }
1186: PetscFunctionReturn(PETSC_SUCCESS);
1187: }
1189: static PetscErrorCode MatRestoreRow_SeqDense(Mat A, PetscInt row, PetscInt *ncols, PetscInt **cols, PetscScalar **vals)
1190: {
1191: PetscFunctionBegin;
1192: if (cols) PetscCall(PetscFree(*cols));
1193: if (vals) PetscCall(PetscFree(*vals));
1194: PetscFunctionReturn(PETSC_SUCCESS);
1195: }
1197: static PetscErrorCode MatSetValues_SeqDense(Mat A, PetscInt m, const PetscInt indexm[], PetscInt n, const PetscInt indexn[], const PetscScalar v[], InsertMode addv)
1198: {
1199: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1200: PetscScalar *av;
1201: PetscInt i, j, idx = 0;
1202: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1203: PetscOffloadMask oldf;
1204: #endif
1206: PetscFunctionBegin;
1207: PetscCall(MatDenseGetArray(A, &av));
1208: if (!mat->roworiented) {
1209: if (addv == INSERT_VALUES) {
1210: for (j = 0; j < n; j++) {
1211: if (indexn[j] < 0) {
1212: idx += m;
1213: continue;
1214: }
1215: PetscCheck(indexn[j] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, indexn[j], A->cmap->n - 1);
1216: for (i = 0; i < m; i++) {
1217: if (indexm[i] < 0) {
1218: idx++;
1219: continue;
1220: }
1221: PetscCheck(indexm[i] < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, indexm[i], A->rmap->n - 1);
1222: av[indexn[j] * mat->lda + indexm[i]] = v ? v[idx++] : (idx++, 0.0);
1223: }
1224: }
1225: } else {
1226: for (j = 0; j < n; j++) {
1227: if (indexn[j] < 0) {
1228: idx += m;
1229: continue;
1230: }
1231: PetscCheck(indexn[j] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, indexn[j], A->cmap->n - 1);
1232: for (i = 0; i < m; i++) {
1233: if (indexm[i] < 0) {
1234: idx++;
1235: continue;
1236: }
1237: PetscCheck(indexm[i] < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, indexm[i], A->rmap->n - 1);
1238: av[indexn[j] * mat->lda + indexm[i]] += v ? v[idx++] : (idx++, 0.0);
1239: }
1240: }
1241: }
1242: } else {
1243: if (addv == INSERT_VALUES) {
1244: for (i = 0; i < m; i++) {
1245: if (indexm[i] < 0) {
1246: idx += n;
1247: continue;
1248: }
1249: PetscCheck(indexm[i] < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, indexm[i], A->rmap->n - 1);
1250: for (j = 0; j < n; j++) {
1251: if (indexn[j] < 0) {
1252: idx++;
1253: continue;
1254: }
1255: PetscCheck(indexn[j] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, indexn[j], A->cmap->n - 1);
1256: av[indexn[j] * mat->lda + indexm[i]] = v ? v[idx++] : (idx++, 0.0);
1257: }
1258: }
1259: } else {
1260: for (i = 0; i < m; i++) {
1261: if (indexm[i] < 0) {
1262: idx += n;
1263: continue;
1264: }
1265: PetscCheck(indexm[i] < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, indexm[i], A->rmap->n - 1);
1266: for (j = 0; j < n; j++) {
1267: if (indexn[j] < 0) {
1268: idx++;
1269: continue;
1270: }
1271: PetscCheck(indexn[j] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, indexn[j], A->cmap->n - 1);
1272: av[indexn[j] * mat->lda + indexm[i]] += v ? v[idx++] : (idx++, 0.0);
1273: }
1274: }
1275: }
1276: }
1277: /* hack to prevent unneeded copy to the GPU while returning the array */
1278: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1279: oldf = A->offloadmask;
1280: A->offloadmask = PETSC_OFFLOAD_GPU;
1281: #endif
1282: PetscCall(MatDenseRestoreArray(A, &av));
1283: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1284: A->offloadmask = (oldf == PETSC_OFFLOAD_UNALLOCATED ? PETSC_OFFLOAD_UNALLOCATED : PETSC_OFFLOAD_CPU);
1285: #endif
1286: PetscFunctionReturn(PETSC_SUCCESS);
1287: }
1289: static PetscErrorCode MatGetValues_SeqDense(Mat A, PetscInt m, const PetscInt indexm[], PetscInt n, const PetscInt indexn[], PetscScalar v[])
1290: {
1291: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1292: const PetscScalar *vv;
1293: PetscInt i, j;
1294: PetscBool roworiented = mat->roworiented;
1295: PetscScalar *value;
1297: PetscFunctionBegin;
1298: PetscCall(MatDenseGetArrayRead(A, &vv));
1299: for (i = 0; i < m; i++) {
1300: if (indexm[i] < 0) continue;
1301: PetscCheck(indexm[i] < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %" PetscInt_FMT " requested larger than number rows %" PetscInt_FMT, indexm[i], A->rmap->n);
1302: for (j = 0; j < n; j++) {
1303: if (indexn[j] < 0) continue;
1304: PetscCheck(indexn[j] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column %" PetscInt_FMT " requested larger than number columns %" PetscInt_FMT, indexn[j], A->cmap->n);
1305: value = roworiented ? &v[j + i * n] : &v[i + j * m];
1306: *value = vv[indexn[j] * mat->lda + indexm[i]];
1307: }
1308: }
1309: PetscCall(MatDenseRestoreArrayRead(A, &vv));
1310: PetscFunctionReturn(PETSC_SUCCESS);
1311: }
1313: PetscErrorCode MatView_Dense_Binary(Mat mat, PetscViewer viewer)
1314: {
1315: PetscBool skipHeader;
1316: PetscViewerFormat format;
1317: PetscInt header[4], M, N, m, lda, i, j;
1318: PetscCount k;
1319: const PetscScalar *v;
1320: PetscScalar *vwork;
1322: PetscFunctionBegin;
1323: PetscCall(PetscViewerSetUp(viewer));
1324: PetscCall(PetscViewerBinaryGetSkipHeader(viewer, &skipHeader));
1325: PetscCall(PetscViewerGetFormat(viewer, &format));
1326: if (skipHeader) format = PETSC_VIEWER_NATIVE;
1328: PetscCall(MatGetSize(mat, &M, &N));
1330: /* write matrix header */
1331: header[0] = MAT_FILE_CLASSID;
1332: header[1] = M;
1333: header[2] = N;
1334: header[3] = (format == PETSC_VIEWER_NATIVE) ? MATRIX_BINARY_FORMAT_DENSE : M * N;
1335: if (!skipHeader) PetscCall(PetscViewerBinaryWrite(viewer, header, 4, PETSC_INT));
1337: PetscCall(MatGetLocalSize(mat, &m, NULL));
1338: if (format != PETSC_VIEWER_NATIVE) {
1339: PetscInt nnz = m * N, *iwork;
1340: /* store row lengths for each row */
1341: PetscCall(PetscMalloc1(nnz, &iwork));
1342: for (i = 0; i < m; i++) iwork[i] = N;
1343: PetscCall(PetscViewerBinaryWriteAll(viewer, iwork, m, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_INT));
1344: /* store column indices (zero start index) */
1345: for (k = 0, i = 0; i < m; i++)
1346: for (j = 0; j < N; j++, k++) iwork[k] = j;
1347: PetscCall(PetscViewerBinaryWriteAll(viewer, iwork, nnz, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_INT));
1348: PetscCall(PetscFree(iwork));
1349: }
1350: /* store matrix values as a dense matrix in row major order */
1351: PetscCall(PetscMalloc1(m * N, &vwork));
1352: PetscCall(MatDenseGetArrayRead(mat, &v));
1353: PetscCall(MatDenseGetLDA(mat, &lda));
1354: for (k = 0, i = 0; i < m; i++)
1355: for (j = 0; j < N; j++, k++) vwork[k] = v[i + (size_t)lda * j];
1356: PetscCall(MatDenseRestoreArrayRead(mat, &v));
1357: PetscCall(PetscViewerBinaryWriteAll(viewer, vwork, m * N, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_SCALAR));
1358: PetscCall(PetscFree(vwork));
1359: PetscFunctionReturn(PETSC_SUCCESS);
1360: }
1362: PetscErrorCode MatLoad_Dense_Binary(Mat mat, PetscViewer viewer)
1363: {
1364: PetscBool skipHeader;
1365: PetscInt header[4], M, N, m, nz, lda, i, j, k;
1366: PetscInt rows, cols;
1367: PetscScalar *v, *vwork;
1369: PetscFunctionBegin;
1370: PetscCall(PetscViewerSetUp(viewer));
1371: PetscCall(PetscViewerBinaryGetSkipHeader(viewer, &skipHeader));
1373: if (!skipHeader) {
1374: PetscCall(PetscViewerBinaryRead(viewer, header, 4, NULL, PETSC_INT));
1375: PetscCheck(header[0] == MAT_FILE_CLASSID, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Not a matrix object in file");
1376: M = header[1];
1377: N = header[2];
1378: PetscCheck(M >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix row size (%" PetscInt_FMT ") in file is negative", M);
1379: PetscCheck(N >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix column size (%" PetscInt_FMT ") in file is negative", N);
1380: nz = header[3];
1381: PetscCheck(nz == MATRIX_BINARY_FORMAT_DENSE || nz >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Unknown matrix format %" PetscInt_FMT " in file", nz);
1382: } else {
1383: PetscCall(MatGetSize(mat, &M, &N));
1384: PetscCheck(M >= 0 && N >= 0, PETSC_COMM_SELF, PETSC_ERR_USER, "Matrix binary file header was skipped, thus the user must specify the global sizes of input matrix");
1385: nz = MATRIX_BINARY_FORMAT_DENSE;
1386: }
1388: /* setup global sizes if not set */
1389: if (mat->rmap->N < 0) mat->rmap->N = M;
1390: if (mat->cmap->N < 0) mat->cmap->N = N;
1391: PetscCall(MatSetUp(mat));
1392: /* check if global sizes are correct */
1393: PetscCall(MatGetSize(mat, &rows, &cols));
1394: PetscCheck(M == rows && N == cols, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%" PetscInt_FMT ", %" PetscInt_FMT ") than the input matrix (%" PetscInt_FMT ", %" PetscInt_FMT ")", M, N, rows, cols);
1396: PetscCall(MatGetSize(mat, NULL, &N));
1397: PetscCall(MatGetLocalSize(mat, &m, NULL));
1398: PetscCall(MatDenseGetArray(mat, &v));
1399: PetscCall(MatDenseGetLDA(mat, &lda));
1400: if (nz == MATRIX_BINARY_FORMAT_DENSE) { /* matrix in file is dense format */
1401: PetscCount nnz = (size_t)m * N;
1402: /* read in matrix values */
1403: PetscCall(PetscMalloc1(nnz, &vwork));
1404: PetscCall(PetscViewerBinaryReadAll(viewer, vwork, nnz, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_SCALAR));
1405: /* store values in column major order */
1406: for (j = 0; j < N; j++)
1407: for (i = 0; i < m; i++) v[i + (size_t)lda * j] = vwork[(size_t)i * N + j];
1408: PetscCall(PetscFree(vwork));
1409: } else { /* matrix in file is sparse format */
1410: PetscInt nnz = 0, *rlens, *icols;
1411: /* read in row lengths */
1412: PetscCall(PetscMalloc1(m, &rlens));
1413: PetscCall(PetscViewerBinaryReadAll(viewer, rlens, m, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_INT));
1414: for (i = 0; i < m; i++) nnz += rlens[i];
1415: /* read in column indices and values */
1416: PetscCall(PetscMalloc2(nnz, &icols, nnz, &vwork));
1417: PetscCall(PetscViewerBinaryReadAll(viewer, icols, nnz, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_INT));
1418: PetscCall(PetscViewerBinaryReadAll(viewer, vwork, nnz, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_SCALAR));
1419: /* store values in column major order */
1420: for (k = 0, i = 0; i < m; i++)
1421: for (j = 0; j < rlens[i]; j++, k++) v[i + lda * icols[k]] = vwork[k];
1422: PetscCall(PetscFree(rlens));
1423: PetscCall(PetscFree2(icols, vwork));
1424: }
1425: PetscCall(MatDenseRestoreArray(mat, &v));
1426: PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
1427: PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
1428: PetscFunctionReturn(PETSC_SUCCESS);
1429: }
1431: static PetscErrorCode MatLoad_SeqDense(Mat newMat, PetscViewer viewer)
1432: {
1433: PetscBool isbinary, ishdf5;
1435: PetscFunctionBegin;
1438: /* force binary viewer to load .info file if it has not yet done so */
1439: PetscCall(PetscViewerSetUp(viewer));
1440: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1441: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
1442: if (isbinary) {
1443: PetscCall(MatLoad_Dense_Binary(newMat, viewer));
1444: } else if (ishdf5) {
1445: #if PetscDefined(HAVE_HDF5)
1446: PetscCall(MatLoad_Dense_HDF5(newMat, viewer));
1447: #else
1448: SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1449: #endif
1450: } else {
1451: 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);
1452: }
1453: PetscFunctionReturn(PETSC_SUCCESS);
1454: }
1456: static PetscErrorCode MatView_SeqDense_ASCII(Mat A, PetscViewer viewer)
1457: {
1458: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
1459: PetscInt i, j;
1460: const char *name;
1461: PetscScalar *v, *av;
1462: PetscViewerFormat format;
1463: PetscBool allreal = PETSC_TRUE;
1465: PetscFunctionBegin;
1466: PetscCall(MatDenseGetArrayRead(A, (const PetscScalar **)&av));
1467: PetscCall(PetscViewerGetFormat(viewer, &format));
1468: if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1469: PetscFunctionReturn(PETSC_SUCCESS); /* do nothing for now */
1470: } else if (format == PETSC_VIEWER_ASCII_COMMON) {
1471: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
1472: for (i = 0; i < A->rmap->n; i++) {
1473: v = av + i;
1474: PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
1475: for (j = 0; j < A->cmap->n; j++) {
1476: if (PetscDefined(USE_COMPLEX) && PetscRealPart(*v) != 0.0 && PetscImaginaryPart(*v) != 0.0) {
1477: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i) ", j, (double)PetscRealPart(*v), (double)PetscImaginaryPart(*v)));
1478: } else if (PetscRealPart(*v)) {
1479: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", j, (double)PetscRealPart(*v)));
1480: }
1481: v += a->lda;
1482: }
1483: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1484: }
1485: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
1486: } else {
1487: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
1488: if (PetscDefined(USE_COMPLEX)) {
1489: /* determine if matrix has all real values */
1490: for (j = 0; j < A->cmap->n; j++) {
1491: v = av + j * a->lda;
1492: for (i = 0; i < A->rmap->n; i++) {
1493: if (PetscImaginaryPart(v[i])) {
1494: allreal = PETSC_FALSE;
1495: break;
1496: }
1497: }
1498: }
1499: }
1500: if (format == PETSC_VIEWER_ASCII_MATLAB) {
1501: PetscCall(PetscObjectGetName((PetscObject)A, &name));
1502: PetscCall(PetscViewerASCIIPrintf(viewer, "%% Size = %" PetscInt_FMT " %" PetscInt_FMT " \n", A->rmap->n, A->cmap->n));
1503: PetscCall(PetscViewerASCIIPrintf(viewer, "%s = zeros(%" PetscInt_FMT ",%" PetscInt_FMT ");\n", name, A->rmap->n, A->cmap->n));
1504: PetscCall(PetscViewerASCIIPrintf(viewer, "%s = [\n", name));
1505: }
1507: for (i = 0; i < A->rmap->n; i++) {
1508: v = av + i;
1509: for (j = 0; j < A->cmap->n; j++) {
1510: if (allreal) {
1511: PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e ", (double)PetscRealPart(*v)));
1512: } else {
1513: PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e + %18.16ei ", (double)PetscRealPart(*v), (double)PetscImaginaryPart(*v)));
1514: }
1515: v += a->lda;
1516: }
1517: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1518: }
1519: if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "];\n"));
1520: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
1521: }
1522: PetscCall(MatDenseRestoreArrayRead(A, (const PetscScalar **)&av));
1523: PetscCall(PetscViewerFlush(viewer));
1524: PetscFunctionReturn(PETSC_SUCCESS);
1525: }
1527: #include <petscdraw.h>
1528: static PetscErrorCode MatView_SeqDense_Draw_Zoom(PetscDraw draw, void *Aa)
1529: {
1530: Mat A = (Mat)Aa;
1531: PetscInt m = A->rmap->n, n = A->cmap->n, i, j;
1532: int color = PETSC_DRAW_WHITE;
1533: const PetscScalar *v;
1534: PetscViewer viewer;
1535: PetscReal xl, yl, xr, yr, x_l, x_r, y_l, y_r;
1536: PetscViewerFormat format;
1538: PetscFunctionBegin;
1539: PetscCall(PetscObjectQuery((PetscObject)A, "Zoomviewer", (PetscObject *)&viewer));
1540: PetscCall(PetscViewerGetFormat(viewer, &format));
1541: PetscCall(PetscDrawGetCoordinates(draw, &xl, &yl, &xr, &yr));
1543: /* Loop over matrix elements drawing boxes */
1544: PetscCall(MatDenseGetArrayRead(A, &v));
1545: if (format != PETSC_VIEWER_DRAW_CONTOUR) {
1546: PetscDrawCollectiveBegin(draw);
1547: /* Blue for negative and Red for positive */
1548: for (j = 0; j < n; j++) {
1549: x_l = j;
1550: x_r = x_l + 1.0;
1551: for (i = 0; i < m; i++) {
1552: y_l = m - i - 1.0;
1553: y_r = y_l + 1.0;
1554: if (PetscRealPart(v[j * m + i]) > 0.) color = PETSC_DRAW_RED;
1555: else if (PetscRealPart(v[j * m + i]) < 0.) color = PETSC_DRAW_BLUE;
1556: else continue;
1557: PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
1558: }
1559: }
1560: PetscDrawCollectiveEnd(draw);
1561: } else {
1562: /* use contour shading to indicate magnitude of values */
1563: /* first determine max of all nonzero values */
1564: PetscReal minv = 0.0, maxv = 0.0;
1565: PetscDraw popup;
1567: for (i = 0; i < m * n; i++) {
1568: if (PetscAbsScalar(v[i]) > maxv) maxv = PetscAbsScalar(v[i]);
1569: }
1570: if (minv >= maxv) maxv = minv + PETSC_SMALL;
1571: PetscCall(PetscDrawGetPopup(draw, &popup));
1572: PetscCall(PetscDrawScalePopup(popup, minv, maxv));
1574: PetscDrawCollectiveBegin(draw);
1575: for (j = 0; j < n; j++) {
1576: x_l = j;
1577: x_r = x_l + 1.0;
1578: for (i = 0; i < m; i++) {
1579: y_l = m - i - 1.0;
1580: y_r = y_l + 1.0;
1581: color = PetscDrawRealToColor(PetscAbsScalar(v[j * m + i]), minv, maxv);
1582: PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
1583: }
1584: }
1585: PetscDrawCollectiveEnd(draw);
1586: }
1587: PetscCall(MatDenseRestoreArrayRead(A, &v));
1588: PetscFunctionReturn(PETSC_SUCCESS);
1589: }
1591: static PetscErrorCode MatView_SeqDense_Draw(Mat A, PetscViewer viewer)
1592: {
1593: PetscDraw draw;
1594: PetscBool isnull;
1595: PetscReal xr, yr, xl, yl, h, w;
1597: PetscFunctionBegin;
1598: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
1599: PetscCall(PetscDrawIsNull(draw, &isnull));
1600: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
1602: xr = A->cmap->n;
1603: yr = A->rmap->n;
1604: h = yr / 10.0;
1605: w = xr / 10.0;
1606: xr += w;
1607: yr += h;
1608: xl = -w;
1609: yl = -h;
1610: PetscCall(PetscDrawSetCoordinates(draw, xl, yl, xr, yr));
1611: PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", (PetscObject)viewer));
1612: PetscCall(PetscDrawZoom(draw, MatView_SeqDense_Draw_Zoom, A));
1613: PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", NULL));
1614: PetscCall(PetscDrawSave(draw));
1615: PetscFunctionReturn(PETSC_SUCCESS);
1616: }
1618: PetscErrorCode MatView_SeqDense(Mat A, PetscViewer viewer)
1619: {
1620: PetscBool isascii, isbinary, isdraw;
1622: PetscFunctionBegin;
1623: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1624: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1625: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
1626: if (isascii) PetscCall(MatView_SeqDense_ASCII(A, viewer));
1627: else if (isbinary) PetscCall(MatView_Dense_Binary(A, viewer));
1628: else if (isdraw) PetscCall(MatView_SeqDense_Draw(A, viewer));
1629: PetscFunctionReturn(PETSC_SUCCESS);
1630: }
1632: static PetscErrorCode MatDensePlaceArray_SeqDense(Mat A, const PetscScalar *array)
1633: {
1634: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
1636: PetscFunctionBegin;
1637: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1638: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1639: PetscCheck(!a->unplacedarray, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseResetArray() first");
1640: a->unplacedarray = a->v;
1641: a->unplaced_user_alloc = a->user_alloc;
1642: a->v = (PetscScalar *)array;
1643: a->user_alloc = PETSC_TRUE;
1644: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1645: A->offloadmask = PETSC_OFFLOAD_CPU;
1646: #endif
1647: PetscFunctionReturn(PETSC_SUCCESS);
1648: }
1650: static PetscErrorCode MatDenseResetArray_SeqDense(Mat A)
1651: {
1652: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
1654: PetscFunctionBegin;
1655: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1656: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1657: a->v = a->unplacedarray;
1658: a->user_alloc = a->unplaced_user_alloc;
1659: a->unplacedarray = NULL;
1660: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1661: A->offloadmask = PETSC_OFFLOAD_CPU;
1662: #endif
1663: PetscFunctionReturn(PETSC_SUCCESS);
1664: }
1666: static PetscErrorCode MatDenseReplaceArray_SeqDense(Mat A, const PetscScalar *array)
1667: {
1668: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
1670: PetscFunctionBegin;
1671: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1672: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1673: if (!a->user_alloc) PetscCall(PetscFree(a->v));
1674: a->v = (PetscScalar *)array;
1675: a->user_alloc = PETSC_FALSE;
1676: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
1677: A->offloadmask = PETSC_OFFLOAD_CPU;
1678: #endif
1679: PetscFunctionReturn(PETSC_SUCCESS);
1680: }
1682: PetscErrorCode MatDestroy_SeqDense(Mat mat)
1683: {
1684: Mat_SeqDense *l = (Mat_SeqDense *)mat->data;
1686: PetscFunctionBegin;
1687: PetscCall(PetscLogObjectState((PetscObject)mat, "Rows %" PetscInt_FMT " Cols %" PetscInt_FMT, mat->rmap->n, mat->cmap->n));
1688: PetscCall(VecDestroy(&l->qrrhs));
1689: PetscCall(PetscFree(l->tau));
1690: PetscCall(PetscFree(l->pivots));
1691: PetscCall(PetscFree(l->fwork));
1692: if (!l->user_alloc) PetscCall(PetscFree(l->v));
1693: if (!l->unplaced_user_alloc) PetscCall(PetscFree(l->unplacedarray));
1694: PetscCheck(!l->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
1695: PetscCheck(!l->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
1696: PetscCall(VecDestroy(&l->cvec));
1697: PetscCall(MatDestroy(&l->cmat));
1698: PetscCall(PetscFree(mat->data));
1700: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, NULL));
1701: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatQRFactor_C", NULL));
1702: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatQRFactorSymbolic_C", NULL));
1703: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatQRFactorNumeric_C", NULL));
1704: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetLDA_C", NULL));
1705: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseSetLDA_C", NULL));
1706: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArray_C", NULL));
1707: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArray_C", NULL));
1708: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDensePlaceArray_C", NULL));
1709: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseResetArray_C", NULL));
1710: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseReplaceArray_C", NULL));
1711: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArrayRead_C", NULL));
1712: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArrayRead_C", NULL));
1713: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetArrayWrite_C", NULL));
1714: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreArrayWrite_C", NULL));
1715: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_seqdense_seqaij_C", NULL));
1716: #if PetscDefined(HAVE_ELEMENTAL)
1717: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_seqdense_elemental_C", NULL));
1718: #endif
1719: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
1720: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_seqdense_scalapack_C", NULL));
1721: #endif
1722: #if PetscDefined(HAVE_CUDA)
1723: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_seqdense_seqdensecuda_C", NULL));
1724: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqdensecuda_seqdensecuda_C", NULL));
1725: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqdensecuda_seqdense_C", NULL));
1726: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqdense_seqdensecuda_C", NULL));
1727: #endif
1728: #if PetscDefined(HAVE_HIP)
1729: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_seqdense_seqdensehip_C", NULL));
1730: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqdensehip_seqdensehip_C", NULL));
1731: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqdensehip_seqdense_C", NULL));
1732: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqdense_seqdensehip_C", NULL));
1733: #endif
1734: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatSeqDenseSetPreallocation_C", NULL));
1735: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqaij_seqdense_C", NULL));
1736: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqdense_seqdense_C", NULL));
1737: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqbaij_seqdense_C", NULL));
1738: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_seqsbaij_seqdense_C", NULL));
1740: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumn_C", NULL));
1741: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumn_C", NULL));
1742: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVec_C", NULL));
1743: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVec_C", NULL));
1744: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVecRead_C", NULL));
1745: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVecRead_C", NULL));
1746: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetColumnVecWrite_C", NULL));
1747: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreColumnVecWrite_C", NULL));
1748: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseGetSubMatrix_C", NULL));
1749: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseRestoreSubMatrix_C", NULL));
1750: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultColumnRange_C", NULL));
1751: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultAddColumnRange_C", NULL));
1752: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultHermitianTransposeColumnRange_C", NULL));
1753: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMultHermitianTransposeAddColumnRange_C", NULL));
1754: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDenseUpdateColumnLayout_C", NULL));
1755: PetscFunctionReturn(PETSC_SUCCESS);
1756: }
1758: static PetscErrorCode MatTranspose_SeqDense(Mat A, MatReuse reuse, Mat *matout)
1759: {
1760: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1761: PetscInt k, j, m = A->rmap->n, M = mat->lda, n = A->cmap->n;
1762: PetscScalar *v, tmp;
1764: PetscFunctionBegin;
1765: if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *matout));
1766: if (reuse == MAT_INPLACE_MATRIX) {
1767: if (m == n) { /* in place transpose */
1768: PetscCall(MatDenseGetArray(A, &v));
1769: for (j = 0; j < m; j++) {
1770: for (k = 0; k < j; k++) {
1771: tmp = v[j + k * M];
1772: v[j + k * M] = v[k + j * M];
1773: v[k + j * M] = tmp;
1774: }
1775: }
1776: PetscCall(MatDenseRestoreArray(A, &v));
1777: } else { /* reuse memory, temporary allocates new memory */
1778: PetscScalar *v2;
1779: PetscLayout tmplayout;
1781: PetscCall(PetscMalloc1((size_t)m * n, &v2));
1782: PetscCall(MatDenseGetArray(A, &v));
1783: for (j = 0; j < n; j++) {
1784: for (k = 0; k < m; k++) v2[j + (size_t)k * n] = v[k + (size_t)j * M];
1785: }
1786: PetscCall(PetscArraycpy(v, v2, (size_t)m * n));
1787: PetscCall(PetscFree(v2));
1788: PetscCall(MatDenseRestoreArray(A, &v));
1789: /* cleanup size dependent quantities */
1790: PetscCall(VecDestroy(&mat->cvec));
1791: PetscCall(MatDestroy(&mat->cmat));
1792: PetscCall(PetscFree(mat->pivots));
1793: PetscCall(PetscFree(mat->fwork));
1794: /* swap row/col layouts */
1795: PetscCall(PetscBLASIntCast(n, &mat->lda));
1796: tmplayout = A->rmap;
1797: A->rmap = A->cmap;
1798: A->cmap = tmplayout;
1799: }
1800: } else { /* out-of-place transpose */
1801: Mat tmat;
1802: Mat_SeqDense *tmatd;
1803: PetscScalar *v2;
1804: PetscInt M2;
1806: if (reuse == MAT_INITIAL_MATRIX) {
1807: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &tmat));
1808: PetscCall(MatSetSizes(tmat, A->cmap->n, A->rmap->n, A->cmap->n, A->rmap->n));
1809: PetscCall(MatSetType(tmat, ((PetscObject)A)->type_name));
1810: PetscCall(MatSeqDenseSetPreallocation(tmat, NULL));
1811: } else tmat = *matout;
1813: PetscCall(MatDenseGetArrayRead(A, (const PetscScalar **)&v));
1814: PetscCall(MatDenseGetArray(tmat, &v2));
1815: tmatd = (Mat_SeqDense *)tmat->data;
1816: M2 = tmatd->lda;
1817: for (j = 0; j < n; j++) {
1818: for (k = 0; k < m; k++) v2[j + k * M2] = v[k + j * M];
1819: }
1820: PetscCall(MatDenseRestoreArray(tmat, &v2));
1821: PetscCall(MatDenseRestoreArrayRead(A, (const PetscScalar **)&v));
1822: PetscCall(MatAssemblyBegin(tmat, MAT_FINAL_ASSEMBLY));
1823: PetscCall(MatAssemblyEnd(tmat, MAT_FINAL_ASSEMBLY));
1824: *matout = tmat;
1825: }
1826: PetscFunctionReturn(PETSC_SUCCESS);
1827: }
1829: static PetscErrorCode MatEqual_SeqDense(Mat A1, Mat A2, PetscBool *flg)
1830: {
1831: Mat_SeqDense *mat1 = (Mat_SeqDense *)A1->data;
1832: Mat_SeqDense *mat2 = (Mat_SeqDense *)A2->data;
1833: PetscInt i;
1834: const PetscScalar *v1, *v2;
1836: PetscFunctionBegin;
1837: if (A1->rmap->n != A2->rmap->n) {
1838: *flg = PETSC_FALSE;
1839: PetscFunctionReturn(PETSC_SUCCESS);
1840: }
1841: if (A1->cmap->n != A2->cmap->n) {
1842: *flg = PETSC_FALSE;
1843: PetscFunctionReturn(PETSC_SUCCESS);
1844: }
1845: PetscCall(MatDenseGetArrayRead(A1, &v1));
1846: PetscCall(MatDenseGetArrayRead(A2, &v2));
1847: for (i = 0; i < A1->cmap->n; i++) {
1848: PetscCall(PetscArraycmp(v1, v2, A1->rmap->n, flg));
1849: if (*flg == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
1850: v1 += mat1->lda;
1851: v2 += mat2->lda;
1852: }
1853: PetscCall(MatDenseRestoreArrayRead(A1, &v1));
1854: PetscCall(MatDenseRestoreArrayRead(A2, &v2));
1855: *flg = PETSC_TRUE;
1856: PetscFunctionReturn(PETSC_SUCCESS);
1857: }
1859: PetscErrorCode MatGetDiagonal_SeqDense(Mat A, Vec v)
1860: {
1861: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1862: PetscInt i, n, len;
1863: PetscScalar *x;
1864: const PetscScalar *vv;
1866: PetscFunctionBegin;
1867: PetscCall(VecGetSize(v, &n));
1868: PetscCall(VecGetArray(v, &x));
1869: len = PetscMin(A->rmap->n, A->cmap->n);
1870: PetscCall(MatDenseGetArrayRead(A, &vv));
1871: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming mat and vec");
1872: for (i = 0; i < len; i++) x[i] = vv[i * mat->lda + i];
1873: PetscCall(MatDenseRestoreArrayRead(A, &vv));
1874: PetscCall(VecRestoreArray(v, &x));
1875: PetscFunctionReturn(PETSC_SUCCESS);
1876: }
1878: PetscErrorCode MatDiagonalScale_SeqDense(Mat A, Vec ll, Vec rr)
1879: {
1880: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1881: const PetscScalar *l, *r;
1882: PetscScalar x, *v, *vv;
1883: PetscInt i, j, m = A->rmap->n, n = A->cmap->n;
1885: PetscFunctionBegin;
1886: PetscCall(MatDenseGetArray(A, &vv));
1887: if (ll) {
1888: PetscCall(VecGetSize(ll, &m));
1889: PetscCall(VecGetArrayRead(ll, &l));
1890: PetscCheck(m == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left scaling vec wrong size");
1891: for (i = 0; i < m; i++) {
1892: x = l[i];
1893: v = vv + i;
1894: for (j = 0; j < n; j++) {
1895: (*v) *= x;
1896: v += mat->lda;
1897: }
1898: }
1899: PetscCall(VecRestoreArrayRead(ll, &l));
1900: PetscCall(PetscLogFlops(1.0 * n * m));
1901: }
1902: if (rr) {
1903: PetscCall(VecGetSize(rr, &n));
1904: PetscCall(VecGetArrayRead(rr, &r));
1905: PetscCheck(n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Right scaling vec wrong size");
1906: for (i = 0; i < n; i++) {
1907: x = r[i];
1908: v = vv + i * mat->lda;
1909: for (j = 0; j < m; j++) (*v++) *= x;
1910: }
1911: PetscCall(VecRestoreArrayRead(rr, &r));
1912: PetscCall(PetscLogFlops(1.0 * n * m));
1913: }
1914: PetscCall(MatDenseRestoreArray(A, &vv));
1915: PetscFunctionReturn(PETSC_SUCCESS);
1916: }
1918: PetscErrorCode MatNorm_SeqDense(Mat A, NormType type, PetscReal *nrm)
1919: {
1920: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1921: PetscScalar *v, *vv, *work, *av = NULL;
1922: PetscReal sum = 0.0;
1923: PetscInt lda, m = A->rmap->n, i, j;
1925: PetscFunctionBegin;
1926: PetscCall(MatDenseGetArrayRead(A, (const PetscScalar **)&vv));
1927: PetscCall(MatDenseGetLDA(A, &lda));
1928: v = vv;
1929: if (type == NORM_FROBENIUS) {
1930: if (lda > m) {
1931: for (j = 0; j < A->cmap->n; j++) {
1932: v = vv + j * lda;
1933: for (i = 0; i < m; i++) {
1934: sum += PetscRealPart(PetscConj(*v) * (*v));
1935: v++;
1936: }
1937: }
1938: } else {
1939: #if PetscDefined(USE_REAL___FP16)
1940: PetscBLASInt one = 1, cnt = A->cmap->n * A->rmap->n;
1941: PetscCallBLAS("BLASnrm2", *nrm = BLASnrm2_(&cnt, v, &one));
1942: }
1943: #else
1944: for (i = 0; i < A->cmap->n * A->rmap->n; i++) {
1945: sum += PetscRealPart(PetscConj(*v) * (*v));
1946: v++;
1947: }
1948: }
1949: *nrm = PetscSqrtReal(sum);
1950: #endif
1951: PetscCall(PetscLogFlops(2.0 * A->cmap->n * A->rmap->n));
1952: } else if (type == NORM_1) {
1953: *nrm = 0.0;
1954: for (j = 0; j < A->cmap->n; j++) {
1955: v = vv + j * mat->lda;
1956: sum = 0.0;
1957: for (i = 0; i < A->rmap->n; i++) {
1958: sum += PetscAbsScalar(*v);
1959: v++;
1960: }
1961: if (sum > *nrm) *nrm = sum;
1962: }
1963: PetscCall(PetscLogFlops(1.0 * A->cmap->n * A->rmap->n));
1964: } else if (type == NORM_INFINITY) {
1965: *nrm = 0.0;
1966: for (j = 0; j < A->rmap->n; j++) {
1967: v = vv + j;
1968: sum = 0.0;
1969: for (i = 0; i < A->cmap->n; i++) {
1970: sum += PetscAbsScalar(*v);
1971: v += mat->lda;
1972: }
1973: if (sum > *nrm) *nrm = sum;
1974: }
1975: PetscCall(PetscLogFlops(1.0 * A->cmap->n * A->rmap->n));
1976: } else if (type == NORM_2) {
1977: PetscReal *s;
1978: PetscBLASInt bm, bn, blda, min, lwork;
1980: PetscCall(PetscBLASIntCast(A->rmap->n, &bm));
1981: PetscCall(PetscBLASIntCast(A->cmap->n, &bn));
1982: PetscCall(PetscBLASIntCast(PetscMax(A->rmap->n, 1), &blda));
1983: min = PetscMin(bm, bn);
1984: if (!min) {
1985: *nrm = 0.0;
1986: PetscCall(MatDenseRestoreArrayRead(A, (const PetscScalar **)&vv));
1987: PetscFunctionReturn(PETSC_SUCCESS);
1988: }
1989: PetscCall(PetscMalloc2(A->rmap->n * A->cmap->n, &av, min, &s));
1990: for (j = 0; j < A->cmap->n; j++) PetscCall(PetscArraycpy(av + j * A->rmap->n, vv + j * lda, A->rmap->n));
1992: lwork = -1;
1993: {
1994: PetscScalar workquery;
1995: #if PetscDefined(USE_COMPLEX)
1996: PetscReal *rwork;
1998: PetscCall(PetscMalloc1(5 * min, &rwork));
1999: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2000: PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &bm, &bn, av, &blda, s, NULL, &bm, NULL, &min, &workquery, &lwork, rwork, &info));
2001: lwork = (PetscBLASInt)PetscRealPart(workquery);
2002: PetscCall(PetscMalloc1(lwork, &work));
2003: PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &bm, &bn, av, &blda, s, NULL, &bm, NULL, &min, work, &lwork, rwork, &info));
2004: PetscCall(PetscFPTrapPop());
2005: PetscCall(PetscFree(rwork));
2006: #else
2007: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2008: PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &bm, &bn, av, &blda, s, NULL, &bm, NULL, &min, &workquery, &lwork, &info));
2009: lwork = (PetscBLASInt)PetscRealPart(workquery);
2010: PetscCall(PetscMalloc1(lwork, &work));
2011: PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &bm, &bn, av, &blda, s, NULL, &bm, NULL, &min, work, &lwork, &info));
2012: PetscCall(PetscFPTrapPop());
2013: #endif
2014: }
2015: *nrm = s[0];
2016: PetscCall(PetscFree(work));
2017: PetscCall(PetscFree2(av, s));
2018: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unsupported norm type %s", NormTypes[type]);
2019: PetscCall(MatDenseRestoreArrayRead(A, (const PetscScalar **)&vv));
2020: PetscFunctionReturn(PETSC_SUCCESS);
2021: }
2023: static PetscErrorCode MatSetOption_SeqDense(Mat A, MatOption op, PetscBool flg)
2024: {
2025: Mat_SeqDense *aij = (Mat_SeqDense *)A->data;
2027: PetscFunctionBegin;
2028: switch (op) {
2029: case MAT_ROW_ORIENTED:
2030: aij->roworiented = flg;
2031: break;
2032: default:
2033: break;
2034: }
2035: PetscFunctionReturn(PETSC_SUCCESS);
2036: }
2038: PetscErrorCode MatZeroEntries_SeqDense(Mat A)
2039: {
2040: Mat_SeqDense *l = (Mat_SeqDense *)A->data;
2041: PetscInt lda = l->lda, m = A->rmap->n, n = A->cmap->n, j;
2042: PetscScalar *v;
2044: PetscFunctionBegin;
2045: PetscCall(MatDenseGetArrayWrite(A, &v));
2046: if (lda > m) {
2047: for (j = 0; j < n; j++) PetscCall(PetscArrayzero(v + PetscInt64Mult(j, lda), m));
2048: } else {
2049: PetscCall(PetscArrayzero(v, PetscInt64Mult(m, n)));
2050: }
2051: PetscCall(MatDenseRestoreArrayWrite(A, &v));
2052: PetscFunctionReturn(PETSC_SUCCESS);
2053: }
2055: static PetscErrorCode MatSetInf_SeqDense(Mat A)
2056: {
2057: // MSVC gives "divide by zero" error at compile time - so declare as volatile to skip this check.
2058: volatile PetscReal one = 1.0, zero = 0.0;
2059: Mat_SeqDense *l = (Mat_SeqDense *)A->data;
2060: PetscInt lda = l->lda, m = A->rmap->n, n = A->cmap->n, i, j;
2061: PetscScalar *v;
2062: PetscScalar inf;
2064: PetscFunctionBegin;
2065: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2066: inf = one / zero;
2067: PetscCall(PetscFPTrapPop());
2068: PetscCall(MatDenseGetArrayWrite(A, &v));
2069: if (lda > m) {
2070: for (j = 0; j < n; j++) {
2071: PetscScalar *vj = v + PetscInt64Mult(j, lda);
2073: for (i = 0; i < m; i++) vj[i] = inf;
2074: }
2075: } else {
2076: const PetscInt64 mn = PetscInt64Mult(m, n);
2078: for (PetscInt64 k = 0; k < mn; k++) v[k] = inf;
2079: }
2080: PetscCall(MatDenseRestoreArrayWrite(A, &v));
2081: PetscFunctionReturn(PETSC_SUCCESS);
2082: }
2084: static PetscErrorCode MatZeroRows_SeqDense(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
2085: {
2086: Mat_SeqDense *l = (Mat_SeqDense *)A->data;
2087: PetscInt m = l->lda, n = A->cmap->n, i, j;
2088: PetscScalar *slot, *bb, *v;
2089: const PetscScalar *xx;
2091: PetscFunctionBegin;
2092: if (PetscDefined(USE_DEBUG)) {
2093: for (i = 0; i < N; i++) {
2094: PetscCheck(rows[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative row requested to be zeroed");
2095: PetscCheck(rows[i] < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %" PetscInt_FMT " requested to be zeroed greater than or equal number of rows %" PetscInt_FMT, rows[i], A->rmap->n);
2096: }
2097: }
2098: if (!N) PetscFunctionReturn(PETSC_SUCCESS);
2100: /* fix right-hand side if needed */
2101: if (x && b) {
2102: PetscCall(VecGetArrayRead(x, &xx));
2103: PetscCall(VecGetArray(b, &bb));
2104: for (i = 0; i < N; i++) bb[rows[i]] = diag * xx[rows[i]];
2105: PetscCall(VecRestoreArrayRead(x, &xx));
2106: PetscCall(VecRestoreArray(b, &bb));
2107: }
2109: PetscCall(MatDenseGetArray(A, &v));
2110: for (i = 0; i < N; i++) {
2111: slot = v + rows[i];
2112: for (j = 0; j < n; j++) {
2113: *slot = 0.0;
2114: slot += m;
2115: }
2116: }
2117: if (diag != 0.0) {
2118: PetscCheck(A->rmap->n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only coded for square matrices");
2119: for (i = 0; i < N; i++) {
2120: slot = v + (m + 1) * rows[i];
2121: *slot = diag;
2122: }
2123: }
2124: PetscCall(MatDenseRestoreArray(A, &v));
2125: PetscFunctionReturn(PETSC_SUCCESS);
2126: }
2128: static PetscErrorCode MatDenseGetLDA_SeqDense(Mat A, PetscInt *lda)
2129: {
2130: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2132: PetscFunctionBegin;
2133: *lda = mat->lda;
2134: PetscFunctionReturn(PETSC_SUCCESS);
2135: }
2137: PetscErrorCode MatDenseGetArray_SeqDense(Mat A, PetscScalar **array)
2138: {
2139: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2141: PetscFunctionBegin;
2142: PetscCheck(!mat->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
2143: *array = mat->v;
2144: PetscFunctionReturn(PETSC_SUCCESS);
2145: }
2147: PetscErrorCode MatDenseRestoreArray_SeqDense(Mat A, PetscScalar **array)
2148: {
2149: PetscFunctionBegin;
2150: if (array) *array = NULL;
2151: PetscFunctionReturn(PETSC_SUCCESS);
2152: }
2154: /*@
2155: MatDenseGetLDA - gets the leading dimension of the array returned from `MatDenseGetArray()`
2157: Not Collective
2159: Input Parameter:
2160: . A - a `MATDENSE` or `MATDENSECUDA` matrix
2162: Output Parameter:
2163: . lda - the leading dimension
2165: Level: intermediate
2167: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseSetLDA()`
2168: @*/
2169: PetscErrorCode MatDenseGetLDA(Mat A, PetscInt *lda)
2170: {
2171: PetscFunctionBegin;
2173: PetscAssertPointer(lda, 2);
2174: MatCheckPreallocated(A, 1);
2175: PetscUseMethod(A, "MatDenseGetLDA_C", (Mat, PetscInt *), (A, lda));
2176: PetscFunctionReturn(PETSC_SUCCESS);
2177: }
2179: /*@
2180: MatDenseSetLDA - Sets the leading dimension of the array used by the `MATDENSE` matrix
2182: Collective if the matrix layouts have not yet been setup
2184: Input Parameters:
2185: + A - a `MATDENSE` or `MATDENSECUDA` matrix
2186: - lda - the leading dimension
2188: Level: intermediate
2190: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetLDA()`
2191: @*/
2192: PetscErrorCode MatDenseSetLDA(Mat A, PetscInt lda)
2193: {
2194: PetscFunctionBegin;
2196: PetscTryMethod(A, "MatDenseSetLDA_C", (Mat, PetscInt), (A, lda));
2197: PetscFunctionReturn(PETSC_SUCCESS);
2198: }
2200: /*@
2201: MatDenseGetArray - gives read-write access to the array where the data for a `MATDENSE` matrix is stored
2203: Logically Collective
2205: Input Parameter:
2206: . A - a dense matrix
2208: Output Parameter:
2209: . array - pointer to the data
2211: Level: intermediate
2213: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2214: @*/
2215: PetscErrorCode MatDenseGetArray(Mat A, PetscScalar *array[]) PeNS
2216: {
2217: PetscFunctionBegin;
2219: PetscAssertPointer(array, 2);
2220: PetscUseMethod(A, "MatDenseGetArray_C", (Mat, PetscScalar **), (A, array));
2221: PetscFunctionReturn(PETSC_SUCCESS);
2222: }
2224: /*@
2225: MatDenseRestoreArray - returns access to the array where the data for a `MATDENSE` matrix is stored obtained by `MatDenseGetArray()`
2227: Logically Collective
2229: Input Parameters:
2230: + A - a dense matrix
2231: - array - pointer to the data (may be `NULL`)
2233: Level: intermediate
2235: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2236: @*/
2237: PetscErrorCode MatDenseRestoreArray(Mat A, PetscScalar *array[]) PeNS
2238: {
2239: PetscFunctionBegin;
2241: if (array) PetscAssertPointer(array, 2);
2242: PetscUseMethod(A, "MatDenseRestoreArray_C", (Mat, PetscScalar **), (A, array));
2243: PetscCall(PetscObjectStateIncrease((PetscObject)A));
2244: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
2245: A->offloadmask = PETSC_OFFLOAD_CPU;
2246: #endif
2247: PetscFunctionReturn(PETSC_SUCCESS);
2248: }
2250: /*@
2251: MatDenseGetArrayRead - gives read-only access to the array where the data for a `MATDENSE` matrix is stored
2253: Not Collective
2255: Input Parameter:
2256: . A - a dense matrix
2258: Output Parameter:
2259: . array - pointer to the data
2261: Level: intermediate
2263: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayRead()`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2264: @*/
2265: PetscErrorCode MatDenseGetArrayRead(Mat A, const PetscScalar *array[]) PeNS
2266: {
2267: PetscFunctionBegin;
2269: PetscAssertPointer(array, 2);
2270: PetscUseMethod(A, "MatDenseGetArrayRead_C", (Mat, PetscScalar **), (A, (PetscScalar **)array));
2271: PetscFunctionReturn(PETSC_SUCCESS);
2272: }
2274: /*@
2275: MatDenseRestoreArrayRead - returns access to the array where the data for a `MATDENSE` matrix is stored obtained by `MatDenseGetArrayRead()`
2277: Not Collective
2279: Input Parameters:
2280: + A - a dense matrix
2281: - array - pointer to the data (may be `NULL`)
2283: Level: intermediate
2285: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayRead()`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2286: @*/
2287: PetscErrorCode MatDenseRestoreArrayRead(Mat A, const PetscScalar *array[]) PeNS
2288: {
2289: PetscFunctionBegin;
2291: if (array) PetscAssertPointer(array, 2);
2292: PetscUseMethod(A, "MatDenseRestoreArrayRead_C", (Mat, PetscScalar **), (A, (PetscScalar **)array));
2293: PetscFunctionReturn(PETSC_SUCCESS);
2294: }
2296: /*@
2297: MatDenseGetArrayWrite - gives write-only access to the array where the data for a `MATDENSE` matrix is stored
2299: Not Collective
2301: Input Parameter:
2302: . A - a dense matrix
2304: Output Parameter:
2305: . array - pointer to the data
2307: Level: intermediate
2309: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayWrite()`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`
2310: @*/
2311: PetscErrorCode MatDenseGetArrayWrite(Mat A, PetscScalar *array[]) PeNS
2312: {
2313: PetscFunctionBegin;
2315: PetscAssertPointer(array, 2);
2316: PetscUseMethod(A, "MatDenseGetArrayWrite_C", (Mat, PetscScalar **), (A, array));
2317: PetscFunctionReturn(PETSC_SUCCESS);
2318: }
2320: /*@
2321: MatDenseRestoreArrayWrite - returns access to the array where the data for a `MATDENSE` matrix is stored obtained by `MatDenseGetArrayWrite()`
2323: Not Collective
2325: Input Parameters:
2326: + A - a dense matrix
2327: - array - pointer to the data (may be `NULL`)
2329: Level: intermediate
2331: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayWrite()`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`
2332: @*/
2333: PetscErrorCode MatDenseRestoreArrayWrite(Mat A, PetscScalar *array[]) PeNS
2334: {
2335: PetscFunctionBegin;
2337: if (array) PetscAssertPointer(array, 2);
2338: PetscUseMethod(A, "MatDenseRestoreArrayWrite_C", (Mat, PetscScalar **), (A, array));
2339: PetscCall(PetscObjectStateIncrease((PetscObject)A));
2340: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
2341: A->offloadmask = PETSC_OFFLOAD_CPU;
2342: #endif
2343: PetscFunctionReturn(PETSC_SUCCESS);
2344: }
2346: /*@
2347: MatDenseGetArrayAndMemType - gives read-write access to the array where the data for a `MATDENSE` matrix is stored
2349: Logically Collective
2351: Input Parameter:
2352: . A - a dense matrix
2354: Output Parameters:
2355: + array - pointer to the data
2356: - mtype - memory type of the returned pointer
2358: Level: intermediate
2360: Note:
2361: If the matrix is of a device type such as `MATDENSECUDA`, `MATDENSEHIP`, etc.,
2362: an array on device is always returned and is guaranteed to contain the matrix's latest data.
2364: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayAndMemType()`, `MatDenseGetArrayReadAndMemType()`, `MatDenseGetArrayWriteAndMemType()`, `MatDenseGetArrayRead()`,
2365: `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`, `MatSeqAIJGetCSRAndMemType()`
2366: @*/
2367: PetscErrorCode MatDenseGetArrayAndMemType(Mat A, PetscScalar *array[], PetscMemType *mtype)
2368: {
2369: PetscBool isMPI;
2371: PetscFunctionBegin;
2373: PetscAssertPointer(array, 2);
2374: PetscCall(MatBindToCPU(A, PETSC_FALSE)); /* We want device matrices to always return device arrays, so we unbind the matrix if it is bound to CPU */
2375: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2376: if (isMPI) {
2377: /* Dispatch here so that the code can be reused for all subclasses of MATDENSE */
2378: PetscCall(MatDenseGetArrayAndMemType(((Mat_MPIDense *)A->data)->A, array, mtype));
2379: } else {
2380: PetscErrorCode (*fptr)(Mat, PetscScalar **, PetscMemType *);
2382: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseGetArrayAndMemType_C", &fptr));
2383: if (fptr) {
2384: PetscCall((*fptr)(A, array, mtype));
2385: } else {
2386: PetscUseMethod(A, "MatDenseGetArray_C", (Mat, PetscScalar **), (A, array));
2387: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2388: }
2389: }
2390: PetscFunctionReturn(PETSC_SUCCESS);
2391: }
2393: /*@
2394: MatDenseRestoreArrayAndMemType - returns access to the array that is obtained by `MatDenseGetArrayAndMemType()`
2396: Logically Collective
2398: Input Parameters:
2399: + A - a dense matrix
2400: - array - pointer to the data
2402: Level: intermediate
2404: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayAndMemType()`, `MatDenseGetArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2405: @*/
2406: PetscErrorCode MatDenseRestoreArrayAndMemType(Mat A, PetscScalar *array[])
2407: {
2408: PetscBool isMPI;
2410: PetscFunctionBegin;
2412: if (array) PetscAssertPointer(array, 2);
2413: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2414: if (isMPI) {
2415: PetscCall(MatDenseRestoreArrayAndMemType(((Mat_MPIDense *)A->data)->A, array));
2416: } else {
2417: PetscErrorCode (*fptr)(Mat, PetscScalar **);
2419: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseRestoreArrayAndMemType_C", &fptr));
2420: if (fptr) {
2421: PetscCall((*fptr)(A, array));
2422: } else {
2423: PetscUseMethod(A, "MatDenseRestoreArray_C", (Mat, PetscScalar **), (A, array));
2424: }
2425: if (array) *array = NULL;
2426: }
2427: PetscCall(PetscObjectStateIncrease((PetscObject)A));
2428: PetscFunctionReturn(PETSC_SUCCESS);
2429: }
2431: /*@
2432: MatDenseGetArrayReadAndMemType - gives read-only access to the array where the data for a `MATDENSE` matrix is stored
2434: Logically Collective
2436: Input Parameter:
2437: . A - a dense matrix
2439: Output Parameters:
2440: + array - pointer to the data
2441: - mtype - memory type of the returned pointer
2443: Level: intermediate
2445: Note:
2446: If the matrix is of a device type such as `MATDENSECUDA`, `MATDENSEHIP`, etc.,
2447: an array on device is always returned and is guaranteed to contain the matrix's latest data.
2449: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayReadAndMemType()`, `MatDenseGetArrayWriteAndMemType()`,
2450: `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`, `MatSeqAIJGetCSRAndMemType()`
2451: @*/
2452: PetscErrorCode MatDenseGetArrayReadAndMemType(Mat A, const PetscScalar *array[], PetscMemType *mtype)
2453: {
2454: PetscBool isMPI;
2456: PetscFunctionBegin;
2458: PetscAssertPointer(array, 2);
2459: PetscCall(MatBindToCPU(A, PETSC_FALSE)); /* We want device matrices to always return device arrays, so we unbind the matrix if it is bound to CPU */
2460: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2461: if (isMPI) { /* Dispatch here so that the code can be reused for all subclasses of MATDENSE */
2462: PetscCall(MatDenseGetArrayReadAndMemType(((Mat_MPIDense *)A->data)->A, array, mtype));
2463: } else {
2464: PetscErrorCode (*fptr)(Mat, const PetscScalar **, PetscMemType *);
2466: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseGetArrayReadAndMemType_C", &fptr));
2467: if (fptr) {
2468: PetscCall((*fptr)(A, array, mtype));
2469: } else {
2470: PetscUseMethod(A, "MatDenseGetArrayRead_C", (Mat, PetscScalar **), (A, (PetscScalar **)array));
2471: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2472: }
2473: }
2474: PetscFunctionReturn(PETSC_SUCCESS);
2475: }
2477: /*@
2478: MatDenseRestoreArrayReadAndMemType - returns access to the array that is obtained by `MatDenseGetArrayReadAndMemType()`
2480: Logically Collective
2482: Input Parameters:
2483: + A - a dense matrix
2484: - array - pointer to the data
2486: Level: intermediate
2488: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayReadAndMemType()`, `MatDenseGetArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2489: @*/
2490: PetscErrorCode MatDenseRestoreArrayReadAndMemType(Mat A, const PetscScalar *array[])
2491: {
2492: PetscBool isMPI;
2494: PetscFunctionBegin;
2496: if (array) PetscAssertPointer(array, 2);
2497: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2498: if (isMPI) {
2499: PetscCall(MatDenseRestoreArrayReadAndMemType(((Mat_MPIDense *)A->data)->A, array));
2500: } else {
2501: PetscErrorCode (*fptr)(Mat, const PetscScalar **);
2503: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseRestoreArrayReadAndMemType_C", &fptr));
2504: if (fptr) {
2505: PetscCall((*fptr)(A, array));
2506: } else {
2507: PetscUseMethod(A, "MatDenseRestoreArrayRead_C", (Mat, PetscScalar **), (A, (PetscScalar **)array));
2508: }
2509: if (array) *array = NULL;
2510: }
2511: PetscFunctionReturn(PETSC_SUCCESS);
2512: }
2514: /*@
2515: MatDenseGetArrayWriteAndMemType - gives write-only access to the array where the data for a `MATDENSE` matrix is stored
2517: Logically Collective
2519: Input Parameter:
2520: . A - a dense matrix
2522: Output Parameters:
2523: + array - pointer to the data
2524: - mtype - memory type of the returned pointer
2526: Level: intermediate
2528: Note:
2529: If the matrix is of a device type such as `MATDENSECUDA`, `MATDENSEHIP`, etc.,
2530: an array on device is always returned and is guaranteed to contain the matrix's latest data.
2532: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayWriteAndMemType()`, `MatDenseGetArrayReadAndMemType()`, `MatDenseGetArrayRead()`,
2533: `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`, `MatSeqAIJGetCSRAndMemType()`
2534: @*/
2535: PetscErrorCode MatDenseGetArrayWriteAndMemType(Mat A, PetscScalar *array[], PetscMemType *mtype)
2536: {
2537: PetscBool isMPI;
2539: PetscFunctionBegin;
2541: PetscAssertPointer(array, 2);
2542: PetscCall(MatBindToCPU(A, PETSC_FALSE)); /* We want device matrices to always return device arrays, so we unbind the matrix if it is bound to CPU */
2543: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2544: if (isMPI) {
2545: PetscCall(MatDenseGetArrayWriteAndMemType(((Mat_MPIDense *)A->data)->A, array, mtype));
2546: } else {
2547: PetscErrorCode (*fptr)(Mat, PetscScalar **, PetscMemType *);
2549: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseGetArrayWriteAndMemType_C", &fptr));
2550: if (fptr) {
2551: PetscCall((*fptr)(A, array, mtype));
2552: } else {
2553: PetscUseMethod(A, "MatDenseGetArrayWrite_C", (Mat, PetscScalar **), (A, array));
2554: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2555: }
2556: }
2557: PetscFunctionReturn(PETSC_SUCCESS);
2558: }
2560: /*@
2561: MatDenseRestoreArrayWriteAndMemType - returns access to the array that is obtained by `MatDenseGetArrayReadAndMemType()`
2563: Logically Collective
2565: Input Parameters:
2566: + A - a dense matrix
2567: - array - pointer to the data
2569: Level: intermediate
2571: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayWriteAndMemType()`, `MatDenseGetArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2572: @*/
2573: PetscErrorCode MatDenseRestoreArrayWriteAndMemType(Mat A, PetscScalar *array[])
2574: {
2575: PetscBool isMPI;
2577: PetscFunctionBegin;
2579: if (array) PetscAssertPointer(array, 2);
2580: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2581: if (isMPI) {
2582: PetscCall(MatDenseRestoreArrayWriteAndMemType(((Mat_MPIDense *)A->data)->A, array));
2583: } else {
2584: PetscErrorCode (*fptr)(Mat, PetscScalar **);
2586: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseRestoreArrayWriteAndMemType_C", &fptr));
2587: if (fptr) {
2588: PetscCall((*fptr)(A, array));
2589: } else {
2590: PetscUseMethod(A, "MatDenseRestoreArrayWrite_C", (Mat, PetscScalar **), (A, array));
2591: }
2592: if (array) *array = NULL;
2593: }
2594: PetscCall(PetscObjectStateIncrease((PetscObject)A));
2595: PetscFunctionReturn(PETSC_SUCCESS);
2596: }
2598: static PetscErrorCode MatCreateSubMatrix_SeqDense(Mat A, IS isrow, IS iscol, MatReuse scall, Mat *B)
2599: {
2600: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2601: PetscInt i, j, nrows, ncols, ldb;
2602: const PetscInt *irow, *icol;
2603: PetscScalar *av, *bv, *v = mat->v;
2604: Mat newmat;
2606: PetscFunctionBegin;
2607: PetscCall(ISGetIndices(isrow, &irow));
2608: PetscCall(ISGetIndices(iscol, &icol));
2609: PetscCall(ISGetLocalSize(isrow, &nrows));
2610: PetscCall(ISGetLocalSize(iscol, &ncols));
2612: /* Check submatrixcall */
2613: if (scall == MAT_REUSE_MATRIX) {
2614: PetscInt n_cols, n_rows;
2615: PetscCall(MatGetSize(*B, &n_rows, &n_cols));
2616: if (n_rows != nrows || n_cols != ncols) {
2617: /* resize the result matrix to match number of requested rows/columns */
2618: PetscCall(MatSetSizes(*B, nrows, ncols, nrows, ncols));
2619: }
2620: newmat = *B;
2621: } else {
2622: /* Create and fill new matrix */
2623: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &newmat));
2624: PetscCall(MatSetSizes(newmat, nrows, ncols, nrows, ncols));
2625: PetscCall(MatSetType(newmat, ((PetscObject)A)->type_name));
2626: PetscCall(MatSeqDenseSetPreallocation(newmat, NULL));
2627: }
2629: /* Now extract the data pointers and do the copy,column at a time */
2630: PetscCall(MatDenseGetArray(newmat, &bv));
2631: PetscCall(MatDenseGetLDA(newmat, &ldb));
2632: for (i = 0; i < ncols; i++) {
2633: av = v + mat->lda * icol[i];
2634: for (j = 0; j < nrows; j++) bv[j] = av[irow[j]];
2635: bv += ldb;
2636: }
2637: PetscCall(MatDenseRestoreArray(newmat, &bv));
2639: /* Assemble the matrices so that the correct flags are set */
2640: PetscCall(MatAssemblyBegin(newmat, MAT_FINAL_ASSEMBLY));
2641: PetscCall(MatAssemblyEnd(newmat, MAT_FINAL_ASSEMBLY));
2643: /* Free work space */
2644: PetscCall(ISRestoreIndices(isrow, &irow));
2645: PetscCall(ISRestoreIndices(iscol, &icol));
2646: *B = newmat;
2647: PetscFunctionReturn(PETSC_SUCCESS);
2648: }
2650: static PetscErrorCode MatCreateSubMatrices_SeqDense(Mat A, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *B[])
2651: {
2652: PetscInt i;
2654: PetscFunctionBegin;
2655: if (scall == MAT_INITIAL_MATRIX) PetscCall(PetscCalloc1(n, B));
2657: for (i = 0; i < n; i++) PetscCall(MatCreateSubMatrix_SeqDense(A, irow[i], icol[i], scall, &(*B)[i]));
2658: PetscFunctionReturn(PETSC_SUCCESS);
2659: }
2661: PetscErrorCode MatCopy_SeqDense(Mat A, Mat B, MatStructure str)
2662: {
2663: Mat_SeqDense *a = (Mat_SeqDense *)A->data, *b = (Mat_SeqDense *)B->data;
2664: const PetscScalar *va;
2665: PetscScalar *vb;
2666: PetscInt lda1 = a->lda, lda2 = b->lda, m = A->rmap->n, n = A->cmap->n, j;
2668: PetscFunctionBegin;
2669: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
2670: if (A->ops->copy != B->ops->copy) {
2671: PetscCall(MatCopy_Basic(A, B, str));
2672: PetscFunctionReturn(PETSC_SUCCESS);
2673: }
2674: PetscCheck(m == B->rmap->n && n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "size(B) != size(A)");
2675: PetscCall(MatDenseGetArrayRead(A, &va));
2676: PetscCall(MatDenseGetArray(B, &vb));
2677: if (lda1 > m || lda2 > m) {
2678: for (j = 0; j < n; j++) PetscCall(PetscArraycpy(vb + j * lda2, va + j * lda1, m));
2679: } else {
2680: PetscCall(PetscArraycpy(vb, va, A->rmap->n * A->cmap->n));
2681: }
2682: PetscCall(MatDenseRestoreArray(B, &vb));
2683: PetscCall(MatDenseRestoreArrayRead(A, &va));
2684: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
2685: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
2686: PetscFunctionReturn(PETSC_SUCCESS);
2687: }
2689: PetscErrorCode MatSetUp_SeqDense(Mat A)
2690: {
2691: PetscFunctionBegin;
2692: PetscCall(PetscLayoutSetUp(A->rmap));
2693: PetscCall(PetscLayoutSetUp(A->cmap));
2694: if (!A->preallocated) PetscCall(MatSeqDenseSetPreallocation(A, NULL));
2695: PetscFunctionReturn(PETSC_SUCCESS);
2696: }
2698: PetscErrorCode MatConjugate_SeqDense(Mat A)
2699: {
2700: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2701: PetscInt i, j;
2702: PetscInt min = PetscMin(A->rmap->n, A->cmap->n);
2703: PetscScalar *aa;
2705: PetscFunctionBegin;
2706: PetscCall(MatDenseGetArray(A, &aa));
2707: for (j = 0; j < A->cmap->n; j++)
2708: for (i = 0; i < A->rmap->n; i++) aa[i + j * mat->lda] = PetscConj(aa[i + j * mat->lda]);
2709: PetscCall(MatDenseRestoreArray(A, &aa));
2710: if (mat->tau)
2711: for (i = 0; i < min; i++) mat->tau[i] = PetscConj(mat->tau[i]);
2712: PetscFunctionReturn(PETSC_SUCCESS);
2713: }
2715: static PetscErrorCode MatRealPart_SeqDense(Mat A)
2716: {
2717: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2718: PetscInt i, j;
2719: PetscScalar *aa;
2721: PetscFunctionBegin;
2722: PetscCall(MatDenseGetArray(A, &aa));
2723: for (j = 0; j < A->cmap->n; j++) {
2724: for (i = 0; i < A->rmap->n; i++) aa[i + j * mat->lda] = PetscRealPart(aa[i + j * mat->lda]);
2725: }
2726: PetscCall(MatDenseRestoreArray(A, &aa));
2727: PetscFunctionReturn(PETSC_SUCCESS);
2728: }
2730: static PetscErrorCode MatImaginaryPart_SeqDense(Mat A)
2731: {
2732: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2733: PetscInt i, j;
2734: PetscScalar *aa;
2736: PetscFunctionBegin;
2737: PetscCall(MatDenseGetArray(A, &aa));
2738: for (j = 0; j < A->cmap->n; j++) {
2739: for (i = 0; i < A->rmap->n; i++) aa[i + j * mat->lda] = PetscImaginaryPart(aa[i + j * mat->lda]);
2740: }
2741: PetscCall(MatDenseRestoreArray(A, &aa));
2742: PetscFunctionReturn(PETSC_SUCCESS);
2743: }
2745: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqDense(Mat A, Mat B, PetscReal fill, Mat C)
2746: {
2747: PetscInt m = A->rmap->n, n = B->cmap->n;
2748: PetscBool cisdense = PETSC_FALSE;
2750: PetscFunctionBegin;
2751: PetscCall(MatSetSizes(C, m, n, m, n));
2752: #if PetscDefined(HAVE_CUDA)
2753: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, ""));
2754: #endif
2755: #if PetscDefined(HAVE_HIP)
2756: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSEHIP, ""));
2757: #endif
2758: if (!cisdense) {
2759: PetscBool flg;
2761: PetscCall(PetscObjectTypeCompare((PetscObject)B, ((PetscObject)A)->type_name, &flg));
2762: PetscCall(MatSetType(C, flg ? ((PetscObject)A)->type_name : MATDENSE));
2763: }
2764: PetscCall(MatSetUp(C));
2765: PetscFunctionReturn(PETSC_SUCCESS);
2766: }
2768: PetscErrorCode MatMatMultNumeric_SeqDense_SeqDense(Mat A, Mat B, Mat C)
2769: {
2770: Mat_SeqDense *a = (Mat_SeqDense *)A->data, *b = (Mat_SeqDense *)B->data, *c = (Mat_SeqDense *)C->data;
2771: const PetscScalar *av, *bv;
2772: PetscScalar *cv;
2773: PetscBLASInt m, n, k;
2774: PetscScalar _DOne = 1.0, _DZero = 0.0;
2776: PetscFunctionBegin;
2777: PetscCall(PetscBLASIntCast(C->rmap->n, &m));
2778: PetscCall(PetscBLASIntCast(C->cmap->n, &n));
2779: PetscCall(PetscBLASIntCast(A->cmap->n, &k));
2780: if (!m || !n || !k) {
2781: PetscCall(MatZeroEntries(C));
2782: PetscFunctionReturn(PETSC_SUCCESS);
2783: }
2784: PetscCall(MatDenseGetArrayRead(A, &av));
2785: PetscCall(MatDenseGetArrayRead(B, &bv));
2786: PetscCall(MatDenseGetArrayWrite(C, &cv));
2787: PetscCallBLAS("BLASgemm", BLASgemm_("N", "N", &m, &n, &k, &_DOne, av, &a->lda, bv, &b->lda, &_DZero, cv, &c->lda));
2788: PetscCall(MatDenseRestoreArrayRead(A, &av));
2789: PetscCall(MatDenseRestoreArrayRead(B, &bv));
2790: PetscCall(MatDenseRestoreArrayWrite(C, &cv));
2791: PetscCall(PetscLogFlops(1.0 * m * n * k + 1.0 * m * n * (k - 1)));
2792: PetscFunctionReturn(PETSC_SUCCESS);
2793: }
2795: PetscErrorCode MatMatTransposeMultSymbolic_SeqDense_SeqDense(Mat A, Mat B, PetscReal fill, Mat C)
2796: {
2797: PetscInt m = A->rmap->n, n = B->rmap->n;
2798: PetscBool cisdense = PETSC_FALSE;
2800: PetscFunctionBegin;
2801: PetscCall(MatSetSizes(C, m, n, m, n));
2802: #if PetscDefined(HAVE_CUDA)
2803: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, ""));
2804: #endif
2805: #if PetscDefined(HAVE_HIP)
2806: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSEHIP, ""));
2807: #endif
2808: if (!cisdense) {
2809: PetscBool flg;
2811: PetscCall(PetscObjectTypeCompare((PetscObject)B, ((PetscObject)A)->type_name, &flg));
2812: PetscCall(MatSetType(C, flg ? ((PetscObject)A)->type_name : MATDENSE));
2813: }
2814: PetscCall(MatSetUp(C));
2815: PetscFunctionReturn(PETSC_SUCCESS);
2816: }
2818: PetscErrorCode MatMatTransposeMultNumeric_SeqDense_SeqDense(Mat A, Mat B, Mat C)
2819: {
2820: Mat_SeqDense *a = (Mat_SeqDense *)A->data, *b = (Mat_SeqDense *)B->data, *c = (Mat_SeqDense *)C->data;
2821: const PetscScalar *av, *bv;
2822: PetscScalar *cv;
2823: PetscBLASInt m, n, k;
2824: PetscScalar _DOne = 1.0, _DZero = 0.0;
2826: PetscFunctionBegin;
2827: PetscCall(PetscBLASIntCast(C->rmap->n, &m));
2828: PetscCall(PetscBLASIntCast(C->cmap->n, &n));
2829: PetscCall(PetscBLASIntCast(A->cmap->n, &k));
2830: if (!m || !n || !k) {
2831: PetscCall(MatZeroEntries(C));
2832: PetscFunctionReturn(PETSC_SUCCESS);
2833: }
2834: PetscCall(MatDenseGetArrayRead(A, &av));
2835: PetscCall(MatDenseGetArrayRead(B, &bv));
2836: PetscCall(MatDenseGetArrayWrite(C, &cv));
2837: PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &m, &n, &k, &_DOne, av, &a->lda, bv, &b->lda, &_DZero, cv, &c->lda));
2838: PetscCall(MatDenseRestoreArrayRead(A, &av));
2839: PetscCall(MatDenseRestoreArrayRead(B, &bv));
2840: PetscCall(MatDenseRestoreArrayWrite(C, &cv));
2841: PetscCall(PetscLogFlops(1.0 * m * n * k + 1.0 * m * n * (k - 1)));
2842: PetscFunctionReturn(PETSC_SUCCESS);
2843: }
2845: PetscErrorCode MatTransposeMatMultSymbolic_SeqDense_SeqDense(Mat A, Mat B, PetscReal fill, Mat C)
2846: {
2847: PetscInt m = A->cmap->n, n = B->cmap->n;
2848: PetscBool cisdense = PETSC_FALSE;
2850: PetscFunctionBegin;
2851: PetscCall(MatSetSizes(C, m, n, m, n));
2852: #if PetscDefined(HAVE_CUDA)
2853: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, ""));
2854: #endif
2855: #if PetscDefined(HAVE_HIP)
2856: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSEHIP, ""));
2857: #endif
2858: if (!cisdense) {
2859: PetscBool flg;
2861: PetscCall(PetscObjectTypeCompare((PetscObject)B, ((PetscObject)A)->type_name, &flg));
2862: PetscCall(MatSetType(C, flg ? ((PetscObject)A)->type_name : MATDENSE));
2863: }
2864: PetscCall(MatSetUp(C));
2865: PetscFunctionReturn(PETSC_SUCCESS);
2866: }
2868: PetscErrorCode MatTransposeMatMultNumeric_SeqDense_SeqDense(Mat A, Mat B, Mat C)
2869: {
2870: Mat_SeqDense *a = (Mat_SeqDense *)A->data, *b = (Mat_SeqDense *)B->data, *c = (Mat_SeqDense *)C->data;
2871: const PetscScalar *av, *bv;
2872: PetscScalar *cv;
2873: PetscBLASInt m, n, k;
2874: PetscScalar _DOne = 1.0, _DZero = 0.0;
2876: PetscFunctionBegin;
2877: PetscCall(PetscBLASIntCast(C->rmap->n, &m));
2878: PetscCall(PetscBLASIntCast(C->cmap->n, &n));
2879: PetscCall(PetscBLASIntCast(A->rmap->n, &k));
2880: if (!m || !n || !k) {
2881: PetscCall(MatZeroEntries(C));
2882: PetscFunctionReturn(PETSC_SUCCESS);
2883: }
2884: PetscCall(MatDenseGetArrayRead(A, &av));
2885: PetscCall(MatDenseGetArrayRead(B, &bv));
2886: PetscCall(MatDenseGetArrayWrite(C, &cv));
2887: PetscCallBLAS("BLASgemm", BLASgemm_("T", "N", &m, &n, &k, &_DOne, av, &a->lda, bv, &b->lda, &_DZero, cv, &c->lda));
2888: PetscCall(MatDenseRestoreArrayRead(A, &av));
2889: PetscCall(MatDenseRestoreArrayRead(B, &bv));
2890: PetscCall(MatDenseRestoreArrayWrite(C, &cv));
2891: PetscCall(PetscLogFlops(1.0 * m * n * k + 1.0 * m * n * (k - 1)));
2892: PetscFunctionReturn(PETSC_SUCCESS);
2893: }
2895: static PetscErrorCode MatProductSetFromOptions_SeqDense_AB(Mat C)
2896: {
2897: PetscFunctionBegin;
2898: C->ops->matmultsymbolic = MatMatMultSymbolic_SeqDense_SeqDense;
2899: C->ops->productsymbolic = MatProductSymbolic_AB;
2900: PetscFunctionReturn(PETSC_SUCCESS);
2901: }
2903: static PetscErrorCode MatProductSetFromOptions_SeqDense_AtB(Mat C)
2904: {
2905: PetscFunctionBegin;
2906: C->ops->transposematmultsymbolic = MatTransposeMatMultSymbolic_SeqDense_SeqDense;
2907: C->ops->productsymbolic = MatProductSymbolic_AtB;
2908: PetscFunctionReturn(PETSC_SUCCESS);
2909: }
2911: static PetscErrorCode MatProductSetFromOptions_SeqDense_ABt(Mat C)
2912: {
2913: PetscFunctionBegin;
2914: C->ops->mattransposemultsymbolic = MatMatTransposeMultSymbolic_SeqDense_SeqDense;
2915: C->ops->productsymbolic = MatProductSymbolic_ABt;
2916: PetscFunctionReturn(PETSC_SUCCESS);
2917: }
2919: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_SeqDense(Mat C)
2920: {
2921: Mat_Product *product = C->product;
2923: PetscFunctionBegin;
2924: switch (product->type) {
2925: case MATPRODUCT_AB:
2926: PetscCall(MatProductSetFromOptions_SeqDense_AB(C));
2927: break;
2928: case MATPRODUCT_AtB:
2929: PetscCall(MatProductSetFromOptions_SeqDense_AtB(C));
2930: break;
2931: case MATPRODUCT_ABt:
2932: PetscCall(MatProductSetFromOptions_SeqDense_ABt(C));
2933: break;
2934: default:
2935: break;
2936: }
2937: PetscFunctionReturn(PETSC_SUCCESS);
2938: }
2940: static PetscErrorCode MatGetRowMax_SeqDense(Mat A, Vec v, PetscInt idx[])
2941: {
2942: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
2943: PetscInt i, j, m = A->rmap->n, n = A->cmap->n, p;
2944: PetscScalar *x;
2945: const PetscScalar *aa;
2947: PetscFunctionBegin;
2948: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
2949: PetscCall(VecGetArray(v, &x));
2950: PetscCall(VecGetLocalSize(v, &p));
2951: PetscCall(MatDenseGetArrayRead(A, &aa));
2952: PetscCheck(p == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
2953: for (i = 0; i < m; i++) {
2954: x[i] = aa[i];
2955: if (idx) idx[i] = 0;
2956: for (j = 1; j < n; j++) {
2957: if (PetscRealPart(x[i]) < PetscRealPart(aa[i + a->lda * j])) {
2958: x[i] = aa[i + a->lda * j];
2959: if (idx) idx[i] = j;
2960: }
2961: }
2962: }
2963: PetscCall(MatDenseRestoreArrayRead(A, &aa));
2964: PetscCall(VecRestoreArray(v, &x));
2965: PetscFunctionReturn(PETSC_SUCCESS);
2966: }
2968: static PetscErrorCode MatGetRowMaxAbs_SeqDense(Mat A, Vec v, PetscInt idx[])
2969: {
2970: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
2971: PetscInt i, j, m = A->rmap->n, n = A->cmap->n, p;
2972: PetscScalar *x;
2973: PetscReal atmp;
2974: const PetscScalar *aa;
2976: PetscFunctionBegin;
2977: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
2978: PetscCall(VecGetArray(v, &x));
2979: PetscCall(VecGetLocalSize(v, &p));
2980: PetscCall(MatDenseGetArrayRead(A, &aa));
2981: PetscCheck(p == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
2982: for (i = 0; i < m; i++) {
2983: x[i] = PetscAbsScalar(aa[i]);
2984: for (j = 1; j < n; j++) {
2985: atmp = PetscAbsScalar(aa[i + a->lda * j]);
2986: if (PetscAbsScalar(x[i]) < atmp) {
2987: x[i] = atmp;
2988: if (idx) idx[i] = j;
2989: }
2990: }
2991: }
2992: PetscCall(MatDenseRestoreArrayRead(A, &aa));
2993: PetscCall(VecRestoreArray(v, &x));
2994: PetscFunctionReturn(PETSC_SUCCESS);
2995: }
2997: static PetscErrorCode MatGetRowMin_SeqDense(Mat A, Vec v, PetscInt idx[])
2998: {
2999: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3000: PetscInt i, j, m = A->rmap->n, n = A->cmap->n, p;
3001: PetscScalar *x;
3002: const PetscScalar *aa;
3004: PetscFunctionBegin;
3005: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3006: PetscCall(MatDenseGetArrayRead(A, &aa));
3007: PetscCall(VecGetArray(v, &x));
3008: PetscCall(VecGetLocalSize(v, &p));
3009: PetscCheck(p == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3010: for (i = 0; i < m; i++) {
3011: x[i] = aa[i];
3012: if (idx) idx[i] = 0;
3013: for (j = 1; j < n; j++) {
3014: if (PetscRealPart(x[i]) > PetscRealPart(aa[i + a->lda * j])) {
3015: x[i] = aa[i + a->lda * j];
3016: if (idx) idx[i] = j;
3017: }
3018: }
3019: }
3020: PetscCall(VecRestoreArray(v, &x));
3021: PetscCall(MatDenseRestoreArrayRead(A, &aa));
3022: PetscFunctionReturn(PETSC_SUCCESS);
3023: }
3025: PetscErrorCode MatGetColumnVector_SeqDense(Mat A, Vec v, PetscInt col)
3026: {
3027: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3028: PetscScalar *x;
3029: const PetscScalar *aa;
3031: PetscFunctionBegin;
3032: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3033: PetscCall(MatDenseGetArrayRead(A, &aa));
3034: PetscCall(VecGetArray(v, &x));
3035: PetscCall(PetscArraycpy(x, aa + col * a->lda, A->rmap->n));
3036: PetscCall(VecRestoreArray(v, &x));
3037: PetscCall(MatDenseRestoreArrayRead(A, &aa));
3038: PetscFunctionReturn(PETSC_SUCCESS);
3039: }
3041: PETSC_INTERN PetscErrorCode MatGetColumnReductions_SeqDense(Mat A, PetscInt type, PetscReal *reductions)
3042: {
3043: PetscInt i, j, m, n;
3044: const PetscScalar *a;
3046: PetscFunctionBegin;
3047: PetscCall(MatGetSize(A, &m, &n));
3048: PetscCall(PetscArrayzero(reductions, n));
3049: PetscCall(MatDenseGetArrayRead(A, &a));
3050: if (type == NORM_2) {
3051: for (i = 0; i < n; i++) {
3052: for (j = 0; j < m; j++) reductions[i] += PetscAbsScalar(a[j] * a[j]);
3053: a = PetscSafePointerPlusOffset(a, m);
3054: }
3055: } else if (type == NORM_1) {
3056: for (i = 0; i < n; i++) {
3057: for (j = 0; j < m; j++) reductions[i] += PetscAbsScalar(a[j]);
3058: a = PetscSafePointerPlusOffset(a, m);
3059: }
3060: } else if (type == NORM_INFINITY) {
3061: for (i = 0; i < n; i++) {
3062: for (j = 0; j < m; j++) reductions[i] = PetscMax(PetscAbsScalar(a[j]), reductions[i]);
3063: a = PetscSafePointerPlusOffset(a, m);
3064: }
3065: } else if (type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART) {
3066: for (i = 0; i < n; i++) {
3067: for (j = 0; j < m; j++) reductions[i] += PetscRealPart(a[j]);
3068: a = PetscSafePointerPlusOffset(a, m);
3069: }
3070: } else if (type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART) {
3071: for (i = 0; i < n; i++) {
3072: for (j = 0; j < m; j++) reductions[i] += PetscImaginaryPart(a[j]);
3073: a = PetscSafePointerPlusOffset(a, m);
3074: }
3075: } else SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Unknown reduction type");
3076: PetscCall(MatDenseRestoreArrayRead(A, &a));
3077: if (type == NORM_2) {
3078: for (i = 0; i < n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
3079: } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
3080: for (i = 0; i < n; i++) reductions[i] /= m;
3081: }
3082: PetscFunctionReturn(PETSC_SUCCESS);
3083: }
3085: PetscErrorCode MatSetRandom_SeqDense(Mat x, PetscRandom rctx)
3086: {
3087: PetscScalar *a;
3088: PetscInt lda, m, n, i, j;
3090: PetscFunctionBegin;
3091: PetscCall(MatGetSize(x, &m, &n));
3092: PetscCall(MatDenseGetLDA(x, &lda));
3093: PetscCall(MatDenseGetArrayWrite(x, &a));
3094: for (j = 0; j < n; j++) {
3095: for (i = 0; i < m; i++) PetscCall(PetscRandomGetValue(rctx, a + j * lda + i));
3096: }
3097: PetscCall(MatDenseRestoreArrayWrite(x, &a));
3098: PetscFunctionReturn(PETSC_SUCCESS);
3099: }
3101: /* vals is not const */
3102: static PetscErrorCode MatDenseGetColumn_SeqDense(Mat A, PetscInt col, PetscScalar **vals)
3103: {
3104: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3105: PetscScalar *v;
3107: PetscFunctionBegin;
3108: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3109: PetscCall(MatDenseGetArray(A, &v));
3110: *vals = v + col * a->lda;
3111: PetscCall(MatDenseRestoreArray(A, &v));
3112: PetscFunctionReturn(PETSC_SUCCESS);
3113: }
3115: static PetscErrorCode MatDenseRestoreColumn_SeqDense(Mat A, PetscScalar **vals)
3116: {
3117: PetscFunctionBegin;
3118: if (vals) *vals = NULL; /* user cannot accidentally use the array later */
3119: PetscFunctionReturn(PETSC_SUCCESS);
3120: }
3122: static struct _MatOps MatOps_Values = {MatSetValues_SeqDense,
3123: MatGetRow_SeqDense,
3124: MatRestoreRow_SeqDense,
3125: MatMult_SeqDense,
3126: /* 4*/ MatMultAdd_SeqDense,
3127: MatMultTranspose_SeqDense,
3128: MatMultTransposeAdd_SeqDense,
3129: NULL,
3130: NULL,
3131: NULL,
3132: /* 10*/ NULL,
3133: MatLUFactor_SeqDense,
3134: MatCholeskyFactor_SeqDense,
3135: MatSOR_SeqDense,
3136: MatTranspose_SeqDense,
3137: /* 15*/ MatGetInfo_SeqDense,
3138: MatEqual_SeqDense,
3139: MatGetDiagonal_SeqDense,
3140: MatDiagonalScale_SeqDense,
3141: MatNorm_SeqDense,
3142: /* 20*/ NULL,
3143: NULL,
3144: MatSetOption_SeqDense,
3145: MatZeroEntries_SeqDense,
3146: /* 24*/ MatZeroRows_SeqDense,
3147: NULL,
3148: NULL,
3149: NULL,
3150: NULL,
3151: /* 29*/ MatSetUp_SeqDense,
3152: NULL,
3153: NULL,
3154: NULL,
3155: MatSetInf_SeqDense,
3156: /* 34*/ MatDuplicate_SeqDense,
3157: NULL,
3158: NULL,
3159: NULL,
3160: NULL,
3161: /* 39*/ MatAXPY_SeqDense,
3162: MatCreateSubMatrices_SeqDense,
3163: NULL,
3164: MatGetValues_SeqDense,
3165: MatCopy_SeqDense,
3166: /* 44*/ MatGetRowMax_SeqDense,
3167: MatScale_SeqDense,
3168: MatShift_SeqDense,
3169: NULL,
3170: MatZeroRowsColumns_SeqDense,
3171: /* 49*/ MatSetRandom_SeqDense,
3172: NULL,
3173: NULL,
3174: NULL,
3175: NULL,
3176: /* 54*/ NULL,
3177: NULL,
3178: NULL,
3179: NULL,
3180: NULL,
3181: /* 59*/ MatCreateSubMatrix_SeqDense,
3182: MatDestroy_SeqDense,
3183: MatView_SeqDense,
3184: NULL,
3185: NULL,
3186: /* 64*/ NULL,
3187: NULL,
3188: NULL,
3189: NULL,
3190: MatGetRowMaxAbs_SeqDense,
3191: /* 69*/ NULL,
3192: NULL,
3193: NULL,
3194: NULL,
3195: NULL,
3196: /* 74*/ NULL,
3197: NULL,
3198: NULL,
3199: NULL,
3200: MatLoad_SeqDense,
3201: /* 79*/ MatIsSymmetric_SeqDense,
3202: MatIsHermitian_SeqDense,
3203: NULL,
3204: NULL,
3205: NULL,
3206: /* 84*/ NULL,
3207: MatMatMultNumeric_SeqDense_SeqDense,
3208: NULL,
3209: NULL,
3210: MatMatTransposeMultNumeric_SeqDense_SeqDense,
3211: /* 89*/ NULL,
3212: MatProductSetFromOptions_SeqDense,
3213: NULL,
3214: NULL,
3215: MatConjugate_SeqDense,
3216: /* 94*/ NULL,
3217: NULL,
3218: MatRealPart_SeqDense,
3219: MatImaginaryPart_SeqDense,
3220: NULL,
3221: /* 99*/ NULL,
3222: NULL,
3223: NULL,
3224: MatGetRowMin_SeqDense,
3225: MatGetColumnVector_SeqDense,
3226: /*104*/ NULL,
3227: NULL,
3228: NULL,
3229: NULL,
3230: NULL,
3231: /*109*/ NULL,
3232: NULL,
3233: MatMultHermitianTranspose_SeqDense,
3234: MatMultHermitianTransposeAdd_SeqDense,
3235: NULL,
3236: /*114*/ NULL,
3237: MatGetColumnReductions_SeqDense,
3238: NULL,
3239: NULL,
3240: NULL,
3241: /*119*/ NULL,
3242: MatTransposeMatMultNumeric_SeqDense_SeqDense,
3243: NULL,
3244: NULL,
3245: NULL,
3246: /*124*/ NULL,
3247: NULL,
3248: NULL,
3249: NULL,
3250: NULL,
3251: /*129*/ MatCreateMPIMatConcatenateSeqMat_SeqDense,
3252: NULL,
3253: NULL,
3254: NULL,
3255: NULL,
3256: /*134*/ NULL,
3257: NULL,
3258: NULL,
3259: NULL,
3260: NULL,
3261: /*139*/ NULL,
3262: NULL,
3263: NULL,
3264: NULL,
3265: NULL,
3266: /*144*/ NULL,
3267: NULL,
3268: NULL,
3269: NULL};
3271: /*@
3272: MatCreateSeqDense - Creates a `MATSEQDENSE` that
3273: is stored in column major order (the usual Fortran format).
3275: Collective
3277: Input Parameters:
3278: + comm - MPI communicator, set to `PETSC_COMM_SELF`
3279: . m - number of rows
3280: . n - number of columns
3281: - data - optional location of matrix data in column major order. Use `NULL` for PETSc
3282: to control all matrix memory allocation.
3284: Output Parameter:
3285: . A - the matrix
3287: Level: intermediate
3289: Note:
3290: The data input variable is intended primarily for Fortran programmers
3291: who wish to allocate their own matrix memory space. Most users should
3292: set `data` = `NULL`.
3294: Developer Note:
3295: Many of the matrix operations for this variant use the BLAS and LAPACK routines.
3297: .seealso: [](ch_matrices), `Mat`, `MATSEQDENSE`, `MatCreate()`, `MatCreateDense()`, `MatSetValues()`
3298: @*/
3299: PetscErrorCode MatCreateSeqDense(MPI_Comm comm, PetscInt m, PetscInt n, PetscScalar data[], Mat *A)
3300: {
3301: PetscFunctionBegin;
3302: PetscCall(MatCreate(comm, A));
3303: PetscCall(MatSetSizes(*A, m, n, m, n));
3304: PetscCall(MatSetType(*A, MATSEQDENSE));
3305: PetscCall(MatSeqDenseSetPreallocation(*A, data));
3306: PetscFunctionReturn(PETSC_SUCCESS);
3307: }
3309: /*@
3310: MatSeqDenseSetPreallocation - Sets the array used for storing the matrix elements of a `MATSEQDENSE` matrix
3312: Collective
3314: Input Parameters:
3315: + B - the matrix
3316: - data - the array (or `NULL`)
3318: Level: intermediate
3320: Note:
3321: The data input variable is intended primarily for Fortran programmers
3322: who wish to allocate their own matrix memory space. Most users should
3323: need not call this routine.
3325: .seealso: [](ch_matrices), `Mat`, `MATSEQDENSE`, `MatCreate()`, `MatCreateDense()`, `MatSetValues()`, `MatDenseSetLDA()`
3326: @*/
3327: PetscErrorCode MatSeqDenseSetPreallocation(Mat B, PetscScalar data[])
3328: {
3329: PetscFunctionBegin;
3331: PetscTryMethod(B, "MatSeqDenseSetPreallocation_C", (Mat, PetscScalar[]), (B, data));
3332: PetscFunctionReturn(PETSC_SUCCESS);
3333: }
3335: PetscErrorCode MatSeqDenseSetPreallocation_SeqDense(Mat B, PetscScalar *data)
3336: {
3337: Mat_SeqDense *b = (Mat_SeqDense *)B->data;
3339: PetscFunctionBegin;
3340: PetscCheck(!b->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3341: B->preallocated = PETSC_TRUE;
3343: PetscCall(PetscLayoutSetUp(B->rmap));
3344: PetscCall(PetscLayoutSetUp(B->cmap));
3346: if (b->lda <= 0) PetscCall(PetscBLASIntCast(B->rmap->n, &b->lda));
3348: if (!data) { /* petsc-allocated storage */
3349: if (!b->user_alloc) PetscCall(PetscFree(b->v));
3350: PetscCall(PetscCalloc1((size_t)b->lda * B->cmap->n, &b->v));
3352: b->user_alloc = PETSC_FALSE;
3353: } else { /* user-allocated storage */
3354: if (!b->user_alloc) PetscCall(PetscFree(b->v));
3355: b->v = data;
3356: b->user_alloc = PETSC_TRUE;
3357: }
3358: B->assembled = PETSC_TRUE;
3359: PetscFunctionReturn(PETSC_SUCCESS);
3360: }
3362: #if PetscDefined(HAVE_ELEMENTAL)
3363: PETSC_INTERN PetscErrorCode MatConvert_SeqDense_Elemental(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
3364: {
3365: Mat mat_elemental;
3366: const PetscScalar *array;
3367: PetscScalar *v_colwise;
3368: PetscInt M = A->rmap->N, N = A->cmap->N, i, j, k, *rows, *cols;
3370: PetscFunctionBegin;
3371: PetscCall(PetscMalloc3(M * N, &v_colwise, M, &rows, N, &cols));
3372: PetscCall(MatDenseGetArrayRead(A, &array));
3373: /* convert column-wise array into row-wise v_colwise, see MatSetValues_Elemental() */
3374: k = 0;
3375: for (j = 0; j < N; j++) {
3376: cols[j] = j;
3377: for (i = 0; i < M; i++) v_colwise[j * M + i] = array[k++];
3378: }
3379: for (i = 0; i < M; i++) rows[i] = i;
3380: PetscCall(MatDenseRestoreArrayRead(A, &array));
3382: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental));
3383: PetscCall(MatSetSizes(mat_elemental, PETSC_DECIDE, PETSC_DECIDE, M, N));
3384: PetscCall(MatSetType(mat_elemental, MATELEMENTAL));
3385: PetscCall(MatSetUp(mat_elemental));
3387: /* PETSc-Elemental interaface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
3388: PetscCall(MatSetValues(mat_elemental, M, rows, N, cols, v_colwise, ADD_VALUES));
3389: PetscCall(MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY));
3390: PetscCall(MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY));
3391: PetscCall(PetscFree3(v_colwise, rows, cols));
3393: if (reuse == MAT_INPLACE_MATRIX) {
3394: PetscCall(MatHeaderReplace(A, &mat_elemental));
3395: } else {
3396: *newmat = mat_elemental;
3397: }
3398: PetscFunctionReturn(PETSC_SUCCESS);
3399: }
3400: #endif
3402: PetscErrorCode MatDenseSetLDA_SeqDense(Mat B, PetscInt lda)
3403: {
3404: Mat_SeqDense *b = (Mat_SeqDense *)B->data;
3405: PetscBool data;
3407: PetscFunctionBegin;
3408: data = (B->rmap->n > 0 && B->cmap->n > 0) ? (b->v ? PETSC_TRUE : PETSC_FALSE) : PETSC_FALSE;
3409: PetscCheck(b->user_alloc || !data || b->lda == lda, PETSC_COMM_SELF, PETSC_ERR_ORDER, "LDA cannot be changed after allocation of internal storage");
3410: PetscCheck(lda >= B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "LDA %" PetscInt_FMT " must be at least matrix dimension %" PetscInt_FMT, lda, B->rmap->n);
3411: PetscCall(PetscBLASIntCast(lda, &b->lda));
3412: PetscFunctionReturn(PETSC_SUCCESS);
3413: }
3415: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqDense(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
3416: {
3417: PetscFunctionBegin;
3418: PetscCall(MatCreateMPIMatConcatenateSeqMat_MPIDense(comm, inmat, n, scall, outmat));
3419: PetscFunctionReturn(PETSC_SUCCESS);
3420: }
3422: PetscErrorCode MatDenseCreateColumnVec_Private(Mat A, Vec *v)
3423: {
3424: PetscBool isstd, iskok, isdevice;
3425: PetscMPIInt size;
3427: PetscFunctionBegin;
3428: *v = NULL;
3429: PetscCall(PetscStrcmpAny(A->defaultvectype, &isstd, VECSTANDARD, VECSEQ, VECMPI, ""));
3430: PetscCall(PetscStrcmpAny(A->defaultvectype, &iskok, VECKOKKOS, VECSEQKOKKOS, VECMPIKOKKOS, ""));
3431: PetscCall(PetscStrcmpAny(A->defaultvectype, &isdevice, VECCUDA, VECSEQCUDA, VECMPICUDA, VECHIP, VECSEQHIP, VECMPIHIP, ""));
3432: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
3433: PetscCheck(isstd || iskok || isdevice, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Not coded for type %s", A->defaultvectype);
3434: if (iskok) {
3435: PetscCheck(PetscDefined(HAVE_KOKKOS_KERNELS), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Reconfigure using KOKKOS kernels support");
3436: #if PetscDefined(HAVE_KOKKOS_KERNELS)
3437: if (size > 1) PetscCall(VecCreateMPIKokkosWithArray(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, A->rmap->N, NULL, v));
3438: else PetscCall(VecCreateSeqKokkosWithArray(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, NULL, v));
3439: #endif
3440: } else {
3441: PetscMemType mtype = PETSC_MEMTYPE_HOST;
3442: const PetscScalar *a = NULL;
3443: PetscBool boundtocpu = PETSC_FALSE;
3445: if (isdevice) {
3446: PetscCall(MatBoundToCPU(A, &boundtocpu));
3447: if (!boundtocpu) {
3448: /* Pass A's device data to avoid an allocation when VecCUPMPlaceArray() is first called. */
3449: PetscCall(MatDenseGetArrayReadAndMemType(A, &a, &mtype));
3450: }
3451: }
3452: if (size > 1) PetscCall(VecCreateMPIWithArrayAndMemType(PetscObjectComm((PetscObject)A), mtype, A->rmap->bs, A->rmap->n, A->rmap->N, a, v));
3453: else PetscCall(VecCreateSeqWithArrayAndMemType(PetscObjectComm((PetscObject)A), mtype, A->rmap->bs, A->rmap->n, a, v));
3454: if (isdevice && !boundtocpu) PetscCall(MatDenseRestoreArrayReadAndMemType(A, &a));
3455: }
3456: PetscFunctionReturn(PETSC_SUCCESS);
3457: }
3459: PetscErrorCode MatDenseGetColumnVec_SeqDense(Mat A, PetscInt col, Vec *v)
3460: {
3461: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3463: PetscFunctionBegin;
3464: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
3465: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3466: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
3467: a->vecinuse = col + 1;
3468: PetscCall(MatDenseGetArray(A, (PetscScalar **)&a->ptrinuse));
3469: PetscCall(VecPlaceArray(a->cvec, a->ptrinuse + (size_t)col * (size_t)a->lda));
3470: *v = a->cvec;
3471: PetscFunctionReturn(PETSC_SUCCESS);
3472: }
3474: PetscErrorCode MatDenseRestoreColumnVec_SeqDense(Mat A, PetscInt col, Vec *v)
3475: {
3476: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3478: PetscFunctionBegin;
3479: PetscCheck(a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
3480: PetscCheck(a->cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector");
3481: VecCheckAssembled(a->cvec);
3482: a->vecinuse = 0;
3483: PetscCall(MatDenseRestoreArray(A, (PetscScalar **)&a->ptrinuse));
3484: PetscCall(VecResetArray(a->cvec));
3485: if (v) *v = NULL;
3486: PetscFunctionReturn(PETSC_SUCCESS);
3487: }
3489: PetscErrorCode MatDenseGetColumnVecRead_SeqDense(Mat A, PetscInt col, Vec *v)
3490: {
3491: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3493: PetscFunctionBegin;
3494: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
3495: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3496: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
3497: a->vecinuse = col + 1;
3498: PetscCall(MatDenseGetArrayRead(A, &a->ptrinuse));
3499: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)a->lda)));
3500: PetscCall(VecLockReadPush(a->cvec));
3501: *v = a->cvec;
3502: PetscFunctionReturn(PETSC_SUCCESS);
3503: }
3505: PetscErrorCode MatDenseRestoreColumnVecRead_SeqDense(Mat A, PetscInt col, Vec *v)
3506: {
3507: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3509: PetscFunctionBegin;
3510: PetscCheck(a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
3511: PetscCheck(a->cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector");
3512: VecCheckAssembled(a->cvec);
3513: a->vecinuse = 0;
3514: PetscCall(MatDenseRestoreArrayRead(A, &a->ptrinuse));
3515: PetscCall(VecLockReadPop(a->cvec));
3516: PetscCall(VecResetArray(a->cvec));
3517: if (v) *v = NULL;
3518: PetscFunctionReturn(PETSC_SUCCESS);
3519: }
3521: PetscErrorCode MatDenseGetColumnVecWrite_SeqDense(Mat A, PetscInt col, Vec *v)
3522: {
3523: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3525: PetscFunctionBegin;
3526: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
3527: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3528: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
3529: a->vecinuse = col + 1;
3530: PetscCall(MatDenseGetArrayWrite(A, (PetscScalar **)&a->ptrinuse));
3531: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)a->lda)));
3532: *v = a->cvec;
3533: PetscFunctionReturn(PETSC_SUCCESS);
3534: }
3536: PetscErrorCode MatDenseRestoreColumnVecWrite_SeqDense(Mat A, PetscInt col, Vec *v)
3537: {
3538: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3540: PetscFunctionBegin;
3541: PetscCheck(a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
3542: PetscCheck(a->cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector");
3543: VecCheckAssembled(a->cvec);
3544: a->vecinuse = 0;
3545: PetscCall(MatDenseRestoreArrayWrite(A, (PetscScalar **)&a->ptrinuse));
3546: PetscCall(VecResetArray(a->cvec));
3547: if (v) *v = NULL;
3548: PetscFunctionReturn(PETSC_SUCCESS);
3549: }
3551: PetscErrorCode MatDenseGetSubMatrix_SeqDense(Mat A, PetscInt rbegin, PetscInt rend, PetscInt cbegin, PetscInt cend, Mat *v)
3552: {
3553: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3555: PetscFunctionBegin;
3556: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
3557: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3558: if (a->cmat && (cend - cbegin != a->cmat->cmap->N || rend - rbegin != a->cmat->rmap->N)) PetscCall(MatDestroy(&a->cmat));
3559: if (!a->cmat) {
3560: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)A), rend - rbegin, PETSC_DECIDE, rend - rbegin, cend - cbegin, PetscSafePointerPlusOffset(a->v, rbegin + (size_t)cbegin * a->lda), &a->cmat));
3561: } else {
3562: PetscCall(MatDensePlaceArray(a->cmat, PetscSafePointerPlusOffset(a->v, rbegin + (size_t)cbegin * a->lda)));
3563: }
3564: PetscCall(MatDenseSetLDA(a->cmat, a->lda));
3565: a->matinuse = cbegin + 1;
3566: *v = a->cmat;
3567: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
3568: A->offloadmask = PETSC_OFFLOAD_CPU;
3569: #endif
3570: PetscFunctionReturn(PETSC_SUCCESS);
3571: }
3573: PetscErrorCode MatDenseRestoreSubMatrix_SeqDense(Mat A, Mat *v)
3574: {
3575: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3577: PetscFunctionBegin;
3578: PetscCheck(a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetSubMatrix() first");
3579: PetscCheck(a->cmat, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column matrix");
3580: PetscCheck(*v == a->cmat, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not the matrix obtained from MatDenseGetSubMatrix()");
3581: a->matinuse = 0;
3582: PetscCall(MatDenseResetArray(a->cmat));
3583: *v = NULL;
3584: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
3585: A->offloadmask = PETSC_OFFLOAD_CPU;
3586: #endif
3587: PetscFunctionReturn(PETSC_SUCCESS);
3588: }
3590: static PetscErrorCode MatDenseUpdateColumnLayout_SeqDense(Mat A, PetscLayout clayout)
3591: {
3592: PetscFunctionBegin;
3593: PetscCall(PetscLayoutReference(clayout, &A->cmap));
3594: PetscFunctionReturn(PETSC_SUCCESS);
3595: }
3597: /*MC
3598: MATSEQDENSE - MATSEQDENSE = "seqdense" - A matrix type to be used for sequential dense matrices.
3600: Options Database Key:
3601: . -mat_type seqdense - sets the matrix type to `MATSEQDENSE` during a call to `MatSetFromOptions()`
3603: Level: beginner
3605: .seealso: [](ch_matrices), `Mat`, `MATSEQDENSE`, `MatCreateSeqDense()`
3606: M*/
3607: PetscErrorCode MatCreate_SeqDense(Mat B)
3608: {
3609: Mat_SeqDense *b;
3610: PetscMPIInt size;
3612: PetscFunctionBegin;
3613: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
3614: PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Comm must be of size 1");
3616: PetscCall(PetscNew(&b));
3617: B->data = (void *)b;
3618: B->ops[0] = MatOps_Values;
3620: b->roworiented = PETSC_TRUE;
3622: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatQRFactor_C", MatQRFactor_SeqDense));
3623: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetLDA_C", MatDenseGetLDA_SeqDense));
3624: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseSetLDA_C", MatDenseSetLDA_SeqDense));
3625: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetArray_C", MatDenseGetArray_SeqDense));
3626: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreArray_C", MatDenseRestoreArray_SeqDense));
3627: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDensePlaceArray_C", MatDensePlaceArray_SeqDense));
3628: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseResetArray_C", MatDenseResetArray_SeqDense));
3629: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseReplaceArray_C", MatDenseReplaceArray_SeqDense));
3630: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetArrayRead_C", MatDenseGetArray_SeqDense));
3631: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreArrayRead_C", MatDenseRestoreArray_SeqDense));
3632: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetArrayWrite_C", MatDenseGetArray_SeqDense));
3633: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreArrayWrite_C", MatDenseRestoreArray_SeqDense));
3634: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_seqaij_C", MatConvert_SeqDense_SeqAIJ));
3635: #if PetscDefined(HAVE_ELEMENTAL)
3636: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_elemental_C", MatConvert_SeqDense_Elemental));
3637: #endif
3638: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
3639: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_scalapack_C", MatConvert_Dense_ScaLAPACK));
3640: #endif
3641: #if PetscDefined(HAVE_CUDA)
3642: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_seqdensecuda_C", MatConvert_SeqDense_SeqDenseCUDA));
3643: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdensecuda_seqdensecuda_C", MatProductSetFromOptions_SeqDense));
3644: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdensecuda_seqdense_C", MatProductSetFromOptions_SeqDense));
3645: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqdensecuda_C", MatProductSetFromOptions_SeqDense));
3646: #endif
3647: #if PetscDefined(HAVE_HIP)
3648: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_seqdensehip_C", MatConvert_SeqDense_SeqDenseHIP));
3649: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdensehip_seqdensehip_C", MatProductSetFromOptions_SeqDense));
3650: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdensehip_seqdense_C", MatProductSetFromOptions_SeqDense));
3651: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqdensehip_C", MatProductSetFromOptions_SeqDense));
3652: #endif
3653: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqDenseSetPreallocation_C", MatSeqDenseSetPreallocation_SeqDense));
3654: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqdense_C", MatProductSetFromOptions_SeqAIJ_SeqDense));
3655: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqdense_C", MatProductSetFromOptions_SeqDense));
3656: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqbaij_seqdense_C", MatProductSetFromOptions_SeqXBAIJ_SeqDense));
3657: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqsbaij_seqdense_C", MatProductSetFromOptions_SeqXBAIJ_SeqDense));
3659: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetColumn_C", MatDenseGetColumn_SeqDense));
3660: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreColumn_C", MatDenseRestoreColumn_SeqDense));
3661: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetColumnVec_C", MatDenseGetColumnVec_SeqDense));
3662: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreColumnVec_C", MatDenseRestoreColumnVec_SeqDense));
3663: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetColumnVecRead_C", MatDenseGetColumnVecRead_SeqDense));
3664: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreColumnVecRead_C", MatDenseRestoreColumnVecRead_SeqDense));
3665: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetColumnVecWrite_C", MatDenseGetColumnVecWrite_SeqDense));
3666: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreColumnVecWrite_C", MatDenseRestoreColumnVecWrite_SeqDense));
3667: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetSubMatrix_C", MatDenseGetSubMatrix_SeqDense));
3668: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreSubMatrix_C", MatDenseRestoreSubMatrix_SeqDense));
3669: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMultColumnRange_C", MatMultColumnRange_SeqDense));
3670: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMultAddColumnRange_C", MatMultAddColumnRange_SeqDense));
3671: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMultHermitianTransposeColumnRange_C", MatMultHermitianTransposeColumnRange_SeqDense));
3672: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMultHermitianTransposeAddColumnRange_C", MatMultHermitianTransposeAddColumnRange_SeqDense));
3673: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseUpdateColumnLayout_C", MatDenseUpdateColumnLayout_SeqDense));
3674: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQDENSE));
3675: PetscFunctionReturn(PETSC_SUCCESS);
3676: }
3678: /*@
3679: MatDenseGetColumn - gives access to a column of a dense matrix. This is only the local part of the column. You MUST call `MatDenseRestoreColumn()` to avoid memory bleeding.
3681: Not Collective
3683: Input Parameters:
3684: + A - a `MATSEQDENSE` or `MATMPIDENSE` matrix
3685: - col - column index
3687: Output Parameter:
3688: . vals - pointer to the data
3690: Level: intermediate
3692: Note:
3693: Use `MatDenseGetColumnVec()` to get access to a column of a `MATDENSE` treated as a `Vec`
3695: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreColumn()`, `MatDenseGetColumnVec()`
3696: @*/
3697: PetscErrorCode MatDenseGetColumn(Mat A, PetscInt col, PetscScalar *vals[])
3698: {
3699: PetscFunctionBegin;
3702: PetscAssertPointer(vals, 3);
3703: PetscUseMethod(A, "MatDenseGetColumn_C", (Mat, PetscInt, PetscScalar **), (A, col, vals));
3704: PetscFunctionReturn(PETSC_SUCCESS);
3705: }
3707: /*@
3708: MatDenseRestoreColumn - returns access to a column of a `MATDENSE` matrix which is returned by `MatDenseGetColumn()`.
3710: Not Collective
3712: Input Parameters:
3713: + A - a `MATSEQDENSE` or `MATMPIDENSE` matrix
3714: - vals - pointer to the data (may be `NULL`)
3716: Level: intermediate
3718: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetColumn()`
3719: @*/
3720: PetscErrorCode MatDenseRestoreColumn(Mat A, PetscScalar *vals[])
3721: {
3722: PetscFunctionBegin;
3724: PetscAssertPointer(vals, 2);
3725: PetscUseMethod(A, "MatDenseRestoreColumn_C", (Mat, PetscScalar **), (A, vals));
3726: PetscFunctionReturn(PETSC_SUCCESS);
3727: }
3729: /*@
3730: MatDenseGetColumnVec - Gives read-write access to a column of a `MATDENSE` matrix, represented as a `Vec`.
3732: Collective
3734: Input Parameters:
3735: + A - the `Mat` object
3736: - col - the column index
3738: Output Parameter:
3739: . v - the vector
3741: Level: intermediate
3743: Notes:
3744: The vector is owned by PETSc. Users need to call `MatDenseRestoreColumnVec()` when the vector is no longer needed.
3746: Use `MatDenseGetColumnVecRead()` to obtain read-only access or `MatDenseGetColumnVecWrite()` for write-only access.
3748: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVecRead()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecRead()`, `MatDenseRestoreColumnVecWrite()`, `MatDenseGetColumn()`
3749: @*/
3750: PetscErrorCode MatDenseGetColumnVec(Mat A, PetscInt col, Vec *v)
3751: {
3752: PetscFunctionBegin;
3756: PetscAssertPointer(v, 3);
3757: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3758: PetscCheck(col >= 0 && col < A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid col %" PetscInt_FMT ", should be in [0,%" PetscInt_FMT ")", col, A->cmap->N);
3759: PetscUseMethod(A, "MatDenseGetColumnVec_C", (Mat, PetscInt, Vec *), (A, col, v));
3760: PetscFunctionReturn(PETSC_SUCCESS);
3761: }
3763: /*@
3764: MatDenseRestoreColumnVec - Returns access to a column of a dense matrix obtained from `MatDenseGetColumnVec()`.
3766: Collective
3768: Input Parameters:
3769: + A - the `Mat` object
3770: . col - the column index
3771: - v - the `Vec` object (may be `NULL`)
3773: Level: intermediate
3775: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecRead()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVecRead()`, `MatDenseRestoreColumnVecWrite()`
3776: @*/
3777: PetscErrorCode MatDenseRestoreColumnVec(Mat A, PetscInt col, Vec *v)
3778: {
3779: PetscFunctionBegin;
3784: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3785: PetscCheck(col >= 0 && col < A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid col %" PetscInt_FMT ", should be in [0,%" PetscInt_FMT ")", col, A->cmap->N);
3786: PetscUseMethod(A, "MatDenseRestoreColumnVec_C", (Mat, PetscInt, Vec *), (A, col, v));
3787: PetscFunctionReturn(PETSC_SUCCESS);
3788: }
3790: /*@
3791: MatDenseGetColumnVecRead - Gives read-only access to a column of a dense matrix, represented as a `Vec`.
3793: Collective
3795: Input Parameters:
3796: + A - the `Mat` object
3797: - col - the column index
3799: Output Parameter:
3800: . v - the vector
3802: Level: intermediate
3804: Notes:
3805: The vector is owned by PETSc and users cannot modify it.
3807: Users need to call `MatDenseRestoreColumnVecRead()` when the vector is no longer needed.
3809: Use `MatDenseGetColumnVec()` to obtain read-write access or `MatDenseGetColumnVecWrite()` for write-only access.
3811: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecRead()`, `MatDenseRestoreColumnVecWrite()`
3812: @*/
3813: PetscErrorCode MatDenseGetColumnVecRead(Mat A, PetscInt col, Vec *v)
3814: {
3815: PetscFunctionBegin;
3819: PetscAssertPointer(v, 3);
3820: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3821: PetscCheck(col >= 0 && col < A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid col %" PetscInt_FMT ", should be in [0,%" PetscInt_FMT ")", col, A->cmap->N);
3822: PetscUseMethod(A, "MatDenseGetColumnVecRead_C", (Mat, PetscInt, Vec *), (A, col, v));
3823: PetscFunctionReturn(PETSC_SUCCESS);
3824: }
3826: /*@
3827: MatDenseRestoreColumnVecRead - Returns access to a column of a dense matrix obtained from `MatDenseGetColumnVecRead()`.
3829: Collective
3831: Input Parameters:
3832: + A - the `Mat` object
3833: . col - the column index
3834: - v - the `Vec` object (may be `NULL`)
3836: Level: intermediate
3838: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecRead()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecWrite()`
3839: @*/
3840: PetscErrorCode MatDenseRestoreColumnVecRead(Mat A, PetscInt col, Vec *v)
3841: {
3842: PetscFunctionBegin;
3847: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3848: PetscCheck(col >= 0 && col < A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid col %" PetscInt_FMT ", should be in [0,%" PetscInt_FMT ")", col, A->cmap->N);
3849: PetscUseMethod(A, "MatDenseRestoreColumnVecRead_C", (Mat, PetscInt, Vec *), (A, col, v));
3850: PetscFunctionReturn(PETSC_SUCCESS);
3851: }
3853: /*@
3854: MatDenseGetColumnVecWrite - Gives write-only access to a column of a dense matrix, represented as a `Vec`.
3856: Collective
3858: Input Parameters:
3859: + A - the `Mat` object
3860: - col - the column index
3862: Output Parameter:
3863: . v - the vector
3865: Level: intermediate
3867: Notes:
3868: The vector is owned by PETSc. Users need to call `MatDenseRestoreColumnVecWrite()` when the vector is no longer needed.
3870: Use `MatDenseGetColumnVec()` to obtain read-write access or `MatDenseGetColumnVecRead()` for read-only access.
3872: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecRead()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecRead()`, `MatDenseRestoreColumnVecWrite()`
3873: @*/
3874: PetscErrorCode MatDenseGetColumnVecWrite(Mat A, PetscInt col, Vec *v)
3875: {
3876: PetscFunctionBegin;
3880: PetscAssertPointer(v, 3);
3881: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3882: PetscCheck(col >= 0 && col < A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid col %" PetscInt_FMT ", should be in [0,%" PetscInt_FMT ")", col, A->cmap->N);
3883: PetscUseMethod(A, "MatDenseGetColumnVecWrite_C", (Mat, PetscInt, Vec *), (A, col, v));
3884: PetscFunctionReturn(PETSC_SUCCESS);
3885: }
3887: /*@
3888: MatDenseRestoreColumnVecWrite - Returns access to a column of a dense matrix obtained from `MatDenseGetColumnVecWrite()`.
3890: Collective
3892: Input Parameters:
3893: + A - the `Mat` object
3894: . col - the column index
3895: - v - the `Vec` object (may be `NULL`)
3897: Level: intermediate
3899: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecRead()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecRead()`
3900: @*/
3901: PetscErrorCode MatDenseRestoreColumnVecWrite(Mat A, PetscInt col, Vec *v)
3902: {
3903: PetscFunctionBegin;
3908: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3909: PetscCheck(col >= 0 && col < A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid col %" PetscInt_FMT ", should be in [0,%" PetscInt_FMT ")", col, A->cmap->N);
3910: PetscUseMethod(A, "MatDenseRestoreColumnVecWrite_C", (Mat, PetscInt, Vec *), (A, col, v));
3911: PetscFunctionReturn(PETSC_SUCCESS);
3912: }
3914: /*@
3915: MatDenseGetSubMatrix - Gives access to a block of rows and columns of a dense matrix, represented as a `Mat`.
3917: Collective
3919: Input Parameters:
3920: + A - the `Mat` object
3921: . rbegin - the first global row index in the block (if `PETSC_DECIDE`, is 0)
3922: . rend - the global row index past the last one in the block (if `PETSC_DECIDE`, is `M`)
3923: . cbegin - the first global column index in the block (if `PETSC_DECIDE`, is 0)
3924: - cend - the global column index past the last one in the block (if `PETSC_DECIDE`, is `N`)
3926: Output Parameter:
3927: . v - the matrix
3929: Level: intermediate
3931: Notes:
3932: The matrix is owned by PETSc. Users need to call `MatDenseRestoreSubMatrix()` when the matrix is no longer needed.
3934: The output matrix is not redistributed by PETSc, so depending on the values of `rbegin` and `rend`, some processes may have no local rows.
3936: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreSubMatrix()`
3937: @*/
3938: PetscErrorCode MatDenseGetSubMatrix(Mat A, PetscInt rbegin, PetscInt rend, PetscInt cbegin, PetscInt cend, Mat *v)
3939: {
3940: PetscFunctionBegin;
3947: PetscAssertPointer(v, 6);
3948: if (rbegin == PETSC_DECIDE) rbegin = 0;
3949: if (rend == PETSC_DECIDE) rend = A->rmap->N;
3950: if (cbegin == PETSC_DECIDE) cbegin = 0;
3951: if (cend == PETSC_DECIDE) cend = A->cmap->N;
3952: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3953: PetscCheck(rbegin >= 0 && rbegin <= A->rmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid rbegin %" PetscInt_FMT ", should be in [0,%" PetscInt_FMT "]", rbegin, A->rmap->N);
3954: PetscCheck(rend >= rbegin && rend <= A->rmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid rend %" PetscInt_FMT ", should be in [%" PetscInt_FMT ",%" PetscInt_FMT "]", rend, rbegin, A->rmap->N);
3955: PetscCheck(cbegin >= 0 && cbegin <= A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid cbegin %" PetscInt_FMT ", should be in [0,%" PetscInt_FMT "]", cbegin, A->cmap->N);
3956: PetscCheck(cend >= cbegin && cend <= A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Invalid cend %" PetscInt_FMT ", should be in [%" PetscInt_FMT ",%" PetscInt_FMT "]", cend, cbegin, A->cmap->N);
3957: PetscUseMethod(A, "MatDenseGetSubMatrix_C", (Mat, PetscInt, PetscInt, PetscInt, PetscInt, Mat *), (A, rbegin, rend, cbegin, cend, v));
3958: PetscFunctionReturn(PETSC_SUCCESS);
3959: }
3961: /*@
3962: MatDenseRestoreSubMatrix - Returns access to a block of columns of a dense matrix obtained from `MatDenseGetSubMatrix()`.
3964: Collective
3966: Input Parameters:
3967: + A - the `Mat` object
3968: - v - the `Mat` object (cannot be `NULL`)
3970: Level: intermediate
3972: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseRestoreColumnVec()`, `MatDenseGetSubMatrix()`
3973: @*/
3974: PetscErrorCode MatDenseRestoreSubMatrix(Mat A, Mat *v)
3975: {
3976: PetscFunctionBegin;
3979: PetscAssertPointer(v, 2);
3981: PetscUseMethod(A, "MatDenseRestoreSubMatrix_C", (Mat, Mat *), (A, v));
3982: PetscFunctionReturn(PETSC_SUCCESS);
3983: }
3985: /*@
3986: MatDenseUpdateColumnLayout - Update the column layout of the dense matrix.
3988: Collective
3990: Input Parameters:
3991: + A - the `Mat` object
3992: - clayout - the `PetscLayout` object (cannot be `NULL`)
3994: Level: advanced
3996: Notes:
3997: Because a dense matrix's storage is independent of its column layout, this routine can update the layout without modifying the underlying storage.
3999: It can be useful when users want to apply the operator, with `MatMult()` on a right vector with a different layout.
4001: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `PetscLayout`, `MatMult()`, `MatMultAdd()`
4002: @*/
4003: PetscErrorCode MatDenseUpdateColumnLayout(Mat A, PetscLayout clayout)
4004: {
4005: PetscMPIInt flag;
4006: MPI_Comm lcomm;
4008: PetscFunctionBegin;
4011: PetscCall(PetscLayoutGetComm(clayout, &lcomm));
4012: PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)A), lcomm, &flag));
4013: PetscCheck(flag == MPI_CONGRUENT || flag == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "Different communicators in the two objects: flag %d", flag);
4014: PetscCheck(A->cmap->N == clayout->N, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_SIZ, "Mat global dim %" PetscInt_FMT " does not match layout global dim %" PetscInt_FMT, A->cmap->N, clayout->N);
4015: PetscUseMethod(A, "MatDenseUpdateColumnLayout_C", (Mat, PetscLayout), (A, clayout));
4016: PetscFunctionReturn(PETSC_SUCCESS);
4017: }
4019: #include <petscblaslapack.h>
4020: #include <petsc/private/kernels/blockinvert.h>
4022: /*@
4023: MatSeqDenseInvert - Invert a small `MATSEQDENSE` matrix in place using a hard-coded kernel.
4025: Not Collective
4027: Input Parameter:
4028: . A - the `MATSEQDENSE` matrix
4030: Level: developer
4032: Note:
4033: Intended for small blocks; specialized kernels are used for sizes up to 7 and `LAPACK` is used for larger sizes.
4034: If the matrix is singular and `MatSetErrorIfFailure()` was called an error will be immediately generated, otherwise the factor error type
4035: in the matrix, which can be obtained with `MatFactorGetError()`, is set to `MAT_FACTOR_NUMERIC_ZEROPIVOT`.
4037: .seealso: `Mat`, `MATSEQDENSE`, `MatInvertBlockDiagonal()`, `MatLUFactor()`, `MatSetErrorIfFailure()`, `MatFactorGetError()`
4038: @*/
4039: PetscErrorCode MatSeqDenseInvert(Mat A)
4040: {
4041: PetscInt m;
4042: const PetscReal shift = 0.0;
4043: PetscBool allowzeropivot, zeropivotdetected = PETSC_FALSE;
4044: PetscScalar *values;
4046: PetscFunctionBegin;
4048: PetscCall(MatDenseGetArray(A, &values));
4049: PetscCall(MatGetLocalSize(A, &m, NULL));
4050: allowzeropivot = PetscNot(A->erroriffailure);
4051: /* factor and invert each block */
4052: switch (m) {
4053: case 1:
4054: values[0] = (PetscScalar)1.0 / (values[0] + shift);
4055: break;
4056: case 2:
4057: PetscCall(PetscKernel_A_gets_inverse_A_2(values, shift, allowzeropivot, &zeropivotdetected));
4058: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4059: break;
4060: case 3:
4061: PetscCall(PetscKernel_A_gets_inverse_A_3(values, shift, allowzeropivot, &zeropivotdetected));
4062: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4063: break;
4064: case 4:
4065: PetscCall(PetscKernel_A_gets_inverse_A_4(values, shift, allowzeropivot, &zeropivotdetected));
4066: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4067: break;
4068: case 5: {
4069: PetscScalar work[25];
4070: PetscInt ipvt[5];
4072: PetscCall(PetscKernel_A_gets_inverse_A_5(values, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
4073: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4074: } break;
4075: case 6:
4076: PetscCall(PetscKernel_A_gets_inverse_A_6(values, shift, allowzeropivot, &zeropivotdetected));
4077: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4078: break;
4079: case 7:
4080: PetscCall(PetscKernel_A_gets_inverse_A_7(values, shift, allowzeropivot, &zeropivotdetected));
4081: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4082: break;
4083: default: {
4084: PetscInt *v_pivots, *IJ, j;
4085: PetscScalar *v_work;
4087: PetscCall(PetscMalloc3(m, &v_work, m, &v_pivots, m, &IJ));
4088: for (j = 0; j < m; j++) IJ[j] = j;
4089: PetscCall(PetscKernel_A_gets_inverse_A(m, values, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
4090: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4091: PetscCall(PetscFree3(v_work, v_pivots, IJ));
4092: }
4093: }
4094: PetscCall(MatDenseRestoreArray(A, &values));
4095: PetscFunctionReturn(PETSC_SUCCESS);
4096: }
4098: /*@
4099: MatDenseReplaceArrayWithMemType - Allows one to replace the array in a `MATDENSE`, `MATDENSECUDA`, or `MATDENSEHIP`
4100: with an array provided by the user and a matching `PetscMemType`. This is useful to avoid copying an array into a matrix.
4102: Not Collective
4104: Input Parameters:
4105: + mat - the matrix
4106: . mtype - the `PetscMemType` of the array
4107: - array - the array in column major order
4109: Level: developer
4111: Note:
4112: Adding `const` to `array` was an oversight, see notes in `VecPlaceArray()`.
4114: This permanently replaces the GPU array and frees the memory associated with the old GPU
4115: array. The memory passed in CANNOT be freed by the user. It will be freed when the matrix is
4116: destroyed. The array should respect the matrix leading dimension.
4118: .seealso: `MatDenseReplaceArray()`, `MatDenseCUDAReplaceArray()`, `MatDenseHIPReplaceArray()`
4119: @*/
4120: PetscErrorCode MatDenseReplaceArrayWithMemType(Mat mat, PetscMemType mtype, const PetscScalar array[])
4121: {
4122: const char *type = PetscMemTypeToString(mtype) + 14; /* skip "PETSC_MEMTYPE_" */
4123: char buffer[256];
4125: PetscFunctionBegin;
4127: PetscAssertPointer(array, 3);
4128: PetscCall(PetscSNPrintf(buffer, sizeof(buffer), "MatDense%sReplaceArray_C", PetscMemTypeHost(mtype) ? "" : type));
4129: PetscUseMethod(mat, buffer, (Mat, const PetscScalar[]), (mat, array));
4130: PetscFunctionReturn(PETSC_SUCCESS);
4131: }