xref: /petsc/src/mat/tests/ex52.c (revision d5b43468fb8780a8feea140ccd6fa3e6a50411cc)
1 
2 static char help[] = "Tests various routines in MatMPIBAIJ format.\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc, char **args)
7 {
8   Mat         A;
9   PetscInt    m = 2, bs = 1, M, row, col, start, end, i, j, k;
10   PetscMPIInt rank, size;
11   PetscScalar data = 100;
12   PetscBool   flg;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
16   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
17   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
18 
19   /* Test MatSetValues() and MatGetValues() */
20   PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_block_size", &bs, NULL));
21   PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_size", &m, NULL));
22 
23   M = m * bs * size;
24   PetscCall(MatCreateBAIJ(PETSC_COMM_WORLD, bs, PETSC_DECIDE, PETSC_DECIDE, M, M, PETSC_DECIDE, NULL, PETSC_DECIDE, NULL, &A));
25 
26   PetscCall(MatGetOwnershipRange(A, &start, &end));
27   PetscCall(PetscOptionsHasName(NULL, NULL, "-column_oriented", &flg));
28   if (flg) PetscCall(MatSetOption(A, MAT_ROW_ORIENTED, PETSC_FALSE));
29 
30   /* inproc assembly */
31   for (row = start; row < end; row++) {
32     for (col = start; col < end; col++, data += 1) PetscCall(MatSetValues(A, 1, &row, 1, &col, &data, INSERT_VALUES));
33   }
34   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
35   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
36 
37   /* offproc assembly */
38   data = 5.0;
39   row  = (M + start - 1) % M;
40   for (col = 0; col < M; col++) PetscCall(MatSetValues(A, 1, &row, 1, &col, &data, ADD_VALUES));
41   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
42   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
43 
44   /* Test MatSetValuesBlocked() */
45   PetscCall(PetscOptionsHasName(NULL, NULL, "-test_setvaluesblocked", &flg));
46   if (flg) {
47     PetscScalar *bval;
48     row /= bs;
49     col = start / bs;
50     PetscCall(PetscMalloc1(bs * bs, &bval));
51     k = 1;
52     /* row oriented - default */
53     for (i = 0; i < bs; i++) {
54       for (j = 0; j < bs; j++) {
55         bval[i * bs + j] = (PetscScalar)k;
56         k++;
57       }
58     }
59     PetscCall(MatSetValuesBlocked(A, 1, &row, 1, &col, bval, INSERT_VALUES));
60     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
61     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
62     PetscCall(PetscFree(bval));
63   }
64 
65   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
66   PetscCall(MatDestroy(&A));
67   PetscCall(PetscFinalize());
68   return 0;
69 }
70 
71 /*TEST
72 
73    test:
74       suffix: 1
75       nsize: 3
76       args: -mat_block_size 2 -test_setvaluesblocked
77 
78    test:
79       suffix: 2
80       nsize: 3
81       args: -mat_block_size 2 -test_setvaluesblocked -column_oriented
82 
83    test:
84       suffix: 3
85       nsize: 3
86       args: -mat_block_size 1 -test_setvaluesblocked
87 
88    test:
89       suffix: 4
90       nsize: 3
91       args: -mat_block_size 1 -test_setvaluesblocked -column_oriented
92 
93 TEST*/
94