kCalculateAmoebaCudaElectrostatic.h 33.2 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)
Peter Eastman's avatar
Peter Eastman committed
31
__launch_bounds__(384, 1)
32
#elif (__CUDA_ARCH__ >= 120)
Peter Eastman's avatar
Peter Eastman committed
33
__launch_bounds__(128, 1)
Mark Friedrichs's avatar
Mark Friedrichs committed
34
#else
Peter Eastman's avatar
Peter Eastman committed
35
__launch_bounds__(64, 1)
Mark Friedrichs's avatar
Mark Friedrichs committed
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
#endif
void METHOD_NAME(kCalculateAmoebaCudaElectrostatic, Forces_kernel)(
                            unsigned int* workUnit,
                            float4* atomCoord,
                            float* labFrameDipole,
                            float* labFrameQuadrupole,
                            float* inducedDipole,
                            float* inducedDipolePolar,
                            float* outputForce,
                            float* outputTorque

#ifdef AMOEBA_DEBUG
                           , float4* debugArray, unsigned int targetAtom
#endif
){

#ifdef AMOEBA_DEBUG
    float4 pullBack[20];
#endif

    extern __shared__ ElectrostaticParticle 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;
    float totalEnergy            = 0.0f;     
65
    float conversionFactor       = (cAmoebaSim.electric/cAmoebaSim.dielec);
Mark Friedrichs's avatar
Mark Friedrichs committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    float scalingFactors[LastScalingIndex];

    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;

        ElectrostaticParticle* psA    = &sA[tbx];
        unsigned int atomI            = x + tgx;
85
86
87
88
        ElectrostaticParticle localParticle;
        loadElectrostaticShared(&localParticle, atomI,
                                atomCoord, labFrameDipole, labFrameQuadrupole,
                                inducedDipole, inducedDipolePolar, cAmoebaSim.pDampingFactorAndThole );
Mark Friedrichs's avatar
Mark Friedrichs committed
89

90
91
92
        localParticle.force[0]        = 0.0f;
        localParticle.force[1]        = 0.0f;
        localParticle.force[2]        = 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
93

94
95
96
        localParticle.torque[0]       = 0.0f;
        localParticle.torque[1]       = 0.0f;
        localParticle.torque[2]       = 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

        scalingFactors[PScaleIndex]   = 1.0f;
        scalingFactors[DScaleIndex]   = 1.0f;
        scalingFactors[UScaleIndex]   = 1.0f;
        scalingFactors[MScaleIndex]   = 1.0f;

        if (x == y) // Handle diagonals uniquely at 50% efficiency
        {

            // load shared data

           loadElectrostaticShared( &(sA[threadIdx.x]), atomI,
                                    atomCoord, labFrameDipole, labFrameQuadrupole,
                                    inducedDipole, inducedDipolePolar, cAmoebaSim.pDampingFactorAndThole );

            if (!bExclusionFlag)
            {

                // 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++)
                {

121
122
                    float4 force;
                    float4 torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
123

124
                    calculateElectrostaticPairIxn_kernel( localParticle, psA[j], scalingFactors, &force, torque
Mark Friedrichs's avatar
Mark Friedrichs committed
125
126
127
128
129
#ifdef AMOEBA_DEBUG
, pullBack
#endif
 );

130
                    if( (atomI != (y + j)) && (atomI < cAmoebaSim.numberOfAtoms) && ((y+j) < cAmoebaSim.numberOfAtoms) ){
Mark Friedrichs's avatar
Mark Friedrichs committed
131

132
133
134
                         localParticle.force[0]            += force.x;
                         localParticle.force[1]            += force.y;
                         localParticle.force[2]            += force.z;
Mark Friedrichs's avatar
Mark Friedrichs committed
135

136
137
138
139
140
141
142
                         totalEnergy                       += 0.5f*force.w;
     
                         localParticle.torque[0]           += torque[0].x;
                         localParticle.torque[1]           += torque[0].y;
                         localParticle.torque[2]           += torque[0].z;
      
                    }
Mark Friedrichs's avatar
Mark Friedrichs committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
                }

            }
            else  // bExclusion
            {
                unsigned int xi       = x >> GRIDBITS;
                unsigned int cell     = xi + xi*cAmoebaSim.paddedNumberOfAtoms/GRID-xi*(xi+1)/2;
                int  dScaleMask       = cAmoebaSim.pD_ScaleIndices[cAmoebaSim.pScaleIndicesIndex[cell]+tgx];
                int2 pScaleMask       = cAmoebaSim.pP_ScaleIndices[cAmoebaSim.pScaleIndicesIndex[cell]+tgx];
                int2 mScaleMask       = cAmoebaSim.pM_ScaleIndices[cAmoebaSim.pScaleIndicesIndex[cell]+tgx];

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

157
158
                    float4 force;
                    float4 torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
159
160
161
162
163
164
165
166
167
168
169

                    unsigned int atomJ = y + j;

                    // set scale factors

                    getMaskedDScaleFactor( j, dScaleMask, scalingFactors + DScaleIndex );
                    getMaskedPScaleFactor( j, pScaleMask, scalingFactors + PScaleIndex );
                    getMaskedMScaleFactor( j, mScaleMask, scalingFactors + MScaleIndex );

                    // force

170
                    calculateElectrostaticPairIxn_kernel( localParticle, psA[j], scalingFactors, &force, torque
Mark Friedrichs's avatar
Mark Friedrichs committed
171
172
173
174
175
176
177
178
#ifdef AMOEBA_DEBUG
, pullBack
#endif
 );

                    // nan*0.0 = nan not 0.0, so explicitly exclude (atomI == atomJ) contribution
                    // by setting match flag

179
                    if( (atomI != atomJ) && (atomI < cAmoebaSim.numberOfAtoms) && (atomJ < cAmoebaSim.numberOfAtoms) ){
Mark Friedrichs's avatar
Mark Friedrichs committed
180

181
182
183
                        localParticle.force[0]            += force.x;
                        localParticle.force[1]            += force.y;
                        localParticle.force[2]            += force.z;
Mark Friedrichs's avatar
Mark Friedrichs committed
184

185
                        totalEnergy                       += 0.5f*force.w;
Mark Friedrichs's avatar
Mark Friedrichs committed
186

187
188
189
190
                        localParticle.torque[0]           += torque[0].x;
                        localParticle.torque[1]           += torque[0].y;
                        localParticle.torque[2]           += torque[0].z;
      
Mark Friedrichs's avatar
Mark Friedrichs committed
191

192
193
                    }
    
Mark Friedrichs's avatar
Mark Friedrichs committed
194
195
196
197
198
199
200
201
#ifdef AMOEBA_DEBUG
if( atomI == targetAtom ){
            unsigned int index                 = (atomI == targetAtom) ? atomJ : atomI;

            debugArray[index].x                = (float) atomI;
            debugArray[index].y                = (float) atomJ;
            debugArray[index].z                = 1.0f;
            debugArray[index].w                = (float) y;
202
203
204
205
206
207
208
209
210

            unsigned int mask                  = (atomI != atomJ) && (atomI < cAmoebaSim.numberOfAtoms) && (atomJ < cAmoebaSim.numberOfAtoms) ? 1 : 0;
            index                             += cAmoebaSim.paddedNumberOfAtoms;
            debugArray[index].x                = mask ? conversionFactor*force.x : 0.0f;
            debugArray[index].y                = mask ? conversionFactor*force.y : 0.0f;
            debugArray[index].z                = mask ? conversionFactor*force.z : 0.0f;
            debugArray[index].w                = -1.1f;


Mark Friedrichs's avatar
Mark Friedrichs committed
211
            index                             += cAmoebaSim.paddedNumberOfAtoms;
212
213
214
215
            debugArray[index].x                = mask ? conversionFactor*torque[0].x : 0.0f;
            debugArray[index].y                = mask ? conversionFactor*torque[0].y : 0.0f;
            debugArray[index].z                = mask ? conversionFactor*torque[0].z : 0.0f;
            debugArray[index].w                = -2.1f;
Mark Friedrichs's avatar
Mark Friedrichs committed
216
217
218


            index                             += cAmoebaSim.paddedNumberOfAtoms;
219
220
221
222
223
            debugArray[index].x                = mask ? conversionFactor*torque[1].x : 0.0f;
            debugArray[index].y                = mask ? conversionFactor*torque[1].y : 0.0f;
            debugArray[index].z                = mask ? conversionFactor*torque[1].z : 0.0f;
            debugArray[index].w                = -2.2f;

Mark Friedrichs's avatar
Mark Friedrichs committed
224
225
226
227
228
229
230
231
232
233

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

            index                             += cAmoebaSim.paddedNumberOfAtoms;
            pullIndex++;
234
235
236
            debugArray[index].x                = conversionFactor*pullBack[pullIndex].x;
            debugArray[index].y                = conversionFactor*pullBack[pullIndex].y;
            debugArray[index].z                = conversionFactor*pullBack[pullIndex].z;
Mark Friedrichs's avatar
Mark Friedrichs committed
237
238
239
240
            debugArray[index].w                = pullBack[pullIndex].w;

            index                             += cAmoebaSim.paddedNumberOfAtoms;
            pullIndex++;
241
242
243
            debugArray[index].x                = conversionFactor*pullBack[pullIndex].x;
            debugArray[index].y                = conversionFactor*pullBack[pullIndex].y;
            debugArray[index].z                = conversionFactor*pullBack[pullIndex].z;
Mark Friedrichs's avatar
Mark Friedrichs committed
244
245
246
247
            debugArray[index].w                = pullBack[pullIndex].w;

            index                             += cAmoebaSim.paddedNumberOfAtoms;
            pullIndex++;
248
249
250
            debugArray[index].x                = conversionFactor*pullBack[pullIndex].x;
            debugArray[index].y                = conversionFactor*pullBack[pullIndex].y;
            debugArray[index].z                = conversionFactor*pullBack[pullIndex].z;
Mark Friedrichs's avatar
Mark Friedrichs committed
251
252
253
254
255
256
257
258
259
260
            debugArray[index].w                = pullBack[pullIndex].w;

}
#endif

                }
            }

            // Write results

261
262
263
264
265
266
267
268
           localParticle.force[0]  *= conversionFactor;
           localParticle.force[1]  *= conversionFactor;
           localParticle.force[2]  *= conversionFactor;

           localParticle.torque[0] *= conversionFactor;
           localParticle.torque[1] *= conversionFactor;
           localParticle.torque[2] *= conversionFactor;

Mark Friedrichs's avatar
Mark Friedrichs committed
269
270
271
272
#ifdef USE_OUTPUT_BUFFER_PER_WARP
            float  of;
            unsigned int offset                 = 3*(x + tgx + warp*cAmoebaSim.paddedNumberOfAtoms);
            of                                  = outputForce[offset];
273
            of                                 += localParticle.force[0];
Mark Friedrichs's avatar
Mark Friedrichs committed
274
275
276
            outputForce[offset]                 = of;

            of                                  = outputForce[offset+1];
277
            of                                 += localParticle.force[1];
Mark Friedrichs's avatar
Mark Friedrichs committed
278
279
280
            outputForce[offset+1]               = of;

            of                                  = outputForce[offset+2];
281
            of                                 += localParticle.force[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
282
283
284
            outputForce[offset+2]               = of;

            of                                  = outputTorque[offset];
285
            of                                 += localParticle.torque[0];
Mark Friedrichs's avatar
Mark Friedrichs committed
286
287
288
            outputTorque[offset]                = of;

            of                                  = outputTorque[offset+1];
289
            of                                 += localParticle.torque[1];
Mark Friedrichs's avatar
Mark Friedrichs committed
290
291
292
            outputTorque[offset+1]              = of;

            of                                  = outputTorque[offset+2];
293
            of                                 += localParticle.torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
294
295
296
297
            outputTorque[offset+2]              = of;

#else
            unsigned int offset                 = 3*(x + tgx + (x >> GRIDBITS) * cAmoebaSim.paddedNumberOfAtoms);
298
299
300
            outputForce[offset]                 = localParticle.force[0];
            outputForce[offset+1]               = localParticle.force[1];
            outputForce[offset+2]               = localParticle.force[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
301

302
303
304
            outputTorque[offset]                = localParticle.torque[0];
            outputTorque[offset+1]              = localParticle.torque[1];
            outputTorque[offset+2]              = localParticle.torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#endif

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

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

               loadElectrostaticShared( &(sA[threadIdx.x]), (y+tgx),
                                        atomCoord, labFrameDipole, labFrameQuadrupole,
                                        inducedDipole, inducedDipolePolar, cAmoebaSim.pDampingFactorAndThole );

            }

322
323
324
            sA[threadIdx.x].force[0]     = 0.0f;
            sA[threadIdx.x].force[1]     = 0.0f;
            sA[threadIdx.x].force[2]     = 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
325

326
327
328
            sA[threadIdx.x].torque[0]    = 0.0f;
            sA[threadIdx.x].torque[1]    = 0.0f;
            sA[threadIdx.x].torque[2]    = 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
329
330
331
332
333
334

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

335
336
                    float4 force;
                    float4 torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
337
338
339

                    unsigned int atomJ = y + tj;

340
                    calculateElectrostaticPairIxn_kernel( localParticle, psA[tj], scalingFactors, &force, torque
Mark Friedrichs's avatar
Mark Friedrichs committed
341
342
343
344
345
#ifdef AMOEBA_DEBUG
, pullBack
#endif
 );
       
346
                    if( (atomI < cAmoebaSim.numberOfAtoms) && (atomJ < cAmoebaSim.numberOfAtoms) ){ 
Mark Friedrichs's avatar
Mark Friedrichs committed
347

348
                        // add force and torque to atom I due atom J
Mark Friedrichs's avatar
Mark Friedrichs committed
349
           
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
                        localParticle.force[0]         += force.x;
                        localParticle.force[1]         += force.y;
                        localParticle.force[2]         += force.z;

                        localParticle.torque[0]        += torque[0].x;
                        localParticle.torque[1]        += torque[0].y;
                        localParticle.torque[2]        += torque[0].z;
    
                        totalEnergy                    += force.w;
               
                        // add force and torque to atom J due atom I
           
                        psA[tj].force[0]               -= force.x;
                        psA[tj].force[1]               -= force.y;
                        psA[tj].force[2]               -= force.z;
           
                        psA[tj].torque[0]              += torque[1].x;
                        psA[tj].torque[1]              += torque[1].y;
                        psA[tj].torque[2]              += torque[1].z;
                    }
Mark Friedrichs's avatar
Mark Friedrichs committed
370
371
372
373
374
375
376
377
378
379
380
381
382
       
#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                = 2.0f;
        debugArray[index].w                = (float) y;

383
384
385
386
387
388
        unsigned int mask                  = (atomI < cAmoebaSim.numberOfAtoms) && (atomJ < cAmoebaSim.numberOfAtoms) ? 1 : 0;
        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = mask ? conversionFactor*forceSign*force.x : 0.0f;
        debugArray[index].y                = mask ? conversionFactor*forceSign*force.y : 0.0f;
        debugArray[index].z                = mask ? conversionFactor*forceSign*force.z : 0.0f;
        debugArray[index].w                = -3.1f;
Mark Friedrichs's avatar
Mark Friedrichs committed
389
390

        index                             += cAmoebaSim.paddedNumberOfAtoms;
391
392
393
394
        debugArray[index].x                = mask ? conversionFactor*torque[indexI].x  : 0.0f;
        debugArray[index].y                = mask ? conversionFactor*torque[indexI].y  : 0.0f;
        debugArray[index].z                = mask ? conversionFactor*torque[indexI].z  : 0.0f;
        debugArray[index].w                = -4.1f;
Mark Friedrichs's avatar
Mark Friedrichs committed
395
396

        index                             += cAmoebaSim.paddedNumberOfAtoms;
397
398
399
400
401
        debugArray[index].x                = mask ? conversionFactor*torque[indexJ].x  : 0.0f;
        debugArray[index].y                = mask ? conversionFactor*torque[indexJ].y  : 0.0f;
        debugArray[index].z                = mask ? conversionFactor*torque[indexJ].z  : 0.0f;
        debugArray[index].w                = -5.1f;

Mark Friedrichs's avatar
Mark Friedrichs committed
402
403
404
405
406
407
408
409
410
        index                             += cAmoebaSim.paddedNumberOfAtoms;
        int pullIndex                      = 0;
        debugArray[index].x                = pullBack[pullIndex].x;
        debugArray[index].y                = pullBack[pullIndex].y;
        debugArray[index].z                = pullBack[pullIndex].z;
        debugArray[index].w                = pullBack[pullIndex].w;

        index                             += cAmoebaSim.paddedNumberOfAtoms;
        pullIndex++;
411
412
413
414
        debugArray[index].x                = conversionFactor*pullBack[pullIndex].x;
        debugArray[index].y                = conversionFactor*pullBack[pullIndex].y;
        debugArray[index].z                = conversionFactor*pullBack[pullIndex].z;
        debugArray[index].w                = conversionFactor*pullBack[pullIndex].w;
Mark Friedrichs's avatar
Mark Friedrichs committed
415
416
417

        index                             += cAmoebaSim.paddedNumberOfAtoms;
        pullIndex++;
418
419
420
421
        debugArray[index].x                = conversionFactor*pullBack[pullIndex].x;
        debugArray[index].y                = conversionFactor*pullBack[pullIndex].y;
        debugArray[index].z                = conversionFactor*pullBack[pullIndex].z;
        debugArray[index].w                = conversionFactor*pullBack[pullIndex].w;
Mark Friedrichs's avatar
Mark Friedrichs committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
       
        index                             += cAmoebaSim.paddedNumberOfAtoms;
        pullIndex++;
        debugArray[index].x                = pullBack[pullIndex].x;
        debugArray[index].y                = pullBack[pullIndex].y;
        debugArray[index].z                = pullBack[pullIndex].z;
        debugArray[index].w                = pullBack[pullIndex].w;
       
}
#endif
       
                           tj                  = (tj + 1) & (GRID - 1);
       
                       }
                   }
                   else  // bExclusion
                   {
                       // Read fixed atom data into registers and GRF
       
                       unsigned int xi   = x >> GRIDBITS;
                       unsigned int yi   = y >> GRIDBITS;
                       unsigned int cell = xi+yi*cAmoebaSim.paddedNumberOfAtoms/GRID-yi*(yi+1)/2;
                       int  dScaleMask   = cAmoebaSim.pD_ScaleIndices[cAmoebaSim.pScaleIndicesIndex[cell]+tgx];
                       int2 pScaleMask   = cAmoebaSim.pP_ScaleIndices[cAmoebaSim.pScaleIndicesIndex[cell]+tgx];
                       int2 mScaleMask   = cAmoebaSim.pM_ScaleIndices[cAmoebaSim.pScaleIndicesIndex[cell]+tgx];
       
                       for (unsigned int j = 0; j < GRID; j++)
                       {
       
451
452
                           float4 force;
                           float4 torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
453
454
455
456
457
458
459
460
461
462
463
       
                           unsigned int atomJ = y + tj;
       
                           // set scale factors
       
                           getMaskedDScaleFactor( tj, dScaleMask, scalingFactors + DScaleIndex );
                           getMaskedPScaleFactor( tj, pScaleMask, scalingFactors + PScaleIndex );
                           getMaskedMScaleFactor( tj, mScaleMask, scalingFactors + MScaleIndex );
       
                           // force
       
464
                           calculateElectrostaticPairIxn_kernel( localParticle, psA[tj], scalingFactors, &force, torque
Mark Friedrichs's avatar
Mark Friedrichs committed
465
466
467
468
469
470
471
#ifdef AMOEBA_DEBUG
, pullBack
#endif
 );

                    // check if atoms out-of-bounds

472
                    if( (atomI < cAmoebaSim.numberOfAtoms) && (atomJ < cAmoebaSim.numberOfAtoms) ){
Mark Friedrichs's avatar
Mark Friedrichs committed
473

474
                        // add force and torque to atom I due atom J
Mark Friedrichs's avatar
Mark Friedrichs committed
475
    
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
                        localParticle.force[0]         += force.x;
                        localParticle.force[1]         += force.y;
                        localParticle.force[2]         += force.z;
    
                        localParticle.torque[0]        += torque[0].x;
                        localParticle.torque[1]        += torque[0].y;
                        localParticle.torque[2]        += torque[0].z;
        
                        totalEnergy                    += force.w;
    
                        // add force and torque to atom J due atom I
    
                        psA[tj].force[0]               -= force.x;
                        psA[tj].force[1]               -= force.y;
                        psA[tj].force[2]               -= force.z;
    
                        psA[tj].torque[0]              += torque[1].x;
                        psA[tj].torque[1]              += torque[1].y;
                        psA[tj].torque[2]              += torque[1].z;
    
                    }
Mark Friedrichs's avatar
Mark Friedrichs committed
497
498
499

#ifdef AMOEBA_DEBUG
if( atomI == targetAtom  || atomJ == targetAtom ){
500
501
502
503
        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;
Mark Friedrichs's avatar
Mark Friedrichs committed
504

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
        debugArray[index].x                   = (float) atomI;
        debugArray[index].y                   = (float) atomJ;
        debugArray[index].z                   = 3.0f;
        debugArray[index].w                   = (float) y;

        unsigned int mask                     = (atomI < cAmoebaSim.numberOfAtoms) && (atomJ < cAmoebaSim.numberOfAtoms) ? 1 : 0;
        index                                += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                   = mask ? conversionFactor*forceSign*force.x : 0.0f;
        debugArray[index].y                   = mask ? conversionFactor*forceSign*force.y : 0.0f;
        debugArray[index].z                   = mask ? conversionFactor*forceSign*force.z : 0.0f;
        debugArray[index].w                   = -6.1f;

        index                                += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                   = mask ? conversionFactor*torque[indexI].x  : 0.0f;
        debugArray[index].y                   = mask ? conversionFactor*torque[indexI].y  : 0.0f;
        debugArray[index].z                   = mask ? conversionFactor*torque[indexI].z  : 0.0f;
        debugArray[index].w                   = -7.1f;

        index                                += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                   = mask ? conversionFactor*torque[indexJ].x  : 0.0f;
        debugArray[index].y                   = mask ? conversionFactor*torque[indexJ].y  : 0.0f;
        debugArray[index].z                   = mask ? conversionFactor*torque[indexJ].z  : 0.0f;
        debugArray[index].w                   = -8.1f;

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

        index                                += cAmoebaSim.paddedNumberOfAtoms;
Mark Friedrichs's avatar
Mark Friedrichs committed
537
        pullIndex++;
538
539
540
541
        debugArray[index].x                   = conversionFactor*pullBack[pullIndex].x;
        debugArray[index].y                   = conversionFactor*pullBack[pullIndex].y;
        debugArray[index].z                   = conversionFactor*pullBack[pullIndex].z;
        debugArray[index].w                   = conversionFactor*pullBack[pullIndex].w;
Mark Friedrichs's avatar
Mark Friedrichs committed
542

543
        index                                += cAmoebaSim.paddedNumberOfAtoms;
Mark Friedrichs's avatar
Mark Friedrichs committed
544
        pullIndex++;
545
546
547
548
        debugArray[index].x                   = conversionFactor*pullBack[pullIndex].x;
        debugArray[index].y                   = conversionFactor*pullBack[pullIndex].y;
        debugArray[index].z                   = conversionFactor*pullBack[pullIndex].z;
        debugArray[index].w                   = conversionFactor*pullBack[pullIndex].w;
Mark Friedrichs's avatar
Mark Friedrichs committed
549
       
550
        index                                += cAmoebaSim.paddedNumberOfAtoms;
Mark Friedrichs's avatar
Mark Friedrichs committed
551
        pullIndex++;
552
553
554
555
        debugArray[index].x                   = pullBack[pullIndex].x;
        debugArray[index].y                   = pullBack[pullIndex].y;
        debugArray[index].z                   = pullBack[pullIndex].z;
        debugArray[index].w                   = pullBack[pullIndex].w;
Mark Friedrichs's avatar
Mark Friedrichs committed
556
557
       
#if 0
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
            index                                += cAmoebaSim.paddedNumberOfAtoms;
            debugArray[index].x                   = scalingFactors[DScaleIndex];
            debugArray[index].y                   = dScaleVal;
            debugArray[index].z                   = scalingFactors[PScaleIndex];
            debugArray[index].w                   = pScaleVal;

            index                                += cAmoebaSim.paddedNumberOfAtoms;
            debugArray[index].x                   = scalingFactors[MScaleIndex];
            debugArray[index].y                   = mScaleVal;
            for( int pIndex    = 0; pIndex < 14; pIndex++ ){
                index                                += cAmoebaSim.paddedNumberOfAtoms;
                debugArray[index].x                   = pullBack[pIndex].x;
                debugArray[index].y                   = pullBack[pIndex].y;
                debugArray[index].z                   = pullBack[pIndex].z;
                debugArray[index].w                   = pullBack[pIndex].w;
Mark Friedrichs's avatar
Mark Friedrichs committed
573
574
            }

575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
            index                                += cAmoebaSim.paddedNumberOfAtoms;
            debugArray[index].x                   = labFrameDipole[3*atomI];
            debugArray[index].y                   = labFrameDipole[3*atomI+1];
            debugArray[index].z                   = labFrameDipole[3*atomI+2];
            debugArray[index].w                   = 25.0f;

            index                                += cAmoebaSim.paddedNumberOfAtoms;
            debugArray[index].x                   = labFrameDipole[3*atomJ];
            debugArray[index].y                   = labFrameDipole[3*atomJ+1];
            debugArray[index].z                   = labFrameDipole[3*atomJ+2];
            debugArray[index].w                   = 26.0f;

            index                                += cAmoebaSim.paddedNumberOfAtoms;
            debugArray[index].x                   = jDipole[0];
            debugArray[index].y                   = jDipole[1];
            debugArray[index].z                   = jDipole[2];
            debugArray[index].w                   = 27.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
592
593
594
595
596
597
598
#endif


}
#endif


599
                    tj                     = (tj + 1) & (GRID - 1);
Mark Friedrichs's avatar
Mark Friedrichs committed
600
601
602
603
604
                }
            }

            // Write results

