From 9b9910e9f94da1a0630517ba34c76c75b6ca40c7 Mon Sep 17 00:00:00 2001 From: Uldis Locans Date: Wed, 15 Mar 2017 10:01:28 +0100 Subject: [PATCH] add seed to random number initialization --- src/CUDA/CudaBase.cu | 5 +++-- src/CUDA/CudaBase.cuh | 3 ++- src/DKSBase.cpp | 10 ++-------- src/DKSBase.h | 25 ++----------------------- 4 files changed, 9 insertions(+), 34 deletions(-) diff --git a/src/CUDA/CudaBase.cu b/src/CUDA/CudaBase.cu index a4f3851..6317567 100644 --- a/src/CUDA/CudaBase.cu +++ b/src/CUDA/CudaBase.cu @@ -48,14 +48,15 @@ CudaBase::~CudaBase() { /* create curandStates */ -int CudaBase::cuda_createCurandStates(int size) { +int CudaBase::cuda_createCurandStates(int size, int seed) { if (defaultRndSet == 1) cuda_deleteCurandStates(); int threads = 128; int blocks = size / threads + 1; - int seed = time(NULL); + if (seed == -1) + seed = time(NULL); //std::cout << "sizeof: " << sizeof(curandState) << std::endl; cudaMalloc(&defaultRndState, sizeof(curandState)*size); diff --git a/src/CUDA/CudaBase.cuh b/src/CUDA/CudaBase.cuh index 8d8c63e..d280ca1 100644 --- a/src/CUDA/CudaBase.cuh +++ b/src/CUDA/CudaBase.cuh @@ -41,9 +41,10 @@ public: * Init cuda random number (cuRand) states. * Create an array of type curandState with "size" elements on the GPU * and create a curandState with different seed for each array entry. + * If no seed is given create a seed based on current time. * Return success or error code */ - int cuda_createCurandStates(int size); + int cuda_createCurandStates(int size, int seed = -1); /** * Delete curandState. diff --git a/src/DKSBase.cpp b/src/DKSBase.cpp index 2960414..13e4358 100644 --- a/src/DKSBase.cpp +++ b/src/DKSBase.cpp @@ -127,17 +127,14 @@ DKSBase::DKSBase(const char* api_name, const char* device_name) { #ifdef DKS_CUDA cbase = new CudaBase(); - cchi = new CudaChiSquare(cbase); #endif #ifdef DKS_OPENCL oclbase = new OpenCLBase(); - oclchi = new OpenCLChiSquare(oclbase); #endif #ifdef DKS_MIC micbase = new MICBase(); - miccol = new MICCollimatorPhysics(micbase); #endif } @@ -155,18 +152,15 @@ DKSBase::~DKSBase() { delete[] m_function_name; #ifdef DKS_CUDA - delete cchi; delete cbase; #endif #ifdef DKS_OPENCL - delete oclchi; delete oclbase; #endif #ifdef DKS_MIC - delete micchi; delete micbase; #endif @@ -446,9 +440,9 @@ int DKSBase::callCreateRandomNumbers(void *mem_ptr, int size) { return DKS_ERROR; } -int DKSBase::callInitRandoms(int size) { +int DKSBase::callInitRandoms(int size, int seed) { if (apiCuda()) - return CUDA_SAFECALL(cbase->cuda_createCurandStates(size)); + return CUDA_SAFECALL(cbase->cuda_createCurandStates(size, seed)); else if (apiOpenCL()) return OPENCL_SAFECALL(oclbase->ocl_createRndStates(size)); else if (apiOpenMP()) diff --git a/src/DKSBase.h b/src/DKSBase.h index d07a7e6..370292d 100644 --- a/src/DKSBase.h +++ b/src/DKSBase.h @@ -29,36 +29,17 @@ #endif #include "OpenCL/OpenCLBase.h" -#include "OpenCL/OpenCLChiSquare.h" -#endif - -#ifdef DKS_AMD -#include "OpenCL/OpenCLFFT.h" -#include "OpenCL/OpenCLCollimatorPhysics.h" -#include "OpenCL/OpenCLGreensFunction.h" #endif #ifdef DKS_CUDA #include "CUDA/CudaBase.cuh" -#include "CUDA/CudaFFT.cuh" -#include "CUDA/CudaGreensFunction.cuh" -#include "CUDA/CudaChiSquare.cuh" -#include "CUDA/CudaCollimatorPhysics.cuh" #include "nvToolsExt.h" #endif #ifdef DKS_MIC #include "MIC/MICBase.h" -#include "MIC/MICChiSquare.h" -#include "MIC/MICFFT.h" -#include "MIC/MICCollimatorPhysics.h" -#include "MIC/MICGreensFunction.hpp" #endif -#include "Algorithms/GreensFunction.h" -#include "Algorithms/CollimatorPhysics.h" -#include "Algorithms/FFT.h" - #include "AutoTuning/DKSConfig.h" /** DKSBase class for handling function calls to DKS library */ @@ -78,17 +59,14 @@ private: #ifdef DKS_OPENCL OpenCLBase *oclbase; - OpenCLChiSquare *oclchi; #endif #ifdef DKS_CUDA CudaBase *cbase; - CudaChiSquare *cchi; #endif #ifdef DKS_MIC MICBase *micbase; - MICChiSquare *micchi; #endif protected: @@ -901,9 +879,10 @@ public: /** * Init random number states and save for reuse on device. + * If seed is -1, a random seed based on current time is taken. * TODO: opencl and mic implementations. */ - int callInitRandoms(int size); + int callInitRandoms(int size, int seed = -1); /** * Print memory information on device (total, used, available)