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