| 50 | |
| 51 | |
| 52 | class GpuTimer: |
| 53 | def __init__(self) -> None: |
| 54 | self.events = [ |
| 55 | cuda.cuEventCreate(cuda.CUevent_flags.CU_EVENT_DEFAULT)[1], |
| 56 | cuda.cuEventCreate(cuda.CUevent_flags.CU_EVENT_DEFAULT)[1], |
| 57 | ] |
| 58 | |
| 59 | def start(self, stream=None): |
| 60 | if not stream: |
| 61 | stream = cuda.CUstream(0) |
| 62 | |
| 63 | (err,) = cuda.cuEventRecord(self.events[0], stream) |
| 64 | if err != cuda.CUresult.CUDA_SUCCESS: |
| 65 | raise RuntimeError(f"CUDA Error {str(err)}") |
| 66 | |
| 67 | def stop(self, stream=None): |
| 68 | if not stream: |
| 69 | stream = cuda.CUstream(0) |
| 70 | |
| 71 | (err,) = cuda.cuEventRecord(self.events[1], stream) |
| 72 | if err != cuda.CUresult.CUDA_SUCCESS: |
| 73 | raise RuntimeError(f"CUDA Error {str(err)}") |
| 74 | pass |
| 75 | |
| 76 | def stop_and_wait(self, stream=None): |
| 77 | if not stream: |
| 78 | stream = cuda.CUstream(0) |
| 79 | |
| 80 | self.stop(stream) |
| 81 | if stream: |
| 82 | (err,) = cuda.cuStreamSynchronize(stream) |
| 83 | if err != cuda.CUresult.CUDA_SUCCESS: |
| 84 | raise RuntimeError(f"CUDA Error {str(err)}") |
| 85 | else: |
| 86 | (err,) = cudart.cudaDeviceSynchronize() |
| 87 | if err != cuda.CUresult.CUDA_SUCCESS: |
| 88 | raise RuntimeError(f"CUDA Error {str(err)}") |
| 89 | |
| 90 | def duration(self, iterations=1): |
| 91 | err, duration = cuda.cuEventElapsedTime(self.events[0], self.events[1]) |
| 92 | if err != cuda.CUresult.CUDA_SUCCESS: |
| 93 | raise RuntimeError(f"CUDA Error {str(err)}") |
| 94 | return duration / float(iterations) |
| 95 | |
| 96 | |
| 97 | class CUDAEventProfiler: |