Actual source code: ex1k.kokkos.cxx

  1: static char help[] = "Benchmarking device kernel launch time\n";
  2: /*
  3:   Running example on Summit at OLCF:
  4:   # run with total 1 resource set (RS) (-n1), 1 RS per node (-r1), 1 MPI process (-a1), 7 cores (-c7) and 1 GPU (-g1) per RS
  5:   $ jsrun -n1 -a1 -c7 -g1 -r1  ./ex1k
  6:   Average asynchronous device kernel launch time = 4.86 microseconds
  7:   Average synchronous device kernel launch time  = 12.83 microseconds

  9:   Frontier@OLCF
 10:   $ srun -n1 -c32 --cpu-bind=threads --gpus-per-node=8 --gpu-bind=closest ./ex1k
 11:   Average asynchronous device kernel launch time = 1.88 microseconds
 12:   Average synchronous device kernel launch time  = 7.78 microseconds

 14:   Aurora@ALCF
 15:   $ mpiexec -n 1 ./ex1k
 16:   Average asynchronous device kernel launch time = 3.34 microseconds
 17:   Average synchronous device kernel launch time  = 6.24 microseconds

 19:   Perlmutter@NERSC
 20:   $ srun -n 1 --gpus-per-task=1 ./ex1k
 21:   Average asynchronous device kernel launch time = 2.31 microseconds
 22:   Average synchronous device kernel launch time  = 7.13 microseconds
 23: */

 25: #include <petsc_kokkos.hpp>
 26: #include <petscdevice.h>

 28: int main(int argc, char **argv)
 29: {
 30:   PetscInt           i, n = 100000, N = 256;
 31:   PetscLogDouble     tstart, tend, time;
 32:   PetscDeviceContext saved = nullptr, work = nullptr;
 33:   PetscBool          reset = PETSC_FALSE;

 35:   PetscFunctionBeginUser;
 36:   PetscCall(PetscInitialize(&argc, &argv, nullptr, help));
 37:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 38:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_stream_reset", &reset, NULL));
 39:   if (reset) {
 40:     PetscCall(PetscDeviceContextGetCurrentContext(&saved));
 41:     PetscCall(PetscDeviceContextDuplicate(saved, &work));
 42:     PetscCall(PetscDeviceContextSetStreamType(work, PETSC_STREAM_NONBLOCKING));
 43:     PetscCall(PetscDeviceContextSetUp(work));
 44:     PetscCall(PetscDeviceContextSetCurrentContext(work));
 45:     // Reconfigure the selected context before Kokkos captures its stream.
 46:     PetscCall(PetscDeviceContextSetStreamType(work, PETSC_STREAM_DEFAULT));
 47:     PetscCall(PetscDeviceContextSetUp(work));
 48:   }
 49:   PetscCall(PetscKokkosInitializeCheck());
 50:   {
 51:     Kokkos::DefaultExecutionSpace                      exec = PetscGetKokkosExecutionSpace();
 52:     Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace> policy(exec, 0, N);

 54:     if (reset) {
 55: #if defined(KOKKOS_ENABLE_CUDA)
 56:       cudaStream_t *stream;

 58:       PetscCall(PetscDeviceContextGetStreamHandle(work, (void **)&stream));
 59:       PetscCheck(exec.cuda_stream() == *stream, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Kokkos must use the reconfigured context's stream");
 60: #elif defined(KOKKOS_ENABLE_HIP)
 61:       hipStream_t *stream;

 63:       PetscCall(PetscDeviceContextGetStreamHandle(work, (void **)&stream));
 64:       PetscCheck(exec.hip_stream() == *stream, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Kokkos must use the reconfigured context's stream");
 65: #endif
 66:     }
 67:     PetscCallCXX(exec.fence()); // Initialize device runtime to get more accurate timing below
 68:     // Launch a sequence of kernels asynchronously. Previous launched kernels do not need to be completed before launching a new one
 69:     PetscCall(PetscTime(&tstart));
 70:     for (i = 0; i < n; i++) PetscCallCXX(Kokkos::parallel_for(policy, KOKKOS_LAMBDA(const PetscInt &i){}));
 71:     PetscCall(PetscTime(&tend));
 72:     PetscCallCXX(exec.fence());
 73:     time = (tend - tstart) * 1e6 / n;
 74:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Average asynchronous device kernel launch time = %.2f microseconds\n", time));

 76:     // Launch a sequence of kernels synchronously. Only launch a new kernel after the one before it has been completed
 77:     PetscCall(PetscTime(&tstart));
 78:     for (i = 0; i < n; i++) {
 79:       PetscCallCXX(Kokkos::parallel_for(policy, KOKKOS_LAMBDA(const PetscInt &i){}));
 80:       PetscCallCXX(exec.fence());
 81:     }
 82:     PetscCall(PetscTime(&tend));
 83:     time = (tend - tstart) * 1e6 / n;
 84:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Average synchronous device kernel launch time  = %.2f microseconds\n", time));
 85:   }

 87:   if (reset) {
 88:     PetscCall(PetscDeviceContextSetCurrentContext(saved));
 89:     PetscCall(PetscDeviceContextDestroy(&work));
 90:   }
 91:   PetscCall(PetscFinalize());
 92:   return 0;
 93: }

 95: /*TEST
 96:   test:
 97:     requires: kokkos
 98:     args: -n 2
 99:     output_file: output/empty.out
100:     filter: grep "DOES_NOT_EXIST"

102:   test:
103:     suffix: cuda_stream_reset
104:     requires: kokkos cuda
105:     args: -n 2 -device_enable lazy -test_stream_reset
106:     output_file: output/empty.out
107:     filter: grep "DOES_NOT_EXIST"

109:   test:
110:     suffix: hip_stream_reset
111:     requires: kokkos hip
112:     args: -n 2 -device_enable lazy -test_stream_reset
113:     output_file: output/empty.out
114:     filter: grep "DOES_NOT_EXIST"

116: TEST*/