Actual source code: benchmark_veccreate.c

  1: static char help[] = "Benchmark VecCreate() for GPU vectors.\n\
  2:   -n <length> : vector length\n\n";

  4: #include <petscvec.h>
  5: #include <petsctime.h>
  6: #include <petscdevice_cuda.h>

  8: int main(int argc, char **argv)
  9: {
 10:   PetscInt       i, n = 5, iter = 10;
 11:   Vec            x;
 12:   PetscLogDouble v0, v1;
 13:   PetscMemType   memtype;
 14:   PetscScalar   *array;

 16:   PetscFunctionBeginUser;
 17:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 18:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 19:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-iter", &iter, NULL));

 21:   for (i = 0; i < iter; i++) {
 22:     PetscCall(PetscTime(&v0));
 23:     PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
 24:     PetscCall(VecSetSizes(x, PETSC_DECIDE, n));
 25:     PetscCall(VecSetFromOptions(x));
 26:     /* make sure the vector's array exists */
 27:     PetscCall(VecGetArrayAndMemType(x, &array, &memtype));
 28:     PetscCall(VecRestoreArrayAndMemType(x, &array));
 29:     PetscCallCUDA(WaitForCUDA());
 30:     PetscCall(PetscTime(&v1));
 31:     PetscCall(VecDestroy(&x));
 32:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Iteration %" PetscInt_FMT ": Time= %g\n", i, (double)(v1 - v0)));
 33:   }
 34:   PetscCall(PetscFinalize());
 35:   return 0;
 36: }
 37: /*TEST
 38:   build:
 39:       requires: cuda
 40:   test:
 41:       args: -vec_type cuda
 42: TEST*/