kRandom.cu 8.25 KB
Newer Older
Peter Eastman's avatar
Peter Eastman committed
1
2
3
4
5
6
7
8
9
10
11
12
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Scott Le Grand, Peter Eastman                                     *
 * Contributors:                                                              *
 *                                                                            *
13
14
15
16
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
Peter Eastman's avatar
Peter Eastman committed
17
 *                                                                            *
18
19
20
21
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
Peter Eastman's avatar
Peter Eastman committed
22
 *                                                                            *
23
24
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
Peter Eastman's avatar
Peter Eastman committed
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
 * -------------------------------------------------------------------------- */

#include <stdio.h>
#include <cuda.h>
#include <vector_functions.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

#include "gputypes.h"

static __constant__ cudaGmxSimulation cSim;

void SetRandomSim(gpuContext gpu)
{
    cudaError_t status;
    status = cudaMemcpyToSymbol(cSim, &gpu->sim, sizeof(cudaGmxSimulation));     
    RTERROR(status, "cudaMemcpyToSymbol: SetSim copy to cSim failed");
}

void GetRandomSim(gpuContext gpu)
{
    cudaError_t status;
    status = cudaMemcpyFromSymbol(&gpu->sim, cSim, sizeof(cudaGmxSimulation));     
    RTERROR(status, "cudaMemcpyFromSymbol: SetSim copy from cSim failed");
}

extern __shared__ float3 sRand[];


