Actual source code: kaij.c
1: /*
2: Defines the basic matrix operations for the KAIJ matrix storage format.
3: This format is used to evaluate matrices of the form:
5: [I \otimes S + A \otimes T]
7: where
8: S is a dense (p \times q) matrix
9: T is a dense (p \times q) matrix
10: A is an AIJ (n \times n) matrix
11: I is the identity matrix
13: The resulting matrix is (np \times nq)
15: We provide:
16: MatMult()
17: MatMultAdd()
18: MatInvertBlockDiagonal()
19: and
20: MatCreateKAIJ(Mat,PetscInt,PetscInt,const PetscScalar[],const PetscScalar[],Mat*)
22: This single directory handles both the sequential and parallel codes
23: */
25: #include <../src/mat/impls/kaij/kaij.h>
26: #include <../src/mat/utils/freespace.h>
27: #include <petsc/private/vecimpl.h>
29: /*@
30: MatKAIJGetAIJ - Get the `MATAIJ` matrix describing the blockwise action of the `MATKAIJ` matrix
32: Not Collective, but if the `MATKAIJ` matrix is parallel, the `MATAIJ` matrix is also parallel
34: Input Parameter:
35: . A - the `MATKAIJ` matrix
37: Output Parameter:
38: . B - the `MATAIJ` matrix
40: Level: advanced
42: Note:
43: The reference count on the `MATAIJ` matrix is not increased so you should not destroy it.
45: .seealso: [](ch_matrices), `Mat`, `MatCreateKAIJ()`, `MATKAIJ`, `MATAIJ`
46: @*/
47: PetscErrorCode MatKAIJGetAIJ(Mat A, Mat *B)
48: {
49: PetscBool ismpikaij, isseqkaij;
51: PetscFunctionBegin;
52: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIKAIJ, &ismpikaij));
53: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSEQKAIJ, &isseqkaij));
54: if (ismpikaij) {
55: Mat_MPIKAIJ *b = (Mat_MPIKAIJ *)A->data;
57: *B = b->A;
58: } else if (isseqkaij) {
59: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
61: *B = b->AIJ;
62: } else SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Matrix passed in is not of type KAIJ");
63: PetscFunctionReturn(PETSC_SUCCESS);
64: }
66: /*@C
67: MatKAIJGetS - Get the `S` matrix describing the shift action of the `MATKAIJ` matrix
69: Not Collective; the entire `S` is stored and returned independently on all processes.
71: Input Parameter:
72: . A - the `MATKAIJ` matrix
74: Output Parameters:
75: + m - the number of rows in `S`
76: . n - the number of columns in `S`
77: - S - the S matrix, in form of a scalar array in column-major format
79: Level: advanced
81: Note:
82: All output parameters are optional (pass `NULL` if not desired)
84: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatCreateKAIJ()`, `MatGetBlockSizes()`
85: @*/
86: PetscErrorCode MatKAIJGetS(Mat A, PetscInt *m, PetscInt *n, PetscScalar *S[])
87: {
88: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
90: PetscFunctionBegin;
91: if (m) *m = b->p;
92: if (n) *n = b->q;
93: if (S) *S = b->S;
94: PetscFunctionReturn(PETSC_SUCCESS);
95: }
97: /*@C
98: MatKAIJGetSRead - Get a read-only pointer to the `S` matrix describing the shift action of the `MATKAIJ` matrix
100: Not Collective; the entire `S` is stored and returned independently on all processes.
102: Input Parameter:
103: . A - the `MATKAIJ` matrix
105: Output Parameters:
106: + m - the number of rows in `S`
107: . n - the number of columns in `S`
108: - S - the S matrix, in form of a scalar array in column-major format
110: Level: advanced
112: Note:
113: All output parameters are optional (pass `NULL` if not desired)
115: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatCreateKAIJ()`, `MatGetBlockSizes()`
116: @*/
117: PetscErrorCode MatKAIJGetSRead(Mat A, PetscInt *m, PetscInt *n, const PetscScalar *S[])
118: {
119: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
121: PetscFunctionBegin;
122: if (m) *m = b->p;
123: if (n) *n = b->q;
124: if (S) *S = b->S;
125: PetscFunctionReturn(PETSC_SUCCESS);
126: }
128: /*@C
129: MatKAIJRestoreS - Restore array obtained with `MatKAIJGetS()`
131: Not Collective
133: Input Parameters:
134: + A - the `MATKAIJ` matrix
135: - S - location of pointer to array obtained with `MatKAIJGetS()`
137: Level: advanced
139: Note:
140: This routine zeros the array pointer to prevent accidental reuse after it has been restored.
141: If `NULL` is passed, it will not attempt to zero the array pointer.
143: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatKAIJGetS()`, `MatKAIJGetSRead()`, `MatKAIJRestoreSRead()`
144: @*/
145: PetscErrorCode MatKAIJRestoreS(Mat A, PetscScalar *S[])
146: {
147: PetscFunctionBegin;
148: if (S) *S = NULL;
149: PetscCall(PetscObjectStateIncrease((PetscObject)A));
150: PetscFunctionReturn(PETSC_SUCCESS);
151: }
153: /*@C
154: MatKAIJRestoreSRead - Restore array obtained with `MatKAIJGetSRead()`
156: Not Collective
158: Input Parameters:
159: + A - the `MATKAIJ` matrix
160: - S - location of pointer to array obtained with `MatKAIJGetS()`
162: Level: advanced
164: Note:
165: This routine zeros the array pointer to prevent accidental reuse after it has been restored.
166: If `NULL` is passed, it will not attempt to zero the array pointer.
168: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatKAIJGetS()`, `MatKAIJGetSRead()`
169: @*/
170: PetscErrorCode MatKAIJRestoreSRead(Mat A, const PetscScalar *S[])
171: {
172: PetscFunctionBegin;
173: if (S) *S = NULL;
174: PetscFunctionReturn(PETSC_SUCCESS);
175: }
177: /*@C
178: MatKAIJGetT - Get the transformation matrix `T` associated with the `MATKAIJ` matrix
180: Not Collective; the entire `T` is stored and returned independently on all processes
182: Input Parameter:
183: . A - the `MATKAIJ` matrix
185: Output Parameters:
186: + m - the number of rows in `T`
187: . n - the number of columns in `T`
188: - T - the T matrix, in form of a scalar array in column-major format
190: Level: advanced
192: Note:
193: All output parameters are optional (pass `NULL` if not desired)
195: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatCreateKAIJ()`, `MatGetBlockSizes()`
196: @*/
197: PetscErrorCode MatKAIJGetT(Mat A, PetscInt *m, PetscInt *n, PetscScalar *T[])
198: {
199: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
201: PetscFunctionBegin;
202: if (m) *m = b->p;
203: if (n) *n = b->q;
204: if (T) *T = b->T;
205: PetscFunctionReturn(PETSC_SUCCESS);
206: }
208: /*@C
209: MatKAIJGetTRead - Get a read-only pointer to the transformation matrix `T` associated with the `MATKAIJ` matrix
211: Not Collective; the entire `T` is stored and returned independently on all processes
213: Input Parameter:
214: . A - the `MATKAIJ` matrix
216: Output Parameters:
217: + m - the number of rows in `T`
218: . n - the number of columns in `T`
219: - T - the T matrix, in form of a scalar array in column-major format
221: Level: advanced
223: Note:
224: All output parameters are optional (pass `NULL` if not desired)
226: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatCreateKAIJ()`, `MatGetBlockSizes()`
227: @*/
228: PetscErrorCode MatKAIJGetTRead(Mat A, PetscInt *m, PetscInt *n, const PetscScalar *T[])
229: {
230: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
232: PetscFunctionBegin;
233: if (m) *m = b->p;
234: if (n) *n = b->q;
235: if (T) *T = b->T;
236: PetscFunctionReturn(PETSC_SUCCESS);
237: }
239: /*@C
240: MatKAIJRestoreT - Restore array obtained with `MatKAIJGetT()`
242: Not Collective
244: Input Parameters:
245: + A - the `MATKAIJ` matrix
246: - T - location of pointer to array obtained with `MatKAIJGetS()`
248: Level: advanced
250: Note:
251: This routine zeros the array pointer to prevent accidental reuse after it has been restored.
252: If `NULL` is passed, it will not attempt to zero the array pointer.
254: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatKAIJGetT()`, `MatKAIJGetTRead()`, `MatKAIJRestoreTRead()`
255: @*/
256: PetscErrorCode MatKAIJRestoreT(Mat A, PetscScalar *T[])
257: {
258: PetscFunctionBegin;
259: if (T) *T = NULL;
260: PetscCall(PetscObjectStateIncrease((PetscObject)A));
261: PetscFunctionReturn(PETSC_SUCCESS);
262: }
264: /*@C
265: MatKAIJRestoreTRead - Restore array obtained with `MatKAIJGetTRead()`
267: Not Collective
269: Input Parameters:
270: + A - the `MATKAIJ` matrix
271: - T - location of pointer to array obtained with `MatKAIJGetS()`
273: Level: advanced
275: Note:
276: This routine zeros the array pointer to prevent accidental reuse after it has been restored.
277: If `NULL` is passed, it will not attempt to zero the array pointer.
279: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatKAIJGetT()`, `MatKAIJGetTRead()`
280: @*/
281: PetscErrorCode MatKAIJRestoreTRead(Mat A, const PetscScalar *T[])
282: {
283: PetscFunctionBegin;
284: if (T) *T = NULL;
285: PetscFunctionReturn(PETSC_SUCCESS);
286: }
288: /*@
289: MatKAIJSetAIJ - Set the `MATAIJ` matrix describing the blockwise action of the `MATKAIJ` matrix
291: Logically Collective; if the `MATAIJ` matrix is parallel, the `MATKAIJ` matrix is also parallel
293: Input Parameters:
294: + A - the `MATKAIJ` matrix
295: - B - the `MATAIJ` matrix
297: Level: advanced
299: Notes:
300: This function increases the reference count on the `MATAIJ` matrix, so the user is free to destroy the matrix if it is not needed.
302: Changes to the entries of the `MATAIJ` matrix will immediately affect the `MATKAIJ` matrix.
304: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatKAIJGetAIJ()`, `MatKAIJSetS()`, `MatKAIJSetT()`
305: @*/
306: PetscErrorCode MatKAIJSetAIJ(Mat A, Mat B)
307: {
308: PetscMPIInt size;
309: PetscBool flg;
311: PetscFunctionBegin;
312: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
313: if (size == 1) {
314: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATSEQAIJ, &flg));
315: PetscCheck(flg, PetscObjectComm((PetscObject)B), PETSC_ERR_SUP, "MatKAIJSetAIJ() with MATSEQKAIJ does not support %s as the AIJ mat", ((PetscObject)B)->type_name);
316: Mat_SeqKAIJ *a = (Mat_SeqKAIJ *)A->data;
317: a->AIJ = B;
318: } else {
319: Mat_MPIKAIJ *a = (Mat_MPIKAIJ *)A->data;
320: a->A = B;
321: }
322: PetscCall(PetscObjectReference((PetscObject)B));
323: PetscFunctionReturn(PETSC_SUCCESS);
324: }
326: /*@
327: MatKAIJSetS - Set the `S` matrix describing the shift action of the `MATKAIJ` matrix
329: Logically Collective; the entire `S` is stored independently on all processes.
331: Input Parameters:
332: + A - the `MATKAIJ` matrix
333: . p - the number of rows in `S`
334: . q - the number of columns in `S`
335: - S - the S matrix, in form of a scalar array in column-major format
337: Level: advanced
339: Notes:
340: The dimensions `p` and `q` must match those of the transformation matrix `T` associated with the `MATKAIJ` matrix.
342: The `S` matrix is copied, so the user can destroy this array.
344: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatKAIJGetS()`, `MatKAIJSetT()`, `MatKAIJSetAIJ()`
345: @*/
346: PetscErrorCode MatKAIJSetS(Mat A, PetscInt p, PetscInt q, const PetscScalar S[])
347: {
348: Mat_SeqKAIJ *a = (Mat_SeqKAIJ *)A->data;
350: PetscFunctionBegin;
351: PetscCall(PetscFree(a->S));
352: if (S) {
353: PetscCall(PetscMalloc1(p * q, &a->S));
354: PetscCall(PetscArraycpy(a->S, S, p * q));
355: } else a->S = NULL;
357: a->p = p;
358: a->q = q;
359: PetscFunctionReturn(PETSC_SUCCESS);
360: }
362: /*@
363: MatKAIJGetScaledIdentity - Check if both `S` and `T` are scaled identities.
365: Logically Collective.
367: Input Parameter:
368: . A - the `MATKAIJ` matrix
370: Output Parameter:
371: . identity - the Boolean value
373: Level: advanced
375: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatKAIJGetS()`, `MatKAIJGetT()`
376: @*/
377: PetscErrorCode MatKAIJGetScaledIdentity(Mat A, PetscBool *identity)
378: {
379: Mat_SeqKAIJ *a = (Mat_SeqKAIJ *)A->data;
380: PetscInt i, j;
382: PetscFunctionBegin;
383: if (a->p != a->q) {
384: *identity = PETSC_FALSE;
385: PetscFunctionReturn(PETSC_SUCCESS);
386: } else *identity = PETSC_TRUE;
387: if (!a->isTI || a->S) {
388: for (i = 0; i < a->p && *identity; i++) {
389: for (j = 0; j < a->p && *identity; j++) {
390: if (i != j) {
391: if (a->S && PetscAbsScalar(a->S[i + j * a->p]) > PETSC_SMALL) *identity = PETSC_FALSE;
392: if (a->T && PetscAbsScalar(a->T[i + j * a->p]) > PETSC_SMALL) *identity = PETSC_FALSE;
393: } else {
394: if (a->S && PetscAbsScalar(a->S[i * (a->p + 1)] - a->S[0]) > PETSC_SMALL) *identity = PETSC_FALSE;
395: if (a->T && PetscAbsScalar(a->T[i * (a->p + 1)] - a->T[0]) > PETSC_SMALL) *identity = PETSC_FALSE;
396: }
397: }
398: }
399: }
400: PetscFunctionReturn(PETSC_SUCCESS);
401: }
403: /*@
404: MatKAIJSetT - Set the transformation matrix `T` associated with the `MATKAIJ` matrix
406: Logically Collective; the entire `T` is stored independently on all processes.
408: Input Parameters:
409: + A - the `MATKAIJ` matrix
410: . p - the number of rows in `S`
411: . q - the number of columns in `S`
412: - T - the `T` matrix, in form of a scalar array in column-major format
414: Level: advanced
416: Notes:
417: The dimensions `p` and `q` must match those of the shift matrix `S` associated with the `MATKAIJ` matrix.
419: The `T` matrix is copied, so the user can destroy this array.
421: .seealso: [](ch_matrices), `Mat`, `MATKAIJ`, `MatKAIJGetT()`, `MatKAIJSetS()`, `MatKAIJSetAIJ()`
422: @*/
423: PetscErrorCode MatKAIJSetT(Mat A, PetscInt p, PetscInt q, const PetscScalar T[])
424: {
425: PetscInt i, j;
426: Mat_SeqKAIJ *a = (Mat_SeqKAIJ *)A->data;
427: PetscBool isTI = PETSC_FALSE;
429: PetscFunctionBegin;
430: /* check if T is an identity matrix */
431: if (T && (p == q)) {
432: isTI = PETSC_TRUE;
433: for (i = 0; i < p; i++) {
434: for (j = 0; j < q; j++) {
435: if (i == j) {
436: /* diagonal term must be 1 */
437: if (T[i + j * p] != 1.0) isTI = PETSC_FALSE;
438: } else {
439: /* off-diagonal term must be 0 */
440: if (T[i + j * p] != 0.0) isTI = PETSC_FALSE;
441: }
442: }
443: }
444: }
445: a->isTI = isTI;
447: PetscCall(PetscFree(a->T));
448: if (T && (!isTI)) {
449: PetscCall(PetscMalloc1(p * q, &a->T));
450: PetscCall(PetscArraycpy(a->T, T, p * q));
451: } else a->T = NULL;
453: a->p = p;
454: a->q = q;
455: PetscFunctionReturn(PETSC_SUCCESS);
456: }
458: static PetscErrorCode MatDestroy_SeqKAIJ(Mat A)
459: {
460: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
462: PetscFunctionBegin;
463: PetscCall(MatDestroy(&b->AIJ));
464: PetscCall(PetscFree(b->S));
465: PetscCall(PetscFree(b->T));
466: PetscCall(PetscFree(b->ibdiag));
467: PetscCall(PetscFree5(b->sor.w, b->sor.y, b->sor.work, b->sor.t, b->sor.arr));
468: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqkaij_seqaij_C", NULL));
469: PetscCall(PetscFree(A->data));
470: PetscFunctionReturn(PETSC_SUCCESS);
471: }
473: static PetscErrorCode MatKAIJ_build_AIJ_OAIJ(Mat A)
474: {
475: Mat_MPIKAIJ *a;
476: Mat_MPIAIJ *mpiaij;
477: PetscScalar *T;
478: PetscInt i, j;
479: PetscObjectState state;
481: PetscFunctionBegin;
482: a = (Mat_MPIKAIJ *)A->data;
483: mpiaij = (Mat_MPIAIJ *)a->A->data;
485: PetscCall(PetscObjectStateGet((PetscObject)a->A, &state));
486: if (state == a->state) {
487: /* The existing AIJ and KAIJ members are up-to-date, so simply exit. */
488: PetscFunctionReturn(PETSC_SUCCESS);
489: } else {
490: PetscCall(MatDestroy(&a->AIJ));
491: PetscCall(MatDestroy(&a->OAIJ));
492: if (a->isTI) {
493: /* If the transformation matrix associated with the parallel matrix A is the identity matrix, then a->T will be NULL.
494: * In this case, if we pass a->T directly to the MatCreateKAIJ() calls to create the sequential submatrices, the routine will
495: * not be able to tell that transformation matrix should be set to the identity; thus we create a temporary identity matrix
496: * to pass in. */
497: PetscCall(PetscMalloc1(a->p * a->q, &T));
498: for (i = 0; i < a->p; i++) {
499: for (j = 0; j < a->q; j++) {
500: if (i == j) T[i + j * a->p] = 1.0;
501: else T[i + j * a->p] = 0.0;
502: }
503: }
504: } else T = a->T;
505: PetscCall(MatCreateKAIJ(mpiaij->A, a->p, a->q, a->S, T, &a->AIJ));
506: PetscCall(MatCreateKAIJ(mpiaij->B, a->p, a->q, NULL, T, &a->OAIJ));
507: if (a->isTI) PetscCall(PetscFree(T));
508: a->state = state;
509: }
510: PetscFunctionReturn(PETSC_SUCCESS);
511: }
513: static PetscErrorCode MatSetUp_KAIJ(Mat A)
514: {
515: PetscInt n;
516: PetscMPIInt size;
517: Mat_SeqKAIJ *seqkaij = (Mat_SeqKAIJ *)A->data;
519: PetscFunctionBegin;
520: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
521: if (size == 1) {
522: PetscCall(MatSetSizes(A, seqkaij->p * seqkaij->AIJ->rmap->n, seqkaij->q * seqkaij->AIJ->cmap->n, seqkaij->p * seqkaij->AIJ->rmap->N, seqkaij->q * seqkaij->AIJ->cmap->N));
523: PetscCall(PetscLayoutSetBlockSize(A->rmap, seqkaij->p));
524: PetscCall(PetscLayoutSetBlockSize(A->cmap, seqkaij->q));
525: PetscCall(PetscLayoutSetUp(A->rmap));
526: PetscCall(PetscLayoutSetUp(A->cmap));
527: } else {
528: Mat_MPIKAIJ *a;
529: Mat_MPIAIJ *mpiaij;
530: IS from, to;
531: Vec gvec;
533: a = (Mat_MPIKAIJ *)A->data;
534: mpiaij = (Mat_MPIAIJ *)a->A->data;
535: PetscCall(MatSetSizes(A, a->p * a->A->rmap->n, a->q * a->A->cmap->n, a->p * a->A->rmap->N, a->q * a->A->cmap->N));
536: PetscCall(PetscLayoutSetBlockSize(A->rmap, seqkaij->p));
537: PetscCall(PetscLayoutSetBlockSize(A->cmap, seqkaij->q));
538: PetscCall(PetscLayoutSetUp(A->rmap));
539: PetscCall(PetscLayoutSetUp(A->cmap));
541: PetscCall(MatKAIJ_build_AIJ_OAIJ(A));
543: PetscCall(VecGetSize(mpiaij->lvec, &n));
544: PetscCall(VecCreate(PETSC_COMM_SELF, &a->w));
545: PetscCall(VecSetSizes(a->w, n * a->q, n * a->q));
546: PetscCall(VecSetBlockSize(a->w, a->q));
547: PetscCall(VecSetType(a->w, VECSEQ));
549: /* create two temporary Index sets for build scatter gather */
550: PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)a->A), a->q, n, mpiaij->garray, PETSC_COPY_VALUES, &from));
551: PetscCall(ISCreateStride(PETSC_COMM_SELF, n * a->q, 0, 1, &to));
553: /* create temporary global vector to generate scatter context */
554: PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)a->A), a->q, a->q * a->A->cmap->n, a->q * a->A->cmap->N, NULL, &gvec));
556: /* generate the scatter context */
557: PetscCall(VecScatterCreate(gvec, from, a->w, to, &a->ctx));
559: PetscCall(ISDestroy(&from));
560: PetscCall(ISDestroy(&to));
561: PetscCall(VecDestroy(&gvec));
562: }
564: A->assembled = PETSC_TRUE;
565: PetscFunctionReturn(PETSC_SUCCESS);
566: }
568: static PetscErrorCode MatView_KAIJ(Mat A, PetscViewer viewer)
569: {
570: PetscViewerFormat format;
571: Mat_SeqKAIJ *a = (Mat_SeqKAIJ *)A->data;
572: Mat B;
573: PetscInt i;
574: PetscBool ismpikaij;
576: PetscFunctionBegin;
577: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIKAIJ, &ismpikaij));
578: PetscCall(PetscViewerGetFormat(viewer, &format));
579: if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL || format == PETSC_VIEWER_ASCII_IMPL) {
580: PetscCall(PetscViewerASCIIPrintf(viewer, "S and T have %" PetscInt_FMT " rows and %" PetscInt_FMT " columns\n", a->p, a->q));
582: /* Print appropriate details for S. */
583: if (!a->S) {
584: PetscCall(PetscViewerASCIIPrintf(viewer, "S is NULL\n"));
585: } else if (format == PETSC_VIEWER_ASCII_IMPL) {
586: PetscCall(PetscViewerASCIIPrintf(viewer, "Entries of S are "));
587: for (i = 0; i < (a->p * a->q); i++) {
588: if (PetscDefined(USE_COMPLEX)) PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e %18.16e ", (double)PetscRealPart(a->S[i]), (double)PetscImaginaryPart(a->S[i])));
589: else PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e ", (double)PetscRealPart(a->S[i])));
590: }
591: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
592: }
594: /* Print appropriate details for T. */
595: if (a->isTI) {
596: PetscCall(PetscViewerASCIIPrintf(viewer, "T is the identity matrix\n"));
597: } else if (!a->T) {
598: PetscCall(PetscViewerASCIIPrintf(viewer, "T is NULL\n"));
599: } else if (format == PETSC_VIEWER_ASCII_IMPL) {
600: PetscCall(PetscViewerASCIIPrintf(viewer, "Entries of T are "));
601: for (i = 0; i < (a->p * a->q); i++) {
602: if (PetscDefined(USE_COMPLEX)) PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e %18.16e ", (double)PetscRealPart(a->T[i]), (double)PetscImaginaryPart(a->T[i])));
603: else PetscCall(PetscViewerASCIIPrintf(viewer, "%18.16e ", (double)PetscRealPart(a->T[i])));
604: }
605: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
606: }
608: /* Now print details for the AIJ matrix, using the AIJ viewer. */
609: PetscCall(PetscViewerASCIIPrintf(viewer, "Now viewing the associated AIJ matrix:\n"));
610: if (ismpikaij) {
611: Mat_MPIKAIJ *b = (Mat_MPIKAIJ *)A->data;
612: PetscCall(MatView(b->A, viewer));
613: } else {
614: PetscCall(MatView(a->AIJ, viewer));
615: }
617: } else {
618: /* For all other matrix viewer output formats, simply convert to an AIJ matrix and call MatView() on that. */
619: PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &B));
620: PetscCall(MatView(B, viewer));
621: PetscCall(MatDestroy(&B));
622: }
623: PetscFunctionReturn(PETSC_SUCCESS);
624: }
626: static PetscErrorCode MatDestroy_MPIKAIJ(Mat A)
627: {
628: Mat_MPIKAIJ *b = (Mat_MPIKAIJ *)A->data;
630: PetscFunctionBegin;
631: PetscCall(MatDestroy(&b->AIJ));
632: PetscCall(MatDestroy(&b->OAIJ));
633: PetscCall(MatDestroy(&b->A));
634: PetscCall(VecScatterDestroy(&b->ctx));
635: PetscCall(VecDestroy(&b->w));
636: PetscCall(PetscFree(b->S));
637: PetscCall(PetscFree(b->T));
638: PetscCall(PetscFree(b->ibdiag));
639: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatGetDiagonalBlock_C", NULL));
640: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_mpikaij_mpiaij_C", NULL));
641: PetscCall(PetscFree(A->data));
642: PetscFunctionReturn(PETSC_SUCCESS);
643: }
645: /* zz = yy + Axx */
646: static PetscErrorCode MatMultAdd_SeqKAIJ(Mat A, Vec xx, Vec yy, Vec zz)
647: {
648: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
649: Mat_SeqAIJ *a = (Mat_SeqAIJ *)b->AIJ->data;
650: const PetscScalar *s = b->S, *t = b->T;
651: const PetscScalar *x, *v, *bx;
652: PetscScalar *y, *sums;
653: const PetscInt m = b->AIJ->rmap->n, *idx, *ii;
654: PetscInt n, i, jrow, j, l, p = b->p, q = b->q, k;
656: PetscFunctionBegin;
657: if (!yy) PetscCall(VecSet(zz, 0.0));
658: else PetscCall(VecCopy(yy, zz));
659: if ((!s) && (!t) && (!b->isTI)) PetscFunctionReturn(PETSC_SUCCESS);
661: PetscCall(VecGetArrayRead(xx, &x));
662: PetscCall(VecGetArray(zz, &y));
663: idx = a->j;
664: v = a->a;
665: ii = a->i;
667: if (b->isTI) {
668: for (i = 0; i < m; i++) {
669: jrow = ii[i];
670: n = ii[i + 1] - jrow;
671: sums = y + p * i;
672: for (j = 0; j < n; j++) {
673: for (k = 0; k < p; k++) sums[k] += v[jrow + j] * x[q * idx[jrow + j] + k];
674: }
675: }
676: PetscCall(PetscLogFlops(3.0 * (a->nz) * p));
677: } else if (t) {
678: for (i = 0; i < m; i++) {
679: jrow = ii[i];
680: n = ii[i + 1] - jrow;
681: sums = y + p * i;
682: for (j = 0; j < n; j++) {
683: for (k = 0; k < p; k++) {
684: for (l = 0; l < q; l++) sums[k] += v[jrow + j] * t[k + l * p] * x[q * idx[jrow + j] + l];
685: }
686: }
687: }
688: /* The flop count below assumes that v[jrow+j] is hoisted out (which an optimizing compiler is likely to do),
689: * and also that T part is hoisted outside this loop (in exchange for temporary storage) as (A \otimes I) (I \otimes T),
690: * so that this multiply doesn't have to be redone for each matrix entry, but just once per column. The latter
691: * transformation is much less likely to be applied, but we nonetheless count the minimum flops required. */
692: PetscCall(PetscLogFlops((2.0 * p * q - p) * m + 2.0 * p * a->nz));
693: }
694: if (s) {
695: for (i = 0; i < m; i++) {
696: sums = y + p * i;
697: bx = x + q * i;
698: if (i < b->AIJ->cmap->n) {
699: for (j = 0; j < q; j++) {
700: for (k = 0; k < p; k++) sums[k] += s[k + j * p] * bx[j];
701: }
702: }
703: }
704: PetscCall(PetscLogFlops(2.0 * m * p * q));
705: }
707: PetscCall(VecRestoreArrayRead(xx, &x));
708: PetscCall(VecRestoreArray(zz, &y));
709: PetscFunctionReturn(PETSC_SUCCESS);
710: }
712: static PetscErrorCode MatMult_SeqKAIJ(Mat A, Vec xx, Vec yy)
713: {
714: PetscFunctionBegin;
715: PetscCall(MatMultAdd_SeqKAIJ(A, xx, NULL, yy));
716: PetscFunctionReturn(PETSC_SUCCESS);
717: }
719: #include <petsc/private/kernels/blockinvert.h>
721: static PetscErrorCode MatInvertBlockDiagonal_SeqKAIJ(Mat A, const PetscScalar **values)
722: {
723: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
724: Mat_SeqAIJ *a = (Mat_SeqAIJ *)b->AIJ->data;
725: const PetscScalar *S = b->S;
726: const PetscScalar *T = b->T;
727: const PetscScalar *v = a->a;
728: const PetscInt p = b->p, q = b->q, m = b->AIJ->rmap->n, *idx = a->j, *ii = a->i;
729: PetscInt i, j, *v_pivots, dof, dof2;
730: PetscScalar *diag, aval, *v_work;
732: PetscFunctionBegin;
733: PetscCheck(p == q, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "MATKAIJ: Block size must be square to calculate inverse.");
734: PetscCheck(S || T || b->isTI, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "MATKAIJ: Cannot invert a zero matrix.");
736: dof = p;
737: dof2 = dof * dof;
739: if (b->ibdiagvalid) {
740: if (values) *values = b->ibdiag;
741: PetscFunctionReturn(PETSC_SUCCESS);
742: }
743: if (!b->ibdiag) PetscCall(PetscMalloc1(dof2 * m, &b->ibdiag));
744: if (values) *values = b->ibdiag;
745: diag = b->ibdiag;
747: PetscCall(PetscMalloc2(dof, &v_work, dof, &v_pivots));
748: for (i = 0; i < m; i++) {
749: if (S) {
750: PetscCall(PetscArraycpy(diag, S, dof2));
751: } else {
752: PetscCall(PetscArrayzero(diag, dof2));
753: }
754: if (b->isTI) {
755: aval = 0;
756: for (j = ii[i]; j < ii[i + 1]; j++)
757: if (idx[j] == i) aval = v[j];
758: for (j = 0; j < dof; j++) diag[j + dof * j] += aval;
759: } else if (T) {
760: aval = 0;
761: for (j = ii[i]; j < ii[i + 1]; j++)
762: if (idx[j] == i) aval = v[j];
763: for (j = 0; j < dof2; j++) diag[j] += aval * T[j];
764: }
765: PetscCall(PetscKernel_A_gets_inverse_A(dof, diag, v_pivots, v_work, PETSC_FALSE, NULL));
766: diag += dof2;
767: }
768: PetscCall(PetscFree2(v_work, v_pivots));
770: b->ibdiagvalid = PETSC_TRUE;
771: PetscFunctionReturn(PETSC_SUCCESS);
772: }
774: static PetscErrorCode MatGetDiagonalBlock_MPIKAIJ(Mat A, Mat *B)
775: {
776: Mat_MPIKAIJ *kaij = (Mat_MPIKAIJ *)A->data;
778: PetscFunctionBegin;
779: *B = kaij->AIJ;
780: PetscFunctionReturn(PETSC_SUCCESS);
781: }
783: static PetscErrorCode MatConvert_KAIJ_AIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
784: {
785: Mat_SeqKAIJ *a = (Mat_SeqKAIJ *)A->data;
786: Mat AIJ, OAIJ, B;
787: PetscInt *d_nnz, *o_nnz = NULL, nz, i, j, m, d;
788: const PetscInt p = a->p, q = a->q;
789: PetscBool ismpikaij, diagDense;
790: const PetscInt *aijdiag;
792: PetscFunctionBegin;
793: if (reuse != MAT_REUSE_MATRIX) {
794: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPIKAIJ, &ismpikaij));
795: if (ismpikaij) {
796: Mat_MPIKAIJ *b = (Mat_MPIKAIJ *)A->data;
797: AIJ = ((Mat_SeqKAIJ *)b->AIJ->data)->AIJ;
798: OAIJ = ((Mat_SeqKAIJ *)b->OAIJ->data)->AIJ;
799: } else {
800: AIJ = a->AIJ;
801: OAIJ = NULL;
802: }
803: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
804: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
805: PetscCall(MatSetType(B, MATAIJ));
806: PetscCall(MatGetSize(AIJ, &m, NULL));
808: /*
809: Determine the first row of AIJ with missing diagonal and assume all successive rows also have a missing diagonal
810: */
811: PetscCall(MatGetDiagonalMarkers_SeqAIJ(AIJ, &aijdiag, &diagDense));
812: if (diagDense || !a->S) d = m;
813: else {
814: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)AIJ->data;
816: for (d = 0; d < m; d++) {
817: if (aijdiag[d] == aij->i[d + 1]) break;
818: }
819: }
820: PetscCall(PetscMalloc1(m * p, &d_nnz));
821: for (i = 0; i < m; ++i) {
822: PetscCall(MatGetRow_SeqAIJ(AIJ, i, &nz, NULL, NULL));
823: for (j = 0; j < p; ++j) d_nnz[i * p + j] = nz * q + (i >= d) * q;
824: PetscCall(MatRestoreRow_SeqAIJ(AIJ, i, &nz, NULL, NULL));
825: }
826: if (OAIJ) {
827: PetscCall(PetscMalloc1(m * p, &o_nnz));
828: for (i = 0; i < m; ++i) {
829: PetscCall(MatGetRow_SeqAIJ(OAIJ, i, &nz, NULL, NULL));
830: for (j = 0; j < p; ++j) o_nnz[i * p + j] = nz * q;
831: PetscCall(MatRestoreRow_SeqAIJ(OAIJ, i, &nz, NULL, NULL));
832: }
833: PetscCall(MatMPIAIJSetPreallocation(B, 0, d_nnz, 0, o_nnz));
834: } else {
835: PetscCall(MatSeqAIJSetPreallocation(B, 0, d_nnz));
836: }
837: PetscCall(PetscFree(d_nnz));
838: PetscCall(PetscFree(o_nnz));
839: } else B = *newmat;
840: PetscCall(MatConvert_Basic(A, newtype, MAT_REUSE_MATRIX, &B));
841: if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &B));
842: else *newmat = B;
843: PetscFunctionReturn(PETSC_SUCCESS);
844: }
846: static PetscErrorCode MatSOR_SeqKAIJ(Mat A, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
847: {
848: Mat_SeqKAIJ *kaij = (Mat_SeqKAIJ *)A->data;
849: Mat_SeqAIJ *a = (Mat_SeqAIJ *)kaij->AIJ->data;
850: const PetscScalar *aa = a->a, *T = kaij->T, *v;
851: const PetscInt m = kaij->AIJ->rmap->n, *ai = a->i, *aj = a->j, p = kaij->p, q = kaij->q, *diag, *vi;
852: const PetscScalar *b, *xb, *idiag;
853: PetscScalar *x, *work, *workt, *w, *y, *arr, *t, *arrt;
854: PetscInt i, j, k, i2, bs, bs2, nz;
856: PetscFunctionBegin;
857: its = its * lits;
858: PetscCheck(!(flag & SOR_EISENSTAT), PETSC_COMM_SELF, PETSC_ERR_SUP, "No support yet for Eisenstat");
859: 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);
860: PetscCheck(!fshift, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for diagonal shift");
861: PetscCheck(!(flag & SOR_APPLY_UPPER) && !(flag & SOR_APPLY_LOWER), PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for applying upper or lower triangular parts");
862: PetscCheck(p == q, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatSOR for KAIJ: No support for non-square dense blocks");
863: bs = p;
864: bs2 = bs * bs;
866: if (!m) PetscFunctionReturn(PETSC_SUCCESS);
868: if (!kaij->ibdiagvalid) PetscCall(MatInvertBlockDiagonal_SeqKAIJ(A, NULL));
869: idiag = kaij->ibdiag;
870: PetscCall(MatGetDiagonalMarkers_SeqAIJ(kaij->AIJ, &diag, NULL));
872: if (!kaij->sor.setup) {
873: PetscCall(PetscMalloc5(bs, &kaij->sor.w, bs, &kaij->sor.y, m * bs, &kaij->sor.work, m * bs, &kaij->sor.t, m * bs2, &kaij->sor.arr));
874: kaij->sor.setup = PETSC_TRUE;
875: }
876: y = kaij->sor.y;
877: w = kaij->sor.w;
878: work = kaij->sor.work;
879: t = kaij->sor.t;
880: arr = kaij->sor.arr;
882: PetscCall(VecGetArray(xx, &x));
883: PetscCall(VecGetArrayRead(bb, &b));
885: if (flag & SOR_ZERO_INITIAL_GUESS) {
886: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
887: PetscKernel_w_gets_Ar_times_v(bs, bs, b, idiag, x); /* x[0:bs] <- D^{-1} b[0:bs] */
888: PetscCall(PetscArraycpy(t, b, bs));
889: i2 = bs;
890: idiag += bs2;
891: for (i = 1; i < m; i++) {
892: v = aa + ai[i];
893: vi = aj + ai[i];
894: nz = diag[i] - ai[i];
896: if (T) { /* b - T (Arow * x) */
897: PetscCall(PetscArrayzero(w, bs));
898: for (j = 0; j < nz; j++) {
899: for (k = 0; k < bs; k++) w[k] -= v[j] * x[vi[j] * bs + k];
900: }
901: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs, w, T, &t[i2]);
902: for (k = 0; k < bs; k++) t[i2 + k] += b[i2 + k];
903: } else if (kaij->isTI) {
904: PetscCall(PetscArraycpy(t + i2, b + i2, bs));
905: for (j = 0; j < nz; j++) {
906: for (k = 0; k < bs; k++) t[i2 + k] -= v[j] * x[vi[j] * bs + k];
907: }
908: } else {
909: PetscCall(PetscArraycpy(t + i2, b + i2, bs));
910: }
912: PetscKernel_w_gets_Ar_times_v(bs, bs, t + i2, idiag, y);
913: for (j = 0; j < bs; j++) x[i2 + j] = omega * y[j];
915: idiag += bs2;
916: i2 += bs;
917: }
918: /* for logging purposes assume number of nonzero in lower half is 1/2 of total */
919: PetscCall(PetscLogFlops(1.0 * bs2 * a->nz));
920: xb = t;
921: } else xb = b;
922: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
923: idiag = kaij->ibdiag + bs2 * (m - 1);
924: i2 = bs * (m - 1);
925: PetscCall(PetscArraycpy(w, xb + i2, bs));
926: PetscKernel_w_gets_Ar_times_v(bs, bs, w, idiag, x + i2);
927: i2 -= bs;
928: idiag -= bs2;
929: for (i = m - 2; i >= 0; i--) {
930: v = aa + diag[i] + 1;
931: vi = aj + diag[i] + 1;
932: nz = ai[i + 1] - diag[i] - 1;
934: if (T) { /* FIXME: This branch untested */
935: PetscCall(PetscArraycpy(w, xb + i2, bs));
936: /* copy all rows of x that are needed into contiguous space */
937: workt = work;
938: for (j = 0; j < nz; j++) {
939: PetscCall(PetscArraycpy(workt, x + bs * (*vi++), bs));
940: workt += bs;
941: }
942: arrt = arr;
943: for (j = 0; j < nz; j++) {
944: PetscCall(PetscArraycpy(arrt, T, bs2));
945: for (k = 0; k < bs2; k++) arrt[k] *= v[j];
946: arrt += bs2;
947: }
948: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
949: } else if (kaij->isTI) {
950: PetscCall(PetscArraycpy(w, t + i2, bs));
951: for (j = 0; j < nz; j++) {
952: for (k = 0; k < bs; k++) w[k] -= v[j] * x[vi[j] * bs + k];
953: }
954: }
956: PetscKernel_w_gets_Ar_times_v(bs, bs, w, idiag, y); /* RHS incorrect for omega != 1.0 */
957: for (j = 0; j < bs; j++) x[i2 + j] = (1.0 - omega) * x[i2 + j] + omega * y[j];
959: idiag -= bs2;
960: i2 -= bs;
961: }
962: PetscCall(PetscLogFlops(1.0 * bs2 * (a->nz)));
963: }
964: its--;
965: }
966: while (its--) { /* FIXME: This branch not updated */
967: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
968: i2 = 0;
969: idiag = kaij->ibdiag;
970: for (i = 0; i < m; i++) {
971: PetscCall(PetscArraycpy(w, b + i2, bs));
973: v = aa + ai[i];
974: vi = aj + ai[i];
975: nz = diag[i] - ai[i];
976: workt = work;
977: for (j = 0; j < nz; j++) {
978: PetscCall(PetscArraycpy(workt, x + bs * (*vi++), bs));
979: workt += bs;
980: }
981: arrt = arr;
982: if (T) {
983: for (j = 0; j < nz; j++) {
984: PetscCall(PetscArraycpy(arrt, T, bs2));
985: for (k = 0; k < bs2; k++) arrt[k] *= v[j];
986: arrt += bs2;
987: }
988: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
989: } else if (kaij->isTI) {
990: for (j = 0; j < nz; j++) {
991: PetscCall(PetscArrayzero(arrt, bs2));
992: for (k = 0; k < bs; k++) arrt[k + bs * k] = v[j];
993: arrt += bs2;
994: }
995: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
996: }
997: PetscCall(PetscArraycpy(t + i2, w, bs));
999: v = aa + diag[i] + 1;
1000: vi = aj + diag[i] + 1;
1001: nz = ai[i + 1] - diag[i] - 1;
1002: workt = work;
1003: for (j = 0; j < nz; j++) {
1004: PetscCall(PetscArraycpy(workt, x + bs * (*vi++), bs));
1005: workt += bs;
1006: }
1007: arrt = arr;
1008: if (T) {
1009: for (j = 0; j < nz; j++) {
1010: PetscCall(PetscArraycpy(arrt, T, bs2));
1011: for (k = 0; k < bs2; k++) arrt[k] *= v[j];
1012: arrt += bs2;
1013: }
1014: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
1015: } else if (kaij->isTI) {
1016: for (j = 0; j < nz; j++) {
1017: PetscCall(PetscArrayzero(arrt, bs2));
1018: for (k = 0; k < bs; k++) arrt[k + bs * k] = v[j];
1019: arrt += bs2;
1020: }
1021: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
1022: }
1024: PetscKernel_w_gets_Ar_times_v(bs, bs, w, idiag, y);
1025: for (j = 0; j < bs; j++) *(x + i2 + j) = (1.0 - omega) * *(x + i2 + j) + omega * *(y + j);
1027: idiag += bs2;
1028: i2 += bs;
1029: }
1030: xb = t;
1031: } else xb = b;
1032: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1033: idiag = kaij->ibdiag + bs2 * (m - 1);
1034: i2 = bs * (m - 1);
1035: if (xb == b) {
1036: for (i = m - 1; i >= 0; i--) {
1037: PetscCall(PetscArraycpy(w, b + i2, bs));
1039: v = aa + ai[i];
1040: vi = aj + ai[i];
1041: nz = diag[i] - ai[i];
1042: workt = work;
1043: for (j = 0; j < nz; j++) {
1044: PetscCall(PetscArraycpy(workt, x + bs * (*vi++), bs));
1045: workt += bs;
1046: }
1047: arrt = arr;
1048: if (T) {
1049: for (j = 0; j < nz; j++) {
1050: PetscCall(PetscArraycpy(arrt, T, bs2));
1051: for (k = 0; k < bs2; k++) arrt[k] *= v[j];
1052: arrt += bs2;
1053: }
1054: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
1055: } else if (kaij->isTI) {
1056: for (j = 0; j < nz; j++) {
1057: PetscCall(PetscArrayzero(arrt, bs2));
1058: for (k = 0; k < bs; k++) arrt[k + bs * k] = v[j];
1059: arrt += bs2;
1060: }
1061: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
1062: }
1064: v = aa + diag[i] + 1;
1065: vi = aj + diag[i] + 1;
1066: nz = ai[i + 1] - diag[i] - 1;
1067: workt = work;
1068: for (j = 0; j < nz; j++) {
1069: PetscCall(PetscArraycpy(workt, x + bs * (*vi++), bs));
1070: workt += bs;
1071: }
1072: arrt = arr;
1073: if (T) {
1074: for (j = 0; j < nz; j++) {
1075: PetscCall(PetscArraycpy(arrt, T, bs2));
1076: for (k = 0; k < bs2; k++) arrt[k] *= v[j];
1077: arrt += bs2;
1078: }
1079: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
1080: } else if (kaij->isTI) {
1081: for (j = 0; j < nz; j++) {
1082: PetscCall(PetscArrayzero(arrt, bs2));
1083: for (k = 0; k < bs; k++) arrt[k + bs * k] = v[j];
1084: arrt += bs2;
1085: }
1086: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
1087: }
1089: PetscKernel_w_gets_Ar_times_v(bs, bs, w, idiag, y);
1090: for (j = 0; j < bs; j++) *(x + i2 + j) = (1.0 - omega) * *(x + i2 + j) + omega * *(y + j);
1091: }
1092: } else {
1093: for (i = m - 1; i >= 0; i--) {
1094: PetscCall(PetscArraycpy(w, xb + i2, bs));
1095: v = aa + diag[i] + 1;
1096: vi = aj + diag[i] + 1;
1097: nz = ai[i + 1] - diag[i] - 1;
1098: workt = work;
1099: for (j = 0; j < nz; j++) {
1100: PetscCall(PetscArraycpy(workt, x + bs * (*vi++), bs));
1101: workt += bs;
1102: }
1103: arrt = arr;
1104: if (T) {
1105: for (j = 0; j < nz; j++) {
1106: PetscCall(PetscArraycpy(arrt, T, bs2));
1107: for (k = 0; k < bs2; k++) arrt[k] *= v[j];
1108: arrt += bs2;
1109: }
1110: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
1111: } else if (kaij->isTI) {
1112: for (j = 0; j < nz; j++) {
1113: PetscCall(PetscArrayzero(arrt, bs2));
1114: for (k = 0; k < bs; k++) arrt[k + bs * k] = v[j];
1115: arrt += bs2;
1116: }
1117: PetscKernel_w_gets_w_minus_Ar_times_v(bs, bs * nz, w, arr, work);
1118: }
1119: PetscKernel_w_gets_Ar_times_v(bs, bs, w, idiag, y);
1120: for (j = 0; j < bs; j++) *(x + i2 + j) = (1.0 - omega) * *(x + i2 + j) + omega * *(y + j);
1121: }
1122: }
1123: PetscCall(PetscLogFlops(1.0 * bs2 * (a->nz)));
1124: }
1125: }
1127: PetscCall(VecRestoreArray(xx, &x));
1128: PetscCall(VecRestoreArrayRead(bb, &b));
1129: PetscFunctionReturn(PETSC_SUCCESS);
1130: }
1132: /*===================================================================================*/
1134: static PetscErrorCode MatMultAdd_MPIKAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1135: {
1136: Mat_MPIKAIJ *b = (Mat_MPIKAIJ *)A->data;
1138: PetscFunctionBegin;
1139: if (!yy) PetscCall(VecSet(zz, 0.0));
1140: else PetscCall(VecCopy(yy, zz));
1141: PetscCall(MatKAIJ_build_AIJ_OAIJ(A)); /* Ensure b->AIJ and b->OAIJ are up to date. */
1142: /* start the scatter */
1143: PetscCall(VecScatterBegin(b->ctx, xx, b->w, INSERT_VALUES, SCATTER_FORWARD));
1144: PetscUseTypeMethod(b->AIJ, multadd, xx, zz, zz);
1145: PetscCall(VecScatterEnd(b->ctx, xx, b->w, INSERT_VALUES, SCATTER_FORWARD));
1146: PetscUseTypeMethod(b->OAIJ, multadd, b->w, zz, zz);
1147: PetscFunctionReturn(PETSC_SUCCESS);
1148: }
1150: static PetscErrorCode MatMult_MPIKAIJ(Mat A, Vec xx, Vec yy)
1151: {
1152: PetscFunctionBegin;
1153: PetscCall(MatMultAdd_MPIKAIJ(A, xx, NULL, yy));
1154: PetscFunctionReturn(PETSC_SUCCESS);
1155: }
1157: static PetscErrorCode MatInvertBlockDiagonal_MPIKAIJ(Mat A, const PetscScalar **values)
1158: {
1159: Mat_MPIKAIJ *b = (Mat_MPIKAIJ *)A->data;
1161: PetscFunctionBegin;
1162: PetscCall(MatKAIJ_build_AIJ_OAIJ(A)); /* Ensure b->AIJ is up to date. */
1163: PetscUseTypeMethod(b->AIJ, invertblockdiagonal, values);
1164: PetscFunctionReturn(PETSC_SUCCESS);
1165: }
1167: static PetscErrorCode MatGetRow_SeqKAIJ(Mat A, PetscInt row, PetscInt *ncols, PetscInt **cols, PetscScalar **values)
1168: {
1169: Mat_SeqKAIJ *b = (Mat_SeqKAIJ *)A->data;
1170: PetscBool diag = PETSC_FALSE;
1171: PetscInt nzaij, nz, *colsaij, *idx, i, j, p = b->p, q = b->q, r = row / p, s = row % p, c;
1172: PetscScalar *vaij, *v, *S = b->S, *T = b->T;
1174: PetscFunctionBegin;
1175: PetscCheck(!b->getrowactive, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Already active");
1176: b->getrowactive = PETSC_TRUE;
1177: PetscCheck(row >= 0 && row < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %" PetscInt_FMT " out of range", row);
1179: if ((!S) && (!T) && (!b->isTI)) {
1180: if (ncols) *ncols = 0;
1181: if (cols) *cols = NULL;
1182: if (values) *values = NULL;
1183: PetscFunctionReturn(PETSC_SUCCESS);
1184: }
1186: if (T || b->isTI) {
1187: PetscCall(MatGetRow_SeqAIJ(b->AIJ, r, &nzaij, &colsaij, &vaij));
1188: c = nzaij;
1189: for (i = 0; i < nzaij; i++) {
1190: /* check if this row contains a diagonal entry */
1191: if (colsaij[i] == r) {
1192: diag = PETSC_TRUE;
1193: c = i;
1194: }
1195: }
1196: } else nzaij = c = 0;
1198: /* calculate size of row */
1199: nz = 0;
1200: if (S) nz += q;
1201: if (T || b->isTI) nz += (diag && S ? (nzaij - 1) * q : nzaij * q);
1203: if (cols || values) {
1204: PetscCall(PetscMalloc2(nz, &idx, nz, &v));
1205: for (i = 0; i < q; i++) {
1206: /* We need to initialize the v[i] to zero to handle the case in which T is NULL (not the identity matrix). */
1207: v[i] = 0.0;
1208: }
1209: if (b->isTI) {
1210: for (i = 0; i < nzaij; i++) {
1211: for (j = 0; j < q; j++) {
1212: idx[i * q + j] = colsaij[i] * q + j;
1213: v[i * q + j] = (j == s ? vaij[i] : 0);
1214: }
1215: }
1216: } else if (T) {
1217: for (i = 0; i < nzaij; i++) {
1218: for (j = 0; j < q; j++) {
1219: idx[i * q + j] = colsaij[i] * q + j;
1220: v[i * q + j] = vaij[i] * T[s + j * p];
1221: }
1222: }
1223: }
1224: if (S) {
1225: for (j = 0; j < q; j++) {
1226: idx[c * q + j] = r * q + j;
1227: v[c * q + j] += S[s + j * p];
1228: }
1229: }
1230: }
1232: if (ncols) *ncols = nz;
1233: if (cols) *cols = idx;
1234: if (values) *values = v;
1235: PetscFunctionReturn(PETSC_SUCCESS);
1236: }
1238: static PetscErrorCode MatRestoreRow_SeqKAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
1239: {
1240: PetscFunctionBegin;
1241: PetscCall(PetscFree2(*idx, *v));
1242: ((Mat_SeqKAIJ *)A->data)->getrowactive = PETSC_FALSE;
1243: PetscFunctionReturn(PETSC_SUCCESS);
1244: }
1246: static PetscErrorCode MatGetRow_MPIKAIJ(Mat A, PetscInt row, PetscInt *ncols, PetscInt **cols, PetscScalar **values)
1247: {
1248: Mat_MPIKAIJ *b = (Mat_MPIKAIJ *)A->data;
1249: Mat AIJ = b->A;
1250: PetscBool diag = PETSC_FALSE;
1251: Mat MatAIJ, MatOAIJ;
1252: const PetscInt rstart = A->rmap->rstart, rend = A->rmap->rend, p = b->p, q = b->q, *garray;
1253: PetscInt nz, *idx, ncolsaij = 0, ncolsoaij = 0, *colsaij, *colsoaij, r, s, c, i, j, lrow;
1254: PetscScalar *v, *vals, *ovals, *S = b->S, *T = b->T;
1256: PetscFunctionBegin;
1257: PetscCall(MatKAIJ_build_AIJ_OAIJ(A)); /* Ensure b->AIJ and b->OAIJ are up to date. */
1258: MatAIJ = ((Mat_SeqKAIJ *)b->AIJ->data)->AIJ;
1259: MatOAIJ = ((Mat_SeqKAIJ *)b->OAIJ->data)->AIJ;
1260: PetscCheck(!b->getrowactive, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Already active");
1261: b->getrowactive = PETSC_TRUE;
1262: PetscCheck(row >= rstart && row < rend, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Only local rows");
1263: lrow = row - rstart;
1265: if ((!S) && (!T) && (!b->isTI)) {
1266: if (ncols) *ncols = 0;
1267: if (cols) *cols = NULL;
1268: if (values) *values = NULL;
1269: PetscFunctionReturn(PETSC_SUCCESS);
1270: }
1272: r = lrow / p;
1273: s = lrow % p;
1275: if (T || b->isTI) {
1276: PetscCall(MatMPIAIJGetSeqAIJ(AIJ, NULL, NULL, &garray));
1277: PetscCall(MatGetRow_SeqAIJ(MatAIJ, lrow / p, &ncolsaij, &colsaij, &vals));
1278: PetscCall(MatGetRow_SeqAIJ(MatOAIJ, lrow / p, &ncolsoaij, &colsoaij, &ovals));
1280: c = ncolsaij + ncolsoaij;
1281: for (i = 0; i < ncolsaij; i++) {
1282: /* check if this row contains a diagonal entry */
1283: if (colsaij[i] == r) {
1284: diag = PETSC_TRUE;
1285: c = i;
1286: }
1287: }
1288: } else c = 0;
1290: /* calculate size of row */
1291: nz = 0;
1292: if (S) nz += q;
1293: if (T || b->isTI) nz += (diag && S ? (ncolsaij + ncolsoaij - 1) * q : (ncolsaij + ncolsoaij) * q);
1295: if (cols || values) {
1296: PetscCall(PetscMalloc2(nz, &idx, nz, &v));
1297: for (i = 0; i < q; i++) {
1298: /* We need to initialize the v[i] to zero to handle the case in which T is NULL (not the identity matrix). */
1299: v[i] = 0.0;
1300: }
1301: if (b->isTI) {
1302: for (i = 0; i < ncolsaij; i++) {
1303: for (j = 0; j < q; j++) {
1304: idx[i * q + j] = (colsaij[i] + rstart / p) * q + j;
1305: v[i * q + j] = (j == s ? vals[i] : 0.0);
1306: }
1307: }
1308: for (i = 0; i < ncolsoaij; i++) {
1309: for (j = 0; j < q; j++) {
1310: idx[(i + ncolsaij) * q + j] = garray[colsoaij[i]] * q + j;
1311: v[(i + ncolsaij) * q + j] = (j == s ? ovals[i] : 0.0);
1312: }
1313: }
1314: } else if (T) {
1315: for (i = 0; i < ncolsaij; i++) {
1316: for (j = 0; j < q; j++) {
1317: idx[i * q + j] = (colsaij[i] + rstart / p) * q + j;
1318: v[i * q + j] = vals[i] * T[s + j * p];
1319: }
1320: }
1321: for (i = 0; i < ncolsoaij; i++) {
1322: for (j = 0; j < q; j++) {
1323: idx[(i + ncolsaij) * q + j] = garray[colsoaij[i]] * q + j;
1324: v[(i + ncolsaij) * q + j] = ovals[i] * T[s + j * p];
1325: }
1326: }
1327: }
1328: if (S) {
1329: for (j = 0; j < q; j++) {
1330: idx[c * q + j] = (r + rstart / p) * q + j;
1331: v[c * q + j] += S[s + j * p];
1332: }
1333: }
1334: }
1336: if (ncols) *ncols = nz;
1337: if (cols) *cols = idx;
1338: if (values) *values = v;
1339: PetscFunctionReturn(PETSC_SUCCESS);
1340: }
1342: static PetscErrorCode MatRestoreRow_MPIKAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
1343: {
1344: PetscFunctionBegin;
1345: PetscCall(PetscFree2(*idx, *v));
1346: ((Mat_SeqKAIJ *)A->data)->getrowactive = PETSC_FALSE;
1347: PetscFunctionReturn(PETSC_SUCCESS);
1348: }
1350: static PetscErrorCode MatCreateSubMatrix_KAIJ(Mat mat, IS isrow, IS iscol, MatReuse cll, Mat *newmat)
1351: {
1352: Mat A;
1354: PetscFunctionBegin;
1355: PetscCall(MatConvert(mat, MATAIJ, MAT_INITIAL_MATRIX, &A));
1356: PetscCall(MatCreateSubMatrix(A, isrow, iscol, cll, newmat));
1357: PetscCall(MatDestroy(&A));
1358: PetscFunctionReturn(PETSC_SUCCESS);
1359: }
1361: /*@C
1362: MatCreateKAIJ - Creates a matrix of type `MATKAIJ`.
1364: Collective
1366: Input Parameters:
1367: + A - the `MATAIJ` matrix
1368: . p - number of rows in `S` and `T`
1369: . q - number of columns in `S` and `T`
1370: . S - the `S` matrix (can be `NULL`), stored as a `PetscScalar` array (column-major)
1371: - T - the `T` matrix (can be `NULL`), stored as a `PetscScalar` array (column-major)
1373: Output Parameter:
1374: . kaij - the new `MATKAIJ` matrix
1376: Level: advanced
1378: Notes:
1379: The created matrix is of the following form\:
1380: .vb
1381: [I \otimes S + A \otimes T]
1382: .ve
1383: where
1384: .vb
1385: S is a dense (p \times q) matrix
1386: T is a dense (p \times q) matrix
1387: A is a `MATAIJ` (n \times n) matrix
1388: I is the identity matrix
1389: .ve
1390: The resulting matrix is (np \times nq)
1392: `S` and `T` are always stored independently on all processes as `PetscScalar` arrays in
1393: column-major format.
1395: This function increases the reference count on the `MATAIJ` matrix, so the user is free to destroy the matrix if it is not needed.
1397: Changes to the entries of the `MATAIJ` matrix will immediately affect the `MATKAIJ` matrix.
1399: Developer Notes:
1400: In the `MATMPIKAIJ` case, the internal 'AIJ' and 'OAIJ' sequential KAIJ matrices are kept up to date by tracking the object state
1401: of the AIJ matrix 'A' that describes the blockwise action of the `MATMPIKAIJ` matrix and, if the object state has changed, lazily
1402: rebuilding 'AIJ' and 'OAIJ' just before executing operations with the `MATMPIKAIJ` matrix. If new types of operations are added,
1403: routines implementing those must also ensure these are rebuilt when needed (by calling the internal MatKAIJ_build_AIJ_OAIJ() routine).
1405: .seealso: [](ch_matrices), `Mat`, `MatKAIJSetAIJ()`, `MatKAIJSetS()`, `MatKAIJSetT()`, `MatKAIJGetAIJ()`, `MatKAIJGetS()`, `MatKAIJGetT()`, `MATKAIJ`
1406: @*/
1407: PetscErrorCode MatCreateKAIJ(Mat A, PetscInt p, PetscInt q, const PetscScalar S[], const PetscScalar T[], Mat *kaij)
1408: {
1409: PetscFunctionBegin;
1410: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), kaij));
1411: PetscCall(MatSetType(*kaij, MATKAIJ));
1412: PetscCall(MatKAIJSetAIJ(*kaij, A));
1413: PetscCall(MatKAIJSetS(*kaij, p, q, S));
1414: PetscCall(MatKAIJSetT(*kaij, p, q, T));
1415: PetscCall(MatSetUp(*kaij));
1416: PetscFunctionReturn(PETSC_SUCCESS);
1417: }
1419: /*MC
1420: MATKAIJ - MATKAIJ = "kaij" - A matrix type to be used to evaluate matrices of form
1421: [I \otimes S + A \otimes T],
1422: where
1423: .vb
1424: S is a dense (p \times q) matrix,
1425: T is a dense (p \times q) matrix,
1426: A is an AIJ (n \times n) matrix,
1427: and I is the identity matrix.
1428: .ve
1429: The resulting matrix is (np \times nq).
1431: S and T are always stored independently on all processes as `PetscScalar` arrays in column-major format.
1433: Level: advanced
1435: Note:
1436: A linear system with multiple right-hand sides, AX = B, can be expressed in the KAIJ-friendly form of (A \otimes I) x = b,
1437: where x and b are column vectors containing the row-major representations of X and B.
1439: .seealso: [](ch_matrices), `Mat`, `MatKAIJSetAIJ()`, `MatKAIJSetS()`, `MatKAIJSetT()`, `MatKAIJGetAIJ()`, `MatKAIJGetS()`, `MatKAIJGetT()`, `MatCreateKAIJ()`
1440: M*/
1442: PETSC_EXTERN PetscErrorCode MatCreate_KAIJ(Mat A)
1443: {
1444: Mat_MPIKAIJ *b;
1445: PetscMPIInt size;
1447: PetscFunctionBegin;
1448: PetscCall(PetscNew(&b));
1449: A->data = (void *)b;
1451: PetscCall(PetscMemzero(A->ops, sizeof(struct _MatOps)));
1453: b->w = NULL;
1454: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
1455: if (size == 1) {
1456: PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATSEQKAIJ));
1457: A->ops->destroy = MatDestroy_SeqKAIJ;
1458: A->ops->mult = MatMult_SeqKAIJ;
1459: A->ops->multadd = MatMultAdd_SeqKAIJ;
1460: A->ops->invertblockdiagonal = MatInvertBlockDiagonal_SeqKAIJ;
1461: A->ops->getrow = MatGetRow_SeqKAIJ;
1462: A->ops->restorerow = MatRestoreRow_SeqKAIJ;
1463: A->ops->sor = MatSOR_SeqKAIJ;
1464: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqkaij_seqaij_C", MatConvert_KAIJ_AIJ));
1465: } else {
1466: PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATMPIKAIJ));
1467: A->ops->destroy = MatDestroy_MPIKAIJ;
1468: A->ops->mult = MatMult_MPIKAIJ;
1469: A->ops->multadd = MatMultAdd_MPIKAIJ;
1470: A->ops->invertblockdiagonal = MatInvertBlockDiagonal_MPIKAIJ;
1471: A->ops->getrow = MatGetRow_MPIKAIJ;
1472: A->ops->restorerow = MatRestoreRow_MPIKAIJ;
1473: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatGetDiagonalBlock_C", MatGetDiagonalBlock_MPIKAIJ));
1474: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_mpikaij_mpiaij_C", MatConvert_KAIJ_AIJ));
1475: }
1476: A->ops->setup = MatSetUp_KAIJ;
1477: A->ops->view = MatView_KAIJ;
1478: A->ops->createsubmatrix = MatCreateSubMatrix_KAIJ;
1479: PetscFunctionReturn(PETSC_SUCCESS);
1480: }