kCalculateAmoebaCudaPmeDirectElectrostatic.h 28.3 KB
Newer Older
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
/* -------------------------------------------------------------------------- *
 *                                   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)
__launch_bounds__(384, 1)
#elif (__CUDA_ARCH__ >= 130)
__launch_bounds__(128, 1)
#else
__launch_bounds__(64, 1)
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
37
38
void METHOD_NAME(kCalculateAmoebaPmeDirectElectrostatic, Forces_kernel)(
                            unsigned int* workUnit, float* outputForce, float* outputTorque
39
40
41
42
43
44
45

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

#ifdef AMOEBA_DEBUG
46
47
    int maxPullIndex = 2;
    float4 pullBack[12];
48
49
#endif

Mark Friedrichs's avatar
Mark Friedrichs committed
50
    extern __shared__ PmeDirectElectrostaticParticle sA[];
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

    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;     

    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;

Mark Friedrichs's avatar
Mark Friedrichs committed
77
        PmeDirectElectrostaticParticle* psA    = &sA[tbx];
78
        unsigned int atomI            = x + tgx;
Mark Friedrichs's avatar
Mark Friedrichs committed
79
80
        PmeDirectElectrostaticParticle localParticle;
        loadPmeDirectElectrostaticShared(&localParticle, atomI );
81

Mark Friedrichs's avatar
Mark Friedrichs committed
82
83
84
        localParticle.force[0]        = 0.0f;
        localParticle.force[1]        = 0.0f;
        localParticle.force[2]        = 0.0f;
85

Mark Friedrichs's avatar
Mark Friedrichs committed
86
87
88
        localParticle.torque[0]       = 0.0f;
        localParticle.torque[1]       = 0.0f;
        localParticle.torque[2]       = 0.0f;
89
90
91
92
93
94
95
96
97
98
99

        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

Mark Friedrichs's avatar
Mark Friedrichs committed
100
            loadPmeDirectElectrostaticShared( &(sA[threadIdx.x]), atomI );
101
102
103
104
105
106
107
108
109
110
111
112
113
114

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

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

                    float energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
115
116
                    calculatePmeDirectElectrostaticPairIxn_kernel( localParticle, psA[j],
                                                                   scalingFactors, force, torque, &energy
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifdef AMOEBA_DEBUG
, pullBack
#endif
 );

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

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

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

                    localParticle.torque[0]           += mask ? torque[0][0] : 0.0f;
                    localParticle.torque[1]           += mask ? torque[0][1] : 0.0f;
                    localParticle.torque[2]           += mask ? torque[0][2] : 0.0f;
 
Mark Friedrichs's avatar
Mark Friedrichs committed
134
                    totalEnergy                       += mask ? 0.5*energy   : 0.0f;
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
                }

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

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

                    unsigned int atomJ = y + j;

                    // set scale factors

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

                    // force

                    float energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
163
164
                    calculatePmeDirectElectrostaticPairIxn_kernel( localParticle,   psA[j],
                                                                   scalingFactors, force, torque, &energy
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifdef AMOEBA_DEBUG
, pullBack
#endif
 );

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

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

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

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
185
                    totalEnergy                       += mask ? 0.5*energy   : 0.0f;
186
187

#ifdef AMOEBA_DEBUG
188
189
190
191
192
193
/*
energy =  mask ? 0.5*energy   : 0.0f;
if( atomI < 200 && (fabs( energy ) > 1.0e+8 || energy != energy) ){
    debugSetup( atomI, atomJ, debugArray, pullBack );
} */

194
if( atomI == targetAtom ){
Mark Friedrichs's avatar
Mark Friedrichs committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    unsigned int index                 = (atomI == targetAtom) ? atomJ : atomI;
    float blockId                      = 1.0f;

    debugArray[index].x                = (float) atomI;
    debugArray[index].y                = (float) atomJ;
    debugArray[index].z                = (float) y;
    debugArray[index].w                = blockId;

    index                             += cAmoebaSim.paddedNumberOfAtoms;
    debugArray[index].x                = mask ? force[0]     : 0.0f;
    debugArray[index].y                = mask ? force[1]     : 0.0f;
    debugArray[index].z                = mask ? force[2]     : 0.0f;
    debugArray[index].w                = blockId;


    index                             += cAmoebaSim.paddedNumberOfAtoms;
    debugArray[index].x                = mask ? torque[0][0] : 0.0f;
    debugArray[index].y                = mask ? torque[0][1] : 0.0f;
    debugArray[index].z                = mask ? torque[0][2] : 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
214
    debugArray[index].w                = mask ? energy       : 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
215
216
217
218
219

    index                             += cAmoebaSim.paddedNumberOfAtoms;
    debugArray[index].x                = mask ? torque[0][0] : 0.0f;
    debugArray[index].y                = mask ? torque[0][1] : 0.0f;
    debugArray[index].z                = mask ? torque[0][2] : 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
220
    debugArray[index].w                = (float) (blockIdx.x * blockDim.x + threadIdx.x);
Mark Friedrichs's avatar
Mark Friedrichs committed
221

222
    for( int pullIndex = 0; pullIndex < maxPullIndex; pullIndex++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
223
224
225
226
227
228
        index                             += cAmoebaSim.paddedNumberOfAtoms;
        debugArray[index].x                = pullBack[pullIndex].x;
        debugArray[index].y                = pullBack[pullIndex].y;
        debugArray[index].z                = pullBack[pullIndex].z;
        debugArray[index].w                = pullBack[pullIndex].w;
    }
229
230
231
232
233
234
}
#endif

                }
            }

