Created
May 30, 2022 23:22
-
-
Save Hurleyworks/eb2dd81ef663d10e2d749d1151533ff9 to your computer and use it in GitHub Desktop.
GPU timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taken from https://github.com/shocker-0x15/OptiX_Utility | |
class Timer { | |
CUcontext m_context; | |
CUevent m_startEvent; | |
CUevent m_endEvent; | |
bool m_startIsValid; | |
bool m_endIsValid; | |
public: | |
void initialize(CUcontext context) { | |
m_context = context; | |
CUDADRV_CHECK(cuCtxSetCurrent(m_context)); | |
CUDADRV_CHECK(cuEventCreate(&m_startEvent, CU_EVENT_BLOCKING_SYNC)); | |
CUDADRV_CHECK(cuEventCreate(&m_endEvent, CU_EVENT_BLOCKING_SYNC)); | |
m_startIsValid = false; | |
m_endIsValid = false; | |
} | |
void finalize() { | |
m_startIsValid = false; | |
m_endIsValid = false; | |
CUDADRV_CHECK(cuCtxSetCurrent(m_context)); | |
CUDADRV_CHECK(cuEventDestroy(m_endEvent)); | |
CUDADRV_CHECK(cuEventDestroy(m_startEvent)); | |
m_context = nullptr; | |
} | |
void start(CUstream stream) { | |
CUDADRV_CHECK(cuEventRecord(m_startEvent, stream)); | |
m_startIsValid = true; | |
} | |
void stop(CUstream stream) { | |
CUDADRV_CHECK(cuEventRecord(m_endEvent, stream)); | |
m_endIsValid = true; | |
} | |
float report() { | |
float ret = 0.0f; | |
if (m_startIsValid && m_endIsValid) { | |
CUDADRV_CHECK(cuEventSynchronize(m_endEvent)); | |
CUDADRV_CHECK(cuEventElapsedTime(&ret, m_startEvent, m_endEvent)); | |
m_startIsValid = false; | |
m_endIsValid = false; | |
} | |
return ret; | |
} | |
}; | |
struct GPUTimer { | |
Timer frame; | |
Timer update; | |
Timer computePDFTexture; | |
Timer setupGBuffers; | |
Timer pathTrace; | |
Timer denoise; | |
void initialize(CUcontext context) { | |
frame.initialize(context); | |
update.initialize(context); | |
computePDFTexture.initialize(context); | |
setupGBuffers.initialize(context); | |
pathTrace.initialize(context); | |
denoise.initialize(context); | |
} | |
void finalize() { | |
denoise.finalize(); | |
pathTrace.finalize(); | |
setupGBuffers.finalize(); | |
computePDFTexture.finalize(); | |
update.finalize(); | |
frame.finalize(); | |
} | |
}; | |
// initialize | |
GPUTimer gpuTimer; | |
gpuTimers.initialize(gpuEnv.cuContext); | |
// use | |
gpuTimer.setupGBuffers.start(cuStream); | |
gpuEnv.gBuffer.optixPipeline.launch( cuStream, plpOnDevice, renderTargetSizeX, renderTargetSizeY, 1); | |
gpuTimer.setupGBuffers.stop(cuStream); | |
// cleanup | |
CUDADRV_CHECK(cuStreamSynchronize(cuStream)); | |
gpuTimer.finalize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment