xref: /petsc/src/mat/tests/ex238.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 static char help[] = "Creates MatSeqBAIJ matrix of given BS for timing tests of MatMult().\n";
2 
3 #include <petscmat.h>
4 
5 int main(int argc, char **args) {
6   Mat         A;
7   Vec         x, y;
8   PetscInt    m = 50000, bs = 12, i, j, k, l, row, col, M, its = 25;
9   PetscScalar rval, *vals;
10   PetscRandom rdm;
11 
12   PetscFunctionBeginUser;
13   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
14   PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_block_size", &bs, NULL));
15   PetscCall(PetscOptionsGetInt(NULL, NULL, "-its", &its, NULL));
16   PetscCall(PetscOptionsGetInt(NULL, NULL, "-mat_size", &m, NULL));
17   M = m * bs;
18   PetscCall(MatCreateSeqBAIJ(PETSC_COMM_SELF, bs, M, M, 27, NULL, &A));
19   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
20 
21   PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &rdm));
22   PetscCall(PetscRandomSetFromOptions(rdm));
23   PetscCall(VecCreateSeq(PETSC_COMM_SELF, M, &x));
24   PetscCall(VecDuplicate(x, &y));
25 
26   /* For each block row insert at most 27 blocks */
27   PetscCall(PetscMalloc1(bs * bs, &vals));
28   for (i = 0; i < m; i++) {
29     row = i;
30     for (j = 0; j < 27; j++) {
31       PetscCall(PetscRandomGetValue(rdm, &rval));
32       col = (PetscInt)(PetscRealPart(rval) * m);
33       for (k = 0; k < bs; k++) {
34         for (l = 0; l < bs; l++) {
35           PetscCall(PetscRandomGetValue(rdm, &rval));
36           vals[k * bs + l] = rval;
37         }
38       }
39       PetscCall(MatSetValuesBlocked(A, 1, &row, 1, &col, vals, INSERT_VALUES));
40     }
41   }
42   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
43   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
44   PetscCall(PetscFree(vals));
45 
46   /* Time MatMult(), MatMultAdd() */
47   for (i = 0; i < its; i++) {
48     PetscCall(VecSetRandom(x, rdm));
49     PetscCall(MatMult(A, x, y));
50     PetscCall(VecSetRandom(x, rdm));
51     PetscCall(VecSetRandom(y, rdm));
52     PetscCall(MatMultAdd(A, x, y, y));
53   }
54 
55   PetscCall(MatDestroy(&A));
56   PetscCall(VecDestroy(&x));
57   PetscCall(VecDestroy(&y));
58   PetscCall(PetscRandomDestroy(&rdm));
59   PetscCall(PetscFinalize());
60   return 0;
61 }
62 
63 /*TEST
64 
65    testset:
66      requires: defined(PETSC_USING_64BIT_PTR)
67      output_file: output/ex238_1.out
68      test:
69        suffix: 1
70        args: -mat_block_size 1 -mat_size 1000 -its 2
71      test:
72        suffix: 2
73        args: -mat_block_size 2 -mat_size 1000 -its 2
74      test:
75        suffix: 4
76        args: -mat_block_size 4 -mat_size 1000 -its 2
77      test:
78        suffix: 5
79        args: -mat_block_size 5 -mat_size 1000 -its 2
80      test:
81        suffix: 6
82        args: -mat_block_size 6 -mat_size 1000 -its 2
83      test:
84        suffix: 8
85        args: -mat_block_size 8 -mat_size 1000 -its 2
86      test:
87        suffix: 12
88        args: -mat_block_size 12 -mat_size 1000 -its 2
89      test:
90        suffix: 15
91        args: -mat_block_size 15 -mat_size 1000 -its 2
92 
93 TEST*/
94