Created
October 28, 2025 03:33
-
-
Save zorchp/a62c69c50a47c63b12e1330c834473ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <cuda_runtime.h> | |
| #define CUDA_CHECK(call) \ | |
| do { \ | |
| cudaError_t err = call; \ | |
| if (err != cudaSuccess) { \ | |
| fprintf(stderr, "CUDA error %s:%d: %s\n", __FILE__, __LINE__, \ | |
| cudaGetErrorString(err)); \ | |
| exit(1); \ | |
| } \ | |
| } while (0) | |
| class TimeCostCuda { | |
| public: | |
| TimeCostCuda() { | |
| CUDA_CHECK(cudaEventCreate(&_start)); | |
| CUDA_CHECK(cudaEventCreate(&_stop)); | |
| CUDA_CHECK(cudaEventRecord(_start, 0)); | |
| } | |
| ~TimeCostCuda() { | |
| CUDA_CHECK(cudaEventDestroy(_start)); | |
| CUDA_CHECK(cudaEventDestroy(_stop)); | |
| } | |
| double get_time_cost(bool is_reset = false) { | |
| CUDA_CHECK(cudaEventRecord(_stop, 0)); | |
| CUDA_CHECK(cudaEventSynchronize(_stop)); | |
| float elapsedTime; | |
| CUDA_CHECK(cudaEventElapsedTime(&elapsedTime, _start, _stop)); | |
| if (is_reset) { | |
| CUDA_CHECK(cudaEventRecord(_start, 0)); | |
| } | |
| return elapsedTime; | |
| } | |
| private: | |
| cudaEvent_t _start, _stop; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment