Actual source code: ex62.c

  1: static char help[] = "Test Vector conversions.\n\n";

  3: #include <petscvec.h>

  5: #define LEN 32

  7: int main(int argc, char **argv)
  8: {
  9:   PetscMPIInt size;
 10:   PetscInt    n = LEN;
 11:   PetscScalar array[LEN];
 12:   Vec         x, y, z;
 13:   PetscReal   nrm, ans;

 15:   PetscFunctionBeginUser;
 16:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 17:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 19:   /* create x with an existing array */
 20:   for (PetscInt i = 0; i < n; i++) array[i] = 1.0;

 22:   if (size == 1) PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, n, array, &x));
 23:   else PetscCall(VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, n, PETSC_DECIDE, array, &x));
 24:   PetscCall(VecSetFromOptions(x));
 25:   PetscCall(VecScale(x, 5.0)); // x = {5,..}

 27:   PetscCall(VecCreate(PETSC_COMM_WORLD, &y));
 28:   PetscCall(VecSetFromOptions(y));
 29:   PetscCall(VecSetSizes(y, n, PETSC_DECIDE));
 30:   PetscCall(VecSet(y, 2.0)); // y = {2,..}

 32:   PetscCall(VecAXPY(x, -2.0, y)); // x += -2.0*y
 33:   PetscCall(VecNorm(x, NORM_2, &nrm));

 35:   PetscCall(VecCreate(PETSC_COMM_WORLD, &z));
 36:   PetscCall(VecSetType(z, VECSTANDARD));
 37:   PetscCall(VecSetSizes(z, n, PETSC_DECIDE));
 38:   PetscCall(VecSet(z, 1.0));
 39:   PetscCall(VecNorm(z, NORM_2, &ans));
 40:   PetscCheck(PetscAbs(nrm - ans) < PETSC_SMALL, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Diff is too big, %g", (double)nrm);
 41:   PetscCall(VecDestroy(&x));
 42:   PetscCall(VecDestroy(&y));
 43:   PetscCall(VecDestroy(&z));
 44:   PetscCall(PetscFinalize());
 45:   return 0;
 46: }

 48: /*TEST

 50:    testset:
 51:       nsize: 1
 52:       output_file: output/empty.out
 53:       test:
 54:         args: -vec_type {{seq mpi standard}}
 55:         suffix: standard

 57:       test:
 58:         requires: cuda
 59:         args: -vec_type {{seqcuda mpicuda cuda}}
 60:         suffix: cuda
 61:       test:
 62:         requires: hip
 63:         args: -vec_type {{seqhip mpihip hip}}
 64:         suffix: hip
 65:       test:
 66:         requires: viennacl
 67:         args: -vec_type {{seqviennacl mpiviennacl viennacl}}
 68:         suffix: viennacl
 69:       test:
 70:         requires: kokkos_kernels
 71:         args: -vec_type {{seqkokkos mpikokkos kokkos}}
 72:         suffix: kokkos

 74:    testset:
 75:       nsize: 2
 76:       output_file: output/empty.out
 77:       test:
 78:         args: -vec_type {{mpi standard}}
 79:         suffix: standard_2
 80:       test:
 81:         requires: cuda
 82:         args: -vec_type {{mpicuda cuda}}
 83:         suffix: cuda_2
 84:       test:
 85:         requires: hip
 86:         args: -vec_type {{mpihip hip}}
 87:         suffix: hip_2
 88:       test:
 89:         requires: viennacl
 90:         args: -vec_type {{mpiviennacl viennacl}}
 91:         suffix: viennacl_2
 92:       test:
 93:         requires: kokkos_kernels
 94:         args: -vec_type {{mpikokkos kokkos}}
 95:         suffix: kokkos_2

 97: TEST*/