605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
           localParticle.force[0]     *= conversionFactor;
           localParticle.force[1]     *= conversionFactor;
           localParticle.force[2]     *= conversionFactor;

           localParticle.torque[0]    *= conversionFactor;
           localParticle.torque[1]    *= conversionFactor;
           localParticle.torque[2]    *= conversionFactor;

           sA[threadIdx.x].force[0]   *= conversionFactor;
           sA[threadIdx.x].force[1]   *= conversionFactor;
           sA[threadIdx.x].force[2]   *= conversionFactor;

           sA[threadIdx.x].torque[0]  *= conversionFactor;
           sA[threadIdx.x].torque[1]  *= conversionFactor;
           sA[threadIdx.x].torque[2]  *= conversionFactor;

Mark Friedrichs's avatar
Mark Friedrichs committed
621
622
623
624
625
#ifdef USE_OUTPUT_BUFFER_PER_WARP

            float of;
            unsigned int offset                 = 3*(x + tgx + warp*cAmoebaSim.paddedNumberOfAtoms);
            of                                  = outputForce[offset];
626
            of                                 += localParticle.force[0];
Mark Friedrichs's avatar
Mark Friedrichs committed
627
628
629
            outputForce[offset]                 = of;

            of                                  = outputForce[offset+1];
630
            of                                 += localParticle.force[1];
