Actual source code: ex27.c
1: static char help[] = "Tests VecSetInf().\n\n";
3: #include <petscvec.h>
5: static PetscErrorCode TestSetInf(Vec v)
6: {
7: PetscScalar *array;
8: PetscInt n;
10: PetscFunctionBegin;
11: // Zero the entries first this ensures a known initial state
12: PetscCall(VecGetLocalSize(v, &n));
13: PetscCall(VecGetArrayWrite(v, &array));
14: PetscCall(PetscArrayzero(array, n));
15: PetscCall(VecRestoreArrayWrite(v, &array));
17: PetscCall(VecSetInf(v));
18: // Check that it works to begin with
19: PetscCall(VecGetLocalSize(v, &n));
20: PetscCall(VecGetArrayRead(v, (const PetscScalar **)&array));
21: for (PetscInt i = 0; i < n; ++i) {
22: const PetscScalar x = array[i];
24: PetscCheck(PetscIsInfOrNanScalar(x), PETSC_COMM_SELF, PETSC_ERR_PLIB, "array[%" PetscInt_FMT "] %g + %gi != infinity", i, (double)PetscRealPart(x), (double)PetscImaginaryPart(x));
25: }
26: PetscCall(VecRestoreArrayRead(v, (const PetscScalar **)&array));
27: PetscFunctionReturn(PETSC_SUCCESS);
28: }
30: int main(int argc, char **argv)
31: {
32: Vec v;
34: PetscFunctionBeginUser;
35: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
37: PetscCall(VecCreate(PETSC_COMM_WORLD, &v));
38: PetscCall(VecSetSizes(v, PETSC_DECIDE, 10));
39: PetscCall(VecSetFromOptions(v));
41: // Perform the test possibly calling v->ops->set
42: PetscCall(TestSetInf(v));
43: // Delete the function pointer to the implementation and do it again. This should now use the
44: // "default" version
45: PetscCall(VecSetOperation(v, VECOP_SET, NULL));
46: PetscCall(TestSetInf(v));
48: PetscCall(VecDestroy(&v));
49: PetscCall(PetscFinalize());
50: return 0;
51: }
53: /*TEST
55: testset:
56: output_file: ./output/empty.out
57: nsize: {{1 2}}
58: test:
59: suffix: standard
60: test:
61: requires: defined(PETSC_USE_SHARED_MEMORY)
62: args: -vec_type shared
63: suffix: shared
64: test:
65: requires: viennacl
66: args: -vec_type viennacl
67: suffix: viennacl
68: test:
69: requires: kokkos_kernels
70: args: -vec_type kokkos
71: suffix: kokkos
72: test:
73: requires: cuda
74: args: -vec_type cuda
75: suffix: cuda
76: test:
77: requires: hip
78: args: -vec_type hip
79: suffix: hip
81: TEST*/