Advanced Chunk Processing Library 0.2.0
A comprehensive C++ library for advanced data chunking strategies and processing operations
Loading...
Searching...
No Matches
gpu_chunking::GPUChunking< T > Class Template Reference

#include <gpu_chunking.hpp>

Public Member Functions

 GPUChunking (int window_sz=32, float thresh=0.1f)
 
 ~GPUChunking ()
 
std::vector< std::vector< T > > chunk (const std::vector< T > &data)
 
float get_threshold () const
 
int get_window_size () const
 
void set_threshold (float thresh)
 
void set_window_size (int size)
 

Static Public Member Functions

static std::string get_gpu_info ()
 
static bool is_gpu_available ()
 

Private Member Functions

template<typename U >
U * allocate_device_memory (size_t size)
 
template<typename U >
void copy_from_device (U *h_ptr, const U *d_ptr, size_t size)
 
template<typename U >
void copy_to_device (U *d_ptr, const U *h_ptr, size_t size)
 

Private Attributes

cudaStream_t stream
 
float threshold
 
int window_size
 

Detailed Description

template<typename T>
class gpu_chunking::GPUChunking< T >

Definition at line 64 of file gpu_chunking.hpp.

Constructor & Destructor Documentation

◆ GPUChunking()

template<typename T >
gpu_chunking::GPUChunking< T >::GPUChunking ( int  window_sz = 32,
float  thresh = 0.1f 
)
inline

Definition at line 91 of file gpu_chunking.hpp.

92 : window_size(window_sz), threshold(thresh) {
93 CUDA_CHECK(cudaStreamCreate(&stream));
94 }
#define CUDA_CHECK(call)

References CUDA_CHECK, and gpu_chunking::GPUChunking< T >::stream.

◆ ~GPUChunking()

template<typename T >
gpu_chunking::GPUChunking< T >::~GPUChunking ( )
inline

Definition at line 96 of file gpu_chunking.hpp.

96 {
97 cudaStreamDestroy(stream);
98 }

References gpu_chunking::GPUChunking< T >::stream.

Member Function Documentation

◆ allocate_device_memory()

template<typename T >
template<typename U >
U * gpu_chunking::GPUChunking< T >::allocate_device_memory ( size_t  size)
inlineprivate

Definition at line 72 of file gpu_chunking.hpp.

72 {
73 U* d_ptr;
74 CUDA_CHECK(cudaMalloc(&d_ptr, size * sizeof(U)));
75 return d_ptr;
76 }

References CUDA_CHECK.

◆ chunk()

template<typename T >
std::vector< std::vector< T > > gpu_chunking::GPUChunking< T >::chunk ( const std::vector< T > &  data)
inline

Definition at line 100 of file gpu_chunking.hpp.

