forked from ROCm/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTHCTensorRandom.cpp
141 lines (125 loc) · 3.87 KB
/
THCTensorRandom.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "THCTensorRandom.h"
#include "THCGenerator.hpp"
#include <random>
#include <curand.h>
void initializeGenerator(THCState *state, THCGenerator* gen);
void createGeneratorState(THCGenerator* gen, uint64_t seed);
/* Frees memory allocated during setup. */
void destroyGenerator(THCState *state, THCGenerator* gen)
{
std::lock_guard<std::mutex> lock(gen->mutex);
if (gen->state.gen_states)
{
THCudaFree(state, gen->state.gen_states);
gen->state.gen_states = NULL;
}
if (gen->state.kernel_params)
{
THCudaFree(state, gen->state.kernel_params);
gen->state.kernel_params = NULL;
}
}
static uint64_t createSeed(std::random_device& rd)
{
// limit to 53 bits to ensure unique representation in double
uint64_t seed = (((uint64_t)rd()) << 32) + rd();
return seed & 0x1FFFFFFFFFFFFF;
}
/* Initialize generator array (must be called before any other function) */
void THCRandom_init(THCState* state, int devices, int current_device)
{
THCRNGState* rng_state = THCState_getRngState(state);
rng_state->num_devices = devices;
rng_state->gen = (THCGenerator*)malloc(rng_state->num_devices * sizeof(THCGenerator));
std::random_device rd;
for (int i = 0; i < rng_state->num_devices; ++i)
{
new (&rng_state->gen[i].mutex) std::mutex();
rng_state->gen[i].state.initf = 0;
rng_state->gen[i].state.initial_seed = createSeed(rd);
rng_state->gen[i].state.philox_seed_offset = 0;
rng_state->gen[i].state.gen_states = NULL;
rng_state->gen[i].state.kernel_params = NULL;
}
}
/* Destroy generators and free memory */
void THCRandom_shutdown(THCState* state)
{
THCRNGState* rng_state = THCState_getRngState(state);
if (rng_state->gen == NULL) return;
for (int i = 0; i < rng_state->num_devices; ++i)
{
destroyGenerator(state, &rng_state->gen[i]);
}
free(rng_state->gen);
rng_state->gen = NULL;
}
/* Get the generator for the current device, but does not initialize the state */
static THCGenerator* THCRandom_rawGenerator(THCState* state)
{
THCRNGState* rng_state = THCState_getRngState(state);
int device;
THCudaCheck(cudaGetDevice(&device));
if (device >= rng_state->num_devices) THError("Invalid device index.");
return &rng_state->gen[device];
}
/* Get the generator for the current device and initializes it if necessary */
THCGenerator* THCRandom_getGenerator(THCState* state)
{
THCGenerator* gen = THCRandom_rawGenerator(state);
std::lock_guard<std::mutex> lock(gen->mutex);
if (gen->state.initf == 0)
{
initializeGenerator(state, gen);
createGeneratorState(gen, gen->state.initial_seed);
gen->state.initf = 1;
}
return gen;
}
struct curandStateMtgp32* THCRandom_generatorStates(struct THCState* state)
{
THCGenerator* gen = THCRandom_getGenerator(state);
return gen->state.gen_states;
}
/* Random seed */
uint64_t THCRandom_seed(THCState* state)
{
std::random_device rd;
uint64_t s = createSeed(rd);
THCRandom_manualSeed(state, s);
return s;
}
uint64_t THCRandom_seedAll(THCState* state)
{
std::random_device rd;
uint64_t s = createSeed(rd);
THCRandom_manualSeedAll(state, s);
return s;
}
/* Manually set the seed */
void THCRandom_manualSeed(THCState* state, uint64_t seed)
{
THCGenerator* gen = THCRandom_rawGenerator(state);
std::lock_guard<std::mutex> lock(gen->mutex);
gen->state.initial_seed = seed;
if (gen->state.initf) {
createGeneratorState(gen, seed);
}
}
void THCRandom_manualSeedAll(THCState* state, uint64_t seed)
{
THCRNGState* rng_state = THCState_getRngState(state);
int currentDevice;
THCudaCheck(cudaGetDevice(¤tDevice));
for (int i = 0; i < rng_state->num_devices; ++i) {
THCudaCheck(cudaSetDevice(i));
THCRandom_manualSeed(state, seed);
}
THCudaCheck(cudaSetDevice(currentDevice));
}
/* Get the initial seed */
uint64_t THCRandom_initialSeed(THCState* state)
{
THCGenerator* gen = THCRandom_getGenerator(state);
return gen->state.initial_seed;
}