@@ -41,27 +41,18 @@ __global__ void amplitude_encode_kernel(
4141 double v1 = 0.0 ;
4242 double v2 = 0.0 ;
4343
44- // Vectorized Load Optimization:
45- // If we are well within bounds, treat input as double2 to issue a single 128-bit load instruction.
46- // Use __ldg() to pull through the read-only cache; cudaMalloc aligns to 256 bytes so the
47- // reinterpret_cast<double2*> load is naturally aligned.
44+ // double2 load via __ldg when aligned and in bounds.
4845 if (state_idx_base + 1 < input_len) {
49- // Reinterpret cast to load two doubles at once
5046 const double2 loaded = __ldg (reinterpret_cast <const double2 *>(input) + idx);
5147 v1 = loaded.x ;
5248 v2 = loaded.y ;
5349 }
54- // Handle edge case: Odd input length
5550 else if (state_idx_base < input_len) {
5651 v1 = __ldg (input + state_idx_base);
57- // v2 remains 0.0
5852 }
5953
60- // Write output:
61- // Apply pre-calculated reciprocal (multiplication is faster than division)
6254 state[state_idx_base] = make_cuDoubleComplex (v1 * inv_norm, 0.0 );
6355
64- // Check boundary for the second element (state_len is usually power of 2, but good to be safe)
6556 if (state_idx_base + 1 < state_len) {
6657 state[state_idx_base + 1 ] = make_cuDoubleComplex (v2 * inv_norm, 0.0 );
6758 }
@@ -82,7 +73,6 @@ __global__ void amplitude_encode_kernel_f32(
8273 float v2 = 0 .0f ;
8374
8475 if (state_idx_base + 1 < input_len) {
85- // Mirror the double kernel: cached vectorized load for two floats
8676 const float2 loaded = __ldg (reinterpret_cast <const float2 *>(input) + idx);
8777 v1 = loaded.x ;
8878 v2 = loaded.y ;
@@ -225,17 +215,7 @@ int launch_amplitude_encode_f32(
225215 return (int )cudaGetLastError ();
226216}
227217
228- // / Optimized batch amplitude encoding kernel
229- // /
230- // / Memory Layout (row-major):
231- // / - input_batch: [sample0_data | sample1_data | ... | sampleN_data]
232- // / - state_batch: [sample0_state | sample1_state | ... | sampleN_state]
233- // /
234- // / Optimizations:
235- // / 1. Vectorized double2 loads for 128-bit memory transactions when aligned
236- // / 2. Grid-stride loop for arbitrary batch sizes
237- // / 3. Coalesced memory access within warps
238- // / 4. Scalar fallback for misaligned sample bases and odd tails
218+ // / Batch amplitude encoding kernel (grid-stride, vectorized loads when aligned).
239219__global__ void amplitude_encode_batch_kernel (
240220 const double * __restrict__ input_batch,
241221 cuDoubleComplex* __restrict__ state_batch,
@@ -244,25 +224,20 @@ __global__ void amplitude_encode_batch_kernel(
244224 size_t input_len,
245225 size_t state_len
246226) {
247- // Grid-stride loop pattern for flexibility
248- const size_t elements_per_sample = state_len / 2 ; // Each thread handles 2 elements
227+ const size_t elements_per_sample = state_len / 2 ;
249228 const size_t total_work = num_samples * elements_per_sample;
250229 const size_t stride = gridDim .x * blockDim .x ;
251230
252231 size_t global_idx = blockIdx .x * blockDim .x + threadIdx .x ;
253232
254- // Process elements in grid-stride fashion
255233 for (size_t idx = global_idx; idx < total_work; idx += stride) {
256- // Decompose linear index into (sample, element_pair)
257234 const size_t sample_idx = idx / elements_per_sample;
258235 const size_t elem_pair = idx % elements_per_sample;
259236
260- // Calculate base addresses (strength-reduced)
261237 const size_t input_base = sample_idx * input_len;
262238 const size_t state_base = sample_idx * state_len;
263239 const size_t elem_offset = elem_pair * 2 ;
264240
265- // Load inverse norm (cached by L1)
266241 const double inv_norm = inv_norms[sample_idx];
267242
268243 double v1, v2;
@@ -281,34 +256,20 @@ __global__ void amplitude_encode_batch_kernel(
281256 ? __ldg (sample_input + elem_offset + 1 )
282257 : 0.0 ;
283258 } else {
284- // Padding region
285259 v1 = v2 = 0.0 ;
286260 }
287261
288- // Normalize and write as complex numbers
289- // Compiler will optimize multiplications
290262 const cuDoubleComplex c1 = make_cuDoubleComplex (v1 * inv_norm, 0.0 );
291263 const cuDoubleComplex c2 = make_cuDoubleComplex (v2 * inv_norm, 0.0 );
292264
293- // Write to global memory (coalesced within warp)
294265 state_batch[state_base + elem_offset] = c1;
295266 if (elem_offset + 1 < state_len) {
296267 state_batch[state_base + elem_offset + 1 ] = c2;
297268 }
298269 }
299270}
300271
301- // / Optimized batch amplitude encoding kernel (float32)
302- // /
303- // / Memory Layout (row-major):
304- // / - input_batch: [sample0_data | sample1_data | ... | sampleN_data]
305- // / - state_batch: [sample0_state | sample1_state | ... | sampleN_state]
306- // /
307- // / Optimizations:
308- // / 1. Vectorized float2 loads for 64-bit memory transactions
309- // / 2. Grid-stride loop for arbitrary batch sizes
310- // / 3. Coalesced memory access within warps
311- // / 4. Minimized register pressure
272+ // / Batch amplitude encoding kernel (float32).
312273__global__ void amplitude_encode_batch_kernel_f32 (
313274 const float * __restrict__ input_batch,
314275 cuComplex* __restrict__ state_batch,
@@ -317,25 +278,20 @@ __global__ void amplitude_encode_batch_kernel_f32(
317278 size_t input_len,
318279 size_t state_len
319280) {
320- // Grid-stride loop pattern for flexibility
321281 const size_t elements_per_sample = state_len / 2 ;
322282 const size_t total_work = num_samples * elements_per_sample;
323283 const size_t stride = gridDim .x * blockDim .x ;
324284
325285 size_t global_idx = blockIdx .x * blockDim .x + threadIdx .x ;
326286
327- // Process elements in grid-stride fashion
328287 for (size_t idx = global_idx; idx < total_work; idx += stride) {
329- // Decompose linear index into (sample, element_pair)
330288 const size_t sample_idx = idx / elements_per_sample;
331289 const size_t elem_pair = idx % elements_per_sample;
332290
333- // Calculate base addresses (strength-reduced)
334291 const size_t input_base = sample_idx * input_len;
335292 const size_t state_base = sample_idx * state_len;
336293 const size_t elem_offset = elem_pair * 2 ;
337294
338- // Load inverse norm (cached by L1)
339295 const float inv_norm = inv_norms[sample_idx];
340296
341297 float v1, v2;
@@ -357,11 +313,9 @@ __global__ void amplitude_encode_batch_kernel_f32(
357313 v1 = v2 = 0 .0f ;
358314 }
359315
360- // Normalize and write as complex numbers
361316 const cuComplex c1 = make_cuComplex (v1 * inv_norm, 0 .0f );
362317 const cuComplex c2 = make_cuComplex (v2 * inv_norm, 0 .0f );
363318
364- // Write to global memory (coalesced within warp)
365319 state_batch[state_base + elem_offset] = c1;
366320 if (elem_offset + 1 < state_len) {
367321 state_batch[state_base + elem_offset + 1 ] = c2;
@@ -397,14 +351,9 @@ int launch_amplitude_encode_batch(
397351
398352 cuDoubleComplex* state_complex_d = static_cast <cuDoubleComplex*>(state_batch_d);
399353
400- // Optimal configuration for modern GPUs (SM 7.0+)
401- // - Block size: DEFAULT_BLOCK_SIZE threads (8 warps, good occupancy)
402- // - Grid size: Enough blocks to saturate GPU, but not excessive
403354 const int blockSize = DEFAULT_BLOCK_SIZE ;
404355 const size_t total_work = num_samples * (state_len / 2 );
405356
406- // Calculate grid size: aim for high occupancy without too many blocks
407- // Limit to reasonable number of blocks to avoid scheduler overhead
408357 const size_t blocks_needed = (total_work + blockSize - 1 ) / blockSize;
409358 const size_t max_blocks = MAX_GRID_BLOCKS ;
410359 const size_t gridSize = (blocks_needed < max_blocks) ? blocks_needed : max_blocks;
@@ -462,7 +411,6 @@ __global__ void l2_norm_kernel(
462411 size_t input_len,
463412 double * __restrict__ out_accum
464413) {
465- // Vectorized double2 loads for bandwidth and coalescing
466414 const size_t vec_idx = blockIdx .x * blockDim .x + threadIdx .x ;
467415 const size_t stride = gridDim .x * blockDim .x ;
468416
@@ -497,7 +445,6 @@ __global__ void l2_norm_kernel_f32(
497445 size_t input_len,
498446 float * __restrict__ out_accum
499447) {
500- // Vectorized float2 loads for bandwidth and coalescing
501448 const size_t vec_idx = blockIdx .x * blockDim .x + threadIdx .x ;
502449 const size_t stride = gridDim .x * blockDim .x ;
503450
0 commit comments