"...cuda/src/kernels/kCalculateAmoebaCudaFixedEAndGkFields.cu" did not exist on "5003591d7bd4816a9dde89dea97fced7192b8dcf"
customGBEnergyN2.cu 15.5 KB
Newer Older
1
2
#define STORE_DERIVATIVE_1(INDEX) atomicAdd(&derivBuffers[offset+(INDEX-1)*PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (deriv##INDEX##_1*0x100000000)));
#define STORE_DERIVATIVE_2(INDEX) atomicAdd(&derivBuffers[offset+(INDEX-1)*PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (localData[threadIdx.x].deriv##INDEX*0x100000000)));
3
4

typedef struct {
5
    real3 pos;
6
    real3 force;
7
8
9
10
11
12
13
14
15
    ATOM_PARAMETER_DATA
#ifdef NEED_PADDING
    float padding;
#endif
} AtomData;

/**
 * Compute a force based on pair interactions.
 */
16
extern "C" __global__ void computeN2Energy(unsigned long long* __restrict__ forceBuffers, mixed* __restrict__ energyBuffer,
17
        const real4* __restrict__ posq, const unsigned int* __restrict__ exclusions, const ushort2* __restrict__ exclusionTiles, bool needEnergy,
18
#ifdef USE_CUTOFF
19
20
21
        const int* __restrict__ tiles, const unsigned int* __restrict__ interactionCount, real4 periodicBoxSize, real4 invPeriodicBoxSize,
        real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ, unsigned int maxTiles, const real4* __restrict__ blockCenter,
        const real4* __restrict__ blockSize, const unsigned int* __restrict__ interactingAtoms
22
23
24
25
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
26
27
28
29
    const unsigned int totalWarps = (blockDim.x*gridDim.x)/TILE_SIZE;
    const unsigned int warp = (blockIdx.x*blockDim.x+threadIdx.x)/TILE_SIZE;
    const unsigned int tgx = threadIdx.x & (TILE_SIZE-1);
    const unsigned int tbx = threadIdx.x - tgx;
30
    mixed energy = 0;
31
    INIT_PARAM_DERIVS
32
    __shared__ AtomData localData[THREAD_BLOCK_SIZE];
33
34

    // First loop: process tiles that contain exclusions.
35
    
36
37
38
39
40
41
    const unsigned int firstExclusionTile = FIRST_EXCLUSION_TILE+warp*(LAST_EXCLUSION_TILE-FIRST_EXCLUSION_TILE)/totalWarps;
    const unsigned int lastExclusionTile = FIRST_EXCLUSION_TILE+(warp+1)*(LAST_EXCLUSION_TILE-FIRST_EXCLUSION_TILE)/totalWarps;
    for (int pos = firstExclusionTile; pos < lastExclusionTile; pos++) {
        const ushort2 tileIndices = exclusionTiles[pos];
        const unsigned int x = tileIndices.x;
        const unsigned int y = tileIndices.y;
42
        real3 force = make_real3(0);
43
        DECLARE_ATOM1_DERIVATIVES
44
        unsigned int atom1 = x*TILE_SIZE + tgx;
45
        real4 pos1 = posq[atom1];
46
47
48
        LOAD_ATOM1_PARAMETERS
#ifdef USE_EXCLUSIONS
        unsigned int excl = exclusions[pos*TILE_SIZE+tgx];
49
#endif
50
51
        if (x == y) {
            // This tile is on the diagonal.
52

53
            const unsigned int localAtomIndex = threadIdx.x;
54
            localData[localAtomIndex].pos = make_real3(pos1.x, pos1.y, pos1.z);
55
56
57
            LOAD_LOCAL_PARAMETERS_FROM_1
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
                int atom2 = tbx+j;
58
59
                real3 pos2 = localData[atom2].pos;
                real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
60
#ifdef USE_PERIODIC
61
                APPLY_PERIODIC_TO_DELTA(delta)
62
63
64
65
66
67
#endif
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
                if (r2 < CUTOFF_SQUARED) {
#endif
                    real invR = RSQRT(r2);
peastman's avatar
peastman committed
68
                    real r = r2*invR;
69
70
71
72
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y*TILE_SIZE+j;
                    real dEdR = 0;
                    real tempEnergy = 0;
73
                    const real interactionScale = 0.5f;
74
#ifdef USE_EXCLUSIONS
75
76
77
78
79
80
                    bool isExcluded = !(excl & 0x1);
#endif
                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS && atom1 != atom2) {
                        COMPUTE_INTERACTION
                        dEdR /= -r;
                    }
81
82
                    if (needEnergy)
                        energy += 0.5f*tempEnergy;
83
84
85
86
87
88
                    delta *= dEdR;
                    force.x -= delta.x;
                    force.y -= delta.y;
                    force.z -= delta.z;
#ifdef USE_CUTOFF
                }
89
90
#endif
#ifdef USE_EXCLUSIONS
91
                excl >>= 1;
92
#endif
93
94
95
96
97
98
99
            }
        }
        else {
            // This is an off-diagonal tile.

            const unsigned int localAtomIndex = threadIdx.x;
            unsigned int j = y*TILE_SIZE + tgx;
100
101
            real4 tempPosq = posq[j];
            localData[localAtomIndex].pos = make_real3(tempPosq.x, tempPosq.y, tempPosq.z);
102
103
104
            LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            localData[localAtomIndex].force = make_real3(0);
            CLEAR_LOCAL_DERIVATIVES
105
#ifdef USE_EXCLUSIONS
106
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
107
#endif
108
109
110
            unsigned int tj = tgx;
            for (j = 0; j < TILE_SIZE; j++) {
                int atom2 = tbx+tj;
111
112
                real3 pos2 = localData[atom2].pos;
                real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
113
#ifdef USE_PERIODIC
114
                APPLY_PERIODIC_TO_DELTA(delta)
115
#endif
116
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
117
#ifdef USE_CUTOFF
118
                if (r2 < CUTOFF_SQUARED) {
119
120
#endif
                    real invR = RSQRT(r2);
peastman's avatar
peastman committed
121
                    real r = r2*invR;
122
                    LOAD_ATOM2_PARAMETERS
123
                    atom2 = y*TILE_SIZE+tj;
124
125
                    real dEdR = 0;
                    real tempEnergy = 0;
126
                    const real interactionScale = 1;
127
128
129
130
#ifdef USE_EXCLUSIONS
                    bool isExcluded = !(excl & 0x1);
#endif
                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
131
132
133
                        COMPUTE_INTERACTION
                        dEdR /= -r;
                    }
134
135
                    if (needEnergy)
                        energy += tempEnergy;
136
                    delta *= dEdR;
137
138
139
                    force.x -= delta.x;
                    force.y -= delta.y;
                    force.z -= delta.z;
140
141
142
143
144
                    atom2 = tbx+tj;
                    localData[atom2].force.x += delta.x;
                    localData[atom2].force.y += delta.y;
                    localData[atom2].force.z += delta.z;
                    RECORD_DERIVATIVE_2
145
#ifdef USE_CUTOFF
146
                }
147
148
#endif
#ifdef USE_EXCLUSIONS
149
                excl >>= 1;
150
#endif
151
                tj = (tj + 1) & (TILE_SIZE - 1);
152
            }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
        }

        // Write results.

        unsigned int offset = x*TILE_SIZE + tgx;
        atomicAdd(&forceBuffers[offset], static_cast<unsigned long long>((long long) (force.x*0x100000000)));
        atomicAdd(&forceBuffers[offset+PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (force.y*0x100000000)));
        atomicAdd(&forceBuffers[offset+2*PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (force.z*0x100000000)));
        STORE_DERIVATIVES_1
        if (x != y) {
            offset = y*TILE_SIZE + tgx;
            atomicAdd(&forceBuffers[offset], static_cast<unsigned long long>((long long) (localData[threadIdx.x].force.x*0x100000000)));
            atomicAdd(&forceBuffers[offset+PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (localData[threadIdx.x].force.y*0x100000000)));
            atomicAdd(&forceBuffers[offset+2*PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (localData[threadIdx.x].force.z*0x100000000)));
            STORE_DERIVATIVES_2
        }
    }

    // Second loop: tiles without exclusions, either from the neighbor list (with cutoff) or just enumerating all
    // of them (no cutoff).

