1 static char help[] = "Tests VecSetInf().\n\n";
2
3 #include <petscvec.h>
4
TestSetInf(Vec v)5 static PetscErrorCode TestSetInf(Vec v)
6 {
7 PetscScalar *array;
8 PetscInt n;
9
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));
16
17 PetscCall(VecFlag(v, 1));
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];
23
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 }
29
main(int argc,char ** argv)30 int main(int argc, char **argv)
31 {
32 Vec v;
33
34 PetscFunctionBeginUser;
35 PetscCall(PetscInitialize(&argc, &argv, NULL, help));
36
37 PetscCall(VecCreate(PETSC_COMM_WORLD, &v));
38 PetscCall(VecSetSizes(v, PETSC_DECIDE, 10));
39 PetscCall(VecSetFromOptions(v));
40
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));
47
48 PetscCall(VecDestroy(&v));
49 PetscCall(PetscFinalize());
50 return 0;
51 }
52
53 /*TEST
54
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
80
81 TEST*/
82