-
Notifications
You must be signed in to change notification settings - Fork 0
/
rng.cu
170 lines (141 loc) · 4.92 KB
/
rng.cu
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**************************************************************
Lokman A. Abbas-Turki code
Those who re-use this code should mention in their code
the name of the author above.
***************************************************************/
#include "rng.h"
#include <cuda_runtime.h>
////////////////////////////////////////////////////////////////
// Memory for RNG use
////////////////////////////////////////////////////////////////
// The state variables of CMRG on GPU
TabSeedCMRG_t *CMRG;
// The state variables of CMRG on CPU
TabSeedCMRG_t *CMRGp;
// Matrixes associated to the post treatment of the CMRG
// - First MRG
double A1[3][3];
// - Second MRG
double A2[3][3];
// Functions from http://www.iro.umontreal.ca/~lecuyer/myftp/streams00/c/
/*-------------------------------------------------------------------------*/
static double MultModM (double a, double s, double c, double m)
/* Compute (a*s + c) % m. m must be < 2^35. Works also for s, c < 0 */
{
double v;
long a1;
v = a * s + c;
if ((v >= two53) || (v <= -two53)) {
a1 = (long) (a / two17);
a -= a1 * two17;
v = a1 * s;
a1 = (long) (v / m);
v -= a1 * m;
v = v * two17 + a * s + c;
}
a1 = (long) (v / m);
if ((v -= a1 * m) < 0.0)
return v += m;
else
return v;
}
/*-------------------------------------------------------------------------*/
static void MatVecModM (double A[3][3], double s[3], double v[3], double m)
/* Returns v = A*s % m. Assumes that -m < s[i] < m. */
/* Works even if v = s. */
{
int i;
double x[3];
for (i = 0; i < 3; ++i) {
x[i] = MultModM (A[i][0], s[0], 0.0, m);
x[i] = MultModM (A[i][1], s[1], x[i], m);
x[i] = MultModM (A[i][2], s[2], x[i], m);
}
for (i = 0; i < 3; ++i)
v[i] = x[i];
}
/*-------------------------------------------------------------------------*/
static void MatMatModM (double A[3][3], double B[3][3], double C[3][3],
double m)
/* Returns C = A*B % m. Work even if A = C or B = C or A = B = C. */
{
int i, j;
double V[3], W[3][3];
for (i = 0; i < 3; ++i) {
for (j = 0; j < 3; ++j)
V[j] = B[j][i];
MatVecModM (A, V, V, m);
for (j = 0; j < 3; ++j)
W[j][i] = V[j];
}
for (i = 0; i < 3; ++i) {
for (j = 0; j < 3; ++j)
C[i][j] = W[i][j];
}
}
/*-------------------------------------------------------------------------*/
static void MatPowModM (double A[3][3], double B[3][3], double m, long n)
/* Compute matrix B = A^n % m ; works even if A = B */
{
int i, j;
double W[3][3];
// initialize: W = A; B = I
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; ++j) {
W[i][j] = A[i][j];
B[i][j] = 0.0;
}
}
for (j = 0; j < 3; ++j)
B[j][j] = 1.0;
// Compute B = A^n % m using the binary decomposition of n
while (n > 0) {
if (n % 2)
MatMatModM (W, B, B, m);
MatMatModM (W, W, W, m);
n /= 2;
}
}
////////////////////////////////////////////////////////////////
// Post initialization of CMRG
////////////////////////////////////////////////////////////////
void PostInitDataCMRG()
{
const int m1 = 2147483647; // Requested for the simulation
const int m2 = 2145483479; // Requested for the simulation
int j; // loop indices
// Init of the posttreatment table for CMRG rng
A1[0][0] = 2119409629.0; A1[0][1] = 302707381.0; A1[0][2] = 655487731.0;
A1[1][0] = 946145520.0; A1[1][1] = 1762689149.0; A1[1][2] = 302707381.0;
A1[2][0] = 1139076568.0; A1[2][1] = 600956040.0; A1[2][2] = 1762689149.0;
A2[0][0] = 1705536521.0; A2[0][1] = 1409357255.0; A2[0][2] = 1489714515.0;
A2[1][0] = 1443451163.0; A2[1][1] = 1705536521.0; A2[1][2] = 1556328147.0;
A2[2][0] = 1624922073.0; A2[2][1] = 1443451163.0; A2[2][2] = 130172503.0;
MatPowModM (A1, A1, m1, 33554432);
MatPowModM (A2, A2, m2, 33554432);
cudaMalloc(&CMRG, sizeof(TabSeedCMRG_t));
CMRGp = (TabSeedCMRG_t*) malloc(sizeof(TabSeedCMRG_t));
////////////////////////////////////////////////////////////////
// Local variables used in the initialization
////////////////////////////////////////////////////////////////
double s1[3] = {1.0, 1.0, 1.0};
double s2[3] = {1.0, 1.0, 1.0};
for (j = 0; j < Mtraj; j++) {
for (int k = 0; k < Mtraj_inner; k++) {
CMRGp[0][j][k][0] = (int)s1[0];
CMRGp[0][j][k][1] = (int)s1[1];
CMRGp[0][j][k][2] = (int)s1[2];
CMRGp[0][j][k][3] = (int)s2[0];
CMRGp[0][j][k][4] = (int)s2[1];
CMRGp[0][j][k][5] = (int)s2[2];
MatVecModM(A1, s1, s1, m1);
MatVecModM(A2, s2, s2, m2);
}
}
// - Copy CMRG data on the GPU
cudaMemcpy(CMRG, CMRGp, sizeof(TabSeedCMRG_t), cudaMemcpyHostToDevice);
}
void FreeCMRG(void){
cudaFree(CMRG);
free(CMRGp);
}