|
Zip and Skip Tries
|
Provides utility functions and classes for CUDA operations. More...
#include <bit>#include <cstddef>#include <cstdint>#include <cstdio>#include <cuda_runtime.h>#include <limits>

Go to the source code of this file.
Classes | |
| class | CudaTimer |
| A simple CUDA timer class using CUDA events. More... | |
Macros | |
| #define | BLOCKS 64 |
| Default number of blocks to launch in CUDA kernels. | |
| #define | THREADS 1024 |
| Default number of threads per block in CUDA kernels. | |
| #define | gpuErrchk(ans) { gpuAssert(ans, __FILE__, __LINE__); } |
| Macro to check CUDA API calls for errors. | |
Functions | |
| void | gpuAssert (cudaError_t code, const char *file, int line, bool abort=true) |
| Asserts if a CUDA error code indicates failure. | |
| __host__ size_t | host_fast_log2 (size_t n) |
| Computes the floor of log base 2 for a host integer using std::bit_width. | |
| __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. | |
| __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. | |
| __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. | |
| template<typename T > | |
| T * | alloc_to_device (size_t size) |
| Allocates memory on the CUDA device. | |
| template<typename T > | |
| T * | copy_to_device (T *d_arr, const T *arr, size_t size) |
| Copies data from host memory to existing device memory. | |
| template<typename T > | |
| T * | copy_to_device (const T *arr, size_t size) |
| Allocates device memory and copies data from host memory to it. | |
| template<typename T > | |
| T * | memset_within_device (T *d_arr, size_t size, int value) |
| Sets a region of device memory to a specific byte value. | |
| template<typename T > | |
| T * | memset_to_device (size_t size, int value) |
| Allocates device memory and sets it to a specific byte value. | |
| template<typename T > | |
| T * | copy_within_device (T *d_dest, const T *d_src, size_t size) |
| Copies data from one device memory location to another. | |
| void | synchronize_device () |
| Synchronizes the host thread with the default CUDA device stream. | |
| template<typename T > | |
| T * | copy_from_device (const T *d_arr, size_t size) |
| Allocates host memory and copies data from device memory to it. | |
| template<typename T > | |
| void | device_free (T *d_arr) |
| Frees memory on the CUDA device. | |
| __device__ __forceinline__ size_t | get_tid () |
| Gets the global thread ID within a CUDA kernel launch. | |
| constexpr size_t | get_num_threads () |
| Gets the total number of threads in the kernel launch (compile-time). | |
Variables | |
| static constexpr bool | CHECK_CUDA_ERRORS = false |
| Compile-time flag to enable/disable CUDA error checking via gpuErrchk/gpuAssert. | |
Provides utility functions and classes for CUDA operations.
This file includes common CUDA helper functions, error checking macros, memory management wrappers, a timer class, and mathematical utilities for both host and device code.
Definition in file cuda_utils.cuh.
| #define BLOCKS 64 |
Default number of blocks to launch in CUDA kernels.
Definition at line 21 of file cuda_utils.cuh.
Macro to check CUDA API calls for errors.
Calls gpuAssert to check the result of a CUDA API call.
| ans | The result of the CUDA API call (cudaError_t). |
Definition at line 35 of file cuda_utils.cuh.
| #define THREADS 1024 |
Default number of threads per block in CUDA kernels.
Definition at line 26 of file cuda_utils.cuh.
Allocates memory on the CUDA device.
Wrapper around cudaMalloc with optional error checking.
| T | The data type of the array elements. |
| size | The number of elements of type T to allocate. |
gpuErrchk if CHECK_CUDA_ERRORS is true. Definition at line 190 of file cuda_utils.cuh.
Allocates host memory and copies data from device memory to it.
Wrapper around cudaMemcpy (DeviceToHost) with optional error checking. Allocates host memory using new[].
| T | The data type of the array elements. |
| d_arr | Pointer to the source device memory. |
| size | The number of elements of type T to copy. |
delete[]. gpuErrchk if CHECK_CUDA_ERRORS is true. Definition at line 359 of file cuda_utils.cuh.
Allocates device memory and copies data from host memory to it.
Combines allocation (alloc_to_device) and copy (copy_to_device).
| T | The data type of the array elements. |
| arr | Pointer to the source host memory. |
| size | The number of elements of type T to allocate and copy. |
gpuErrchk indirectly if CHECK_CUDA_ERRORS is true. Definition at line 245 of file cuda_utils.cuh.
Copies data from host memory to existing device memory.
Wrapper around cudaMemcpy (HostToDevice) with optional error checking.
| T | The data type of the array elements. |
| d_arr | Pointer to the destination device memory (must be pre-allocated). |
| arr | Pointer to the source host memory. |
| size | The number of elements of type T to copy. |
d_arr). gpuErrchk if CHECK_CUDA_ERRORS is true. Definition at line 219 of file cuda_utils.cuh.
Copies data from one device memory location to another.
Wrapper around cudaMemcpy (DeviceToDevice) with optional error checking.
| T | The data type of the array elements. |
| d_dest | Pointer to the destination device memory. |
| d_src | Pointer to the source device memory. |
| size | The number of elements of type T to copy. |
d_dest). gpuErrchk if CHECK_CUDA_ERRORS is true. Definition at line 311 of file cuda_utils.cuh.
| __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.
| n | The input value. Must be greater than 0. |
k such that 2^k <= n. __clzll (count leading zeros for long long). Definition at line 103 of file cuda_utils.cuh.
| __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.
| n | The input value. Must be greater than 0. |
k such that 2^k >= n. __clzll (count leading zeros for long long). Definition at line 118 of file cuda_utils.cuh.
Frees memory on the CUDA device.
Wrapper around cudaFree with optional error checking.
| T | The data type of the array elements (used for pointer type). |
| d_arr | Pointer to the device memory to free. |
gpuErrchk if CHECK_CUDA_ERRORS is true. Definition at line 385 of file cuda_utils.cuh.
Gets the total number of threads in the kernel launch (compile-time).
Calculates the total number of threads based on the predefined BLOCKS and THREADS macros.
constexpr function, evaluated at compile time. Definition at line 424 of file cuda_utils.cuh.
| __device__ __forceinline__ size_t get_tid | ( | ) |
Gets the global thread ID within a CUDA kernel launch.
Calculates the unique ID for the current thread across all blocks in the grid. Assumes a 1D grid and 1D block configuration.
Definition at line 406 of file cuda_utils.cuh.
Asserts if a CUDA error code indicates failure.
Checks the provided CUDA error code. If it's not cudaSuccess, it prints an error message including the error string, file name, and line number. If abort is true, it terminates the program.
| code | The CUDA error code to check. |
| file | The source file where the check occurs (FILE). |
| line | The line number where the check occurs (LINE). |
| abort | If true (default), exits the program upon error. |
Definition at line 49 of file cuda_utils.cuh.
Computes the floor of log base 2 for a host integer using std::bit_width.
| n | The input value. Must be greater than 0. |
k such that 2^k <= n. Definition at line 74 of file cuda_utils.cuh.
Computes the ceiling of log base 2 for a host integer using std::bit_width.
| n | The input value. Must be greater than 0. |
k such that 2^k >= n. Definition at line 88 of file cuda_utils.cuh.
Allocates device memory and sets it to a specific byte value.
Combines allocation (alloc_to_device) and memory set (memset_within_device).
| T | The data type of the array elements. |
| size | The number of elements of type T to allocate and set. |
| value | The integer byte value to set (e.g., 0). |
gpuErrchk indirectly if CHECK_CUDA_ERRORS is true. Definition at line 291 of file cuda_utils.cuh.
Sets a region of device memory to a specific byte value.
Wrapper around cudaMemset with optional error checking.
| T | The data type of the array elements (used for size calculation). |
| d_arr | Pointer to the device memory region to set. |
| size | The number of elements of type T in the region. |
| value | The integer byte value to set (e.g., 0). |
d_arr). gpuErrchk if CHECK_CUDA_ERRORS is true. Definition at line 265 of file cuda_utils.cuh.
|
inline |
Synchronizes the host thread with the default CUDA device stream.
Waits until all preceding commands in the default stream have completed. Wrapper around cudaDeviceSynchronize with optional error checking.
gpuErrchk if CHECK_CUDA_ERRORS is true. Definition at line 333 of file cuda_utils.cuh.
Compile-time flag to enable/disable CUDA error checking via gpuErrchk/gpuAssert.
true to enable runtime checks (potentially slower), false to disable them. Definition at line 66 of file cuda_utils.cuh.