Actual source code: lognvtx.c

  1: #include <petsc/private/logimpl.h>
  2: #include <petsc/private/loghandlerimpl.h>
  3: #include <petscdevice.h>
  4: #include <nvtx3/nvToolsExt.h>

  6: static PetscErrorCode PetscLogHandlerEventBegin_NVTX(PetscLogHandler handler, PetscLogEvent event, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
  7: {
  8:   PetscLogState     state;
  9:   PetscLogEventInfo info;

 11:   PetscFunctionBegin;
 12:   if (PetscDeviceInitialized(PETSC_DEVICE_CUDA)) {
 13:     PetscCall(PetscLogHandlerGetState(handler, &state));
 14:     PetscCall(PetscLogStateEventGetInfo(state, event, &info));
 15:     (void)nvtxRangePushA(info.name);
 16:   }
 17:   PetscFunctionReturn(PETSC_SUCCESS);
 18: }

 20: static PetscErrorCode PetscLogHandlerEventEnd_NVTX(PetscLogHandler handler, PetscLogEvent event, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
 21: {
 22:   PetscFunctionBegin;
 23:   if (PetscDeviceInitialized(PETSC_DEVICE_CUDA)) (void)nvtxRangePop();
 24:   PetscFunctionReturn(PETSC_SUCCESS);
 25: }

 27: /*MC
 28:   PETSCLOGHANDLERNVTX - PETSCLOGHANDLERNVTX = "nvtx" -  A `PetscLogHandler` that creates an NVTX range (which appears in NVIDIA Nsight profiling) for each PETSc event.

 30:   Options Database Key:
 31: . -log_nvtx (true|false) - start an `nvtx` log handler manually or prevent the `nvtx` log handler from starting automatically in `PetscInitialize()`
 32:                            in a program run within an `nsys` profiling session (see Note)

 34:   Level: developer

 36:   Note:
 37:   If `PetscInitialize()` detects the environment variable `NSYS_PROFILING_SESSION_ID` (which is defined by `nsys
 38:   profile`) or `NVPROF_ID` (which is defined by `nvprof`) an instance of this log handler will automatically be
 39:   started.

 41: .seealso: [](ch_profiling), `PetscLogHandler`
 42: M*/

 44: PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_NVTX(PetscLogHandler handler)
 45: {
 46:   PetscFunctionBegin;
 47:   handler->ops->eventbegin = PetscLogHandlerEventBegin_NVTX;
 48:   handler->ops->eventend   = PetscLogHandlerEventEnd_NVTX;
 49:   PetscCall(PetscInfo(handler, "nvtx log handler created\n"));
 50:   PetscFunctionReturn(PETSC_SUCCESS);
 51: }