Actual source code: ex301.c
1: static char help[] = "Tests for bugs in A->offloadmask consistency for GPU matrices\n\n";
3: #include <petscmat.h>
5: int main(int argc, char **args)
6: {
7: Mat A;
8: PetscInt i, j, rstart, rend, m = 3;
9: PetscScalar one = 1.0, zero = 0.0, negativeone = -1.0;
10: PetscReal norm;
11: PetscBool check_assemblies = PETSC_FALSE;
12: Vec x, y;
14: PetscFunctionBeginUser;
15: PetscCall(PetscInitialize(&argc, &args, NULL, help));
16: PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
17: PetscCall(PetscOptionsGetBool(NULL, NULL, "-check_assemblies", &check_assemblies, NULL));
18: if (check_assemblies) PetscCall(PetscLogDefaultBegin());
20: for (i = 0; i < 2; i++) {
21: /* Create the matrix and set it to contain explicit zero entries on the diagonal. */
22: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
23: PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, m * m, m * m));
24: PetscCall(MatSetFromOptions(A));
25: PetscCall(MatSetUp(A));
26: PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
27: PetscCall(MatCreateVecs(A, &x, &y));
28: PetscCall(VecSet(x, one));
29: PetscCall(VecSet(y, zero));
30: PetscCall(MatDiagonalSet(A, y, INSERT_VALUES));
32: /* Now set A to be the identity using various approaches.
33: * Note that there may be other approaches that should be added here. */
34: switch (i) {
35: case 0:
36: PetscCall(MatDiagonalSet(A, x, INSERT_VALUES));
37: break;
38: case 1:
39: for (j = rstart; j < rend; j++) PetscCall(MatSetValue(A, j, j, one, INSERT_VALUES));
40: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
41: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
42: break;
43: case 2:
44: for (j = rstart; j < rend; j++) PetscCall(MatSetValuesRow(A, j, &one));
45: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
46: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
47: default:
48: break;
49: }
51: /* Compute y <- A*x and verify that the difference between y and x is negligible, as it should be since A is the identity. */
52: PetscCall(MatMult(A, x, y));
53: PetscCall(VecAXPY(y, negativeone, x));
54: PetscCall(VecNorm(y, NORM_2, &norm));
55: if (norm > PETSC_SQRT_MACHINE_EPSILON) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Test %" PetscInt_FMT ": Norm of error is %g, but should be near 0.\n", i, (double)norm));
57: /* C = A*A is the identity, built on the device by some GPU types. It has a full diagonal, so MatDiagonalSet()
58: and MatShift() must update it in place on every rank, including one with no rows when m is 1 */
59: if (check_assemblies) {
60: Mat C;
61: PetscLogEvent assembly;
62: PetscEventPerfInfo before, after;
64: PetscCall(MatMatMult(A, A, MAT_INITIAL_MATRIX, PETSC_DETERMINE, &C));
65: PetscCall(PetscLogEventGetId("MatAssemblyEnd", &assembly));
66: PetscCall(PetscLogEventGetPerfInfo(PETSC_DETERMINE, assembly, &before));
67: PetscCall(MatDiagonalSet(C, x, ADD_VALUES));
68: PetscCall(MatShift(C, negativeone));
69: PetscCall(PetscLogEventGetPerfInfo(PETSC_DETERMINE, assembly, &after));
70: PetscCheck(after.count == before.count, PETSC_COMM_SELF, PETSC_ERR_PLIB, "%d unexpected matrix assemblies in MatDiagonalSet() and MatShift()", after.count - before.count);
71: PetscCall(MatMult(C, x, y));
72: PetscCall(VecAXPY(y, negativeone, x));
73: PetscCall(VecNorm(y, NORM_2, &norm));
74: PetscCheck(norm <= PETSC_SQRT_MACHINE_EPSILON, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "MatDiagonalSet() and MatShift() give the wrong diagonal");
75: PetscCall(MatDestroy(&C));
76: }
78: PetscCall(MatDestroy(&A));
79: PetscCall(VecDestroy(&x));
80: PetscCall(VecDestroy(&y));
81: }
83: PetscCall(PetscFinalize());
84: return 0;
85: }
87: /*TEST
88: testset:
89: output_file: output/empty.out
91: test:
92: suffix: aijviennacl_1
93: nsize: {{1 2}}
94: args: -mat_type aijviennacl
95: requires: viennacl
97: test:
98: suffix: aijcusparse_1
99: nsize: {{1 2}}
100: args: -mat_type aijcusparse
101: requires: cuda
103: test:
104: suffix: kokkos
105: nsize: {{1 2}}
106: args: -mat_type aijkokkos
107: requires: kokkos_kernels
109: test:
110: suffix: kokkos_empty_rank
111: nsize: 2
112: args: -mat_type aijkokkos -m 1 -check_assemblies
113: requires: kokkos_kernels defined(PETSC_USE_LOG)
115: TEST*/