kCalculateCDLJForces.cu 17.6 KB
Newer Older
Peter Eastman's avatar
Peter Eastman committed
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* -------------------------------------------------------------------------- *
 *                                   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:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

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

#include "gputypes.h"
#include "cudatypes.h"

#define UNROLLXX 0
#define UNROLLXY 0

struct Atom {
    float x;
    float y;
    float z;
    float q;
    float sig;
    float eps;
    float fx;
    float fy;
    float fz;
    float eps2;
    float sig2;
};


__shared__ Atom sA[G8X_NONBOND_THREADS_PER_BLOCK];
__shared__ unsigned int sWorkUnit[G8X_NONBOND_WORKUNITS_PER_SM];
__shared__ unsigned int sNext[GRID];

static __constant__ cudaGmxSimulation cSim;

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

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

__global__ void kCalculateCDLJForces_kernel()
{
    // Read queue of work blocks once so the remainder of
    // kernel can run asynchronously    
    int pos = cSim.nbWorkUnitsPerBlock * blockIdx.x + min(blockIdx.x, cSim.nbWorkUnitsPerBlockRemainder);
    int end = cSim.nbWorkUnitsPerBlock * (blockIdx.x + 1) + min((blockIdx.x + 1), cSim.nbWorkUnitsPerBlockRemainder);    
    if (threadIdx.x < end - pos)
    {
        sWorkUnit[threadIdx.x] = cSim.pWorkUnit[pos + threadIdx.x];
    }
    if (threadIdx.x < GRID)
    {
        sNext[threadIdx.x] = (threadIdx.x + 1) & (GRID - 1);
    }
    __syncthreads();

    // Now change pos and end to reflect work queue just read
    // into shared memory
    end = end - pos; 
    pos = end - (threadIdx.x >> GRIDBITS) - 1;
       
    while (pos >= 0)
    {  
    
        // Extract cell coordinates from appropriate work unit
        unsigned int x = sWorkUnit[pos];
        unsigned int y = ((x >> 2) & 0x7fff) << GRIDBITS;
        bool bExclusionFlag = (x & 0x1);
        x = (x >> 17) << GRIDBITS;
        float4      apos;   // Local atom x, y, z, q
        float3      af;     // Local atom fx, fy, fz
        float dx; 
        float dy; 
        float dz; 
        float r2; 
        float invR; 
        float sig; 
        float sig2; 
        float sig6; 
        float eps; 
        float dEdR;  
        unsigned int tgx = threadIdx.x & (GRID - 1);
        unsigned int tbx = threadIdx.x - tgx;
        int tj = tgx; 
        Atom* psA = &sA[tbx];
        if (!bExclusionFlag)
        {
            if (x == y) // Handle diagonals uniquely at 50% efficiency
            { 
                // Read fixed atom data into registers and GRF
                unsigned int i      = x + tgx;
                apos                = cSim.pPosq[i];
                float2 a            = cSim.pAttr[i];
                sA[threadIdx.x].x   = apos.x;
                sA[threadIdx.x].y   = apos.y;
                sA[threadIdx.x].z   = apos.z;
                sA[threadIdx.x].q   = apos.w;
                sA[threadIdx.x].sig = a.x;
                sA[threadIdx.x].eps = a.y;
                af.x                = 0.0f;
                af.y                = 0.0f;
                af.z                = 0.0f;
                apos.w             *= cSim.epsfac;
                for (unsigned int j = 0; j < GRID; j++)
                {
                    dx              = psA[j].x - apos.x; 
                    dy              = psA[j].y - apos.y; 
                    dz              = psA[j].z - apos.z; 
                    r2              = dx * dx + dy * dy + dz * dz; 
                    invR            = 1.0f / sqrt(r2);
                    sig             = a.x + psA[j].sig; 
                    sig2            = invR * sig; 
                    sig2           *= sig2;
                    sig6            = sig2 * sig2 * sig2; 
                    eps             = a.y * psA[j].eps; 
                    dEdR            = eps * (12.0f * sig6 - 6.0f) * sig6; 
                    dEdR           += apos.w * psA[j].q * invR; 
                    dEdR           *= invR * invR; 
                    dx             *= dEdR; 
                    dy             *= dEdR; 
                    dz             *= dEdR; 
                    af.x           -= dx; 
                    af.y           -= dy; 
                    af.z           -= dz; 
                }
                
                // Write results
                float4 of;
                of.x                                = af.x;
                of.y                                = af.y;
                of.z                                = af.z;
                of.w                                = 0.0f;
                int offset                          = x + tgx + (x >> GRIDBITS) * cSim.stride;
                cSim.pForce4a[offset]               = of;
            }         
            else        // 100% utilization
            {
                // Read fixed atom data into registers and GRF
                int j                   = y + tgx;
                unsigned int i          = x + tgx;
                float4 temp             = cSim.pPosq[j];
                float2 temp1            = cSim.pAttr[j];
                apos                    = cSim.pPosq[i];
                float2 a                = cSim.pAttr[i];
                sA[threadIdx.x].x       = temp.x;
                sA[threadIdx.x].y       = temp.y;
                sA[threadIdx.x].z       = temp.z;
                sA[threadIdx.x].q       = temp.w;
                sA[threadIdx.x].sig     = temp1.x;
                sA[threadIdx.x].eps     = temp1.y;
                sA[threadIdx.x].fx      = af.x = 0.0f;
                sA[threadIdx.x].fy      = af.y = 0.0f;
                sA[threadIdx.x].fz      = af.z = 0.0f;
                sA[threadIdx.x].sig2    = a.x;
                sA[threadIdx.x].eps2    = a.y;
                apos.w                 *= cSim.epsfac;
                
                for (j = 0; j < GRID; j++)
                {
                    dx              = psA[tj].x - apos.x; 
                    dy              = psA[tj].y - apos.y; 
                    dz              = psA[tj].z - apos.z; 
                    r2              = dx * dx + dy * dy + dz * dz; 
                    invR            = 1.0f / sqrt(r2);
                    sig             = a.x + psA[tj].sig; 
                    sig2            = invR * sig; 
                    sig2           *= sig2;
                    sig6            = sig2 * sig2 * sig2; 
                    eps             = a.y * psA[tj].eps; 
                    dEdR            = eps * (12.0f * sig6 - 6.0f) * sig6; 
                    dEdR           += apos.w * psA[tj].q * invR; 
                    dEdR           *= invR * invR; 
                    dx             *= dEdR; 
                    dy             *= dEdR; 
                    dz             *= dEdR; 
                    af.x           -= dx; 
                    af.y           -= dy; 
                    af.z           -= dz; 
                    psA[tj].fx     += dx; 
                    psA[tj].fy     += dy; 
                    psA[tj].fz     += dz;
                    tj              = sNext[tj]; 
                }
                
                // Write results
                float4 of;
                of.x                                = af.x;
                of.y                                = af.y;
                of.z                                = af.z;
                of.w                                = 0.0f;
                int offset                          = x + tgx + (y >> GRIDBITS) * cSim.stride;
                cSim.pForce4a[offset]               = of;
                of.x                                = sA[threadIdx.x].fx;
                of.y                                = sA[threadIdx.x].fy;
                of.z                                = sA[threadIdx.x].fz;
                offset                              = y + tgx + (x >> GRIDBITS) * cSim.stride;
                cSim.pForce4a[offset]               = of;
            }
        }
        else  // bExclusion
        {
            // Read exclusion data
            
            if (x == y) // Handle diagonals uniquely at 50% efficiency
            { 
                // Read fixed atom data into registers and GRF
                unsigned int excl       = cSim.pExclusion[x * cSim.exclusionStride + y + tgx];                          
                unsigned int i          = x + tgx;
                apos                    = cSim.pPosq[i];
                float2 a                = cSim.pAttr[i];
                sA[threadIdx.x].x       = apos.x;
                sA[threadIdx.x].y       = apos.y;
                sA[threadIdx.x].z       = apos.z;
                sA[threadIdx.x].q       = apos.w;
                sA[threadIdx.x].sig     = a.x;
                sA[threadIdx.x].eps     = a.y;
                af.x                    = 0.0f;
                af.y                    = 0.0f;
                af.z                    = 0.0f;
                sA[threadIdx.x].sig2    = a.x;
                sA[threadIdx.x].eps2    = a.y;
                apos.w                 *= cSim.epsfac;
                
                for (unsigned int j = 0; j < GRID; j++)
                {
                    dx              = psA[j].x - apos.x; 
                    dy              = psA[j].y - apos.y; 
                    dz              = psA[j].z - apos.z; 
                    r2              = dx * dx + dy * dy + dz * dz; 
                    invR            = 1.0f / sqrt(r2);
                    sig             = psA[tgx].sig2 + psA[j].sig; 
                    sig2            = invR * sig; 
                    sig2           *= sig2;
                    sig6            = sig2 * sig2 * sig2; 
                    eps             = psA[tgx].eps2 * psA[j].eps; 
                    dEdR            = eps * (12.0f * sig6 - 6.0f) * sig6; 
                    dEdR           += apos.w * psA[j].q * invR; 
                    dEdR           *= invR * invR; 
                    if (!(excl & 0x1))
                    {
                        dEdR = 0.0f;
                    }
                    dx             *= dEdR; 
                    dy             *= dEdR; 
                    dz             *= dEdR; 
                    af.x           -= dx; 
                    af.y           -= dy; 
                    af.z           -= dz;
                    excl          >>= 1;               
                }
                
                // Write results
                float4 of;
                of.x                                = af.x;
                of.y                                = af.y;
                of.z                                = af.z;
                of.w                                = 0.0f;
                int offset                          = x + tgx + (x >> GRIDBITS) * cSim.stride;
                cSim.pForce4a[offset]               = of;
            }         
            else        // 100% utilization
            {
                // Read fixed atom data into registers and GRF        
                unsigned int excl       = cSim.pExclusion[x * cSim.exclusionStride + y + tgx];
                excl                    = (excl >> tgx) | (excl << (GRID - tgx));
                int j                   = y + tgx;
                unsigned int i          = x + tgx;
                float4 temp             = cSim.pPosq[j];
                float2 temp1            = cSim.pAttr[j];
                apos                    = cSim.pPosq[i];
                float2 a                = cSim.pAttr[i];
                sA[threadIdx.x].x       = temp.x;
                sA[threadIdx.x].y       = temp.y;
                sA[threadIdx.x].z       = temp.z;
                sA[threadIdx.x].q       = temp.w;
                sA[threadIdx.x].sig     = temp1.x;
                sA[threadIdx.x].eps     = temp1.y;
                sA[threadIdx.x].fx      = af.x = 0.0f;
                sA[threadIdx.x].fy      = af.y = 0.0f;
                sA[threadIdx.x].fz      = af.z = 0.0f;
                sA[threadIdx.x].sig2    = a.x;
                sA[threadIdx.x].eps2    = a.y;
                apos.w                 *= cSim.epsfac;
                
                for (j = 0; j < GRID; j++)
                {
                    dx              = psA[tj].x - apos.x; 
                    dy              = psA[tj].y - apos.y; 
                    dz              = psA[tj].z - apos.z; 
                    r2              = dx * dx + dy * dy + dz * dz; 
                    invR            = 1.0f / sqrt(r2);
                    sig             = psA[tgx].sig2 + psA[tj].sig; 
                    sig2            = invR * sig; 
                    sig2           *= sig2;
                    sig6            = sig2 * sig2 * sig2; 
                    eps             = psA[tgx].eps2 * psA[tj].eps; 
                    dEdR            = eps * (12.0f * sig6 - 6.0f) * sig6; 
                    dEdR           += apos.w * psA[tj].q * invR; 
                    dEdR           *= invR * invR; 
                    if (!(excl & 0x1))
                    {
                        dEdR = 0.0f;
                    }
                    dx             *= dEdR; 
                    dy             *= dEdR; 
                    dz             *= dEdR; 
                    af.x           -= dx; 
                    af.y           -= dy; 
                    af.z           -= dz; 
                    psA[tj].fx     += dx; 
                    psA[tj].fy     += dy; 
                    psA[tj].fz     += dz;
                    excl          >>= 1;
                    tj              = sNext[tj]; 
                }
                
                // Write results
                float4 of;
                of.x                                = af.x;
                of.y                                = af.y;
                of.z                                = af.z;
                of.w                                = 0.0f;
                int offset                          = x + tgx + (y >> GRIDBITS) * cSim.stride;
                cSim.pForce4a[offset]               = of;
                of.x                                = sA[threadIdx.x].fx;
                of.y                                = sA[threadIdx.x].fy;
                of.z                                = sA[threadIdx.x].fz;
                offset                              = y + tgx + (x >> GRIDBITS) * cSim.stride;
                cSim.pForce4a[offset]               = of;
            }
        }

        pos -= cSim.nonbond_workBlock;     
    }
}

__global__ extern void kCalculateCDLJForces_12_kernel();

void kCalculateCDLJForces(gpuContext gpu)
{
//    printf("kCalculateCDLJForces\n");
    if (gpu->sm_version < SM_12)
        kCalculateCDLJForces_kernel<<<gpu->sim.nonbond_blocks, gpu->sim.nonbond_threads_per_block>>>();
    else
        kCalculateCDLJForces_12_kernel<<<gpu->sim.nonbond_blocks, gpu->sim.nonbond_threads_per_block>>>();
    LAUNCHERROR("kCalculateCDLJForces");
}