CUDA Networks
vector_randomize.cu
Go to the documentation of this file.
1 /**
2  * @file vector_randomize.cu
3  * @brief Implementation of the Vector::randomize method to fill the vector with random values.
4  */
5 
6 #include "vector.h"
7 #include <curand_kernel.h> // Include CUDA random library for random number generation
8 #include <cuda_runtime.h>
9 
10 /**
11  * @brief CUDA kernel function that fills each element in the vector with a random value between -0.5 and 0.5.
12  * @param data Pointer to the vector data on the GPU.
13  * @param rows Number of elements in the vector.
14  * @param seed Seed for random number generator.
15  */
16 __global__ void randomizeKernel(double* data, int rows, unsigned long seed) {
17  // Calculate the global thread index
18  int idx = blockIdx.x * blockDim.x + threadIdx.x;
19 
20  // Ensure the thread's index is within the vector bounds
21  if (idx < rows) {
22  // Initialize a random state for this thread
23  curandState state;
24  curand_init(seed, idx, 0, &state);
25 
26  // Generate a random value between 0 and 1, then shift it to be between -0.5 and 0.5
27  double randomValue = curand_uniform(&state) - 0.5;
28 
29  // Assign the random value to the current vector element
30  data[idx] = randomValue;
31  }
32 }
33 
34 /**
35  * @brief Fills the vector with random values between -0.5 and 0.5.
36  */
38  // Define the number of threads per block (a common choice for good occupancy)
39  int threadsPerBlock = 256;
40 
41  // Calculate the number of blocks needed to cover all elements
42  // We use ceiling division to ensure we have enough blocks
43  int blocksPerGrid = (rows + threadsPerBlock - 1) / threadsPerBlock;
44 
45  // Generate a seed for the random number generator
46  // We use the current time to ensure different seeds across runs
47  unsigned long seed = time(NULL);
48 
49  // Launch the CUDA kernel
50  randomizeKernel<<<blocksPerGrid, threadsPerBlock>>>(d_data, rows, seed);
51 
52  // Wait for the kernel to complete before returning
53  // This ensures all random values are generated before any subsequent operations
54  cudaDeviceSynchronize();
55 }
void randomize()
Randomize the vector elements with values between -0.5 and 0.5.
Defines the Vector class for GPU-accelerated vector operations.
__global__ void randomizeKernel(double *data, int rows, unsigned long seed)
CUDA kernel function that fills each element in the vector with a random value between -0....