57
58
59
__global__ 
#if (__CUDA_ARCH__ >= 200)
__launch_bounds__(GF1XX_RANDOM_THREADS_PER_BLOCK, 1)
60
#elif (__CUDA_ARCH__ >= 120)
61
62
63
64
__launch_bounds__(GT2XX_RANDOM_THREADS_PER_BLOCK, 1)
#else
__launch_bounds__(G8X_RANDOM_THREADS_PER_BLOCK, 1)
#endif
Scott Le Grand's avatar
Scott Le Grand committed
65
void kGenerateRandoms_kernel()
Peter Eastman's avatar
Peter Eastman committed
66
67
68
69
70
71
72
73
74
75
{
    unsigned int pos            = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int increment      = blockDim.x * gridDim.x;
    
    // Read generator state
    uint4 state                 = cSim.pRandomSeed[pos];
    unsigned int carry          = 0;
    
    float4 random4;
    float2 random2;
76
    while (pos < cSim.totalRandoms)
Peter Eastman's avatar
Peter Eastman committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    {
        
        // Generate 6 randoms in GRF
        unsigned int pos1       = threadIdx.x;
        for (int i = 0; i < 2; i++)
        {
            state.x             = state.x * 69069 + 1;
            state.y            ^= state.y << 13;
            state.y            ^= state.y >> 17;
            state.y            ^= state.y << 5;
            unsigned int k      = (state.z >> 2) + (state.w >> 3) + (carry >> 2);
            unsigned int m      = state.w + state.w + state.z + carry;
            state.z             = state.w;
            state.w             = m;
            carry               = k >> 30;
            float x1            = (float)max(state.x + state.y + state.w, 0x00000001) / (float)0xffffffff;
            state.x             = state.x * 69069 + 1;
            state.y            ^= state.y << 13;
            state.y            ^= state.y >> 17;
            state.y            ^= state.y << 5;
97
            x1                  = sqrtf(-2.0f * logf(x1));
Peter Eastman's avatar
Peter Eastman committed
98
99
100
101
102
103
104
105
106
107
108
            k                   = (state.z >> 2) + (state.w >> 3) + (carry >> 2);
            m                   = state.w + state.w + state.z + carry;
            state.z             = state.w;
            state.w             = m;
            carry               = k >> 30;
            float x2            = (float)(state.x + state.y + state.w) / (float)0xffffffff;
            
            state.x             = state.x * 69069 + 1;
            state.y            ^= state.y << 13;
            state.y            ^= state.y >> 17;
            state.y            ^= state.y << 5;
109
            sRand[pos1].x       = x1 * cosf(2.0f * 3.14159265f * x2);
Peter Eastman's avatar
Peter Eastman committed
110
111
112
113
114
115
116
117
118
119
            k                   = (state.z >> 2) + (state.w >> 3) + (carry >> 2);
            m                   = state.w + state.w + state.z + carry;
            state.z             = state.w;
            state.w             = m;
            carry               = k >> 30;
            float x3            = (float)max(state.x + state.y + state.w, 0x00000001) / (float)0xffffffff;
            state.x             = state.x * 69069 + 1;
            state.y            ^= state.y << 13;
            state.y            ^= state.y >> 17;
            state.y            ^= state.y << 5;
120
            x3                  = sqrtf(-2.0f * logf(x3));
Peter Eastman's avatar
Peter Eastman committed
121
122
123
124
125
126
127
128
129
130
131
            k                   = (state.z >> 2) + (state.w >> 3) + (carry >> 2);
            m                   = state.w + state.w + state.z + carry;
            state.z             = state.w;
            state.w             = m;
            carry               = k >> 30;
            float x4            = (float)(state.x + state.y + state.w) / (float)0xffffffff;
            
            state.x             = state.x * 69069 + 1;
            state.y            ^= state.y << 13;
            state.y            ^= state.y >> 17;
            state.y            ^= state.y << 5;
132
            sRand[pos1].y       = x3 * cosf(2.0f * 3.14159265f * x4);
Peter Eastman's avatar
Peter Eastman committed
133
134
135
136
137
138
139
140
141
142
            k                   = (state.z >> 2) + (state.w >> 3) + (carry >> 2);
            m                   = state.w + state.w + state.z + carry;
            state.z             = state.w;
            state.w             = m;
            carry               = k >> 30;
            float x5            = (float)max(state.x + state.y + state.w, 0x00000001) / (float)0xffffffff;
            state.x             = state.x * 69069 + 1;
            state.y            ^= state.y << 13;
            state.y            ^= state.y >> 17;
            state.y            ^= state.y << 5;
143
            x5                  = sqrtf(-2.0f * logf(x5));
Peter Eastman's avatar
Peter Eastman committed
144
145
146
147
148
149
            k                   = (state.z >> 2) + (state.w >> 3) + (carry >> 2);
            m                   = state.w + state.w + state.z + carry;
            state.z             = state.w;
            state.w             = m;
            carry               = k >> 30;
            float x6            = (float)(state.x + state.y + state.w) / (float)0xffffffff;
150
            sRand[pos1].z       = x5 * cosf(2.0f * 3.14159265f * x6); 
Peter Eastman's avatar
Peter Eastman committed
151
152
153
154
            pos1               += blockDim.x;
        }
        
        // Output final randoms
155
156
157
158
        random4.x               = sRand[threadIdx.x].x;
        random4.y               = sRand[threadIdx.x].y;
        random4.z               = sRand[threadIdx.x].z;
        random4.w               = sRand[threadIdx.x + blockDim.x].x;
159
        cSim.pRandom4[pos]     = random4;
160
161
        random2.x               = sRand[threadIdx.x + blockDim.x].y;
        random2.y               = sRand[threadIdx.x + blockDim.x].z;
162
        cSim.pRandom2[pos]     = random2;
Peter Eastman's avatar
Peter Eastman committed
163
164
165
166
167
168
169
170
171
172
173
174
175
        
        pos += increment;
    }
    
    
    // Write generator state
    pos                     = blockIdx.x * blockDim.x + threadIdx.x;
    cSim.pRandomSeed[pos]   = state;
}

void kGenerateRandoms(gpuContext gpu)
{
    kGenerateRandoms_kernel<<<gpu->sim.blocks, gpu->sim.random_threads_per_block, gpu->sim.random_threads_per_block * 2 * sizeof(float3)>>>();
176
177
}