Mark Friedrichs's avatar
Mark Friedrichs committed
235
236
237
238
            // include self energy and self torque

            if( atomI < cAmoebaSim.numberOfAtoms ){
                calculatePmeSelfTorqueElectrostaticPairIxn_kernel( localParticle );
Mark Friedrichs's avatar
Mark Friedrichs committed
239
240
241
                float energy;
                calculatePmeSelfEnergyElectrostaticPairIxn_kernel( localParticle, &energy );
                totalEnergy += energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
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
            // Write results

#ifdef USE_OUTPUT_BUFFER_PER_WARP
            float  of;
            unsigned int offset                 = 3*(x + tgx + warp*cAmoebaSim.paddedNumberOfAtoms);
            of                                  = outputForce[offset];
            of                                 += localParticle.force[0];
            outputForce[offset]                 = of;

            of                                  = outputForce[offset+1];
            of                                 += localParticle.force[1];
            outputForce[offset+1]               = of;

            of                                  = outputForce[offset+2];
            of                                 += localParticle.force[2];
            outputForce[offset+2]               = of;

            of                                  = outputTorque[offset];
            of                                 += localParticle.torque[0];
            outputTorque[offset]                = of;

            of                                  = outputTorque[offset+1];
            of                                 += localParticle.torque[1];
            outputTorque[offset+1]              = of;

            of                                  = outputTorque[offset+2];
            of                                 += localParticle.torque[2];
            outputTorque[offset+2]              = of;

#else
            unsigned int offset                 = 3*(x + tgx + (x >> GRIDBITS) * cAmoebaSim.paddedNumberOfAtoms);
            outputForce[offset]                 = localParticle.force[0];
            outputForce[offset+1]               = localParticle.force[1];
            outputForce[offset+2]               = localParticle.force[2];

            outputTorque[offset]                = localParticle.torque[0];
            outputTorque[offset+1]              = localParticle.torque[1];
            outputTorque[offset+2]              = localParticle.torque[2];
#endif

        }
Mark Friedrichs's avatar
Mark Friedrichs committed
285
        else
