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