Actual source code: ex2hip.hip.cxx

  1: static char help[] = "Benchmarking hipPointerGetAttributes() time\n";
  2: /*
  3:   Running example on Crusher at OLCF:
  4:     # run with 1 mpi rank (-n1), 32 CPUs (-c32), and map the process to CPU 0 and GPU 0
  5:   $ srun -n1 -c32 --cpu-bind=map_cpu:0 --gpus-per-node=8 --gpu-bind=map_gpu:0 ./ex2hip
  6:     Average hipPointerGetAttributes() time = 0.24 microseconds
  7: */
  8: #include <petscdevice_hip.h>

 10: int main(int argc, char **argv)
 11: {
 12:   PetscInt              i, n = 4000;
 13:   hipError_t            cerr;
 14:   PetscScalar         **ptrs;
 15:   PetscLogDouble        tstart, tend, time;
 16:   hipPointerAttribute_t attr;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscInitialize(&argc, &argv, nullptr, help));
 20:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 21:   PetscCallHIP(hipStreamSynchronize(NULL)); /* Initialize HIP runtime to get more accurate timing below */

 23:   PetscCall(PetscMalloc1(n, &ptrs));
 24:   for (i = 0; i < n; i++) {
 25:     if (i % 2) PetscCall(PetscMalloc1(i + 16, &ptrs[i]));
 26:     else PetscCallHIP(hipMalloc((void **)&ptrs[i], (i + 16) * sizeof(PetscScalar)));
 27:   }

 29:   PetscCall(PetscTime(&tstart));
 30:   for (i = 0; i < n; i++) {
 31:     cerr = hipPointerGetAttributes(&attr, ptrs[i]);
 32:     if (cerr) cerr = hipGetLastError();
 33:   }
 34:   PetscCall(PetscTime(&tend));
 35:   time = (tend - tstart) * 1e6 / n;

 37:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Average hipPointerGetAttributes() time = %.2f microseconds\n", time));

 39:   for (i = 0; i < n; i++) {
 40:     if (i % 2) PetscCall(PetscFree(ptrs[i]));
 41:     else PetscCallHIP(hipFree(ptrs[i]));
 42:   }
 43:   PetscCall(PetscFree(ptrs));
 44:   PetscCall(PetscFinalize());
 45:   return 0;
 46: }

 48: /*TEST
 49:   build:
 50:     requires: hip

 52:   test:
 53:     requires: hip
 54:     args: -n 2
 55:     output_file: output/empty.out
 56:     filter: grep "DOES_NOT_EXIST"

 58: TEST*/