-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreefryGPU.h
102 lines (73 loc) · 2.71 KB
/
ThreefryGPU.h
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
#include "Threefry.h"
#pragma once
__shared__ unsigned long long* Random123_Threefry_Device_d_Random;
__constant__ Threefry::Threefry123::key_type Random123_Threefry_Device_Key;
inline void Threefry::initDevice()
{
cudaMemcpyToSymbol(Random123_Threefry_Device_Key, &seed_, sizeof(seed_));
}
class Threefry::Device {
Threefry123::ctr_type m_ctr;
Threefry123 m_rng;
public:
__device__ Device(void* d, size_t indiv_id, Phase phase, int nb_indiv) {
m_ctr[0] = indiv_id + nb_indiv*phase;
if(threadIdx.x == 0)
Random123_Threefry_Device_d_Random = (unsigned long long*)d;
m_ctr.v[1] = ((unsigned long long*)d)[m_ctr[0]];
}
__device__ ~Device() {
Random123_Threefry_Device_d_Random[m_ctr[0]] = m_ctr[1];
}
__device__ unsigned long long random() {
++m_ctr[1];
return m_rng(m_ctr, Random123_Threefry_Device_Key)[0];
}
__device__ unsigned int random32() {
return random();
}
__device__ double randomDouble() {
return (random()&((1llu<<48)-1))/double(1llu<<48);
}
__device__ unsigned int random(unsigned int max) {
return randomDouble()*max;
}
__device__ float randomFloat() {
return (random32()&((1llu<<24)-1))/double(1llu<<24);
}
__device__ int32_t roulette_random(double* probs, int32_t nb_elts);
__device__ int32_t binomial_random(int32_t nb, double prob); // Binomial drawing of parameters (nb, prob)
};
class Threefry::DeviceCollectiveBlock {
Threefry123::ctr_type m_ctr;
Threefry123 m_rng;
public:
__device__ DeviceCollectiveBlock(void* d, size_t indiv_id, Phase phase, int nb_indiv) {
m_ctr[0] = indiv_id + nb_indiv*phase;
if(threadIdx.x == 0)
Random123_Threefry_Device_d_Random = (unsigned long long*)d;
m_ctr.v[1] = ((unsigned long long*)d)[m_ctr[0]]-blockDim.x+threadIdx.x+1;
}
__device__ ~DeviceCollectiveBlock() {
if(threadIdx.x == blockDim.x-1)
Random123_Threefry_Device_d_Random[m_ctr[0]] = m_ctr[1];
}
__device__ unsigned long long random() {
m_ctr[1] += blockDim.x;
return m_rng(m_ctr, Random123_Threefry_Device_Key)[0];
}
__device__ unsigned int random32() {
return random();
}
__device__ double randomDouble() {
return (random()&((1llu<<48)-1))/double(1llu<<48);
}
__device__ unsigned int random(unsigned int max) {
return randomDouble()*max;
}
__device__ float randomFloat() {
return (random32()&((1llu<<24)-1))/double(1llu<<24);
}
__device__ int32_t roulette_random(double* probs, int32_t nb_elts);
__device__ int32_t binomial_random(int32_t nb, double prob); // Binomial drawing of parameters (nb, prob)
};