286
287
288
289
290
        {
            if (lasty != y)
            {
                // load shared data

Mark Friedrichs's avatar
Mark Friedrichs committed
291
               loadPmeDirectElectrostaticShared( &(sA[threadIdx.x]), (y+tgx) );
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

            }

            sA[threadIdx.x].force[0]     = 0.0f;
            sA[threadIdx.x].force[1]     = 0.0f;
            sA[threadIdx.x].force[2]     = 0.0f;

            sA[threadIdx.x].torque[0]    = 0.0f;
            sA[threadIdx.x].torque[1]    = 0.0f;
            sA[threadIdx.x].torque[2]    = 0.0f;

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

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

                    unsigned int atomJ = y + tj;

                    float energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
314
315
                    calculatePmeDirectElectrostaticPairIxn_kernel( localParticle,  psA[tj],
                                                                   scalingFactors, force, torque, &energy
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#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
       
                           localParticle.force[0]         += mask ? force[0]      : 0.0f;
                           localParticle.force[1]         += mask ? force[1]      : 0.0f;
                           localParticle.force[2]         += mask ? force[2]      : 0.0f;
       
                           localParticle.torque[0]        += mask ? torque[0][0]  : 0.0f;
                           localParticle.torque[1]        += mask ? torque[0][1]  : 0.0f;
                           localParticle.torque[2]        += mask ? torque[0][2]  : 0.0f;

Mark Friedrichs's avatar
Mark Friedrichs committed
333
                           totalEnergy                    += mask ? energy        : 0.0f;
334
335
336
           
                           // add force and torque to atom J due atom I
       
Mark Friedrichs's avatar
Mark Friedrichs committed
337
338
339
                           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;
340
       
Mark Friedrichs's avatar
Mark Friedrichs committed
341
342
343
                           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;
344
345
346
       
       
#ifdef AMOEBA_DEBUG
347
348
349
350
351
/*
energy =  mask ? 0.5*energy   : 0.0f;
if( atomI < 200 && (fabs( energy ) > 1.0e+8 || energy != energy) ){
    debugSetup( atomI, atomJ, debugArray, pullBack );
} */
352
353
if( atomI == targetAtom  || atomJ == targetAtom ){

Mark Friedrichs's avatar
Mark Friedrichs committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
    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;
    float blockId                      = 2.0f;

    debugArray[index].x                = (float) atomI;
    debugArray[index].y                = (float) atomJ;
    debugArray[index].z                = (float) y;
    debugArray[index].w                = blockId;

    index                             += cAmoebaSim.paddedNumberOfAtoms;
    debugArray[index].x                = mask ? forceSign*force[0]     : 0.0f;
    debugArray[index].y                = mask ? forceSign*force[1]     : 0.0f;
    debugArray[index].z                = mask ? forceSign*force[2]     : 0.0f;
    debugArray[index].w                = blockId;


    index                             += cAmoebaSim.paddedNumberOfAtoms;
    debugArray[index].x                = mask ? torque[indexI][0] : 0.0f;
    debugArray[index].y                = mask ? torque[indexI][1] : 0.0f;
    debugArray[index].z                = mask ? torque[indexI][2] : 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
376
    debugArray[index].w                = mask ? energy       : 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
377
378
379
380
381

    index                             += cAmoebaSim.paddedNumberOfAtoms;
    debugArray[index].x                = mask ? torque[indexJ][0] : 0.0f;
    debugArray[index].y                = mask ? torque[indexJ][1] : 0.0f;
    debugArray[index].z                = mask ? torque[indexJ][2] : 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
382
    debugArray[index].w                = (float) (blockIdx.x * blockDim.x + threadIdx.x);
Mark Friedrichs's avatar
Mark Friedrichs committed
383

384
    for( int pullIndex = 0; pullIndex < maxPullIndex; pullIndex++ ){
385
386
387
388
389
        index                             += cAmoebaSim.paddedNumberOfAtoms;
        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
390
    }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
}
#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++)
                       {
       
                           float force[3];
                           float torque[2][3];
       
                           unsigned int atomJ = y + tj;
       
                           // set scale factors
       
                           getMaskedDScaleFactor( tj, dScaleMask, scalingFactors + DScaleIndex );
                           getMaskedPScaleFactor( tj, pScaleMask, scalingFactors + PScaleIndex );
                           getMaskedMScaleFactor( tj, mScaleMask, scalingFactors + MScaleIndex );
       
                           // force
       
                    float energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
426
427
                    calculatePmeDirectElectrostaticPairIxn_kernel( localParticle,   psA[tj],
                                                                   scalingFactors, force, torque, &energy
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#ifdef AMOEBA_DEBUG
, pullBack
#endif
 );

                    // check if atoms out-of-bounds

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

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

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

                    localParticle.torque[0]        += mask ? torque[0][0]  : 0.0f;
                    localParticle.torque[1]        += mask ? torque[0][1]  : 0.0f;
                    localParticle.torque[2]        += mask ? torque[0][2]  : 0.0f;
    
Mark Friedrichs's avatar
Mark Friedrichs committed
447
                    totalEnergy                    += mask ? energy        : 0.0f;
448
449
450

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

Mark Friedrichs's avatar
Mark Friedrichs committed
451
452
453
                    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;
454

Mark Friedrichs's avatar
Mark Friedrichs committed
455
456
457
                    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;
458
459
460


#ifdef AMOEBA_DEBUG
461
462
463
464
465
/*
energy =  mask ? 0.5*energy   : 0.0f;
if( atomI < 200 && (fabs( energy ) > 1.0e+8 || energy != energy) ){
    debugSetup( atomI, atomJ, debugArray, pullBack );
} */
466
if( atomI == targetAtom  || atomJ == targetAtom ){
Mark Friedrichs's avatar
Mark Friedrichs committed
467
468
469
470
471
        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;
        float blockId                      = 3.0f;
472

Mark Friedrichs's avatar
Mark Friedrichs committed
473
474
475
476
477
        debugArray[index].x                = (float) atomI;
        debugArray[index].y                = (float) atomJ;
        debugArray[index].z                = (float) y;
        debugArray[index].w                = blockId;

Mark Friedrichs's avatar
Mark Friedrichs committed
478
        index                             += cAmoebaSim.paddedNumberOfAtoms;
Mark Friedrichs's avatar
Mark Friedrichs committed
479
480
481
482
483
        debugArray[index].x                = mask ? forceSign*force[0]     : 0.0f;
        debugArray[index].y                = mask ? forceSign*force[1]     : 0.0f;
        debugArray[index].z                = mask ? forceSign*force[2]     : 0.0f;
        debugArray[index].w                = blockId;

484
        index                             += cAmoebaSim.paddedNumberOfAtoms;
Mark Friedrichs's avatar
Mark Friedrichs committed
485
486
487
        debugArray[index].x                = mask ? torque[indexI][0] : 0.0f;
        debugArray[index].y                = mask ? torque[indexI][1] : 0.0f;
        debugArray[index].z                = mask ? torque[indexI][2] : 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
488
        debugArray[index].w                = energy;
Mark Friedrichs's avatar
Mark Friedrichs committed
489

490
        index                             += cAmoebaSim.paddedNumberOfAtoms;
Mark Friedrichs's avatar
Mark Friedrichs committed
491
492
493
        debugArray[index].x                = mask ? torque[indexJ][0] : 0.0f;
        debugArray[index].y                = mask ? torque[indexJ][1] : 0.0f;
        debugArray[index].z                = mask ? torque[indexJ][2] : 0.0f;
Mark Friedrichs's avatar
Mark Friedrichs committed
494
        debugArray[index].w                = (float) (blockIdx.x * blockDim.x + threadIdx.x);
Mark Friedrichs's avatar
Mark Friedrichs committed
495

496
        for( int pullIndex = 0; pullIndex < maxPullIndex; pullIndex++ ){
Mark Friedrichs's avatar
Mark Friedrichs committed
497
498
499
500
501
502
            index                             += cAmoebaSim.paddedNumberOfAtoms;
            debugArray[index].x                = pullBack[pullIndex].x;
            debugArray[index].y                = pullBack[pullIndex].y;
            debugArray[index].z                = pullBack[pullIndex].z;
            debugArray[index].w                = pullBack[pullIndex].w;
        }
503
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
       
#if 0
            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;

            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;
#endif


}
#endif


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

            // Write results

#ifdef USE_OUTPUT_BUFFER_PER_WARP

            float of;
            unsigned int offset                 = 3*(x + tgx + warp*cAmoebaSim.paddedNumberOfAtoms);
            of                                  = outputForce[offset];
            of                                 += localParticle.force[0];
            outputForce[offset]                 = of;

            of                                  = outputForce[offset+1];
            of                                 += localParticle.force[1];
            outputForce[offset+1]               = of;

            of                                  = outputForce[offset+2];
            of                                 += localParticle.force[2];
            outputForce[offset+2]               = of;

            of                                  = outputTorque[offset];
            of                                 += localParticle.torque[0];
            outputTorque[offset]                 = of;

            of                                  = outputTorque[offset+1];
            of                                 += localParticle.torque[1];
            outputTorque[offset+1]               = of;

            of                                  = outputTorque[offset+2];
            of                                 += localParticle.torque[2];
            outputTorque[offset+2]               = of;

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

            of                                  = outputForce[offset];
            of                                 += sA[threadIdx.x].force[0];
            outputForce[offset]                 = of;

            of                                  = outputForce[offset+1];
            of                                 += sA[threadIdx.x].force[1];
            outputForce[offset+1]               = of;

            of                                  = outputForce[offset+2];
            of                                 += sA[threadIdx.x].force[2];
            outputForce[offset+2]               = of;

            of                                  = outputTorque[offset];
            of                                 += sA[threadIdx.x].torque[0];
            outputTorque[offset]                = of;

            of                                  = outputTorque[offset+1];
            of                                 += sA[threadIdx.x].torque[1];
            outputTorque[offset+1]              = of;

            of                                  = outputTorque[offset+2];
            of                                 += sA[threadIdx.x].torque[2];
            outputTorque[offset+2]              = of;

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

            outputForce[offset]                 = localParticle.force[0];
            outputForce[offset+1]               = localParticle.force[1];
            outputForce[offset+2]               = localParticle.force[2];

            outputTorque[offset]                = localParticle.torque[0];
            outputTorque[offset+1]              = localParticle.torque[1];
            outputTorque[offset+2]              = localParticle.torque[2];

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

            outputForce[offset]                 = sA[threadIdx.x].force[0];
            outputForce[offset+1]               = sA[threadIdx.x].force[1];
            outputForce[offset+2]               = sA[threadIdx.x].force[2];

            outputTorque[offset]                = sA[threadIdx.x].torque[0];
            outputTorque[offset+1]              = sA[threadIdx.x].torque[1];
            outputTorque[offset+2]              = sA[threadIdx.x].torque[2];


#endif
            lasty = y;
        }

        pos++;
    }
Mark Friedrichs's avatar
Mark Friedrichs committed
627
//printf( "Hello thread: %d %d %d %d\n", blockIdx.x * blockDim.x + threadIdx.x, blockIdx.x, blockDim.x, threadIdx.x );
628
629
    cSim.pEnergy[blockIdx.x * blockDim.x + threadIdx.x] += totalEnergy;
}