static void Main(string[] args) { CudaSettings.Load(); var range = 100_000; for (int i = 0; i < 1000; i++) { var cuRand = new CuRand(1); Console.WriteLine("Executing CUDA kernels on a " + cuRand.GetCudaDeviceName(1)); IEnumerable <float> uniform_rand; IEnumerable <double> uniform_rand_double; PerformanceTimer(() => uniform_rand = cuRand.GenerateUniformDistribution(range), nameof(cuRand.GenerateUniformDistribution)); PerformanceTimer(() => uniform_rand_double = cuRand.GenerateUniformDistributionDP(range), nameof(cuRand.GenerateUniformDistributionDP)); IEnumerable <float> normal_rand; IEnumerable <double> normal_rand_double; PerformanceTimer(() => normal_rand = cuRand.GenerateNormalDistribution(range), nameof(cuRand.GenerateNormalDistribution)); PerformanceTimer(() => normal_rand_double = cuRand.GenerateNormalDistributionDP(range), nameof(cuRand.GenerateNormalDistributionDP)); IEnumerable <float> log_normal_rand; IEnumerable <double> log_normal_rand_double; PerformanceTimer(() => log_normal_rand = cuRand.GenerateLogNormalDistribution(range, 5, 1), nameof(cuRand.GenerateLogNormalDistribution)); PerformanceTimer(() => log_normal_rand_double = cuRand.GenerateLogNormalDistributionDP(range, 5, 1), nameof(cuRand.GenerateNormalDistributionDP)); IEnumerable <int> poisson_rand; PerformanceTimer(() => poisson_rand = cuRand.GeneratePoissonDistribution(range, 3), nameof(cuRand.GeneratePoissonDistribution)); Console.ReadKey(); } }
public static unsafe void cuda_random(float[] x, ulong n) { if (!curandInit) { curandGenerator_st *handle; SafeCall(CuRand.curandCreateGenerator(&handle, curandRngType.CURAND_RNG_PSEUDO_DEFAULT)); SafeCall(CuRand.curandSetPseudoRandomGeneratorSeed(handle, (ulong)DateTime.Now.Millisecond)); curandInit = true; gen = handle; } using (var gpuX = Gpu.Default.AllocateDevice(x.ToArray())) { SafeCall(CuRand.curandGenerateUniform(gen, (float *)gpuX.Handle, n)); CopyValues(x, gpuX); } }
/// <summary> /// Generates random numbers using the Nvidia cuRand library. /// </summary> static void FillCuRand(Accelerator accelerator) { if (!(accelerator is CudaAccelerator cudaAccelerator)) { return; } // Use one of the cuRand algorithms to generate random values to populate // a GPU buffer. Console.WriteLine("CuRand GPU"); using var rand = CuRand.CreateGPU(cudaAccelerator, CuRandRngType.CURAND_RNG_PSEUDO_DEFAULT); rand.SetSeed(1234); using var buffer = accelerator.Allocate1D <float>(16); rand.FillNormal(accelerator.DefaultStream, buffer.View, mean: 1.5f, stddev: 0.5f); // Reads data from the GPU buffer into a new CPU array. // Implicitly calls accelerator.DefaultStream.Synchronize() to ensure // that the kernel and memory copy are completed first. var randomValues = buffer.GetAsArray1D(); for (int i = 0, e = randomValues.Length; i < e; ++i) { Console.WriteLine($"Random[{i}] = {randomValues[i]}"); } Console.WriteLine(); // cuRand can also be used on the CPU to generate random values. Console.WriteLine("CuRand CPU"); using var cpuRand = CuRand.CreateCPU(accelerator.Context, CuRandRngType.CURAND_RNG_PSEUDO_XORWOW); cpuRand.SetSeed(5678); var randomCpuValues = new double[16]; cpuRand.FillUniform(randomCpuValues); for (int i = 0, e = randomCpuValues.Length; i < e; ++i) { Console.WriteLine($"Random[{i}] = {randomCpuValues[i]}"); } Console.WriteLine(); }