GPUMLib  0.2.2
GPU Machine Learning Library
random.cu
1 /*
2  Noel Lopes is an Assistant Professor at the Polytechnic Institute of Guarda, Portugal
3  Copyright (C) 2009, 2010, 2011, 2012 Noel de Jesus Mendonša Lopes
4 
5  This file is part of GPUMLib.
6 
7  GPUMLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <cstdlib>
22 #include "random.h"
23 
24 using namespace std;
25 
26 namespace GPUMLib {
27 
28 curandGenerator_t Random::randomGenerator = nullptr;
29 curandRngType_t Random::randomGeneratorType = CURAND_RNG_PSEUDO_DEFAULT;
30 
31 curandGenerator_t Random::RandomGenerator() {
32  if (randomGenerator == nullptr) {
33  curandCreateGenerator(&randomGenerator, randomGeneratorType);
34  atexit(&CleanUp);
35  }
36 
37  return randomGenerator;
38 }
39 
40 void Random::CleanUp() {
41  if (randomGenerator != nullptr) {
42  curandDestroyGenerator(randomGenerator);
43  randomGenerator = nullptr;
44  }
45 }
46 
47 void Random::SetSeed(unsigned long long seed, curandRngType_t generatorType) {
48  if (generatorType != randomGeneratorType) {
49  randomGeneratorType = generatorType;
50  CleanUp();
51  }
52 
53  curandSetPseudoRandomGeneratorSeed(RandomGenerator(), seed);
54 }
55 
56 void Random::Fill(DeviceArray<float> & a) {
57  curandGenerateUniform(RandomGenerator(), a.Pointer(), a.Length());
58 }
59 
60 }
Type * Pointer() const
Definition: BaseArray.h:70
int Length() const
Definition: BaseArray.h:63