174
#ifdef USE_CUTOFF
175
    unsigned int numTiles = interactionCount[0];
176
177
    if (numTiles > maxTiles)
        return; // There wasn't enough memory for the neighbor list.
178
179
    int pos = (int) (warp*(numTiles > maxTiles ? NUM_BLOCKS*((long long)NUM_BLOCKS+1)/2 : (long)numTiles)/totalWarps);
    int end = (int) ((warp+1)*(numTiles > maxTiles ? NUM_BLOCKS*((long long)NUM_BLOCKS+1)/2 : (long)numTiles)/totalWarps);
180
#else
181
182
    int pos = (int) (warp*(long long)numTiles/totalWarps);
    int end = (int) ((warp+1)*(long long)numTiles/totalWarps);
183
184
185
186
#endif
    int skipBase = 0;
    int currentSkipIndex = tbx;
    __shared__ int atomIndices[THREAD_BLOCK_SIZE];
187
    __shared__ volatile int skipTiles[THREAD_BLOCK_SIZE];
188
189
190
191
192
193
194
195
196
197
    skipTiles[threadIdx.x] = -1;
    
    while (pos < end) {
        const bool isExcluded = false;
        real3 force = make_real3(0);
        DECLARE_ATOM1_DERIVATIVES
        bool includeTile = true;
        
        // Extract the coordinates of this tile.
        
198
        int x, y;
199
200
        bool singlePeriodicCopy = false;
#ifdef USE_CUTOFF
201
            x = tiles[pos];
202
203
204
205
            real4 blockSizeX = blockSize[x];
            singlePeriodicCopy = (0.5f*periodicBoxSize.x-blockSizeX.x >= CUTOFF &&
                                  0.5f*periodicBoxSize.y-blockSizeX.y >= CUTOFF &&
                                  0.5f*periodicBoxSize.z-blockSizeX.z >= CUTOFF);
206
207
208
209
210
#else
        y = (int) floor(NUM_BLOCKS+0.5f-SQRT((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
        x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
        if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
            y += (x < y ? -1 : 1);
211
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
212
        }
213

214
        // Skip over tiles that have exclusions, since they were already processed.
215

216
217
218
219
        while (skipTiles[tbx+TILE_SIZE-1] < pos) {
            if (skipBase+tgx < NUM_TILES_WITH_EXCLUSIONS) {
                ushort2 tile = exclusionTiles[skipBase+tgx];
                skipTiles[threadIdx.x] = tile.x + tile.y*NUM_BLOCKS - tile.y*(tile.y+1)/2;
220
            }
221
222
223
224
            else
                skipTiles[threadIdx.x] = end;
            skipBase += TILE_SIZE;            
            currentSkipIndex = tbx;
225
        }
226
227
228
229
        while (skipTiles[currentSkipIndex] < pos)
            currentSkipIndex++;
        includeTile = (skipTiles[currentSkipIndex] != pos);
#endif
230
231
232
233
234
        if (includeTile) {
            unsigned int atom1 = x*TILE_SIZE + tgx;

            // Load atom data for this tile.

235
            real4 pos1 = posq[atom1];
236
237
238
239
240
241
            LOAD_ATOM1_PARAMETERS
            const unsigned int localAtomIndex = threadIdx.x;
#ifdef USE_CUTOFF
            unsigned int j = (numTiles <= maxTiles ? interactingAtoms[pos*TILE_SIZE+tgx] : y*TILE_SIZE + tgx);
#else
            unsigned int j = y*TILE_SIZE + tgx;
242
#endif
243
244
            atomIndices[threadIdx.x] = j;
            if (j < PADDED_NUM_ATOMS) {
245
246
                real4 tempPosq = posq[j];
                localData[localAtomIndex].pos = make_real3(tempPosq.x, tempPosq.y, tempPosq.z);
247
248
249
250
251
252
253
254
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
                localData[localAtomIndex].force = make_real3(0);
                CLEAR_LOCAL_DERIVATIVES
            }
#ifdef USE_PERIODIC
            if (singlePeriodicCopy) {
                // The box is small enough that we can just translate all the atoms into a single periodic
                // box, then skip having to apply periodic boundary conditions later.
255

256
                real4 blockCenterX = blockCenter[x];
257
258
                APPLY_PERIODIC_TO_POS_WITH_CENTER(pos1, blockCenterX)
                APPLY_PERIODIC_TO_POS_WITH_CENTER(localData[threadIdx.x].pos, blockCenterX)
259
260
261
                unsigned int tj = tgx;
                for (j = 0; j < TILE_SIZE; j++) {
                    int atom2 = tbx+tj;
262
263
                    real3 pos2 = localData[atom2].pos;
                    real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
264
265
266
                    real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
                    if (r2 < CUTOFF_SQUARED) {
267
#endif
268
                        real invR = RSQRT(r2);
peastman's avatar
peastman committed
269
                        real r = r2*invR;
270
271
272
273
                        LOAD_ATOM2_PARAMETERS
                        atom2 = atomIndices[tbx+tj];
                        real dEdR = 0;
                        real tempEnergy = 0;
274
                        const real interactionScale = 1;
275
276
277
278
                        if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                            COMPUTE_INTERACTION
                            dEdR /= -r;
                        }
279
280
                        if (needEnergy)
                            energy += tempEnergy;
281
282
283
284
285
286
287
288
289
290
291
                        delta *= dEdR;
                        force.x -= delta.x;
                        force.y -= delta.y;
                        force.z -= delta.z;
                        atom2 = tbx+tj;
                        localData[atom2].force.x += delta.x;
                        localData[atom2].force.y += delta.y;
                        localData[atom2].force.z += delta.z;
                        RECORD_DERIVATIVE_2
#ifdef USE_CUTOFF
                    }
292
#endif
293
294
295
296
297
298
299
300
301
302
303
                    tj = (tj + 1) & (TILE_SIZE - 1);
                }
            }
            else
#endif
            {
                // We need to apply periodic boundary conditions separately for each interaction.

                unsigned int tj = tgx;
                for (j = 0; j < TILE_SIZE; j++) {
                    int atom2 = tbx+tj;
304
305
                    real3 pos2 = localData[atom2].pos;
                    real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
306
#ifdef USE_PERIODIC
307
                    APPLY_PERIODIC_TO_DELTA(delta)
308
#endif
309
                    real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
310
#ifdef USE_CUTOFF
311
                    if (r2 < CUTOFF_SQUARED) {
312
313
#endif
                        real invR = RSQRT(r2);
peastman's avatar
peastman committed
314
                        real r = r2*invR;
315
                        LOAD_ATOM2_PARAMETERS
316
                        atom2 = atomIndices[tbx+tj];
317
318
                        real dEdR = 0;
                        real tempEnergy = 0;
319
                        const real interactionScale = 1;
320
321
322
323
                        if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                            COMPUTE_INTERACTION
                            dEdR /= -r;
                        }
324
325
                        if (needEnergy)
                            energy += tempEnergy;
326
                        delta *= dEdR;
327
328
329
                        force.x -= delta.x;
                        force.y -= delta.y;
                        force.z -= delta.z;
330
                        atom2 = tbx+tj;
331
332
333
                        localData[atom2].force.x += delta.x;
                        localData[atom2].force.y += delta.y;
                        localData[atom2].force.z += delta.z;
334
335
336
                        RECORD_DERIVATIVE_2
#ifdef USE_CUTOFF
                    }
337
338
#endif
                    tj = (tj + 1) & (TILE_SIZE - 1);
339
340
341
                }
            }
        
342
343
344
345
346
347
            // Write results.

            atomicAdd(&forceBuffers[atom1], static_cast<unsigned long long>((long long) (force.x*0x100000000)));
            atomicAdd(&forceBuffers[atom1+PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (force.y*0x100000000)));
            atomicAdd(&forceBuffers[atom1+2*PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (force.z*0x100000000)));
            unsigned int offset = atom1;
348
            STORE_DERIVATIVES_1
349
350
351
352
353
354
355
356
357
358
359
360
#ifdef USE_CUTOFF
            unsigned int atom2 = atomIndices[threadIdx.x];
#else
            unsigned int atom2 = y*TILE_SIZE + tgx;
#endif
            if (atom2 < PADDED_NUM_ATOMS) {
                atomicAdd(&forceBuffers[atom2], static_cast<unsigned long long>((long long) (localData[threadIdx.x].force.x*0x100000000)));
                atomicAdd(&forceBuffers[atom2+PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (localData[threadIdx.x].force.y*0x100000000)));
                atomicAdd(&forceBuffers[atom2+2*PADDED_NUM_ATOMS], static_cast<unsigned long long>((long long) (localData[threadIdx.x].force.z*0x100000000)));
                offset = atom2;
                STORE_DERIVATIVES_2
            }
361
362
        }
        pos++;
363
    }
364
    energyBuffer[blockIdx.x*blockDim.x+threadIdx.x] += energy;
365
    SAVE_PARAM_DERIVS
366
}