14#include <cuda_runtime.h>
35#define gpuErrchk(ans) { gpuAssert(ans, __FILE__, __LINE__); }
49inline void gpuAssert(cudaError_t code,
const char *file,
int line,
bool abort =
true)
51 if (code != cudaSuccess)
53 fprintf(stderr,
"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
79 return std::bit_width(n) - 1;
94 return std::bit_width(n - 1);
109 return std::numeric_limits<size_t>::digits - 1 - __clzll(n);
123 return std::numeric_limits<size_t>::digits - __clzll(n - 1);
140 cudaEventCreate(&
start);
141 cudaEventCreate(&
stop);
149 cudaEventDestroy(
start);
150 cudaEventDestroy(
stop);
158 cudaEventRecord(
start);
168 cudaEventRecord(
stop);
169 cudaEventSynchronize(
stop);
196 gpuErrchk(cudaMalloc(&d_arr, size *
sizeof(
T)));
200 cudaMalloc(&d_arr, size *
sizeof(
T));
223 gpuErrchk(cudaMemcpy(d_arr, arr, size *
sizeof(
T), cudaMemcpyHostToDevice));
227 cudaMemcpy(d_arr, arr, size *
sizeof(
T), cudaMemcpyHostToDevice);
361 T*
arr =
new T[size];
A simple CUDA timer class using CUDA events.
~CudaTimer()
Destroys the CUDA events.
cudaEvent_t stop
CUDA events to mark start and stop points.
float stop_timer()
Records the stop event, synchronizes, and calculates elapsed time.
float time
Stores the calculated elapsed time in milliseconds.
void start_timer()
Records the start event in the default CUDA stream.
CudaTimer()
Constructs the CudaTimer and creates CUDA events.
static constexpr bool CHECK_CUDA_ERRORS
Compile-time flag to enable/disable CUDA error checking via gpuErrchk/gpuAssert.
constexpr size_t get_num_threads()
Gets the total number of threads in the kernel launch (compile-time).
#define THREADS
Default number of threads per block in CUDA kernels.
#define gpuErrchk(ans)
Macro to check CUDA API calls for errors.
void synchronize_device()
Synchronizes the host thread with the default CUDA device stream.
__device__ __forceinline__ size_t device_fast_log2(size_t n)
Computes the floor of log base 2 for a device integer using count leading zeros.
T * memset_to_device(size_t size, int value)
Allocates device memory and sets it to a specific byte value.
__device__ __forceinline__ size_t device_fast_log2_ceil(size_t n)
Computes the ceiling of log base 2 for a device integer using count leading zeros.
T * memset_within_device(T *d_arr, size_t size, int value)
Sets a region of device memory to a specific byte value.
T * copy_within_device(T *d_dest, const T *d_src, size_t size)
Copies data from one device memory location to another.
__host__ size_t host_fast_log2_ceil(size_t n)
Computes the ceiling of log base 2 for a host integer using std::bit_width.
T * copy_from_device(const T *d_arr, size_t size)
Allocates host memory and copies data from device memory to it.
void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
Asserts if a CUDA error code indicates failure.
void device_free(T *d_arr)
Frees memory on the CUDA device.
T * copy_to_device(T *d_arr, const T *arr, size_t size)
Copies data from host memory to existing device memory.
__device__ __forceinline__ size_t get_tid()
Gets the global thread ID within a CUDA kernel launch.
#define BLOCKS
Default number of blocks to launch in CUDA kernels.
T * alloc_to_device(size_t size)
Allocates memory on the CUDA device.
__host__ size_t host_fast_log2(size_t n)
Computes the floor of log base 2 for a host integer using std::bit_width.