CUDA Networks
Functions
vector_randomize.cu File Reference

Implementation of the Vector::randomize method to fill the vector with random values. More...

#include "vector.h"
#include <curand_kernel.h>
#include <cuda_runtime.h>
Include dependency graph for vector_randomize.cu:

Go to the source code of this file.

Functions

__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.5 and 0.5. More...
 

Detailed Description

Implementation of the Vector::randomize method to fill the vector with random values.

Definition in file vector_randomize.cu.

Function Documentation

◆ randomizeKernel()

__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.5 and 0.5.

Parameters
dataPointer to the vector data on the GPU.
rowsNumber of elements in the vector.
seedSeed for random number generator.

Definition at line 16 of file vector_randomize.cu.

16  {
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 }