// SPDX-FileCopyrightText: Copyright (c) 2008-2009, NVIDIA Corporation. All rights reserved. // SPDX-License-Identifier: Apache-2.0 #pragma once // A simple timer class #ifdef __CUDACC__ // use CUDA's high-resolution timers when possible # include # include # include # include void cuda_safe_call(cudaError_t error, const std::string& message = "") { if (error) { throw thrust::system_error(error, thrust::cuda_category(), message); } } struct timer { cudaEvent_t start; cudaEvent_t end; timer() { cuda_safe_call(cudaEventCreate(&start)); cuda_safe_call(cudaEventCreate(&end)); restart(); } ~timer() { static_cast(cudaEventDestroy(start)); static_cast(cudaEventDestroy(end)); } void restart() { cuda_safe_call(cudaEventRecord(start, nullptr)); } double elapsed() { cuda_safe_call(cudaEventRecord(end, nullptr)); cuda_safe_call(cudaEventSynchronize(end)); float ms_elapsed; cuda_safe_call(cudaEventElapsedTime(&ms_elapsed, start, end)); return ms_elapsed / 1e3; } double epsilon() { return 0.5e-6; } }; #else // fallback to clock() # include struct timer { clock_t start; clock_t end; timer() { restart(); } ~timer() = default; void restart() { start = clock(); } double elapsed() { end = clock(); return static_cast(end - start) / static_cast(CLOCKS_PER_SEC); } double epsilon() { return 1.0 / static_cast(CLOCKS_PER_SEC); } }; #endif