Mark Friedrichs's avatar
Mark Friedrichs committed
631
632
633
            outputForce[offset+1]               = of;

            of                                  = outputForce[offset+2];
634
            of                                 += localParticle.force[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
635
636
637
            outputForce[offset+2]               = of;

            of                                  = outputTorque[offset];
638
            of                                 += localParticle.torque[0];
Mark Friedrichs's avatar
Mark Friedrichs committed
639
640
641
            outputTorque[offset]                 = of;

            of                                  = outputTorque[offset+1];
642
            of                                 += localParticle.torque[1];
Mark Friedrichs's avatar
Mark Friedrichs committed
643
644
645
            outputTorque[offset+1]               = of;

            of                                  = outputTorque[offset+2];
646
            of                                 += localParticle.torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
647
648
649
650
651
            outputTorque[offset+2]               = of;

            offset                              = 3*(y + tgx + warp*cAmoebaSim.paddedNumberOfAtoms);

            of                                  = outputForce[offset];
652
            of                                 += sA[threadIdx.x].force[0];
Mark Friedrichs's avatar
Mark Friedrichs committed
653
654
655
            outputForce[offset]                 = of;

            of                                  = outputForce[offset+1];
656
            of                                 += sA[threadIdx.x].force[1];
Mark Friedrichs's avatar
Mark Friedrichs committed
657
658
659
            outputForce[offset+1]               = of;

            of                                  = outputForce[offset+2];
660
            of                                 += sA[threadIdx.x].force[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
661
662
663
            outputForce[offset+2]               = of;

            of                                  = outputTorque[offset];
664
            of                                 += sA[threadIdx.x].torque[0];
Mark Friedrichs's avatar
Mark Friedrichs committed
665
666
667
            outputTorque[offset]                = of;

            of                                  = outputTorque[offset+1];
668
            of                                 += sA[threadIdx.x].torque[1];
Mark Friedrichs's avatar
Mark Friedrichs committed
669
670
671
            outputTorque[offset+1]              = of;

            of                                  = outputTorque[offset+2];
672
            of                                 += sA[threadIdx.x].torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
673
674
675
676
677
            outputTorque[offset+2]              = of;

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

678
679
680
            outputForce[offset]                 = localParticle.force[0];
            outputForce[offset+1]               = localParticle.force[1];
            outputForce[offset+2]               = localParticle.force[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
681

682
683
684
            outputTorque[offset]                = localParticle.torque[0];
            outputTorque[offset+1]              = localParticle.torque[1];
            outputTorque[offset+2]              = localParticle.torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
685
686
687

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

688
689
690
            outputForce[offset]                 = sA[threadIdx.x].force[0];
            outputForce[offset+1]               = sA[threadIdx.x].force[1];
            outputForce[offset+2]               = sA[threadIdx.x].force[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
691

692
693
694
            outputTorque[offset]                = sA[threadIdx.x].torque[0];
            outputTorque[offset+1]              = sA[threadIdx.x].torque[1];
            outputTorque[offset+2]              = sA[threadIdx.x].torque[2];
Mark Friedrichs's avatar
Mark Friedrichs committed
695
696
697
698
699
700
701
702


#endif
            lasty = y;
        }

        pos++;
    }
703
    cSim.pEnergy[blockIdx.x * blockDim.x + threadIdx.x] += (conversionFactor*totalEnergy);
Mark Friedrichs's avatar
Mark Friedrichs committed
704
}