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: PetscFunctionReturn(PETSC_SUCCESS);
1755: }
1757: static PetscErrorCode MatTranspose_SeqDense(Mat A, MatReuse reuse, Mat *matout)
1758: {
1759: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1760: PetscInt k, j, m = A->rmap->n, M = mat->lda, n = A->cmap->n;
1761: PetscScalar *v, tmp;
1763: PetscFunctionBegin;
1764: if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *matout));
1765: if (reuse == MAT_INPLACE_MATRIX) {
1766: if (m == n) { /* in place transpose */
1767: PetscCall(MatDenseGetArray(A, &v));
1768: for (j = 0; j < m; j++) {
1769: for (k = 0; k < j; k++) {
1770: tmp = v[j + k * M];
1771: v[j + k * M] = v[k + j * M];
1772: v[k + j * M] = tmp;
1773: }
1774: }
1775: PetscCall(MatDenseRestoreArray(A, &v));
1776: } else { /* reuse memory, temporary allocates new memory */
1777: PetscScalar *v2;
1778: PetscLayout tmplayout;
1780: PetscCall(PetscMalloc1((size_t)m * n, &v2));
1781: PetscCall(MatDenseGetArray(A, &v));
1782: for (j = 0; j < n; j++) {
1783: for (k = 0; k < m; k++) v2[j + (size_t)k * n] = v[k + (size_t)j * M];
1784: }
1785: PetscCall(PetscArraycpy(v, v2, (size_t)m * n));
1786: PetscCall(PetscFree(v2));
1787: PetscCall(MatDenseRestoreArray(A, &v));
1788: /* cleanup size dependent quantities */
1789: PetscCall(VecDestroy(&mat->cvec));
1790: PetscCall(MatDestroy(&mat->cmat));
1791: PetscCall(PetscFree(mat->pivots));
1792: PetscCall(PetscFree(mat->fwork));
1793: /* swap row/col layouts */
1794: PetscCall(PetscBLASIntCast(n, &mat->lda));
1795: tmplayout = A->rmap;
1796: A->rmap = A->cmap;
1797: A->cmap = tmplayout;
1798: }
1799: } else { /* out-of-place transpose */
1800: Mat tmat;
1801: Mat_SeqDense *tmatd;
1802: PetscScalar *v2;
1803: PetscInt M2;
1805: if (reuse == MAT_INITIAL_MATRIX) {
1806: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &tmat));
1807: PetscCall(MatSetSizes(tmat, A->cmap->n, A->rmap->n, A->cmap->n, A->rmap->n));
1808: PetscCall(MatSetType(tmat, ((PetscObject)A)->type_name));
1809: PetscCall(MatSeqDenseSetPreallocation(tmat, NULL));
1810: } else tmat = *matout;
1812: PetscCall(MatDenseGetArrayRead(A, (const PetscScalar **)&v));
1813: PetscCall(MatDenseGetArray(tmat, &v2));
1814: tmatd = (Mat_SeqDense *)tmat->data;
1815: M2 = tmatd->lda;
1816: for (j = 0; j < n; j++) {
1817: for (k = 0; k < m; k++) v2[j + k * M2] = v[k + j * M];
1818: }
1819: PetscCall(MatDenseRestoreArray(tmat, &v2));
1820: PetscCall(MatDenseRestoreArrayRead(A, (const PetscScalar **)&v));
1821: PetscCall(MatAssemblyBegin(tmat, MAT_FINAL_ASSEMBLY));
1822: PetscCall(MatAssemblyEnd(tmat, MAT_FINAL_ASSEMBLY));
1823: *matout = tmat;
1824: }
1825: PetscFunctionReturn(PETSC_SUCCESS);
1826: }
1828: static PetscErrorCode MatEqual_SeqDense(Mat A1, Mat A2, PetscBool *flg)
1829: {
1830: Mat_SeqDense *mat1 = (Mat_SeqDense *)A1->data;
1831: Mat_SeqDense *mat2 = (Mat_SeqDense *)A2->data;
1832: PetscInt i;
1833: const PetscScalar *v1, *v2;
1835: PetscFunctionBegin;
1836: if (A1->rmap->n != A2->rmap->n) {
1837: *flg = PETSC_FALSE;
1838: PetscFunctionReturn(PETSC_SUCCESS);
1839: }
1840: if (A1->cmap->n != A2->cmap->n) {
1841: *flg = PETSC_FALSE;
1842: PetscFunctionReturn(PETSC_SUCCESS);
1843: }
1844: PetscCall(MatDenseGetArrayRead(A1, &v1));
1845: PetscCall(MatDenseGetArrayRead(A2, &v2));
1846: for (i = 0; i < A1->cmap->n; i++) {
1847: PetscCall(PetscArraycmp(v1, v2, A1->rmap->n, flg));
1848: if (*flg == PETSC_FALSE) PetscFunctionReturn(PETSC_SUCCESS);
1849: v1 += mat1->lda;
1850: v2 += mat2->lda;
1851: }
1852: PetscCall(MatDenseRestoreArrayRead(A1, &v1));
1853: PetscCall(MatDenseRestoreArrayRead(A2, &v2));
1854: *flg = PETSC_TRUE;
1855: PetscFunctionReturn(PETSC_SUCCESS);
1856: }
1858: PetscErrorCode MatGetDiagonal_SeqDense(Mat A, Vec v)
1859: {
1860: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1861: PetscInt i, n, len;
1862: PetscScalar *x;
1863: const PetscScalar *vv;
1865: PetscFunctionBegin;
1866: PetscCall(VecGetSize(v, &n));
1867: PetscCall(VecGetArray(v, &x));
1868: len = PetscMin(A->rmap->n, A->cmap->n);
1869: PetscCall(MatDenseGetArrayRead(A, &vv));
1870: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming mat and vec");
1871: for (i = 0; i < len; i++) x[i] = vv[i * mat->lda + i];
1872: PetscCall(MatDenseRestoreArrayRead(A, &vv));
1873: PetscCall(VecRestoreArray(v, &x));
1874: PetscFunctionReturn(PETSC_SUCCESS);
1875: }
1877: PetscErrorCode MatDiagonalScale_SeqDense(Mat A, Vec ll, Vec rr)
1878: {
1879: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1880: const PetscScalar *l, *r;
1881: PetscScalar x, *v, *vv;
1882: PetscInt i, j, m = A->rmap->n, n = A->cmap->n;
1884: PetscFunctionBegin;
1885: PetscCall(MatDenseGetArray(A, &vv));
1886: if (ll) {
1887: PetscCall(VecGetSize(ll, &m));
1888: PetscCall(VecGetArrayRead(ll, &l));
1889: PetscCheck(m == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left scaling vec wrong size");
1890: for (i = 0; i < m; i++) {
1891: x = l[i];
1892: v = vv + i;
1893: for (j = 0; j < n; j++) {
1894: (*v) *= x;
1895: v += mat->lda;
1896: }
1897: }
1898: PetscCall(VecRestoreArrayRead(ll, &l));
1899: PetscCall(PetscLogFlops(1.0 * n * m));
1900: }
1901: if (rr) {
1902: PetscCall(VecGetSize(rr, &n));
1903: PetscCall(VecGetArrayRead(rr, &r));
1904: PetscCheck(n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Right scaling vec wrong size");
1905: for (i = 0; i < n; i++) {
1906: x = r[i];
1907: v = vv + i * mat->lda;
1908: for (j = 0; j < m; j++) (*v++) *= x;
1909: }
1910: PetscCall(VecRestoreArrayRead(rr, &r));
1911: PetscCall(PetscLogFlops(1.0 * n * m));
1912: }
1913: PetscCall(MatDenseRestoreArray(A, &vv));
1914: PetscFunctionReturn(PETSC_SUCCESS);
1915: }
1917: PetscErrorCode MatNorm_SeqDense(Mat A, NormType type, PetscReal *nrm)
1918: {
1919: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
1920: PetscScalar *v, *vv, *work, *av = NULL;
1921: PetscReal sum = 0.0;
1922: PetscInt lda, m = A->rmap->n, i, j;
1924: PetscFunctionBegin;
1925: PetscCall(MatDenseGetArrayRead(A, (const PetscScalar **)&vv));
1926: PetscCall(MatDenseGetLDA(A, &lda));
1927: v = vv;
1928: if (type == NORM_FROBENIUS) {
1929: if (lda > m) {
1930: for (j = 0; j < A->cmap->n; j++) {
1931: v = vv + j * lda;
1932: for (i = 0; i < m; i++) {
1933: sum += PetscRealPart(PetscConj(*v) * (*v));
1934: v++;
1935: }
1936: }
1937: } else {
1938: #if PetscDefined(USE_REAL___FP16)
1939: PetscBLASInt one = 1, cnt = A->cmap->n * A->rmap->n;
1940: PetscCallBLAS("BLASnrm2", *nrm = BLASnrm2_(&cnt, v, &one));
1941: }
1942: #else
1943: for (i = 0; i < A->cmap->n * A->rmap->n; i++) {
1944: sum += PetscRealPart(PetscConj(*v) * (*v));
1945: v++;
1946: }
1947: }
1948: *nrm = PetscSqrtReal(sum);
1949: #endif
1950: PetscCall(PetscLogFlops(2.0 * A->cmap->n * A->rmap->n));
1951: } else if (type == NORM_1) {
1952: *nrm = 0.0;
1953: for (j = 0; j < A->cmap->n; j++) {
1954: v = vv + j * mat->lda;
1955: sum = 0.0;
1956: for (i = 0; i < A->rmap->n; i++) {
1957: sum += PetscAbsScalar(*v);
1958: v++;
1959: }
1960: if (sum > *nrm) *nrm = sum;
1961: }
1962: PetscCall(PetscLogFlops(1.0 * A->cmap->n * A->rmap->n));
1963: } else if (type == NORM_INFINITY) {
1964: *nrm = 0.0;
1965: for (j = 0; j < A->rmap->n; j++) {
1966: v = vv + j;
1967: sum = 0.0;
1968: for (i = 0; i < A->cmap->n; i++) {
1969: sum += PetscAbsScalar(*v);
1970: v += mat->lda;
1971: }
1972: if (sum > *nrm) *nrm = sum;
1973: }
1974: PetscCall(PetscLogFlops(1.0 * A->cmap->n * A->rmap->n));
1975: } else if (type == NORM_2) {
1976: PetscReal *s;
1977: PetscBLASInt bm, bn, blda, min, lwork;
1979: PetscCall(PetscBLASIntCast(A->rmap->n, &bm));
1980: PetscCall(PetscBLASIntCast(A->cmap->n, &bn));
1981: PetscCall(PetscBLASIntCast(PetscMax(A->rmap->n, 1), &blda));
1982: min = PetscMin(bm, bn);
1983: if (!min) {
1984: *nrm = 0.0;
1985: PetscCall(MatDenseRestoreArrayRead(A, (const PetscScalar **)&vv));
1986: PetscFunctionReturn(PETSC_SUCCESS);
1987: }
1988: PetscCall(PetscMalloc2(A->rmap->n * A->cmap->n, &av, min, &s));
1989: for (j = 0; j < A->cmap->n; j++) PetscCall(PetscArraycpy(av + j * A->rmap->n, vv + j * lda, A->rmap->n));
1991: lwork = -1;
1992: {
1993: PetscScalar workquery;
1994: #if PetscDefined(USE_COMPLEX)
1995: PetscReal *rwork;
1997: PetscCall(PetscMalloc1(5 * min, &rwork));
1998: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
1999: PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &bm, &bn, av, &blda, s, NULL, &bm, NULL, &min, &workquery, &lwork, rwork, &info));
2000: lwork = (PetscBLASInt)PetscRealPart(workquery);
2001: PetscCall(PetscMalloc1(lwork, &work));
2002: PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &bm, &bn, av, &blda, s, NULL, &bm, NULL, &min, work, &lwork, rwork, &info));
2003: PetscCall(PetscFPTrapPop());
2004: PetscCall(PetscFree(rwork));
2005: #else
2006: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2007: PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &bm, &bn, av, &blda, s, NULL, &bm, NULL, &min, &workquery, &lwork, &info));
2008: lwork = (PetscBLASInt)PetscRealPart(workquery);
2009: PetscCall(PetscMalloc1(lwork, &work));
2010: PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &bm, &bn, av, &blda, s, NULL, &bm, NULL, &min, work, &lwork, &info));
2011: PetscCall(PetscFPTrapPop());
2012: #endif
2013: }
2014: *nrm = s[0];
2015: PetscCall(PetscFree(work));
2016: PetscCall(PetscFree2(av, s));
2017: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unsupported norm type %s", NormTypes[type]);
2018: PetscCall(MatDenseRestoreArrayRead(A, (const PetscScalar **)&vv));
2019: PetscFunctionReturn(PETSC_SUCCESS);
2020: }
2022: static PetscErrorCode MatSetOption_SeqDense(Mat A, MatOption op, PetscBool flg)
2023: {
2024: Mat_SeqDense *aij = (Mat_SeqDense *)A->data;
2026: PetscFunctionBegin;
2027: switch (op) {
2028: case MAT_ROW_ORIENTED:
2029: aij->roworiented = flg;
2030: break;
2031: default:
2032: break;
2033: }
2034: PetscFunctionReturn(PETSC_SUCCESS);
2035: }
2037: PetscErrorCode MatZeroEntries_SeqDense(Mat A)
2038: {
2039: Mat_SeqDense *l = (Mat_SeqDense *)A->data;
2040: PetscInt lda = l->lda, m = A->rmap->n, n = A->cmap->n, j;
2041: PetscScalar *v;
2043: PetscFunctionBegin;
2044: PetscCall(MatDenseGetArrayWrite(A, &v));
2045: if (lda > m) {
2046: for (j = 0; j < n; j++) PetscCall(PetscArrayzero(v + j * lda, m));
2047: } else {
2048: PetscCall(PetscArrayzero(v, PetscInt64Mult(m, n)));
2049: }
2050: PetscCall(MatDenseRestoreArrayWrite(A, &v));
2051: PetscFunctionReturn(PETSC_SUCCESS);
2052: }
2054: static PetscErrorCode MatZeroRows_SeqDense(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
2055: {
2056: Mat_SeqDense *l = (Mat_SeqDense *)A->data;
2057: PetscInt m = l->lda, n = A->cmap->n, i, j;
2058: PetscScalar *slot, *bb, *v;
2059: const PetscScalar *xx;
2061: PetscFunctionBegin;
2062: if (PetscDefined(USE_DEBUG)) {
2063: for (i = 0; i < N; i++) {
2064: PetscCheck(rows[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative row requested to be zeroed");
2065: 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);
2066: }
2067: }
2068: if (!N) PetscFunctionReturn(PETSC_SUCCESS);
2070: /* fix right-hand side if needed */
2071: if (x && b) {
2072: PetscCall(VecGetArrayRead(x, &xx));
2073: PetscCall(VecGetArray(b, &bb));
2074: for (i = 0; i < N; i++) bb[rows[i]] = diag * xx[rows[i]];
2075: PetscCall(VecRestoreArrayRead(x, &xx));
2076: PetscCall(VecRestoreArray(b, &bb));
2077: }
2079: PetscCall(MatDenseGetArray(A, &v));
2080: for (i = 0; i < N; i++) {
2081: slot = v + rows[i];
2082: for (j = 0; j < n; j++) {
2083: *slot = 0.0;
2084: slot += m;
2085: }
2086: }
2087: if (diag != 0.0) {
2088: PetscCheck(A->rmap->n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only coded for square matrices");
2089: for (i = 0; i < N; i++) {
2090: slot = v + (m + 1) * rows[i];
2091: *slot = diag;
2092: }
2093: }
2094: PetscCall(MatDenseRestoreArray(A, &v));
2095: PetscFunctionReturn(PETSC_SUCCESS);
2096: }
2098: static PetscErrorCode MatDenseGetLDA_SeqDense(Mat A, PetscInt *lda)
2099: {
2100: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2102: PetscFunctionBegin;
2103: *lda = mat->lda;
2104: PetscFunctionReturn(PETSC_SUCCESS);
2105: }
2107: PetscErrorCode MatDenseGetArray_SeqDense(Mat A, PetscScalar **array)
2108: {
2109: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2111: PetscFunctionBegin;
2112: PetscCheck(!mat->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
2113: *array = mat->v;
2114: PetscFunctionReturn(PETSC_SUCCESS);
2115: }
2117: PetscErrorCode MatDenseRestoreArray_SeqDense(Mat A, PetscScalar **array)
2118: {
2119: PetscFunctionBegin;
2120: if (array) *array = NULL;
2121: PetscFunctionReturn(PETSC_SUCCESS);
2122: }
2124: /*@
2125: MatDenseGetLDA - gets the leading dimension of the array returned from `MatDenseGetArray()`
2127: Not Collective
2129: Input Parameter:
2130: . A - a `MATDENSE` or `MATDENSECUDA` matrix
2132: Output Parameter:
2133: . lda - the leading dimension
2135: Level: intermediate
2137: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseSetLDA()`
2138: @*/
2139: PetscErrorCode MatDenseGetLDA(Mat A, PetscInt *lda)
2140: {
2141: PetscFunctionBegin;
2143: PetscAssertPointer(lda, 2);
2144: MatCheckPreallocated(A, 1);
2145: PetscUseMethod(A, "MatDenseGetLDA_C", (Mat, PetscInt *), (A, lda));
2146: PetscFunctionReturn(PETSC_SUCCESS);
2147: }
2149: /*@
2150: MatDenseSetLDA - Sets the leading dimension of the array used by the `MATDENSE` matrix
2152: Collective if the matrix layouts have not yet been setup
2154: Input Parameters:
2155: + A - a `MATDENSE` or `MATDENSECUDA` matrix
2156: - lda - the leading dimension
2158: Level: intermediate
2160: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetLDA()`
2161: @*/
2162: PetscErrorCode MatDenseSetLDA(Mat A, PetscInt lda)
2163: {
2164: PetscFunctionBegin;
2166: PetscTryMethod(A, "MatDenseSetLDA_C", (Mat, PetscInt), (A, lda));
2167: PetscFunctionReturn(PETSC_SUCCESS);
2168: }
2170: /*@C
2171: MatDenseGetArray - gives read-write access to the array where the data for a `MATDENSE` matrix is stored
2173: Logically Collective
2175: Input Parameter:
2176: . A - a dense matrix
2178: Output Parameter:
2179: . array - pointer to the data
2181: Level: intermediate
2183: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2184: @*/
2185: PetscErrorCode MatDenseGetArray(Mat A, PetscScalar *array[]) PeNS
2186: {
2187: PetscFunctionBegin;
2189: PetscAssertPointer(array, 2);
2190: PetscUseMethod(A, "MatDenseGetArray_C", (Mat, PetscScalar **), (A, array));
2191: PetscFunctionReturn(PETSC_SUCCESS);
2192: }
2194: /*@C
2195: MatDenseRestoreArray - returns access to the array where the data for a `MATDENSE` matrix is stored obtained by `MatDenseGetArray()`
2197: Logically Collective
2199: Input Parameters:
2200: + A - a dense matrix
2201: - array - pointer to the data (may be `NULL`)
2203: Level: intermediate
2205: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2206: @*/
2207: PetscErrorCode MatDenseRestoreArray(Mat A, PetscScalar *array[]) PeNS
2208: {
2209: PetscFunctionBegin;
2211: if (array) PetscAssertPointer(array, 2);
2212: PetscUseMethod(A, "MatDenseRestoreArray_C", (Mat, PetscScalar **), (A, array));
2213: PetscCall(PetscObjectStateIncrease((PetscObject)A));
2214: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
2215: A->offloadmask = PETSC_OFFLOAD_CPU;
2216: #endif
2217: PetscFunctionReturn(PETSC_SUCCESS);
2218: }
2220: /*@C
2221: MatDenseGetArrayRead - gives read-only access to the array where the data for a `MATDENSE` matrix is stored
2223: Not Collective
2225: Input Parameter:
2226: . A - a dense matrix
2228: Output Parameter:
2229: . array - pointer to the data
2231: Level: intermediate
2233: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayRead()`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2234: @*/
2235: PetscErrorCode MatDenseGetArrayRead(Mat A, const PetscScalar *array[]) PeNS
2236: {
2237: PetscFunctionBegin;
2239: PetscAssertPointer(array, 2);
2240: PetscUseMethod(A, "MatDenseGetArrayRead_C", (Mat, PetscScalar **), (A, (PetscScalar **)array));
2241: PetscFunctionReturn(PETSC_SUCCESS);
2242: }
2244: /*@C
2245: MatDenseRestoreArrayRead - returns access to the array where the data for a `MATDENSE` matrix is stored obtained by `MatDenseGetArrayRead()`
2247: Not Collective
2249: Input Parameters:
2250: + A - a dense matrix
2251: - array - pointer to the data (may be `NULL`)
2253: Level: intermediate
2255: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayRead()`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2256: @*/
2257: PetscErrorCode MatDenseRestoreArrayRead(Mat A, const PetscScalar *array[]) PeNS
2258: {
2259: PetscFunctionBegin;
2261: if (array) PetscAssertPointer(array, 2);
2262: PetscUseMethod(A, "MatDenseRestoreArrayRead_C", (Mat, PetscScalar **), (A, (PetscScalar **)array));
2263: PetscFunctionReturn(PETSC_SUCCESS);
2264: }
2266: /*@C
2267: MatDenseGetArrayWrite - gives write-only access to the array where the data for a `MATDENSE` matrix is stored
2269: Not Collective
2271: Input Parameter:
2272: . A - a dense matrix
2274: Output Parameter:
2275: . array - pointer to the data
2277: Level: intermediate
2279: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayWrite()`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`
2280: @*/
2281: PetscErrorCode MatDenseGetArrayWrite(Mat A, PetscScalar *array[]) PeNS
2282: {
2283: PetscFunctionBegin;
2285: PetscAssertPointer(array, 2);
2286: PetscUseMethod(A, "MatDenseGetArrayWrite_C", (Mat, PetscScalar **), (A, array));
2287: PetscFunctionReturn(PETSC_SUCCESS);
2288: }
2290: /*@C
2291: MatDenseRestoreArrayWrite - returns access to the array where the data for a `MATDENSE` matrix is stored obtained by `MatDenseGetArrayWrite()`
2293: Not Collective
2295: Input Parameters:
2296: + A - a dense matrix
2297: - array - pointer to the data (may be `NULL`)
2299: Level: intermediate
2301: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayWrite()`, `MatDenseGetArray()`, `MatDenseRestoreArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`
2302: @*/
2303: PetscErrorCode MatDenseRestoreArrayWrite(Mat A, PetscScalar *array[]) PeNS
2304: {
2305: PetscFunctionBegin;
2307: if (array) PetscAssertPointer(array, 2);
2308: PetscUseMethod(A, "MatDenseRestoreArrayWrite_C", (Mat, PetscScalar **), (A, array));
2309: PetscCall(PetscObjectStateIncrease((PetscObject)A));
2310: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
2311: A->offloadmask = PETSC_OFFLOAD_CPU;
2312: #endif
2313: PetscFunctionReturn(PETSC_SUCCESS);
2314: }
2316: /*@C
2317: MatDenseGetArrayAndMemType - gives read-write access to the array where the data for a `MATDENSE` matrix is stored
2319: Logically Collective
2321: Input Parameter:
2322: . A - a dense matrix
2324: Output Parameters:
2325: + array - pointer to the data
2326: - mtype - memory type of the returned pointer
2328: Level: intermediate
2330: Note:
2331: If the matrix is of a device type such as `MATDENSECUDA`, `MATDENSEHIP`, etc.,
2332: an array on device is always returned and is guaranteed to contain the matrix's latest data.
2334: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayAndMemType()`, `MatDenseGetArrayReadAndMemType()`, `MatDenseGetArrayWriteAndMemType()`, `MatDenseGetArrayRead()`,
2335: `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`, `MatSeqAIJGetCSRAndMemType()`
2336: @*/
2337: PetscErrorCode MatDenseGetArrayAndMemType(Mat A, PetscScalar *array[], PetscMemType *mtype)
2338: {
2339: PetscBool isMPI;
2341: PetscFunctionBegin;
2343: PetscAssertPointer(array, 2);
2344: 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 */
2345: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2346: if (isMPI) {
2347: /* Dispatch here so that the code can be reused for all subclasses of MATDENSE */
2348: PetscCall(MatDenseGetArrayAndMemType(((Mat_MPIDense *)A->data)->A, array, mtype));
2349: } else {
2350: PetscErrorCode (*fptr)(Mat, PetscScalar **, PetscMemType *);
2352: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseGetArrayAndMemType_C", &fptr));
2353: if (fptr) {
2354: PetscCall((*fptr)(A, array, mtype));
2355: } else {
2356: PetscUseMethod(A, "MatDenseGetArray_C", (Mat, PetscScalar **), (A, array));
2357: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2358: }
2359: }
2360: PetscFunctionReturn(PETSC_SUCCESS);
2361: }
2363: /*@C
2364: MatDenseRestoreArrayAndMemType - returns access to the array that is obtained by `MatDenseGetArrayAndMemType()`
2366: Logically Collective
2368: Input Parameters:
2369: + A - a dense matrix
2370: - array - pointer to the data
2372: Level: intermediate
2374: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayAndMemType()`, `MatDenseGetArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2375: @*/
2376: PetscErrorCode MatDenseRestoreArrayAndMemType(Mat A, PetscScalar *array[])
2377: {
2378: PetscBool isMPI;
2380: PetscFunctionBegin;
2382: if (array) PetscAssertPointer(array, 2);
2383: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2384: if (isMPI) {
2385: PetscCall(MatDenseRestoreArrayAndMemType(((Mat_MPIDense *)A->data)->A, array));
2386: } else {
2387: PetscErrorCode (*fptr)(Mat, PetscScalar **);
2389: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseRestoreArrayAndMemType_C", &fptr));
2390: if (fptr) {
2391: PetscCall((*fptr)(A, array));
2392: } else {
2393: PetscUseMethod(A, "MatDenseRestoreArray_C", (Mat, PetscScalar **), (A, array));
2394: }
2395: if (array) *array = NULL;
2396: }
2397: PetscCall(PetscObjectStateIncrease((PetscObject)A));
2398: PetscFunctionReturn(PETSC_SUCCESS);
2399: }
2401: /*@C
2402: MatDenseGetArrayReadAndMemType - gives read-only access to the array where the data for a `MATDENSE` matrix is stored
2404: Logically Collective
2406: Input Parameter:
2407: . A - a dense matrix
2409: Output Parameters:
2410: + array - pointer to the data
2411: - mtype - memory type of the returned pointer
2413: Level: intermediate
2415: Note:
2416: If the matrix is of a device type such as `MATDENSECUDA`, `MATDENSEHIP`, etc.,
2417: an array on device is always returned and is guaranteed to contain the matrix's latest data.
2419: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayReadAndMemType()`, `MatDenseGetArrayWriteAndMemType()`,
2420: `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`, `MatSeqAIJGetCSRAndMemType()`
2421: @*/
2422: PetscErrorCode MatDenseGetArrayReadAndMemType(Mat A, const PetscScalar *array[], PetscMemType *mtype)
2423: {
2424: PetscBool isMPI;
2426: PetscFunctionBegin;
2428: PetscAssertPointer(array, 2);
2429: 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 */
2430: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2431: if (isMPI) { /* Dispatch here so that the code can be reused for all subclasses of MATDENSE */
2432: PetscCall(MatDenseGetArrayReadAndMemType(((Mat_MPIDense *)A->data)->A, array, mtype));
2433: } else {
2434: PetscErrorCode (*fptr)(Mat, const PetscScalar **, PetscMemType *);
2436: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseGetArrayReadAndMemType_C", &fptr));
2437: if (fptr) {
2438: PetscCall((*fptr)(A, array, mtype));
2439: } else {
2440: PetscUseMethod(A, "MatDenseGetArrayRead_C", (Mat, PetscScalar **), (A, (PetscScalar **)array));
2441: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2442: }
2443: }
2444: PetscFunctionReturn(PETSC_SUCCESS);
2445: }
2447: /*@C
2448: MatDenseRestoreArrayReadAndMemType - returns access to the array that is obtained by `MatDenseGetArrayReadAndMemType()`
2450: Logically Collective
2452: Input Parameters:
2453: + A - a dense matrix
2454: - array - pointer to the data
2456: Level: intermediate
2458: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayReadAndMemType()`, `MatDenseGetArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2459: @*/
2460: PetscErrorCode MatDenseRestoreArrayReadAndMemType(Mat A, const PetscScalar *array[])
2461: {
2462: PetscBool isMPI;
2464: PetscFunctionBegin;
2466: if (array) PetscAssertPointer(array, 2);
2467: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2468: if (isMPI) {
2469: PetscCall(MatDenseRestoreArrayReadAndMemType(((Mat_MPIDense *)A->data)->A, array));
2470: } else {
2471: PetscErrorCode (*fptr)(Mat, const PetscScalar **);
2473: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseRestoreArrayReadAndMemType_C", &fptr));
2474: if (fptr) {
2475: PetscCall((*fptr)(A, array));
2476: } else {
2477: PetscUseMethod(A, "MatDenseRestoreArrayRead_C", (Mat, PetscScalar **), (A, (PetscScalar **)array));
2478: }
2479: if (array) *array = NULL;
2480: }
2481: PetscFunctionReturn(PETSC_SUCCESS);
2482: }
2484: /*@C
2485: MatDenseGetArrayWriteAndMemType - gives write-only access to the array where the data for a `MATDENSE` matrix is stored
2487: Logically Collective
2489: Input Parameter:
2490: . A - a dense matrix
2492: Output Parameters:
2493: + array - pointer to the data
2494: - mtype - memory type of the returned pointer
2496: Level: intermediate
2498: Note:
2499: If the matrix is of a device type such as `MATDENSECUDA`, `MATDENSEHIP`, etc.,
2500: an array on device is always returned and is guaranteed to contain the matrix's latest data.
2502: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreArrayWriteAndMemType()`, `MatDenseGetArrayReadAndMemType()`, `MatDenseGetArrayRead()`,
2503: `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`, `MatSeqAIJGetCSRAndMemType()`
2504: @*/
2505: PetscErrorCode MatDenseGetArrayWriteAndMemType(Mat A, PetscScalar *array[], PetscMemType *mtype)
2506: {
2507: PetscBool isMPI;
2509: PetscFunctionBegin;
2511: PetscAssertPointer(array, 2);
2512: 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 */
2513: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2514: if (isMPI) {
2515: PetscCall(MatDenseGetArrayWriteAndMemType(((Mat_MPIDense *)A->data)->A, array, mtype));
2516: } else {
2517: PetscErrorCode (*fptr)(Mat, PetscScalar **, PetscMemType *);
2519: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseGetArrayWriteAndMemType_C", &fptr));
2520: if (fptr) {
2521: PetscCall((*fptr)(A, array, mtype));
2522: } else {
2523: PetscUseMethod(A, "MatDenseGetArrayWrite_C", (Mat, PetscScalar **), (A, array));
2524: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
2525: }
2526: }
2527: PetscFunctionReturn(PETSC_SUCCESS);
2528: }
2530: /*@C
2531: MatDenseRestoreArrayWriteAndMemType - returns access to the array that is obtained by `MatDenseGetArrayReadAndMemType()`
2533: Logically Collective
2535: Input Parameters:
2536: + A - a dense matrix
2537: - array - pointer to the data
2539: Level: intermediate
2541: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetArrayWriteAndMemType()`, `MatDenseGetArray()`, `MatDenseGetArrayRead()`, `MatDenseRestoreArrayRead()`, `MatDenseGetArrayWrite()`, `MatDenseRestoreArrayWrite()`
2542: @*/
2543: PetscErrorCode MatDenseRestoreArrayWriteAndMemType(Mat A, PetscScalar *array[])
2544: {
2545: PetscBool isMPI;
2547: PetscFunctionBegin;
2549: if (array) PetscAssertPointer(array, 2);
2550: PetscCall(PetscObjectBaseTypeCompare((PetscObject)A, MATMPIDENSE, &isMPI));
2551: if (isMPI) {
2552: PetscCall(MatDenseRestoreArrayWriteAndMemType(((Mat_MPIDense *)A->data)->A, array));
2553: } else {
2554: PetscErrorCode (*fptr)(Mat, PetscScalar **);
2556: PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatDenseRestoreArrayWriteAndMemType_C", &fptr));
2557: if (fptr) {
2558: PetscCall((*fptr)(A, array));
2559: } else {
2560: PetscUseMethod(A, "MatDenseRestoreArrayWrite_C", (Mat, PetscScalar **), (A, array));
2561: }
2562: if (array) *array = NULL;
2563: }
2564: PetscCall(PetscObjectStateIncrease((PetscObject)A));
2565: PetscFunctionReturn(PETSC_SUCCESS);
2566: }
2568: static PetscErrorCode MatCreateSubMatrix_SeqDense(Mat A, IS isrow, IS iscol, MatReuse scall, Mat *B)
2569: {
2570: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2571: PetscInt i, j, nrows, ncols, ldb;
2572: const PetscInt *irow, *icol;
2573: PetscScalar *av, *bv, *v = mat->v;
2574: Mat newmat;
2576: PetscFunctionBegin;
2577: PetscCall(ISGetIndices(isrow, &irow));
2578: PetscCall(ISGetIndices(iscol, &icol));
2579: PetscCall(ISGetLocalSize(isrow, &nrows));
2580: PetscCall(ISGetLocalSize(iscol, &ncols));
2582: /* Check submatrixcall */
2583: if (scall == MAT_REUSE_MATRIX) {
2584: PetscInt n_cols, n_rows;
2585: PetscCall(MatGetSize(*B, &n_rows, &n_cols));
2586: if (n_rows != nrows || n_cols != ncols) {
2587: /* resize the result matrix to match number of requested rows/columns */
2588: PetscCall(MatSetSizes(*B, nrows, ncols, nrows, ncols));
2589: }
2590: newmat = *B;
2591: } else {
2592: /* Create and fill new matrix */
2593: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &newmat));
2594: PetscCall(MatSetSizes(newmat, nrows, ncols, nrows, ncols));
2595: PetscCall(MatSetType(newmat, ((PetscObject)A)->type_name));
2596: PetscCall(MatSeqDenseSetPreallocation(newmat, NULL));
2597: }
2599: /* Now extract the data pointers and do the copy,column at a time */
2600: PetscCall(MatDenseGetArray(newmat, &bv));
2601: PetscCall(MatDenseGetLDA(newmat, &ldb));
2602: for (i = 0; i < ncols; i++) {
2603: av = v + mat->lda * icol[i];
2604: for (j = 0; j < nrows; j++) bv[j] = av[irow[j]];
2605: bv += ldb;
2606: }
2607: PetscCall(MatDenseRestoreArray(newmat, &bv));
2609: /* Assemble the matrices so that the correct flags are set */
2610: PetscCall(MatAssemblyBegin(newmat, MAT_FINAL_ASSEMBLY));
2611: PetscCall(MatAssemblyEnd(newmat, MAT_FINAL_ASSEMBLY));
2613: /* Free work space */
2614: PetscCall(ISRestoreIndices(isrow, &irow));
2615: PetscCall(ISRestoreIndices(iscol, &icol));
2616: *B = newmat;
2617: PetscFunctionReturn(PETSC_SUCCESS);
2618: }
2620: static PetscErrorCode MatCreateSubMatrices_SeqDense(Mat A, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *B[])
2621: {
2622: PetscInt i;
2624: PetscFunctionBegin;
2625: if (scall == MAT_INITIAL_MATRIX) PetscCall(PetscCalloc1(n, B));
2627: for (i = 0; i < n; i++) PetscCall(MatCreateSubMatrix_SeqDense(A, irow[i], icol[i], scall, &(*B)[i]));
2628: PetscFunctionReturn(PETSC_SUCCESS);
2629: }
2631: PetscErrorCode MatCopy_SeqDense(Mat A, Mat B, MatStructure str)
2632: {
2633: Mat_SeqDense *a = (Mat_SeqDense *)A->data, *b = (Mat_SeqDense *)B->data;
2634: const PetscScalar *va;
2635: PetscScalar *vb;
2636: PetscInt lda1 = a->lda, lda2 = b->lda, m = A->rmap->n, n = A->cmap->n, j;
2638: PetscFunctionBegin;
2639: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
2640: if (A->ops->copy != B->ops->copy) {
2641: PetscCall(MatCopy_Basic(A, B, str));
2642: PetscFunctionReturn(PETSC_SUCCESS);
2643: }
2644: PetscCheck(m == B->rmap->n && n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "size(B) != size(A)");
2645: PetscCall(MatDenseGetArrayRead(A, &va));
2646: PetscCall(MatDenseGetArray(B, &vb));
2647: if (lda1 > m || lda2 > m) {
2648: for (j = 0; j < n; j++) PetscCall(PetscArraycpy(vb + j * lda2, va + j * lda1, m));
2649: } else {
2650: PetscCall(PetscArraycpy(vb, va, A->rmap->n * A->cmap->n));
2651: }
2652: PetscCall(MatDenseRestoreArray(B, &vb));
2653: PetscCall(MatDenseRestoreArrayRead(A, &va));
2654: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
2655: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
2656: PetscFunctionReturn(PETSC_SUCCESS);
2657: }
2659: PetscErrorCode MatSetUp_SeqDense(Mat A)
2660: {
2661: PetscFunctionBegin;
2662: PetscCall(PetscLayoutSetUp(A->rmap));
2663: PetscCall(PetscLayoutSetUp(A->cmap));
2664: if (!A->preallocated) PetscCall(MatSeqDenseSetPreallocation(A, NULL));
2665: PetscFunctionReturn(PETSC_SUCCESS);
2666: }
2668: PetscErrorCode MatConjugate_SeqDense(Mat A)
2669: {
2670: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2671: PetscInt i, j;
2672: PetscInt min = PetscMin(A->rmap->n, A->cmap->n);
2673: PetscScalar *aa;
2675: PetscFunctionBegin;
2676: PetscCall(MatDenseGetArray(A, &aa));
2677: for (j = 0; j < A->cmap->n; j++)
2678: for (i = 0; i < A->rmap->n; i++) aa[i + j * mat->lda] = PetscConj(aa[i + j * mat->lda]);
2679: PetscCall(MatDenseRestoreArray(A, &aa));
2680: if (mat->tau)
2681: for (i = 0; i < min; i++) mat->tau[i] = PetscConj(mat->tau[i]);
2682: PetscFunctionReturn(PETSC_SUCCESS);
2683: }
2685: static PetscErrorCode MatRealPart_SeqDense(Mat A)
2686: {
2687: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2688: PetscInt i, j;
2689: PetscScalar *aa;
2691: PetscFunctionBegin;
2692: PetscCall(MatDenseGetArray(A, &aa));
2693: for (j = 0; j < A->cmap->n; j++) {
2694: for (i = 0; i < A->rmap->n; i++) aa[i + j * mat->lda] = PetscRealPart(aa[i + j * mat->lda]);
2695: }
2696: PetscCall(MatDenseRestoreArray(A, &aa));
2697: PetscFunctionReturn(PETSC_SUCCESS);
2698: }
2700: static PetscErrorCode MatImaginaryPart_SeqDense(Mat A)
2701: {
2702: Mat_SeqDense *mat = (Mat_SeqDense *)A->data;
2703: PetscInt i, j;
2704: PetscScalar *aa;
2706: PetscFunctionBegin;
2707: PetscCall(MatDenseGetArray(A, &aa));
2708: for (j = 0; j < A->cmap->n; j++) {
2709: for (i = 0; i < A->rmap->n; i++) aa[i + j * mat->lda] = PetscImaginaryPart(aa[i + j * mat->lda]);
2710: }
2711: PetscCall(MatDenseRestoreArray(A, &aa));
2712: PetscFunctionReturn(PETSC_SUCCESS);
2713: }
2715: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqDense(Mat A, Mat B, PetscReal fill, Mat C)
2716: {
2717: PetscInt m = A->rmap->n, n = B->cmap->n;
2718: PetscBool cisdense = PETSC_FALSE;
2720: PetscFunctionBegin;
2721: PetscCall(MatSetSizes(C, m, n, m, n));
2722: #if PetscDefined(HAVE_CUDA)
2723: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, ""));
2724: #endif
2725: #if PetscDefined(HAVE_HIP)
2726: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSEHIP, ""));
2727: #endif
2728: if (!cisdense) {
2729: PetscBool flg;
2731: PetscCall(PetscObjectTypeCompare((PetscObject)B, ((PetscObject)A)->type_name, &flg));
2732: PetscCall(MatSetType(C, flg ? ((PetscObject)A)->type_name : MATDENSE));
2733: }
2734: PetscCall(MatSetUp(C));
2735: PetscFunctionReturn(PETSC_SUCCESS);
2736: }
2738: PetscErrorCode MatMatMultNumeric_SeqDense_SeqDense(Mat A, Mat B, Mat C)
2739: {
2740: Mat_SeqDense *a = (Mat_SeqDense *)A->data, *b = (Mat_SeqDense *)B->data, *c = (Mat_SeqDense *)C->data;
2741: const PetscScalar *av, *bv;
2742: PetscScalar *cv;
2743: PetscBLASInt m, n, k;
2744: PetscScalar _DOne = 1.0, _DZero = 0.0;
2746: PetscFunctionBegin;
2747: PetscCall(PetscBLASIntCast(C->rmap->n, &m));
2748: PetscCall(PetscBLASIntCast(C->cmap->n, &n));
2749: PetscCall(PetscBLASIntCast(A->cmap->n, &k));
2750: if (!m || !n || !k) {
2751: PetscCall(MatZeroEntries(C));
2752: PetscFunctionReturn(PETSC_SUCCESS);
2753: }
2754: PetscCall(MatDenseGetArrayRead(A, &av));
2755: PetscCall(MatDenseGetArrayRead(B, &bv));
2756: PetscCall(MatDenseGetArrayWrite(C, &cv));
2757: PetscCallBLAS("BLASgemm", BLASgemm_("N", "N", &m, &n, &k, &_DOne, av, &a->lda, bv, &b->lda, &_DZero, cv, &c->lda));
2758: PetscCall(MatDenseRestoreArrayRead(A, &av));
2759: PetscCall(MatDenseRestoreArrayRead(B, &bv));
2760: PetscCall(MatDenseRestoreArrayWrite(C, &cv));
2761: PetscCall(PetscLogFlops(1.0 * m * n * k + 1.0 * m * n * (k - 1)));
2762: PetscFunctionReturn(PETSC_SUCCESS);
2763: }
2765: PetscErrorCode MatMatTransposeMultSymbolic_SeqDense_SeqDense(Mat A, Mat B, PetscReal fill, Mat C)
2766: {
2767: PetscInt m = A->rmap->n, n = B->rmap->n;
2768: PetscBool cisdense = PETSC_FALSE;
2770: PetscFunctionBegin;
2771: PetscCall(MatSetSizes(C, m, n, m, n));
2772: #if PetscDefined(HAVE_CUDA)
2773: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, ""));
2774: #endif
2775: #if PetscDefined(HAVE_HIP)
2776: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSEHIP, ""));
2777: #endif
2778: if (!cisdense) {
2779: PetscBool flg;
2781: PetscCall(PetscObjectTypeCompare((PetscObject)B, ((PetscObject)A)->type_name, &flg));
2782: PetscCall(MatSetType(C, flg ? ((PetscObject)A)->type_name : MATDENSE));
2783: }
2784: PetscCall(MatSetUp(C));
2785: PetscFunctionReturn(PETSC_SUCCESS);
2786: }
2788: PetscErrorCode MatMatTransposeMultNumeric_SeqDense_SeqDense(Mat A, Mat B, Mat C)
2789: {
2790: Mat_SeqDense *a = (Mat_SeqDense *)A->data, *b = (Mat_SeqDense *)B->data, *c = (Mat_SeqDense *)C->data;
2791: const PetscScalar *av, *bv;
2792: PetscScalar *cv;
2793: PetscBLASInt m, n, k;
2794: PetscScalar _DOne = 1.0, _DZero = 0.0;
2796: PetscFunctionBegin;
2797: PetscCall(PetscBLASIntCast(C->rmap->n, &m));
2798: PetscCall(PetscBLASIntCast(C->cmap->n, &n));
2799: PetscCall(PetscBLASIntCast(A->cmap->n, &k));
2800: if (!m || !n || !k) {
2801: PetscCall(MatZeroEntries(C));
2802: PetscFunctionReturn(PETSC_SUCCESS);
2803: }
2804: PetscCall(MatDenseGetArrayRead(A, &av));
2805: PetscCall(MatDenseGetArrayRead(B, &bv));
2806: PetscCall(MatDenseGetArrayWrite(C, &cv));
2807: PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &m, &n, &k, &_DOne, av, &a->lda, bv, &b->lda, &_DZero, cv, &c->lda));
2808: PetscCall(MatDenseRestoreArrayRead(A, &av));
2809: PetscCall(MatDenseRestoreArrayRead(B, &bv));
2810: PetscCall(MatDenseRestoreArrayWrite(C, &cv));
2811: PetscCall(PetscLogFlops(1.0 * m * n * k + 1.0 * m * n * (k - 1)));
2812: PetscFunctionReturn(PETSC_SUCCESS);
2813: }
2815: PetscErrorCode MatTransposeMatMultSymbolic_SeqDense_SeqDense(Mat A, Mat B, PetscReal fill, Mat C)
2816: {
2817: PetscInt m = A->cmap->n, n = B->cmap->n;
2818: PetscBool cisdense = PETSC_FALSE;
2820: PetscFunctionBegin;
2821: PetscCall(MatSetSizes(C, m, n, m, n));
2822: #if PetscDefined(HAVE_CUDA)
2823: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, ""));
2824: #endif
2825: #if PetscDefined(HAVE_HIP)
2826: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSEHIP, ""));
2827: #endif
2828: if (!cisdense) {
2829: PetscBool flg;
2831: PetscCall(PetscObjectTypeCompare((PetscObject)B, ((PetscObject)A)->type_name, &flg));
2832: PetscCall(MatSetType(C, flg ? ((PetscObject)A)->type_name : MATDENSE));
2833: }
2834: PetscCall(MatSetUp(C));
2835: PetscFunctionReturn(PETSC_SUCCESS);
2836: }
2838: PetscErrorCode MatTransposeMatMultNumeric_SeqDense_SeqDense(Mat A, Mat B, Mat C)
2839: {
2840: Mat_SeqDense *a = (Mat_SeqDense *)A->data, *b = (Mat_SeqDense *)B->data, *c = (Mat_SeqDense *)C->data;
2841: const PetscScalar *av, *bv;
2842: PetscScalar *cv;
2843: PetscBLASInt m, n, k;
2844: PetscScalar _DOne = 1.0, _DZero = 0.0;
2846: PetscFunctionBegin;
2847: PetscCall(PetscBLASIntCast(C->rmap->n, &m));
2848: PetscCall(PetscBLASIntCast(C->cmap->n, &n));
2849: PetscCall(PetscBLASIntCast(A->rmap->n, &k));
2850: if (!m || !n || !k) {
2851: PetscCall(MatZeroEntries(C));
2852: PetscFunctionReturn(PETSC_SUCCESS);
2853: }
2854: PetscCall(MatDenseGetArrayRead(A, &av));
2855: PetscCall(MatDenseGetArrayRead(B, &bv));
2856: PetscCall(MatDenseGetArrayWrite(C, &cv));
2857: PetscCallBLAS("BLASgemm", BLASgemm_("T", "N", &m, &n, &k, &_DOne, av, &a->lda, bv, &b->lda, &_DZero, cv, &c->lda));
2858: PetscCall(MatDenseRestoreArrayRead(A, &av));
2859: PetscCall(MatDenseRestoreArrayRead(B, &bv));
2860: PetscCall(MatDenseRestoreArrayWrite(C, &cv));
2861: PetscCall(PetscLogFlops(1.0 * m * n * k + 1.0 * m * n * (k - 1)));
2862: PetscFunctionReturn(PETSC_SUCCESS);
2863: }
2865: static PetscErrorCode MatProductSetFromOptions_SeqDense_AB(Mat C)
2866: {
2867: PetscFunctionBegin;
2868: C->ops->matmultsymbolic = MatMatMultSymbolic_SeqDense_SeqDense;
2869: C->ops->productsymbolic = MatProductSymbolic_AB;
2870: PetscFunctionReturn(PETSC_SUCCESS);
2871: }
2873: static PetscErrorCode MatProductSetFromOptions_SeqDense_AtB(Mat C)
2874: {
2875: PetscFunctionBegin;
2876: C->ops->transposematmultsymbolic = MatTransposeMatMultSymbolic_SeqDense_SeqDense;
2877: C->ops->productsymbolic = MatProductSymbolic_AtB;
2878: PetscFunctionReturn(PETSC_SUCCESS);
2879: }
2881: static PetscErrorCode MatProductSetFromOptions_SeqDense_ABt(Mat C)
2882: {
2883: PetscFunctionBegin;
2884: C->ops->mattransposemultsymbolic = MatMatTransposeMultSymbolic_SeqDense_SeqDense;
2885: C->ops->productsymbolic = MatProductSymbolic_ABt;
2886: PetscFunctionReturn(PETSC_SUCCESS);
2887: }
2889: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_SeqDense(Mat C)
2890: {
2891: Mat_Product *product = C->product;
2893: PetscFunctionBegin;
2894: switch (product->type) {
2895: case MATPRODUCT_AB:
2896: PetscCall(MatProductSetFromOptions_SeqDense_AB(C));
2897: break;
2898: case MATPRODUCT_AtB:
2899: PetscCall(MatProductSetFromOptions_SeqDense_AtB(C));
2900: break;
2901: case MATPRODUCT_ABt:
2902: PetscCall(MatProductSetFromOptions_SeqDense_ABt(C));
2903: break;
2904: default:
2905: break;
2906: }
2907: PetscFunctionReturn(PETSC_SUCCESS);
2908: }
2910: static PetscErrorCode MatGetRowMax_SeqDense(Mat A, Vec v, PetscInt idx[])
2911: {
2912: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
2913: PetscInt i, j, m = A->rmap->n, n = A->cmap->n, p;
2914: PetscScalar *x;
2915: const PetscScalar *aa;
2917: PetscFunctionBegin;
2918: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
2919: PetscCall(VecGetArray(v, &x));
2920: PetscCall(VecGetLocalSize(v, &p));
2921: PetscCall(MatDenseGetArrayRead(A, &aa));
2922: PetscCheck(p == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
2923: for (i = 0; i < m; i++) {
2924: x[i] = aa[i];
2925: if (idx) idx[i] = 0;
2926: for (j = 1; j < n; j++) {
2927: if (PetscRealPart(x[i]) < PetscRealPart(aa[i + a->lda * j])) {
2928: x[i] = aa[i + a->lda * j];
2929: if (idx) idx[i] = j;
2930: }
2931: }
2932: }
2933: PetscCall(MatDenseRestoreArrayRead(A, &aa));
2934: PetscCall(VecRestoreArray(v, &x));
2935: PetscFunctionReturn(PETSC_SUCCESS);
2936: }
2938: static PetscErrorCode MatGetRowMaxAbs_SeqDense(Mat A, Vec v, PetscInt idx[])
2939: {
2940: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
2941: PetscInt i, j, m = A->rmap->n, n = A->cmap->n, p;
2942: PetscScalar *x;
2943: PetscReal atmp;
2944: const PetscScalar *aa;
2946: PetscFunctionBegin;
2947: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
2948: PetscCall(VecGetArray(v, &x));
2949: PetscCall(VecGetLocalSize(v, &p));
2950: PetscCall(MatDenseGetArrayRead(A, &aa));
2951: PetscCheck(p == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
2952: for (i = 0; i < m; i++) {
2953: x[i] = PetscAbsScalar(aa[i]);
2954: for (j = 1; j < n; j++) {
2955: atmp = PetscAbsScalar(aa[i + a->lda * j]);
2956: if (PetscAbsScalar(x[i]) < atmp) {
2957: x[i] = atmp;
2958: if (idx) idx[i] = j;
2959: }
2960: }
2961: }
2962: PetscCall(MatDenseRestoreArrayRead(A, &aa));
2963: PetscCall(VecRestoreArray(v, &x));
2964: PetscFunctionReturn(PETSC_SUCCESS);
2965: }
2967: static PetscErrorCode MatGetRowMin_SeqDense(Mat A, Vec v, PetscInt idx[])
2968: {
2969: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
2970: PetscInt i, j, m = A->rmap->n, n = A->cmap->n, p;
2971: PetscScalar *x;
2972: const PetscScalar *aa;
2974: PetscFunctionBegin;
2975: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
2976: PetscCall(MatDenseGetArrayRead(A, &aa));
2977: PetscCall(VecGetArray(v, &x));
2978: PetscCall(VecGetLocalSize(v, &p));
2979: PetscCheck(p == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
2980: for (i = 0; i < m; i++) {
2981: x[i] = aa[i];
2982: if (idx) idx[i] = 0;
2983: for (j = 1; j < n; j++) {
2984: if (PetscRealPart(x[i]) > PetscRealPart(aa[i + a->lda * j])) {
2985: x[i] = aa[i + a->lda * j];
2986: if (idx) idx[i] = j;
2987: }
2988: }
2989: }
2990: PetscCall(VecRestoreArray(v, &x));
2991: PetscCall(MatDenseRestoreArrayRead(A, &aa));
2992: PetscFunctionReturn(PETSC_SUCCESS);
2993: }
2995: PetscErrorCode MatGetColumnVector_SeqDense(Mat A, Vec v, PetscInt col)
2996: {
2997: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
2998: PetscScalar *x;
2999: const PetscScalar *aa;
3001: PetscFunctionBegin;
3002: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3003: PetscCall(MatDenseGetArrayRead(A, &aa));
3004: PetscCall(VecGetArray(v, &x));
3005: PetscCall(PetscArraycpy(x, aa + col * a->lda, A->rmap->n));
3006: PetscCall(VecRestoreArray(v, &x));
3007: PetscCall(MatDenseRestoreArrayRead(A, &aa));
3008: PetscFunctionReturn(PETSC_SUCCESS);
3009: }
3011: PETSC_INTERN PetscErrorCode MatGetColumnReductions_SeqDense(Mat A, PetscInt type, PetscReal *reductions)
3012: {
3013: PetscInt i, j, m, n;
3014: const PetscScalar *a;
3016: PetscFunctionBegin;
3017: PetscCall(MatGetSize(A, &m, &n));
3018: PetscCall(PetscArrayzero(reductions, n));
3019: PetscCall(MatDenseGetArrayRead(A, &a));
3020: if (type == NORM_2) {
3021: for (i = 0; i < n; i++) {
3022: for (j = 0; j < m; j++) reductions[i] += PetscAbsScalar(a[j] * a[j]);
3023: a = PetscSafePointerPlusOffset(a, m);
3024: }
3025: } else if (type == NORM_1) {
3026: for (i = 0; i < n; i++) {
3027: for (j = 0; j < m; j++) reductions[i] += PetscAbsScalar(a[j]);
3028: a = PetscSafePointerPlusOffset(a, m);
3029: }
3030: } else if (type == NORM_INFINITY) {
3031: for (i = 0; i < n; i++) {
3032: for (j = 0; j < m; j++) reductions[i] = PetscMax(PetscAbsScalar(a[j]), reductions[i]);
3033: a = PetscSafePointerPlusOffset(a, m);
3034: }
3035: } else if (type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART) {
3036: for (i = 0; i < n; i++) {
3037: for (j = 0; j < m; j++) reductions[i] += PetscRealPart(a[j]);
3038: a = PetscSafePointerPlusOffset(a, m);
3039: }
3040: } else if (type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART) {
3041: for (i = 0; i < n; i++) {
3042: for (j = 0; j < m; j++) reductions[i] += PetscImaginaryPart(a[j]);
3043: a = PetscSafePointerPlusOffset(a, m);
3044: }
3045: } else SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Unknown reduction type");
3046: PetscCall(MatDenseRestoreArrayRead(A, &a));
3047: if (type == NORM_2) {
3048: for (i = 0; i < n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
3049: } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
3050: for (i = 0; i < n; i++) reductions[i] /= m;
3051: }
3052: PetscFunctionReturn(PETSC_SUCCESS);
3053: }
3055: PetscErrorCode MatSetRandom_SeqDense(Mat x, PetscRandom rctx)
3056: {
3057: PetscScalar *a;
3058: PetscInt lda, m, n, i, j;
3060: PetscFunctionBegin;
3061: PetscCall(MatGetSize(x, &m, &n));
3062: PetscCall(MatDenseGetLDA(x, &lda));
3063: PetscCall(MatDenseGetArrayWrite(x, &a));
3064: for (j = 0; j < n; j++) {
3065: for (i = 0; i < m; i++) PetscCall(PetscRandomGetValue(rctx, a + j * lda + i));
3066: }
3067: PetscCall(MatDenseRestoreArrayWrite(x, &a));
3068: PetscFunctionReturn(PETSC_SUCCESS);
3069: }
3071: /* vals is not const */
3072: static PetscErrorCode MatDenseGetColumn_SeqDense(Mat A, PetscInt col, PetscScalar **vals)
3073: {
3074: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3075: PetscScalar *v;
3077: PetscFunctionBegin;
3078: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3079: PetscCall(MatDenseGetArray(A, &v));
3080: *vals = v + col * a->lda;
3081: PetscCall(MatDenseRestoreArray(A, &v));
3082: PetscFunctionReturn(PETSC_SUCCESS);
3083: }
3085: static PetscErrorCode MatDenseRestoreColumn_SeqDense(Mat A, PetscScalar **vals)
3086: {
3087: PetscFunctionBegin;
3088: if (vals) *vals = NULL; /* user cannot accidentally use the array later */
3089: PetscFunctionReturn(PETSC_SUCCESS);
3090: }
3092: static struct _MatOps MatOps_Values = {MatSetValues_SeqDense,
3093: MatGetRow_SeqDense,
3094: MatRestoreRow_SeqDense,
3095: MatMult_SeqDense,
3096: /* 4*/ MatMultAdd_SeqDense,
3097: MatMultTranspose_SeqDense,
3098: MatMultTransposeAdd_SeqDense,
3099: NULL,
3100: NULL,
3101: NULL,
3102: /* 10*/ NULL,
3103: MatLUFactor_SeqDense,
3104: MatCholeskyFactor_SeqDense,
3105: MatSOR_SeqDense,
3106: MatTranspose_SeqDense,
3107: /* 15*/ MatGetInfo_SeqDense,
3108: MatEqual_SeqDense,
3109: MatGetDiagonal_SeqDense,
3110: MatDiagonalScale_SeqDense,
3111: MatNorm_SeqDense,
3112: /* 20*/ NULL,
3113: NULL,
3114: MatSetOption_SeqDense,
3115: MatZeroEntries_SeqDense,
3116: /* 24*/ MatZeroRows_SeqDense,
3117: NULL,
3118: NULL,
3119: NULL,
3120: NULL,
3121: /* 29*/ MatSetUp_SeqDense,
3122: NULL,
3123: NULL,
3124: NULL,
3125: NULL,
3126: /* 34*/ MatDuplicate_SeqDense,
3127: NULL,
3128: NULL,
3129: NULL,
3130: NULL,
3131: /* 39*/ MatAXPY_SeqDense,
3132: MatCreateSubMatrices_SeqDense,
3133: NULL,
3134: MatGetValues_SeqDense,
3135: MatCopy_SeqDense,
3136: /* 44*/ MatGetRowMax_SeqDense,
3137: MatScale_SeqDense,
3138: MatShift_SeqDense,
3139: NULL,
3140: MatZeroRowsColumns_SeqDense,
3141: /* 49*/ MatSetRandom_SeqDense,
3142: NULL,
3143: NULL,
3144: NULL,
3145: NULL,
3146: /* 54*/ NULL,
3147: NULL,
3148: NULL,
3149: NULL,
3150: NULL,
3151: /* 59*/ MatCreateSubMatrix_SeqDense,
3152: MatDestroy_SeqDense,
3153: MatView_SeqDense,
3154: NULL,
3155: NULL,
3156: /* 64*/ NULL,
3157: NULL,
3158: NULL,
3159: NULL,
3160: MatGetRowMaxAbs_SeqDense,
3161: /* 69*/ NULL,
3162: NULL,
3163: NULL,
3164: NULL,
3165: NULL,
3166: /* 74*/ NULL,
3167: NULL,
3168: NULL,
3169: NULL,
3170: MatLoad_SeqDense,
3171: /* 79*/ MatIsSymmetric_SeqDense,
3172: MatIsHermitian_SeqDense,
3173: NULL,
3174: NULL,
3175: NULL,
3176: /* 84*/ NULL,
3177: MatMatMultNumeric_SeqDense_SeqDense,
3178: NULL,
3179: NULL,
3180: MatMatTransposeMultNumeric_SeqDense_SeqDense,
3181: /* 89*/ NULL,
3182: MatProductSetFromOptions_SeqDense,
3183: NULL,
3184: NULL,
3185: MatConjugate_SeqDense,
3186: /* 94*/ NULL,
3187: NULL,
3188: MatRealPart_SeqDense,
3189: MatImaginaryPart_SeqDense,
3190: NULL,
3191: /* 99*/ NULL,
3192: NULL,
3193: NULL,
3194: MatGetRowMin_SeqDense,
3195: MatGetColumnVector_SeqDense,
3196: /*104*/ NULL,
3197: NULL,
3198: NULL,
3199: NULL,
3200: NULL,
3201: /*109*/ NULL,
3202: NULL,
3203: MatMultHermitianTranspose_SeqDense,
3204: MatMultHermitianTransposeAdd_SeqDense,
3205: NULL,
3206: /*114*/ NULL,
3207: MatGetColumnReductions_SeqDense,
3208: NULL,
3209: NULL,
3210: NULL,
3211: /*119*/ NULL,
3212: MatTransposeMatMultNumeric_SeqDense_SeqDense,
3213: NULL,
3214: NULL,
3215: NULL,
3216: /*124*/ NULL,
3217: NULL,
3218: NULL,
3219: NULL,
3220: NULL,
3221: /*129*/ MatCreateMPIMatConcatenateSeqMat_SeqDense,
3222: NULL,
3223: NULL,
3224: NULL,
3225: NULL,
3226: /*134*/ NULL,
3227: NULL,
3228: NULL,
3229: NULL,
3230: NULL,
3231: /*139*/ NULL,
3232: NULL,
3233: NULL,
3234: NULL,
3235: MatADot_Default,
3236: /*144*/ MatANorm_Default,
3237: NULL,
3238: NULL,
3239: NULL};
3241: /*@
3242: MatCreateSeqDense - Creates a `MATSEQDENSE` that
3243: is stored in column major order (the usual Fortran format).
3245: Collective
3247: Input Parameters:
3248: + comm - MPI communicator, set to `PETSC_COMM_SELF`
3249: . m - number of rows
3250: . n - number of columns
3251: - data - optional location of matrix data in column major order. Use `NULL` for PETSc
3252: to control all matrix memory allocation.
3254: Output Parameter:
3255: . A - the matrix
3257: Level: intermediate
3259: Note:
3260: The data input variable is intended primarily for Fortran programmers
3261: who wish to allocate their own matrix memory space. Most users should
3262: set `data` = `NULL`.
3264: Developer Note:
3265: Many of the matrix operations for this variant use the BLAS and LAPACK routines.
3267: .seealso: [](ch_matrices), `Mat`, `MATSEQDENSE`, `MatCreate()`, `MatCreateDense()`, `MatSetValues()`
3268: @*/
3269: PetscErrorCode MatCreateSeqDense(MPI_Comm comm, PetscInt m, PetscInt n, PetscScalar data[], Mat *A)
3270: {
3271: PetscFunctionBegin;
3272: PetscCall(MatCreate(comm, A));
3273: PetscCall(MatSetSizes(*A, m, n, m, n));
3274: PetscCall(MatSetType(*A, MATSEQDENSE));
3275: PetscCall(MatSeqDenseSetPreallocation(*A, data));
3276: PetscFunctionReturn(PETSC_SUCCESS);
3277: }
3279: /*@
3280: MatSeqDenseSetPreallocation - Sets the array used for storing the matrix elements of a `MATSEQDENSE` matrix
3282: Collective
3284: Input Parameters:
3285: + B - the matrix
3286: - data - the array (or `NULL`)
3288: Level: intermediate
3290: Note:
3291: The data input variable is intended primarily for Fortran programmers
3292: who wish to allocate their own matrix memory space. Most users should
3293: need not call this routine.
3295: .seealso: [](ch_matrices), `Mat`, `MATSEQDENSE`, `MatCreate()`, `MatCreateDense()`, `MatSetValues()`, `MatDenseSetLDA()`
3296: @*/
3297: PetscErrorCode MatSeqDenseSetPreallocation(Mat B, PetscScalar data[])
3298: {
3299: PetscFunctionBegin;
3301: PetscTryMethod(B, "MatSeqDenseSetPreallocation_C", (Mat, PetscScalar[]), (B, data));
3302: PetscFunctionReturn(PETSC_SUCCESS);
3303: }
3305: PetscErrorCode MatSeqDenseSetPreallocation_SeqDense(Mat B, PetscScalar *data)
3306: {
3307: Mat_SeqDense *b = (Mat_SeqDense *)B->data;
3309: PetscFunctionBegin;
3310: PetscCheck(!b->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3311: B->preallocated = PETSC_TRUE;
3313: PetscCall(PetscLayoutSetUp(B->rmap));
3314: PetscCall(PetscLayoutSetUp(B->cmap));
3316: if (b->lda <= 0) PetscCall(PetscBLASIntCast(B->rmap->n, &b->lda));
3318: if (!data) { /* petsc-allocated storage */
3319: if (!b->user_alloc) PetscCall(PetscFree(b->v));
3320: PetscCall(PetscCalloc1((size_t)b->lda * B->cmap->n, &b->v));
3322: b->user_alloc = PETSC_FALSE;
3323: } else { /* user-allocated storage */
3324: if (!b->user_alloc) PetscCall(PetscFree(b->v));
3325: b->v = data;
3326: b->user_alloc = PETSC_TRUE;
3327: }
3328: B->assembled = PETSC_TRUE;
3329: PetscFunctionReturn(PETSC_SUCCESS);
3330: }
3332: #if PetscDefined(HAVE_ELEMENTAL)
3333: PETSC_INTERN PetscErrorCode MatConvert_SeqDense_Elemental(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
3334: {
3335: Mat mat_elemental;
3336: const PetscScalar *array;
3337: PetscScalar *v_colwise;
3338: PetscInt M = A->rmap->N, N = A->cmap->N, i, j, k, *rows, *cols;
3340: PetscFunctionBegin;
3341: PetscCall(PetscMalloc3(M * N, &v_colwise, M, &rows, N, &cols));
3342: PetscCall(MatDenseGetArrayRead(A, &array));
3343: /* convert column-wise array into row-wise v_colwise, see MatSetValues_Elemental() */
3344: k = 0;
3345: for (j = 0; j < N; j++) {
3346: cols[j] = j;
3347: for (i = 0; i < M; i++) v_colwise[j * M + i] = array[k++];
3348: }
3349: for (i = 0; i < M; i++) rows[i] = i;
3350: PetscCall(MatDenseRestoreArrayRead(A, &array));
3352: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &mat_elemental));
3353: PetscCall(MatSetSizes(mat_elemental, PETSC_DECIDE, PETSC_DECIDE, M, N));
3354: PetscCall(MatSetType(mat_elemental, MATELEMENTAL));
3355: PetscCall(MatSetUp(mat_elemental));
3357: /* PETSc-Elemental interaface uses axpy for setting off-processor entries, only ADD_VALUES is allowed */
3358: PetscCall(MatSetValues(mat_elemental, M, rows, N, cols, v_colwise, ADD_VALUES));
3359: PetscCall(MatAssemblyBegin(mat_elemental, MAT_FINAL_ASSEMBLY));
3360: PetscCall(MatAssemblyEnd(mat_elemental, MAT_FINAL_ASSEMBLY));
3361: PetscCall(PetscFree3(v_colwise, rows, cols));
3363: if (reuse == MAT_INPLACE_MATRIX) {
3364: PetscCall(MatHeaderReplace(A, &mat_elemental));
3365: } else {
3366: *newmat = mat_elemental;
3367: }
3368: PetscFunctionReturn(PETSC_SUCCESS);
3369: }
3370: #endif
3372: PetscErrorCode MatDenseSetLDA_SeqDense(Mat B, PetscInt lda)
3373: {
3374: Mat_SeqDense *b = (Mat_SeqDense *)B->data;
3375: PetscBool data;
3377: PetscFunctionBegin;
3378: data = (B->rmap->n > 0 && B->cmap->n > 0) ? (b->v ? PETSC_TRUE : PETSC_FALSE) : PETSC_FALSE;
3379: PetscCheck(b->user_alloc || !data || b->lda == lda, PETSC_COMM_SELF, PETSC_ERR_ORDER, "LDA cannot be changed after allocation of internal storage");
3380: 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);
3381: PetscCall(PetscBLASIntCast(lda, &b->lda));
3382: PetscFunctionReturn(PETSC_SUCCESS);
3383: }
3385: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqDense(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
3386: {
3387: PetscFunctionBegin;
3388: PetscCall(MatCreateMPIMatConcatenateSeqMat_MPIDense(comm, inmat, n, scall, outmat));
3389: PetscFunctionReturn(PETSC_SUCCESS);
3390: }
3392: PetscErrorCode MatDenseCreateColumnVec_Private(Mat A, Vec *v)
3393: {
3394: PetscBool isstd, iskok, iscuda, iship;
3395: PetscMPIInt size;
3396: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
3397: /* we pass the data of A, to prevent allocating needless GPU memory the first time VecCUPMPlaceArray is called. */
3398: const PetscScalar *a;
3399: #endif
3401: PetscFunctionBegin;
3402: *v = NULL;
3403: PetscCall(PetscStrcmpAny(A->defaultvectype, &isstd, VECSTANDARD, VECSEQ, VECMPI, ""));
3404: PetscCall(PetscStrcmpAny(A->defaultvectype, &iskok, VECKOKKOS, VECSEQKOKKOS, VECMPIKOKKOS, ""));
3405: PetscCall(PetscStrcmpAny(A->defaultvectype, &iscuda, VECCUDA, VECSEQCUDA, VECMPICUDA, ""));
3406: PetscCall(PetscStrcmpAny(A->defaultvectype, &iship, VECHIP, VECSEQHIP, VECMPIHIP, ""));
3407: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
3408: if (isstd) {
3409: if (size > 1) PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, A->rmap->N, NULL, v));
3410: else PetscCall(VecCreateSeqWithArray(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, NULL, v));
3411: } else if (iskok) {
3412: PetscCheck(PetscDefined(HAVE_KOKKOS_KERNELS), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Reconfigure using KOKKOS kernels support");
3413: #if PetscDefined(HAVE_KOKKOS_KERNELS)
3414: if (size > 1) PetscCall(VecCreateMPIKokkosWithArray(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, A->rmap->N, NULL, v));
3415: else PetscCall(VecCreateSeqKokkosWithArray(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, NULL, v));
3416: #endif
3417: } else if (iscuda) {
3418: PetscCheck(PetscDefined(HAVE_CUDA), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Reconfigure using CUDA support");
3419: #if PetscDefined(HAVE_CUDA)
3420: PetscCall(MatDenseCUDAGetArrayRead(A, &a));
3421: if (size > 1) PetscCall(VecCreateMPICUDAWithArrays(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, A->rmap->N, NULL, a, v));
3422: else PetscCall(VecCreateSeqCUDAWithArrays(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, NULL, a, v));
3423: #endif
3424: } else if (iship) {
3425: PetscCheck(PetscDefined(HAVE_HIP), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Reconfigure using HIP support");
3426: #if PetscDefined(HAVE_HIP)
3427: PetscCall(MatDenseHIPGetArrayRead(A, &a));
3428: if (size > 1) PetscCall(VecCreateMPIHIPWithArrays(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, A->rmap->N, NULL, a, v));
3429: else PetscCall(VecCreateSeqHIPWithArrays(PetscObjectComm((PetscObject)A), A->rmap->bs, A->rmap->n, NULL, a, v));
3430: #endif
3431: }
3432: PetscCheck(*v, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Not coded for type %s", A->defaultvectype);
3433: PetscFunctionReturn(PETSC_SUCCESS);
3434: }
3436: PetscErrorCode MatDenseGetColumnVec_SeqDense(Mat A, PetscInt col, Vec *v)
3437: {
3438: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3440: PetscFunctionBegin;
3441: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
3442: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3443: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
3444: a->vecinuse = col + 1;
3445: PetscCall(MatDenseGetArray(A, (PetscScalar **)&a->ptrinuse));
3446: PetscCall(VecPlaceArray(a->cvec, a->ptrinuse + (size_t)col * (size_t)a->lda));
3447: *v = a->cvec;
3448: PetscFunctionReturn(PETSC_SUCCESS);
3449: }
3451: PetscErrorCode MatDenseRestoreColumnVec_SeqDense(Mat A, PetscInt col, Vec *v)
3452: {
3453: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3455: PetscFunctionBegin;
3456: PetscCheck(a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
3457: PetscCheck(a->cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector");
3458: VecCheckAssembled(a->cvec);
3459: a->vecinuse = 0;
3460: PetscCall(MatDenseRestoreArray(A, (PetscScalar **)&a->ptrinuse));
3461: PetscCall(VecResetArray(a->cvec));
3462: if (v) *v = NULL;
3463: PetscFunctionReturn(PETSC_SUCCESS);
3464: }
3466: PetscErrorCode MatDenseGetColumnVecRead_SeqDense(Mat A, PetscInt col, Vec *v)
3467: {
3468: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3470: PetscFunctionBegin;
3471: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
3472: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3473: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
3474: a->vecinuse = col + 1;
3475: PetscCall(MatDenseGetArrayRead(A, &a->ptrinuse));
3476: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)a->lda)));
3477: PetscCall(VecLockReadPush(a->cvec));
3478: *v = a->cvec;
3479: PetscFunctionReturn(PETSC_SUCCESS);
3480: }
3482: PetscErrorCode MatDenseRestoreColumnVecRead_SeqDense(Mat A, PetscInt col, Vec *v)
3483: {
3484: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3486: PetscFunctionBegin;
3487: PetscCheck(a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
3488: PetscCheck(a->cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector");
3489: VecCheckAssembled(a->cvec);
3490: a->vecinuse = 0;
3491: PetscCall(MatDenseRestoreArrayRead(A, &a->ptrinuse));
3492: PetscCall(VecLockReadPop(a->cvec));
3493: PetscCall(VecResetArray(a->cvec));
3494: if (v) *v = NULL;
3495: PetscFunctionReturn(PETSC_SUCCESS);
3496: }
3498: PetscErrorCode MatDenseGetColumnVecWrite_SeqDense(Mat A, PetscInt col, Vec *v)
3499: {
3500: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3502: PetscFunctionBegin;
3503: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
3504: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3505: if (!a->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &a->cvec));
3506: a->vecinuse = col + 1;
3507: PetscCall(MatDenseGetArrayWrite(A, (PetscScalar **)&a->ptrinuse));
3508: PetscCall(VecPlaceArray(a->cvec, PetscSafePointerPlusOffset(a->ptrinuse, (size_t)col * (size_t)a->lda)));
3509: *v = a->cvec;
3510: PetscFunctionReturn(PETSC_SUCCESS);
3511: }
3513: PetscErrorCode MatDenseRestoreColumnVecWrite_SeqDense(Mat A, PetscInt col, Vec *v)
3514: {
3515: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3517: PetscFunctionBegin;
3518: PetscCheck(a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first");
3519: PetscCheck(a->cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector");
3520: VecCheckAssembled(a->cvec);
3521: a->vecinuse = 0;
3522: PetscCall(MatDenseRestoreArrayWrite(A, (PetscScalar **)&a->ptrinuse));
3523: PetscCall(VecResetArray(a->cvec));
3524: if (v) *v = NULL;
3525: PetscFunctionReturn(PETSC_SUCCESS);
3526: }
3528: PetscErrorCode MatDenseGetSubMatrix_SeqDense(Mat A, PetscInt rbegin, PetscInt rend, PetscInt cbegin, PetscInt cend, Mat *v)
3529: {
3530: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3532: PetscFunctionBegin;
3533: PetscCheck(!a->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first");
3534: PetscCheck(!a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first");
3535: if (a->cmat && (cend - cbegin != a->cmat->cmap->N || rend - rbegin != a->cmat->rmap->N)) PetscCall(MatDestroy(&a->cmat));
3536: if (!a->cmat) {
3537: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)A), rend - rbegin, PETSC_DECIDE, rend - rbegin, cend - cbegin, PetscSafePointerPlusOffset(a->v, rbegin + (size_t)cbegin * a->lda), &a->cmat));
3538: } else {
3539: PetscCall(MatDensePlaceArray(a->cmat, PetscSafePointerPlusOffset(a->v, rbegin + (size_t)cbegin * a->lda)));
3540: }
3541: PetscCall(MatDenseSetLDA(a->cmat, a->lda));
3542: a->matinuse = cbegin + 1;
3543: *v = a->cmat;
3544: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
3545: A->offloadmask = PETSC_OFFLOAD_CPU;
3546: #endif
3547: PetscFunctionReturn(PETSC_SUCCESS);
3548: }
3550: PetscErrorCode MatDenseRestoreSubMatrix_SeqDense(Mat A, Mat *v)
3551: {
3552: Mat_SeqDense *a = (Mat_SeqDense *)A->data;
3554: PetscFunctionBegin;
3555: PetscCheck(a->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetSubMatrix() first");
3556: PetscCheck(a->cmat, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column matrix");
3557: PetscCheck(*v == a->cmat, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not the matrix obtained from MatDenseGetSubMatrix()");
3558: a->matinuse = 0;
3559: PetscCall(MatDenseResetArray(a->cmat));
3560: *v = NULL;
3561: #if PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
3562: A->offloadmask = PETSC_OFFLOAD_CPU;
3563: #endif
3564: PetscFunctionReturn(PETSC_SUCCESS);
3565: }
3567: /*MC
3568: MATSEQDENSE - MATSEQDENSE = "seqdense" - A matrix type to be used for sequential dense matrices.
3570: Options Database Key:
3571: . -mat_type seqdense - sets the matrix type to `MATSEQDENSE` during a call to `MatSetFromOptions()`
3573: Level: beginner
3575: .seealso: [](ch_matrices), `Mat`, `MATSEQDENSE`, `MatCreateSeqDense()`
3576: M*/
3577: PetscErrorCode MatCreate_SeqDense(Mat B)
3578: {
3579: Mat_SeqDense *b;
3580: PetscMPIInt size;
3582: PetscFunctionBegin;
3583: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
3584: PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Comm must be of size 1");
3586: PetscCall(PetscNew(&b));
3587: B->data = (void *)b;
3588: B->ops[0] = MatOps_Values;
3590: b->roworiented = PETSC_TRUE;
3592: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatQRFactor_C", MatQRFactor_SeqDense));
3593: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetLDA_C", MatDenseGetLDA_SeqDense));
3594: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseSetLDA_C", MatDenseSetLDA_SeqDense));
3595: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetArray_C", MatDenseGetArray_SeqDense));
3596: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreArray_C", MatDenseRestoreArray_SeqDense));
3597: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDensePlaceArray_C", MatDensePlaceArray_SeqDense));
3598: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseResetArray_C", MatDenseResetArray_SeqDense));
3599: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseReplaceArray_C", MatDenseReplaceArray_SeqDense));
3600: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetArrayRead_C", MatDenseGetArray_SeqDense));
3601: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreArrayRead_C", MatDenseRestoreArray_SeqDense));
3602: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetArrayWrite_C", MatDenseGetArray_SeqDense));
3603: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreArrayWrite_C", MatDenseRestoreArray_SeqDense));
3604: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_seqaij_C", MatConvert_SeqDense_SeqAIJ));
3605: #if PetscDefined(HAVE_ELEMENTAL)
3606: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_elemental_C", MatConvert_SeqDense_Elemental));
3607: #endif
3608: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
3609: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_scalapack_C", MatConvert_Dense_ScaLAPACK));
3610: #endif
3611: #if PetscDefined(HAVE_CUDA)
3612: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_seqdensecuda_C", MatConvert_SeqDense_SeqDenseCUDA));
3613: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdensecuda_seqdensecuda_C", MatProductSetFromOptions_SeqDense));
3614: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdensecuda_seqdense_C", MatProductSetFromOptions_SeqDense));
3615: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqdensecuda_C", MatProductSetFromOptions_SeqDense));
3616: #endif
3617: #if PetscDefined(HAVE_HIP)
3618: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqdense_seqdensehip_C", MatConvert_SeqDense_SeqDenseHIP));
3619: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdensehip_seqdensehip_C", MatProductSetFromOptions_SeqDense));
3620: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdensehip_seqdense_C", MatProductSetFromOptions_SeqDense));
3621: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqdensehip_C", MatProductSetFromOptions_SeqDense));
3622: #endif
3623: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqDenseSetPreallocation_C", MatSeqDenseSetPreallocation_SeqDense));
3624: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqdense_C", MatProductSetFromOptions_SeqAIJ_SeqDense));
3625: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqdense_C", MatProductSetFromOptions_SeqDense));
3626: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqbaij_seqdense_C", MatProductSetFromOptions_SeqXBAIJ_SeqDense));
3627: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqsbaij_seqdense_C", MatProductSetFromOptions_SeqXBAIJ_SeqDense));
3629: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetColumn_C", MatDenseGetColumn_SeqDense));
3630: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreColumn_C", MatDenseRestoreColumn_SeqDense));
3631: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetColumnVec_C", MatDenseGetColumnVec_SeqDense));
3632: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreColumnVec_C", MatDenseRestoreColumnVec_SeqDense));
3633: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetColumnVecRead_C", MatDenseGetColumnVecRead_SeqDense));
3634: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreColumnVecRead_C", MatDenseRestoreColumnVecRead_SeqDense));
3635: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetColumnVecWrite_C", MatDenseGetColumnVecWrite_SeqDense));
3636: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreColumnVecWrite_C", MatDenseRestoreColumnVecWrite_SeqDense));
3637: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseGetSubMatrix_C", MatDenseGetSubMatrix_SeqDense));
3638: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDenseRestoreSubMatrix_C", MatDenseRestoreSubMatrix_SeqDense));
3639: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMultColumnRange_C", MatMultColumnRange_SeqDense));
3640: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMultAddColumnRange_C", MatMultAddColumnRange_SeqDense));
3641: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMultHermitianTransposeColumnRange_C", MatMultHermitianTransposeColumnRange_SeqDense));
3642: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMultHermitianTransposeAddColumnRange_C", MatMultHermitianTransposeAddColumnRange_SeqDense));
3643: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQDENSE));
3644: PetscFunctionReturn(PETSC_SUCCESS);
3645: }
3647: /*@C
3648: 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.
3650: Not Collective
3652: Input Parameters:
3653: + A - a `MATSEQDENSE` or `MATMPIDENSE` matrix
3654: - col - column index
3656: Output Parameter:
3657: . vals - pointer to the data
3659: Level: intermediate
3661: Note:
3662: Use `MatDenseGetColumnVec()` to get access to a column of a `MATDENSE` treated as a `Vec`
3664: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseRestoreColumn()`, `MatDenseGetColumnVec()`
3665: @*/
3666: PetscErrorCode MatDenseGetColumn(Mat A, PetscInt col, PetscScalar *vals[])
3667: {
3668: PetscFunctionBegin;
3671: PetscAssertPointer(vals, 3);
3672: PetscUseMethod(A, "MatDenseGetColumn_C", (Mat, PetscInt, PetscScalar **), (A, col, vals));
3673: PetscFunctionReturn(PETSC_SUCCESS);
3674: }
3676: /*@C
3677: MatDenseRestoreColumn - returns access to a column of a `MATDENSE` matrix which is returned by `MatDenseGetColumn()`.
3679: Not Collective
3681: Input Parameters:
3682: + A - a `MATSEQDENSE` or `MATMPIDENSE` matrix
3683: - vals - pointer to the data (may be `NULL`)
3685: Level: intermediate
3687: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MatDenseGetColumn()`
3688: @*/
3689: PetscErrorCode MatDenseRestoreColumn(Mat A, PetscScalar *vals[])
3690: {
3691: PetscFunctionBegin;
3693: PetscAssertPointer(vals, 2);
3694: PetscUseMethod(A, "MatDenseRestoreColumn_C", (Mat, PetscScalar **), (A, vals));
3695: PetscFunctionReturn(PETSC_SUCCESS);
3696: }
3698: /*@
3699: MatDenseGetColumnVec - Gives read-write access to a column of a `MATDENSE` matrix, represented as a `Vec`.
3701: Collective
3703: Input Parameters:
3704: + A - the `Mat` object
3705: - col - the column index
3707: Output Parameter:
3708: . v - the vector
3710: Level: intermediate
3712: Notes:
3713: The vector is owned by PETSc. Users need to call `MatDenseRestoreColumnVec()` when the vector is no longer needed.
3715: Use `MatDenseGetColumnVecRead()` to obtain read-only access or `MatDenseGetColumnVecWrite()` for write-only access.
3717: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVecRead()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecRead()`, `MatDenseRestoreColumnVecWrite()`, `MatDenseGetColumn()`
3718: @*/
3719: PetscErrorCode MatDenseGetColumnVec(Mat A, PetscInt col, Vec *v)
3720: {
3721: PetscFunctionBegin;
3725: PetscAssertPointer(v, 3);
3726: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3727: 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);
3728: PetscUseMethod(A, "MatDenseGetColumnVec_C", (Mat, PetscInt, Vec *), (A, col, v));
3729: PetscFunctionReturn(PETSC_SUCCESS);
3730: }
3732: /*@
3733: MatDenseRestoreColumnVec - Returns access to a column of a dense matrix obtained from `MatDenseGetColumnVec()`.
3735: Collective
3737: Input Parameters:
3738: + A - the `Mat` object
3739: . col - the column index
3740: - v - the `Vec` object (may be `NULL`)
3742: Level: intermediate
3744: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecRead()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVecRead()`, `MatDenseRestoreColumnVecWrite()`
3745: @*/
3746: PetscErrorCode MatDenseRestoreColumnVec(Mat A, PetscInt col, Vec *v)
3747: {
3748: PetscFunctionBegin;
3753: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3754: 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);
3755: PetscUseMethod(A, "MatDenseRestoreColumnVec_C", (Mat, PetscInt, Vec *), (A, col, v));
3756: PetscFunctionReturn(PETSC_SUCCESS);
3757: }
3759: /*@
3760: MatDenseGetColumnVecRead - Gives read-only access to a column of a dense matrix, represented as a `Vec`.
3762: Collective
3764: Input Parameters:
3765: + A - the `Mat` object
3766: - col - the column index
3768: Output Parameter:
3769: . v - the vector
3771: Level: intermediate
3773: Notes:
3774: The vector is owned by PETSc and users cannot modify it.
3776: Users need to call `MatDenseRestoreColumnVecRead()` when the vector is no longer needed.
3778: Use `MatDenseGetColumnVec()` to obtain read-write access or `MatDenseGetColumnVecWrite()` for write-only access.
3780: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecRead()`, `MatDenseRestoreColumnVecWrite()`
3781: @*/
3782: PetscErrorCode MatDenseGetColumnVecRead(Mat A, PetscInt col, Vec *v)
3783: {
3784: PetscFunctionBegin;
3788: PetscAssertPointer(v, 3);
3789: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3790: 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);
3791: PetscUseMethod(A, "MatDenseGetColumnVecRead_C", (Mat, PetscInt, Vec *), (A, col, v));
3792: PetscFunctionReturn(PETSC_SUCCESS);
3793: }
3795: /*@
3796: MatDenseRestoreColumnVecRead - Returns access to a column of a dense matrix obtained from `MatDenseGetColumnVecRead()`.
3798: Collective
3800: Input Parameters:
3801: + A - the `Mat` object
3802: . col - the column index
3803: - v - the `Vec` object (may be `NULL`)
3805: Level: intermediate
3807: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecRead()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecWrite()`
3808: @*/
3809: PetscErrorCode MatDenseRestoreColumnVecRead(Mat A, PetscInt col, Vec *v)
3810: {
3811: PetscFunctionBegin;
3816: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3817: 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);
3818: PetscUseMethod(A, "MatDenseRestoreColumnVecRead_C", (Mat, PetscInt, Vec *), (A, col, v));
3819: PetscFunctionReturn(PETSC_SUCCESS);
3820: }
3822: /*@
3823: MatDenseGetColumnVecWrite - Gives write-only access to a column of a dense matrix, represented as a `Vec`.
3825: Collective
3827: Input Parameters:
3828: + A - the `Mat` object
3829: - col - the column index
3831: Output Parameter:
3832: . v - the vector
3834: Level: intermediate
3836: Notes:
3837: The vector is owned by PETSc. Users need to call `MatDenseRestoreColumnVecWrite()` when the vector is no longer needed.
3839: Use `MatDenseGetColumnVec()` to obtain read-write access or `MatDenseGetColumnVecRead()` for read-only access.
3841: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecRead()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecRead()`, `MatDenseRestoreColumnVecWrite()`
3842: @*/
3843: PetscErrorCode MatDenseGetColumnVecWrite(Mat A, PetscInt col, Vec *v)
3844: {
3845: PetscFunctionBegin;
3849: PetscAssertPointer(v, 3);
3850: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3851: 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);
3852: PetscUseMethod(A, "MatDenseGetColumnVecWrite_C", (Mat, PetscInt, Vec *), (A, col, v));
3853: PetscFunctionReturn(PETSC_SUCCESS);
3854: }
3856: /*@
3857: MatDenseRestoreColumnVecWrite - Returns access to a column of a dense matrix obtained from `MatDenseGetColumnVecWrite()`.
3859: Collective
3861: Input Parameters:
3862: + A - the `Mat` object
3863: . col - the column index
3864: - v - the `Vec` object (may be `NULL`)
3866: Level: intermediate
3868: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseGetColumnVecRead()`, `MatDenseGetColumnVecWrite()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreColumnVecRead()`
3869: @*/
3870: PetscErrorCode MatDenseRestoreColumnVecWrite(Mat A, PetscInt col, Vec *v)
3871: {
3872: PetscFunctionBegin;
3877: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3878: 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);
3879: PetscUseMethod(A, "MatDenseRestoreColumnVecWrite_C", (Mat, PetscInt, Vec *), (A, col, v));
3880: PetscFunctionReturn(PETSC_SUCCESS);
3881: }
3883: /*@
3884: MatDenseGetSubMatrix - Gives access to a block of rows and columns of a dense matrix, represented as a `Mat`.
3886: Collective
3888: Input Parameters:
3889: + A - the `Mat` object
3890: . rbegin - the first global row index in the block (if `PETSC_DECIDE`, is 0)
3891: . rend - the global row index past the last one in the block (if `PETSC_DECIDE`, is `M`)
3892: . cbegin - the first global column index in the block (if `PETSC_DECIDE`, is 0)
3893: - cend - the global column index past the last one in the block (if `PETSC_DECIDE`, is `N`)
3895: Output Parameter:
3896: . v - the matrix
3898: Level: intermediate
3900: Notes:
3901: The matrix is owned by PETSc. Users need to call `MatDenseRestoreSubMatrix()` when the matrix is no longer needed.
3903: The output matrix is not redistributed by PETSc, so depending on the values of `rbegin` and `rend`, some processes may have no local rows.
3905: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseRestoreColumnVec()`, `MatDenseRestoreSubMatrix()`
3906: @*/
3907: PetscErrorCode MatDenseGetSubMatrix(Mat A, PetscInt rbegin, PetscInt rend, PetscInt cbegin, PetscInt cend, Mat *v)
3908: {
3909: PetscFunctionBegin;
3916: PetscAssertPointer(v, 6);
3917: if (rbegin == PETSC_DECIDE) rbegin = 0;
3918: if (rend == PETSC_DECIDE) rend = A->rmap->N;
3919: if (cbegin == PETSC_DECIDE) cbegin = 0;
3920: if (cend == PETSC_DECIDE) cend = A->cmap->N;
3921: PetscCheck(A->preallocated, PetscObjectComm((PetscObject)A), PETSC_ERR_ORDER, "Matrix not preallocated");
3922: 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);
3923: 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);
3924: 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);
3925: 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);
3926: PetscUseMethod(A, "MatDenseGetSubMatrix_C", (Mat, PetscInt, PetscInt, PetscInt, PetscInt, Mat *), (A, rbegin, rend, cbegin, cend, v));
3927: PetscFunctionReturn(PETSC_SUCCESS);
3928: }
3930: /*@
3931: MatDenseRestoreSubMatrix - Returns access to a block of columns of a dense matrix obtained from `MatDenseGetSubMatrix()`.
3933: Collective
3935: Input Parameters:
3936: + A - the `Mat` object
3937: - v - the `Mat` object (cannot be `NULL`)
3939: Level: intermediate
3941: .seealso: [](ch_matrices), `Mat`, `MATDENSE`, `MATDENSECUDA`, `MATDENSEHIP`, `MatDenseGetColumnVec()`, `MatDenseRestoreColumnVec()`, `MatDenseGetSubMatrix()`
3942: @*/
3943: PetscErrorCode MatDenseRestoreSubMatrix(Mat A, Mat *v)
3944: {
3945: PetscFunctionBegin;
3948: PetscAssertPointer(v, 2);
3950: PetscUseMethod(A, "MatDenseRestoreSubMatrix_C", (Mat, Mat *), (A, v));
3951: PetscFunctionReturn(PETSC_SUCCESS);
3952: }
3954: #include <petscblaslapack.h>
3955: #include <petsc/private/kernels/blockinvert.h>
3957: /*@
3958: MatSeqDenseInvert - Invert a small `MATSEQDENSE` matrix in place using a hard-coded kernel.
3960: Not Collective
3962: Input Parameter:
3963: . A - the `MATSEQDENSE` matrix
3965: Level: developer
3967: Note:
3968: Intended for small blocks; specialized kernels are used for sizes up to 7 and `LAPACK` is used for larger sizes.
3969: If the matrix is singular and `MatSetErrorIfFailure()` was called an error will be immediately generated, otherwise the factor error type
3970: in the matrix, which can be obtained with `MatFactorGetError()`, is set to `MAT_FACTOR_NUMERIC_ZEROPIVOT`.
3972: .seealso: `Mat`, `MATSEQDENSE`, `MatInvertBlockDiagonal()`, `MatLUFactor()`, `MatSetErrorIfFailure()`, `MatFactorGetError()`
3973: @*/
3974: PetscErrorCode MatSeqDenseInvert(Mat A)
3975: {
3976: PetscInt m;
3977: const PetscReal shift = 0.0;
3978: PetscBool allowzeropivot, zeropivotdetected = PETSC_FALSE;
3979: PetscScalar *values;
3981: PetscFunctionBegin;
3983: PetscCall(MatDenseGetArray(A, &values));
3984: PetscCall(MatGetLocalSize(A, &m, NULL));
3985: allowzeropivot = PetscNot(A->erroriffailure);
3986: /* factor and invert each block */
3987: switch (m) {
3988: case 1:
3989: values[0] = (PetscScalar)1.0 / (values[0] + shift);
3990: break;
3991: case 2:
3992: PetscCall(PetscKernel_A_gets_inverse_A_2(values, shift, allowzeropivot, &zeropivotdetected));
3993: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3994: break;
3995: case 3:
3996: PetscCall(PetscKernel_A_gets_inverse_A_3(values, shift, allowzeropivot, &zeropivotdetected));
3997: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3998: break;
3999: case 4:
4000: PetscCall(PetscKernel_A_gets_inverse_A_4(values, shift, allowzeropivot, &zeropivotdetected));
4001: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4002: break;
4003: case 5: {
4004: PetscScalar work[25];
4005: PetscInt ipvt[5];
4007: PetscCall(PetscKernel_A_gets_inverse_A_5(values, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
4008: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4009: } break;
4010: case 6:
4011: PetscCall(PetscKernel_A_gets_inverse_A_6(values, shift, allowzeropivot, &zeropivotdetected));
4012: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4013: break;
4014: case 7:
4015: PetscCall(PetscKernel_A_gets_inverse_A_7(values, shift, allowzeropivot, &zeropivotdetected));
4016: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4017: break;
4018: default: {
4019: PetscInt *v_pivots, *IJ, j;
4020: PetscScalar *v_work;
4022: PetscCall(PetscMalloc3(m, &v_work, m, &v_pivots, m, &IJ));
4023: for (j = 0; j < m; j++) IJ[j] = j;
4024: PetscCall(PetscKernel_A_gets_inverse_A(m, values, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
4025: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
4026: PetscCall(PetscFree3(v_work, v_pivots, IJ));
4027: }
4028: }
4029: PetscCall(MatDenseRestoreArray(A, &values));
4030: PetscFunctionReturn(PETSC_SUCCESS);
4031: }
4033: /*@
4034: MatDenseReplaceArrayWithMemType - Allows one to replace the array in a `MATDENSE`, `MATDENSECUDA`, or `MATDENSEHIP`
4035: with an array provided by the user and a matching `PetscMemType`. This is useful to avoid copying an array into a matrix.
4037: Not Collective
4039: Input Parameters:
4040: + mat - the matrix
4041: . mtype - the `PetscMemType` of the array
4042: - array - the array in column major order
4044: Level: developer
4046: Note:
4047: Adding `const` to `array` was an oversight, see notes in `VecPlaceArray()`.
4049: This permanently replaces the GPU array and frees the memory associated with the old GPU
4050: array. The memory passed in CANNOT be freed by the user. It will be freed when the matrix is
4051: destroyed. The array should respect the matrix leading dimension.
4053: .seealso: `MatDenseReplaceArray()`, `MatDenseCUDAReplaceArray()`, `MatDenseHIPReplaceArray()`
4054: @*/
4055: PetscErrorCode MatDenseReplaceArrayWithMemType(Mat mat, PetscMemType mtype, const PetscScalar array[])
4056: {
4057: const char *type = PetscMemTypeToString(mtype) + 14; /* skip "PETSC_MEMTYPE_" */
4058: char buffer[256];
4060: PetscFunctionBegin;
4062: PetscAssertPointer(array, 3);
4063: PetscCall(PetscSNPrintf(buffer, sizeof(buffer), "MatDense%sReplaceArray_C", PetscMemTypeHost(mtype) ? "" : type));
4064: PetscUseMethod(mat, buffer, (Mat, const PetscScalar[]), (mat, array));
4065: PetscFunctionReturn(PETSC_SUCCESS);
4066: }