Actual source code: ex31.c

  1: static const char help[] = "Tests VecMaxPointwiseDivide()\n\n";

  3: #include <petscvec.h>

  5: int main(int argc, char **argv)
  6: {
  7:   Vec          x, y;
  8:   PetscScalar *x_array;
  9:   PetscInt     n, N, start;
 10:   PetscReal    max, expected;

 12:   PetscFunctionBeginUser;
 13:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

 15:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
 16:   PetscCall(VecSetSizes(x, PETSC_DECIDE, 10));
 17:   PetscCall(VecSetFromOptions(x));

 19:   PetscCall(VecGetOwnershipRange(x, &start, NULL));
 20:   PetscCall(VecGetLocalSize(x, &n));
 21:   PetscCall(VecGetSize(x, &N));
 22:   PetscCall(VecGetArrayWrite(x, &x_array));
 23:   for (PetscInt i = 0; i < n; ++i) x_array[i] = (PetscScalar)(start + i + 1);
 24:   PetscCall(VecRestoreArrayWrite(x, &x_array));
 25:   expected = (PetscReal)N;

 27:   PetscCall(VecDuplicate(x, &y));

 29:   // check that it works at all
 30:   PetscCall(VecSet(y, 1.0));
 31:   PetscCall(VecMaxPointwiseDivide(x, y, &max));
 32:   PetscCheck(PetscIsCloseAtTol(max, expected, 1e-12, 0.0), PETSC_COMM_SELF, PETSC_ERR_PLIB, "VecMaxPointwiseDivide() returned %g != expected %g for y = 1.0", (double)max, (double)expected);

 34:   // check that it takes the absolute value
 35:   PetscCall(VecSet(y, -1.0));
 36:   PetscCall(VecMaxPointwiseDivide(x, y, &max));
 37:   PetscCheck(PetscIsCloseAtTol(max, expected, 1e-12, 0.0), PETSC_COMM_SELF, PETSC_ERR_PLIB, "VecMaxPointwiseDivide() returned %g != expected %g for y = -1.0", (double)max, (double)expected);

 39:   // check that it ignores zero entries in y (treats them as 1.0)
 40:   PetscCall(VecZeroEntries(y));
 41:   PetscCall(VecMaxPointwiseDivide(x, y, &max));
 42:   PetscCheck(PetscIsCloseAtTol(max, expected, 1e-12, 0.0), PETSC_COMM_SELF, PETSC_ERR_PLIB, "VecMaxPointwiseDivide() returned %g != expected %g for y = 0.0", (double)max, (double)expected);

 44:   PetscCall(VecDestroy(&y));
 45:   PetscCall(VecDestroy(&x));
 46:   PetscCall(PetscFinalize());
 47:   return 0;
 48: }

 50: /*TEST

 52:   testset:
 53:     output_file: ./output/empty.out
 54:     nsize: {{1 2}}
 55:     test:
 56:       suffix: standard
 57:     test:
 58:       requires: defined(PETSC_USE_SHARED_MEMORY)
 59:       args: -vec_type shared
 60:       suffix: shared
 61:     test:
 62:       requires: viennacl
 63:       args: -vec_type viennacl
 64:       suffix: viennacl
 65:     test:
 66:       requires: kokkos_kernels
 67:       args: -vec_type kokkos
 68:       suffix: kokkos
 69:     test:
 70:       requires: cuda
 71:       args: -vec_type cuda
 72:       suffix: cuda
 73:     test:
 74:       requires: hip
 75:       args: -vec_type hip
 76:       suffix: hip

 78: TEST*/