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:   PetscInt    i;
 12:   PetscScalar array[LEN];
 13:   Vec         x, y, z;
 14:   PetscReal   nrm, ans;

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

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

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

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

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

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

 49: /*TEST

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

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

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

 98: TEST*/