100 {
101 if (data.empty())
102 return {};
103
104 // Allocate device memory
105 int* d_data = allocate_device_memory<int>(data.size());
106 int* d_boundaries = allocate_device_memory<int>(data.size());
107
108 // Copy input data to GPU
109 copy_to_device(d_data, data.data(), data.size());
110
111 // Configure kernel launch parameters
112 const int BLOCK_SIZE = 256;
113 int num_blocks = (data.size() + BLOCK_SIZE - 1) / BLOCK_SIZE;
114
115 // Launch kernel
116 chunk_kernel<<<num_blocks, BLOCK_SIZE, 0, stream>>>(d_data, d_boundaries, data.size(),
118
119 // Check for kernel errors
120 CUDA_CHECK(cudaGetLastError());
121
122 // Copy boundaries back to host
123 std::vector<int> boundaries(data.size());
124 copy_from_device(boundaries.data(), d_boundaries, data.size());
125
126 // Synchronize stream
127 CUDA_CHECK(cudaStreamSynchronize(stream));
128
129 // Free device memory
130 CUDA_CHECK(cudaFree(d_data));
131 CUDA_CHECK(cudaFree(d_boundaries));
132
133 // Create chunks based on boundaries
134 std::vector<std::vector<T>> chunks;
135 std::vector<T> current_chunk;
136
137 for (size_t i = 0; i < data.size(); ++i) {
138 current_chunk.push_back(data[i]);
139 if (boundaries[i] || i == data.size() - 1) {
140 if (!current_chunk.empty()) {
141 chunks.push_back(std::move(current_chunk));
142 current_chunk = std::vector<T>();
143 }
144 }
145 }
146
147 return chunks;
148 }
void copy_from_device(U *h_ptr, const U *d_ptr, size_t size)
void copy_to_device(U *d_ptr, const U *h_ptr, size_t size)

References gpu_chunking::GPUChunking< T >::copy_from_device(), gpu_chunking::GPUChunking< T >::copy_to_device(), CUDA_CHECK, gpu_chunking::GPUChunking< T >::stream, gpu_chunking::GPUChunking< T >::threshold, and gpu_chunking::GPUChunking< T >::window_size.

◆ copy_from_device()

template<typename T >
template<typename U >
void gpu_chunking::GPUChunking< T >::copy_from_device ( U *  h_ptr,
const U *  d_ptr,
size_t  size 
)
inlineprivate

Definition at line 86 of file gpu_chunking.hpp.

86 {
87 CUDA_CHECK(cudaMemcpyAsync(h_ptr, d_ptr, size * sizeof(U), cudaMemcpyDeviceToHost, stream));
88 }

References CUDA_CHECK, and gpu_chunking::GPUChunking< T >::stream.

Referenced by gpu_chunking::GPUChunking< T >::chunk().

◆ copy_to_device()

template<typename T >
template<typename U >
void gpu_chunking::GPUChunking< T >::copy_to_device ( U *  d_ptr,
const U *  h_ptr,
size_t  size 
)
inlineprivate

Definition at line 80 of file gpu_chunking.hpp.

80 {
81 CUDA_CHECK(cudaMemcpyAsync(d_ptr, h_ptr, size * sizeof(U), cudaMemcpyHostToDevice, stream));
82 }

References CUDA_CHECK, and gpu_chunking::GPUChunking< T >::stream.

Referenced by gpu_chunking::GPUChunking< T >::chunk().

◆ get_gpu_info()

template<typename T >
static std::string gpu_chunking::GPUChunking< T >::get_gpu_info ( )
inlinestatic

Definition at line 180 of file gpu_chunking.hpp.

180 {
181 if (!is_gpu_available()) {
182 return "No CUDA-capable GPU found";
183 }
184
185 cudaDeviceProp prop;
186 CUDA_CHECK(cudaGetDeviceProperties(&prop, 0));
187
188 return std::string("GPU Device: ") + prop.name +
189 "\nCompute capability: " + std::to_string(prop.major) + "." +
190 std::to_string(prop.minor);
191 }

References CUDA_CHECK, and gpu_chunking::GPUChunking< T >::is_gpu_available().

◆ get_threshold()

template<typename T >
float gpu_chunking::GPUChunking< T >::get_threshold ( ) const
inline

Definition at line 168 of file gpu_chunking.hpp.

168 {
169 return threshold;
170 }

References gpu_chunking::GPUChunking< T >::threshold.

◆ get_window_size()

template<typename T >
int gpu_chunking::GPUChunking< T >::get_window_size ( ) const
inline

Definition at line 165 of file gpu_chunking.hpp.

165 {
166 return window_size;
167 }

References gpu_chunking::GPUChunking< T >::window_size.

◆ is_gpu_available()

template<typename T >
static bool gpu_chunking::GPUChunking< T >::is_gpu_available ( )
inlinestatic

Definition at line 173 of file gpu_chunking.hpp.

173 {
174 int device_count;
175 cudaError_t error = cudaGetDeviceCount(&device_count);
176 return (error == cudaSuccess) && (device_count > 0);
177 }

Referenced by gpu_chunking::GPUChunking< T >::get_gpu_info().

◆ set_threshold()

template<typename T >
void gpu_chunking::GPUChunking< T >::set_threshold ( float  thresh)
inline

Definition at line 158 of file gpu_chunking.hpp.

158 {
159 if (thresh <= 0.0f || thresh >= 1.0f) {
160 throw std::invalid_argument("Threshold must be between 0 and 1");
161 }
162 threshold = thresh;
163 }

References gpu_chunking::GPUChunking< T >::threshold.

◆ set_window_size()

template<typename T >
void gpu_chunking::GPUChunking< T >::set_window_size ( int  size)
inline

Definition at line 151 of file gpu_chunking.hpp.

151 {
152 if (size <= 0) {
153 throw std::invalid_argument("Window size must be positive");
154 }
155 window_size = size;
156 }

References gpu_chunking::GPUChunking< T >::window_size.

Member Data Documentation

◆ stream

◆ threshold

◆ window_size


The documentation for this class was generated from the following file: