kCalculateAmoebaCudaKirkwood.h 19.6 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs 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
/* -------------------------------------------------------------------------- *
 *                                   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:                                                              *
 *                                                                            *
 * 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.                                        *
 *                                                                            *
 * 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.                        *
 *                                                                            *
 * 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/>.      *
 * -------------------------------------------------------------------------- */

#include "amoebaScaleFactors.h"

__global__
#if (__CUDA_ARCH__ >= 200)
31
__launch_bounds__(256, 1)
Mark Friedrichs's avatar
Mark Friedrichs committed
32
#elif (__CUDA_ARCH__ >= 130)
33
__launch_bounds__(128, 1)
Mark Friedrichs's avatar
Mark Friedrichs committed
34
#else
35
__launch_bounds__(64, 1)
Mark Friedrichs's avatar
Mark Friedrichs committed
36
37
#endif
void METHOD_NAME(kCalculateAmoebaCudaKirkwood, Forces_kernel)(
38
                            unsigned int* workUnit
Mark Friedrichs's avatar
Mark Friedrichs committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifdef AMOEBA_DEBUG
                           , float4* debugArray, unsigned int targetAtom
#endif
){

#ifdef AMOEBA_DEBUG
    float4 pullBack[20];
#endif

    extern __shared__ KirkwoodParticle sA[];

    unsigned int totalWarps      = gridDim.x*blockDim.x/GRID;
    unsigned int warp            = (blockIdx.x*blockDim.x+threadIdx.x)/GRID;
    unsigned int numWorkUnits    = cSim.pInteractionCount[0];
    unsigned int pos             = warp*numWorkUnits/totalWarps;
    unsigned int end             = (warp+1)*numWorkUnits/totalWarps;
    unsigned int lasty           = 0xFFFFFFFF;

57
58
59
60
61
62
    // pWorkArray_3_1 == force
    // pWorkArray_3_2 == torque

    // pWorkArray_1_1 == dBorn
    // pWorkArray_1_2 == dBornPolar

Mark Friedrichs's avatar
Mark Friedrichs committed
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
    float4 jCoord;
    float jDipole[3];     
    float jQuadrupole[9];     
    float jInducedDipole[3];     
    float jInducedDipolePolar[3];     
    float jBornRadius;     

    float energySum = 0.0f;     

    while (pos < end)
    {

        unsigned int x;
        unsigned int y;
        bool bExclusionFlag;

        // extract cell coordinates

        decodeCell( workUnit[pos], &x, &y, &bExclusionFlag );

        unsigned int tgx              = threadIdx.x & (GRID - 1);
        unsigned int tbx              = threadIdx.x - tgx;
        unsigned int tj               = tgx;

        KirkwoodParticle* psA         = &sA[tbx];

        unsigned int atomI            = x + tgx;
90
        float4 iCoord                 =  cSim.pPosq[atomI];
Mark Friedrichs's avatar
Mark Friedrichs committed
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

        float forceSum[3];
        float torqueSum[3];
        float dBornSum;
        float dBornPolarSum;

        forceSum[0]                   = 0.0f;
        forceSum[1]                   = 0.0f;
        forceSum[2]                   = 0.0f;

        torqueSum[0]                  = 0.0f;
        torqueSum[1]                  = 0.0f;
        torqueSum[2]                  = 0.0f;

        dBornSum                      = 0.0f;
        dBornPolarSum                 = 0.0f;

        // handle diagonals uniquely at 50% efficiency

        if (x == y) 
        {

            // load shared data

            loadKirkwoodShared( &(sA[threadIdx.x]), atomI,
116
117
                                cSim.pPosq, cAmoebaSim.pLabFrameDipole, cAmoebaSim.pLabFrameQuadrupole,
                                cAmoebaSim.pInducedDipoleS,  cAmoebaSim.pInducedDipolePolarS, cSim.pBornRadii );
Mark Friedrichs's avatar
Mark Friedrichs committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

            // this branch is never exercised since it includes the
            // interaction between atomI and itself which is always excluded

            for (unsigned int j = 0; j < GRID; j++)
            {

                float force[3];
                float torque[2][3];
                float dBorn[2];
                float dBornPolar[2];
                float energy;

                unsigned int atomJ            = y + j;
                unsigned int sameAtom         = atomI == atomJ ? 1 : 0;

                // load coords, charge, ...

                loadKirkwoodData( &(psA[j]), &jCoord, jDipole, jQuadrupole,
                                  jInducedDipole, jInducedDipolePolar, &jBornRadius );

                calculateKirkwoodPairIxn_kernel( sameAtom,
                                                 iCoord,                                     jCoord,
141
142
143
144
145
                                                 &(cAmoebaSim.pLabFrameDipole[3*atomI]),     jDipole,
                                                 &(cAmoebaSim.pLabFrameQuadrupole[9*atomI]), jQuadrupole,
                                                 &(cAmoebaSim.pInducedDipoleS[3*atomI]),     jInducedDipole,
                                                 &(cAmoebaSim.pInducedDipolePolarS[3*atomI]),jInducedDipolePolar,
                                                 cSim.pBornRadii[atomI],                     jBornRadius,
Mark Friedrichs's avatar
Mark Friedrichs committed
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
                                                 force, torque, dBorn, dBornPolar, &energy
#ifdef AMOEBA_DEBUG
                                          , pullBack
#endif
                                        );

                unsigned int mask       =  ( (atomI >= cAmoebaSim.numberOfAtoms) || (atomJ >= cAmoebaSim.numberOfAtoms) ) ? 0 : 1;

                // torques include i == j contribution

                torqueSum[0]           += mask ? torque[0][0]  : 0.0f;
                torqueSum[1]           += mask ? torque[0][1]  : 0.0f;
                torqueSum[2]           += mask ? torque[0][2]  : 0.0f;

                dBornSum               += mask ? dBorn[0]      : 0.0f;
                dBornPolarSum          += mask ? dBornPolar[0] : 0.0f;
                energySum              += mask ? 0.5f*energy   : 0.0f;

                // add to field at atomI the field due atomJ's charge/dipole/quadrupole

                mask                    =  (atomI == atomJ) ? 0 : mask;

                forceSum[0]            += mask ? force[0]      : 0.0f;
                forceSum[1]            += mask ? force[1]      : 0.0f;
                forceSum[2]            += mask ? force[2]      : 0.0f;


#ifdef AMOEBA_DEBUG
if( atomI == targetAtom  || atomJ == targetAtom ){

        unsigned int index                 = (atomI == targetAtom) ? atomJ : atomI;
        unsigned int indexI                = (atomI == targetAtom) ? 0 : 1;
        unsigned int indexJ                = (atomI == targetAtom) ? 1 : 0;
        //float forceSign                    = (atomI == targetAtom) ? 1.0f : -1.0f;

        debugArray[index].x                = (float) atomI;
        debugArray[index].y                = (float) atomJ;
        debugArray[index].z                = (float) (mask + 1);
184
        //debugArray[index].z                = cSim.pBornRadii[atomI];
Mark Friedrichs's avatar
Mark Friedrichs committed
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
        //debugArray[index].z                = energy;
        //debugArray[index].w                = (float) (blockIdx.x*blockDim.x+threadIdx.x);
        debugArray[index].w                = jBornRadius;

        index = debugAccumulate( index, debugArray, force,          mask, 1.0f );

        mask  =  ( (atomI >= cAmoebaSim.numberOfAtoms) || (atomJ >= cAmoebaSim.numberOfAtoms) ) ? 0 : 1;
        index = debugAccumulate( index, debugArray, torque[indexI], mask, 2.0f );
        index = debugAccumulate( index, debugArray, torque[indexJ], mask, 3.0f );

        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = pullBack[0].x;
        debugArray[index].y                = pullBack[0].y;
        debugArray[index].z                = pullBack[0].z;
        debugArray[index].w                = pullBack[0].w;

        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = pullBack[1].x;
        debugArray[index].y                = pullBack[1].y;
        debugArray[index].z                = pullBack[1].z;
        debugArray[index].w                = pullBack[1].w;

        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = pullBack[2].x;
        debugArray[index].y                = pullBack[2].y;
        debugArray[index].z                = pullBack[2].z;
        debugArray[index].w                = pullBack[2].w;
/*
        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = dBorn[0];
        debugArray[index].y                = dBornPolar[0];
        debugArray[index].z                = dBorn[1];
        debugArray[index].w                = dBornPolar[1];
*/
}
#endif

            } // end of j-loop

            // write results

#ifdef USE_OUTPUT_BUFFER_PER_WARP
            float  of;
            unsigned int offset                 = x + tgx + warp*cAmoebaSim.paddedNumberOfAtoms;

230
            of                                  = cAmoebaSim.pWorkArray_1_1[offset];
Mark Friedrichs's avatar
Mark Friedrichs committed
231
            of                                 += dBornSum;
232
            cAmoebaSim.pWorkArray_1_1[offset]   = of;
Mark Friedrichs's avatar
Mark Friedrichs committed
233

234
            of                                  = cAmoebaSim.pWorkArray_1_2[offset];
Mark Friedrichs's avatar
Mark Friedrichs committed
235
            of                                 += dBornPolarSum;
236
            cAmoebaSim.pWorkArray_1_2[offset]   = of;
Mark Friedrichs's avatar
Mark Friedrichs committed
237
238
239
240


            offset                             *= 3;

241
242
            load3dArrayBufferPerWarp( offset, forceSum,  cAmoebaSim.pWorkArray_3_1 );
            load3dArrayBufferPerWarp( offset, torqueSum, cAmoebaSim.pWorkArray_3_2 );
Mark Friedrichs's avatar
Mark Friedrichs committed
243
244
245
246

#else
            unsigned int offset                 = x + tgx + (x >> GRIDBITS) * cAmoebaSim.paddedNumberOfAtoms;

247
248
            cAmoebaSim.pWorkArray_1_1[offset]   = dBornSum;
            cAmoebaSim.pWorkArray_1_2[offset]   = dBornPolarSum;
Mark Friedrichs's avatar
Mark Friedrichs committed
249
250
251

            offset                             *= 3;

252
253
            load3dArray( offset, forceSum,  cAmoebaSim.pWorkArray_3_1 );
            load3dArray( offset, torqueSum, cAmoebaSim.pWorkArray_3_2 );
Mark Friedrichs's avatar
Mark Friedrichs committed
254
255
256
257
258
259
260
261
262
263
264
265
266

#endif

        }
        else        // 100% utilization
        {
            // Read fixed atom data into registers and GRF

            if (lasty != y)
            {
                // load shared data

                loadKirkwoodShared( &(sA[threadIdx.x]), (y+tgx),
267
268
                                    cSim.pPosq, cAmoebaSim.pLabFrameDipole, cAmoebaSim.pLabFrameQuadrupole,
                                    cAmoebaSim.pInducedDipoleS, cAmoebaSim.pInducedDipolePolarS, cSim.pBornRadii);
Mark Friedrichs's avatar
Mark Friedrichs committed
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

            }

            // zero j-atom output fields

            zeroKirkwoodParticleSharedField( &(sA[threadIdx.x]) );

            for (unsigned int j = 0; j < GRID; j++)
            {

                float force[3];
                float torque[2][3];

                float dBorn[2];
                float dBornPolar[2];
                float energy;

                unsigned int atomJ    = y + tj;
                unsigned int sameAtom = 0;

                // load coords, charge, ...

                loadKirkwoodData( &(psA[tj]), &jCoord, jDipole, jQuadrupole,
                                  jInducedDipole, jInducedDipolePolar, &jBornRadius );

                calculateKirkwoodPairIxn_kernel( sameAtom, 
295
296
297
298
299
300
                                                 iCoord,                                       jCoord,
                                                 &(cAmoebaSim.pLabFrameDipole[3*atomI]),       jDipole,
                                                 &(cAmoebaSim.pLabFrameQuadrupole[9*atomI]),   jQuadrupole,
                                                 &(cAmoebaSim.pInducedDipoleS[3*atomI]),       jInducedDipole,
                                                 &(cAmoebaSim.pInducedDipolePolarS[3*atomI]),  jInducedDipolePolar,
                                                 cSim.pBornRadii[atomI],                       jBornRadius,
Mark Friedrichs's avatar
Mark Friedrichs committed
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
                                                 force, torque, dBorn, dBornPolar, &energy
#ifdef AMOEBA_DEBUG
                                          , pullBack
#endif
                                        );

                unsigned int mask          =  ( (atomI >= cAmoebaSim.numberOfAtoms) || ( atomJ >= cAmoebaSim.numberOfAtoms) ) ? 0 : 1;

                // add force and torque to atom I due atom J

                forceSum[0]               += mask ? force[0]      : 0.0f;
                forceSum[1]               += mask ? force[1]      : 0.0f;
                forceSum[2]               += mask ? force[2]      : 0.0f;

                torqueSum[0]              += mask ? torque[0][0]  : 0.0f;
                torqueSum[1]              += mask ? torque[0][1]  : 0.0f;
                torqueSum[2]              += mask ? torque[0][2]  : 0.0f;

                dBornSum                  += mask ? dBorn[0]      : 0.0f;
                dBornPolarSum             += mask ? dBornPolar[0] : 0.0f;
                energySum                 += mask ? energy        : 0.0f;

                // add force and torque to atom J due atom I

                psA[tj].force[0]          -= mask ? force[0]      : 0.0f;
                psA[tj].force[1]          -= mask ? force[1]      : 0.0f;
                psA[tj].force[2]          -= mask ? force[2]      : 0.0f;

                psA[tj].torque[0]         += mask ? torque[1][0]  : 0.0f;
                psA[tj].torque[1]         += mask ? torque[1][1]  : 0.0f;
                psA[tj].torque[2]         += mask ? torque[1][2]  : 0.0f;

                psA[tj].dBornRadius       += mask ? dBorn[1]      : 0.0f;
                psA[tj].dBornRadiusPolar  += mask ? dBornPolar[1] : 0.0f;

#ifdef AMOEBA_DEBUG
if( atomI == targetAtom || atomJ == targetAtom ){
        unsigned int index                 = (atomI == targetAtom) ? atomJ : atomI;
        unsigned int indexI                = (atomI == targetAtom) ? 0 : 1;
        unsigned int indexJ                = (atomI == targetAtom) ? 1 : 0;
        // float forceSign                    = (atomI == targetAtom) ? 1.0f : -1.0f;

        debugArray[index].x                = (float) atomI;
        debugArray[index].y                = (float) atomJ;
        debugArray[index].z                = (float) (mask+1);
346
        //debugArray[index].z                = cSim.pBornRadii[atomI];
Mark Friedrichs's avatar
Mark Friedrichs committed
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
        //debugArray[index].z                = energy;
        debugArray[index].w                = (float) (blockIdx.x*blockDim.x+threadIdx.x);

        index = debugAccumulate( index, debugArray, force,          mask, -1.0f );
        index = debugAccumulate( index, debugArray, torque[indexI], mask, -2.0f );
        index = debugAccumulate( index, debugArray, torque[indexJ], mask, -3.0f );

        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = pullBack[0].x;
        debugArray[index].y                = pullBack[0].y;
        debugArray[index].z                = pullBack[0].z;
        debugArray[index].w                = -1.0f;

        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = pullBack[1].x;
        debugArray[index].y                = pullBack[1].y;
        debugArray[index].z                = pullBack[1].z;
        debugArray[index].w                = pullBack[1].w;

        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = pullBack[2].x;
        debugArray[index].y                = pullBack[2].y;
        debugArray[index].z                = pullBack[2].z;
        debugArray[index].w                = pullBack[2].w;
/*
        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = dBorn[0];
        debugArray[index].y                = dBornPolar[0];
        debugArray[index].z                = dBorn[1];
        debugArray[index].w                = dBornPolar[1];
*/

}
#endif
//#ifdef AMOEBA_DEBUG
#if 0
if( mask || !mask ){
        unsigned int index                 = atomJ + atomI*cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = (float) atomI;
        debugArray[index].y                = (float) atomJ;
        debugArray[index].z                = energy;
        debugArray[index].w                = jBornRadius;
}
#endif

                tj                  = (tj + 1) & (GRID - 1);

            }

            // Write results

#ifdef USE_OUTPUT_BUFFER_PER_WARP

            float of;
            unsigned int offset                 = x + tgx + warp*cAmoebaSim.paddedNumberOfAtoms;

403
            of                                  = cAmoebaSim.pWorkArray_1_1[offset];
Mark Friedrichs's avatar
Mark Friedrichs committed
404
            of                                 += dBornSum;
405
            cAmoebaSim.pWorkArray_1_1[offset]   = of;
Mark Friedrichs's avatar
Mark Friedrichs committed
406

407
            of                                  = cAmoebaSim.pWorkArray_1_2[offset];
Mark Friedrichs's avatar
Mark Friedrichs committed
408
            of                                 += dBornPolarSum;
409
            cAmoebaSim.pWorkArray_1_2[offset]   = of;
Mark Friedrichs's avatar
Mark Friedrichs committed
410
411
412

            offset                             *= 3;

413
414
            load3dArrayBufferPerWarp( offset, forceSum,  cAmoebaSim.pWorkArray_3_1 );
            load3dArrayBufferPerWarp( offset, torqueSum, cAmoebaSim.pWorkArray_3_2 );
Mark Friedrichs's avatar
Mark Friedrichs committed
415
416
417

            offset                              = y + tgx + warp*cAmoebaSim.paddedNumberOfAtoms;

418
            of                                  = cAmoebaSim.pWorkArray_1_1[offset];
Mark Friedrichs's avatar
Mark Friedrichs committed
419
            of                                 += dBornSum;
420
            cAmoebaSim.pWorkArray_1_1[offset]   = of;
Mark Friedrichs's avatar
Mark Friedrichs committed
421

422
            of                                  = cAmoebaSim.pWorkArray_1_2[offset];
Mark Friedrichs's avatar
Mark Friedrichs committed
423
            of                                 += dBornPolarSum;
424
            cAmoebaSim.pWorkArray_1_2[offset]   = of;
Mark Friedrichs's avatar
Mark Friedrichs committed
425
426
427

            offset                             *= 3;

428
429
            load3dArrayBufferPerWarp( offset, sA[threadIdx.x].force,  cAmoebaSim.pWorkArray_3_1 );
            load3dArrayBufferPerWarp( offset, sA[threadIdx.x].torque, cAmoebaSim.pWorkArray_3_2 );
Mark Friedrichs's avatar
Mark Friedrichs committed
430
431
432
#else
            unsigned int offset                 = x + tgx + (y >> GRIDBITS) * cAmoebaSim.paddedNumberOfAtoms;

433
434
            cAmoebaSim.pWorkArray_1_1[offset]   = dBornSum;
            cAmoebaSim.pWorkArray_1_2[offset]   = dBornPolarSum;
Mark Friedrichs's avatar
Mark Friedrichs committed
435
436
437

            offset                             *= 3;

438
439
            load3dArray( offset, forceSum,  cAmoebaSim.pWorkArray_3_1 );
            load3dArray( offset, torqueSum, cAmoebaSim.pWorkArray_3_2 );
Mark Friedrichs's avatar
Mark Friedrichs committed
440
441
442
443


            offset                              = y + tgx + (x >> GRIDBITS) * cAmoebaSim.paddedNumberOfAtoms;

444
445
            cAmoebaSim.pWorkArray_1_1[offset]   = sA[threadIdx.x].dBornRadius;
            cAmoebaSim.pWorkArray_1_2[offset]   = sA[threadIdx.x].dBornRadiusPolar;
Mark Friedrichs's avatar
Mark Friedrichs committed
446
447
448

            offset                             *= 3;

449
450
            load3dArray( offset, sA[threadIdx.x].force,  cAmoebaSim.pWorkArray_3_1 );
            load3dArray( offset, sA[threadIdx.x].torque, cAmoebaSim.pWorkArray_3_2 );
Mark Friedrichs's avatar
Mark Friedrichs committed
451
452
453
454
455
456
457
458

#endif
            lasty = y;
        }
        pos++;
    }
    cSim.pEnergy[blockIdx.x*blockDim.x+threadIdx.x] += energySum;
}