7 #include <curand_kernel.h>
8 #include <cuda_runtime.h>
17 __global__
void randomizeKernel(
double* data,
int rows,
int cols,
unsigned long seed) {
19 int idx = blockIdx.x * blockDim.x + threadIdx.x;
20 int totalElements = rows * cols;
23 if (idx < totalElements) {
30 curand_init(seed, idx, 0, &state);
33 double randomValue = curand_uniform(&state) - 0.5;
36 data[row * cols + col] = randomValue;
45 int totalElements = rows * cols;
48 int threadsPerBlock = 256;
52 int blocksPerGrid = (totalElements + threadsPerBlock - 1) / threadsPerBlock;
56 unsigned long seed = time(NULL);
59 randomizeKernel<<<blocksPerGrid, threadsPerBlock>>>(d_data, rows, cols, seed);
63 cudaDeviceSynchronize();
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....