Actual source code: ex2hip.hip.cpp

  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.10 microseconds
  7: */
  8: #include <petscsys.h>
  9: #include <petscdevice.h>

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

 19:   PetscInitialize(&argc,&argv,(char*)0,help);
 20:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

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

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

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

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

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

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

 57: TEST*/