Actual source code: ex54.c
1: static const char help[] = "Tests VecPointwiseMaxAbs()\n\n";
3: #include <petscvec.h>
5: static PetscErrorCode TestPointwiseMaxAbs(Vec result, Vec x, Vec y, Vec ref)
6: {
7: PetscInt n;
8: const PetscScalar *array, *ref_array;
10: PetscFunctionBegin;
11: PetscCall(VecPointwiseMaxAbs(result, x, y));
12: PetscCall(VecGetLocalSize(result, &n));
13: PetscCall(VecGetArrayRead(result, &array));
14: PetscCall(VecGetArrayRead(ref, &ref_array));
15: for (PetscInt i = 0; i < n; ++i) {
16: const PetscReal expected = PetscAbsScalar(ref_array[i]);
17: const PetscReal actual_real = PetscRealPart(array[i]), actual_imag = PetscImaginaryPart(array[i]);
19: PetscCheck(actual_imag == (PetscReal)0.0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "VecPointwiseMaxAbs() did not properly take absolute value, imaginary part %g != 0.0", (double)actual_imag);
20: PetscCheck(actual_real >= (PetscReal)0.0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "VecPointwiseMaxAbs() did not properly take absolute value, real part %g < 0.0", (double)actual_real);
21: PetscCheck(actual_real == expected, PETSC_COMM_SELF, PETSC_ERR_PLIB, "VecShift() returned array[%" PetscInt_FMT "] %g + %gi != expected_array[%" PetscInt_FMT "] %g + 0.0i", i, (double)actual_real, (double)actual_imag, i, (double)expected);
22: }
23: PetscCall(VecRestoreArrayRead(ref, &ref_array));
24: PetscCall(VecRestoreArrayRead(result, &array));
25: PetscFunctionReturn(PETSC_SUCCESS);
26: }
28: int main(int argc, char **argv)
29: {
30: Vec x, y, z;
32: PetscFunctionBeginUser;
33: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
35: PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
36: PetscCall(VecSetSizes(x, PETSC_DECIDE, 10));
37: PetscCall(VecSetFromOptions(x));
38: PetscCall(VecDuplicate(x, &y));
39: PetscCall(VecDuplicate(x, &z));
41: PetscCall(VecSet(y, 10.0));
43: // Basic correctness tests, z should always match abs(y) exactly
44: PetscCall(TestPointwiseMaxAbs(z, x, y, y));
45: PetscCall(VecSet(x, 1.0));
46: PetscCall(TestPointwiseMaxAbs(z, x, y, y));
47: PetscCall(VecSet(x, -1.0));
48: PetscCall(TestPointwiseMaxAbs(z, x, y, y));
49: PetscCall(VecSet(y, -10.0));
50: PetscCall(TestPointwiseMaxAbs(z, x, y, y));
52: // Test that it works if x and y are the same vector
53: PetscCall(VecSet(x, 0.0));
54: PetscCall(TestPointwiseMaxAbs(z, x, x, x));
55: PetscCall(VecSet(x, 1.0));
56: PetscCall(TestPointwiseMaxAbs(z, x, x, x));
57: PetscCall(VecSet(x, -1.0));
58: PetscCall(TestPointwiseMaxAbs(z, x, x, x));
60: // Test that it works if z is one of x or y
61: PetscCall(VecSet(z, 0.0));
62: PetscCall(VecSet(x, 0.0));
63: PetscCall(TestPointwiseMaxAbs(z, x, z, x));
64: PetscCall(VecSet(x, 1.0));
65: PetscCall(TestPointwiseMaxAbs(z, z, x, x));
66: PetscCall(VecSet(x, -10.0));
67: PetscCall(TestPointwiseMaxAbs(z, x, z, x));
69: // Test that it works if all vectors are the same
70: PetscCall(VecSet(z, 0.0));
71: PetscCall(TestPointwiseMaxAbs(z, z, z, z));
72: PetscCall(VecSet(z, 1.0));
73: PetscCall(TestPointwiseMaxAbs(z, z, z, z));
74: PetscCall(VecSet(z, -1.0));
75: PetscCall(TestPointwiseMaxAbs(z, z, z, z));
77: PetscCall(VecDestroy(&x));
78: PetscCall(VecDestroy(&y));
79: PetscCall(VecDestroy(&z));
80: PetscCall(PetscFinalize());
81: return 0;
82: }
84: /*TEST
86: testset:
87: output_file: output/empty.out
88: nsize: {{1 2}}
89: test:
90: suffix: standard
91: test:
92: requires: defined(PETSC_USE_SHARED_MEMORY)
93: args: -vec_type shared
94: suffix: shared
95: test:
96: requires: viennacl
97: args: -vec_type viennacl
98: suffix: viennacl
99: test:
100: requires: kokkos_kernels
101: args: -vec_type kokkos
102: suffix: kokkos
103: test:
104: requires: cuda
105: args: -vec_type cuda
106: suffix: cuda
107: test:
108: requires: hip
109: args: -vec_type hip
110: suffix: hip
112: TEST*/