customGBEnergyN2.cc 14.5 KB
Newer Older
1
2
#define STORE_DERIVATIVE_1(INDEX) ATOMIC_ADD(&derivBuffers[offset+(INDEX-1)*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(deriv##INDEX##_1));
#define STORE_DERIVATIVE_2(INDEX) ATOMIC_ADD(&derivBuffers[offset+(INDEX-1)*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(local_deriv##INDEX[LOCAL_ID]));
3
4
5
6

/**
 * Compute a force based on pair interactions.
 */
7
8
9
10
KERNEL void computeN2Energy(
        GLOBAL mm_ulong* RESTRICT forceBuffers,
        GLOBAL mixed* RESTRICT energyBuffer,
        GLOBAL const real4* RESTRICT posq, GLOBAL const unsigned int* RESTRICT exclusions,
11
        GLOBAL const int2* exclusionTiles, int needEnergy,
12
#ifdef USE_CUTOFF
13
14
15
        GLOBAL const int* RESTRICT tiles, GLOBAL const unsigned int* RESTRICT interactionCount, real4 periodicBoxSize, real4 invPeriodicBoxSize,
        real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ, unsigned int maxTiles, GLOBAL const real4* RESTRICT blockCenter,
        GLOBAL const real4* RESTRICT blockSize, GLOBAL const int* RESTRICT interactingAtoms
16
17
18
19
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
20
21
22
23
    const unsigned int totalWarps = GLOBAL_SIZE/TILE_SIZE;
    const unsigned int warp = GLOBAL_ID/TILE_SIZE;
    const unsigned int tgx = LOCAL_ID & (TILE_SIZE-1);
    const unsigned int tbx = LOCAL_ID - tgx;
24
    mixed energy = 0;
25
    INIT_PARAM_DERIVS
26
27
28
    LOCAL real3 local_pos[LOCAL_BUFFER_SIZE];
    LOCAL real3 local_force[LOCAL_BUFFER_SIZE];
    ATOM_PARAMETER_DATA
29
30
31

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

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

96
            const unsigned int localAtomIndex = LOCAL_ID;
97
            unsigned int j = y*TILE_SIZE + tgx;
98
            local_pos[localAtomIndex] = trimTo3(posq[j]);
99
            LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
100
            local_force[localAtomIndex] = make_real3(0);
101
102
103
104
105
106
107
108
            CLEAR_LOCAL_DERIVATIVES
            SYNC_WARPS;
#ifdef USE_EXCLUSIONS
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
#endif
            unsigned int tj = tgx;
            for (j = 0; j < TILE_SIZE; j++) {
                int atom2 = tbx+tj;
109
110
                real3 pos2 = local_pos[atom2];
                real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
111
#ifdef USE_PERIODIC
112
                APPLY_PERIODIC_TO_DELTA(delta)
113
114
115
116
117
118
#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
119
                    real r = r2*invR;
120
121
122
123
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y*TILE_SIZE+tj;
                    real dEdR = 0;
                    real tempEnergy = 0;
124
                    const real interactionScale = 1;
125
126
127
128
129
130
131
#ifdef USE_EXCLUSIONS
                    bool isExcluded = !(excl & 0x1);
#endif
                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                        COMPUTE_INTERACTION
                        dEdR /= -r;
                    }
132
133
                    if (needEnergy)
                        energy += tempEnergy;
134
135
136
137
                    delta *= dEdR;
                    force.x -= delta.x;
                    force.y -= delta.y;
                    force.z -= delta.z;
138
                    atom2 = tbx+tj;
139
                    local_force[atom2] += delta;
140
141
142
143
144
145
146
147
148
149
150
                    RECORD_DERIVATIVE_2
#ifdef USE_CUTOFF
                }
#endif
#ifdef USE_EXCLUSIONS
                excl >>= 1;
#endif
                tj = (tj + 1) & (TILE_SIZE - 1);
                SYNC_WARPS;
            }
        }
151

152
        // Write results.
153

154
        unsigned int offset = x*TILE_SIZE + tgx;
155
156
157
        ATOMIC_ADD(&forceBuffers[offset], (mm_ulong) realToFixedPoint(force.x));
        ATOMIC_ADD(&forceBuffers[offset+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force.y));
        ATOMIC_ADD(&forceBuffers[offset+2*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force.z));
158
159
160
        STORE_DERIVATIVES_1
        if (x != y) {
            offset = y*TILE_SIZE + tgx;
161
162
163
            ATOMIC_ADD(&forceBuffers[offset], (mm_ulong) realToFixedPoint(local_force[LOCAL_ID].x));
            ATOMIC_ADD(&forceBuffers[offset+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(local_force[LOCAL_ID].y));
            ATOMIC_ADD(&forceBuffers[offset+2*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(local_force[LOCAL_ID].z));
164
165
166
167
168
169
170
171
172
            STORE_DERIVATIVES_2
        }
    }

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

#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
173
174
    if (numTiles > maxTiles)
        return; // There wasn't enough memory for the neighbor list.
175
176
    int pos = (int) (warp*(numTiles > maxTiles ? NUM_BLOCKS*((mm_long)NUM_BLOCKS+1)/2 : (mm_long)numTiles)/totalWarps);
    int end = (int) ((warp+1)*(numTiles > maxTiles ? NUM_BLOCKS*((mm_long)NUM_BLOCKS+1)/2 : (mm_long)numTiles)/totalWarps);
177
#else
178
179
    int pos = (int) (warp*(mm_long)numTiles/totalWarps);
    int end = (int) ((warp+1)*(mm_long)numTiles/totalWarps);
180
181
182
#endif
    int skipBase = 0;
    int currentSkipIndex = tbx;
183
184
185
    LOCAL int atomIndices[LOCAL_BUFFER_SIZE];
    LOCAL volatile int skipTiles[LOCAL_BUFFER_SIZE];
    skipTiles[LOCAL_ID] = -1;
186
187
188

    while (pos < end) {
        const bool isExcluded = false;
189
        real3 force = make_real3(0);
190
191
192
193
194
        DECLARE_ATOM1_DERIVATIVES
        bool includeTile = true;

        // Extract the coordinates of this tile.
        
195
        int x, y;
196
197
        bool singlePeriodicCopy = false;
#ifdef USE_CUTOFF
198
199
200
201
202
203
204
205
206
207
        x = tiles[pos];
        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);
#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);
208
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
209
        }
210

211
        // Skip over tiles that have exclusions, since they were already processed.
212

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

            // Load atom data for this tile.
234
235

            real3 pos1 = trimTo3(posq[atom1]);
236
            LOAD_ATOM1_PARAMETERS
237
            const unsigned int localAtomIndex = LOCAL_ID;
238
#ifdef USE_CUTOFF
peastman's avatar
peastman committed
239
            unsigned int j = interactingAtoms[pos*TILE_SIZE+tgx];
240
241
242
#else
            unsigned int j = y*TILE_SIZE + tgx;
#endif
243
            atomIndices[LOCAL_ID] = j;
244
            if (j < PADDED_NUM_ATOMS) {
245
                local_pos[localAtomIndex] = trimTo3(posq[j]);
246
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
247
                local_force[localAtomIndex] = make_real3(0);
248
249
250
251
252
253
254
255
256
                CLEAR_LOCAL_DERIVATIVES
            }
            SYNC_WARPS;
#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.

                real4 blockCenterX = blockCenter[x];
257
258
                APPLY_PERIODIC_TO_POS_WITH_CENTER(pos1, blockCenterX)
                APPLY_PERIODIC_TO_POS_WITH_CENTER(local_pos[LOCAL_ID], blockCenterX)
259
260
261
262
                SYNC_WARPS;
                unsigned int tj = tgx;
                for (j = 0; j < TILE_SIZE; j++) {
                    int atom2 = tbx+tj;
263
264
                    real3 pos2 = local_pos[atom2];
                    real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
265
266
267
                    real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                    if (r2 < CUTOFF_SQUARED) {
                        real invR = RSQRT(r2);
peastman's avatar
peastman committed
268
                        real r = r2*invR;
269
270
271
272
                        LOAD_ATOM2_PARAMETERS
                        atom2 = atomIndices[tbx+tj];
                        real dEdR = 0;
                        real tempEnergy = 0;
273
                        const real interactionScale = 1;
274
275
276
277
                        if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                            COMPUTE_INTERACTION
                            dEdR /= -r;
                        }
278
279
                        if (needEnergy)
                            energy += tempEnergy;
280
281
282
283
                        delta *= dEdR;
                        force.x -= delta.x;
                        force.y -= delta.y;
                        force.z -= delta.z;
284
                        atom2 = tbx+tj;
285
                        local_force[atom2] += delta;
286
287
288
289
290
291
292
293
294
295
296
297
298
299
                        RECORD_DERIVATIVE_2
                    }
                    tj = (tj + 1) & (TILE_SIZE - 1);
                    SYNC_WARPS;
                }
            }
            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;
300
301
                    real3 pos2 = local_pos[atom2];
                    real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
302
#ifdef USE_PERIODIC
303
                    APPLY_PERIODIC_TO_DELTA(delta)
304
305
306
307
308
309
#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
310
                        real r = r2*invR;
311
312
313
314
                        LOAD_ATOM2_PARAMETERS
                        atom2 = atomIndices[tbx+tj];
                        real dEdR = 0;
                        real tempEnergy = 0;
315
                        const real interactionScale = 1;
316
317
318
319
                        if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                            COMPUTE_INTERACTION
                            dEdR /= -r;
                        }
320
321
                        if (needEnergy)
                            energy += tempEnergy;
322
323
324
325
                        delta *= dEdR;
                        force.x -= delta.x;
                        force.y -= delta.y;
                        force.z -= delta.z;
326
                        atom2 = tbx+tj;
327
                        local_force[atom2] += delta;
328
329
330
331
332
333
334
335
336
337
                        RECORD_DERIVATIVE_2
#ifdef USE_CUTOFF
                    }
#endif
                    tj = (tj + 1) & (TILE_SIZE - 1);
                    SYNC_WARPS;
                }
            }
        
            // Write results.
338

339
#ifdef USE_CUTOFF
340
            unsigned int atom2 = atomIndices[LOCAL_ID];
341
342
343
#else
            unsigned int atom2 = y*TILE_SIZE + tgx;
#endif
344
345
346
            ATOMIC_ADD(&forceBuffers[atom1], (mm_ulong) realToFixedPoint(force.x));
            ATOMIC_ADD(&forceBuffers[atom1+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force.y));
            ATOMIC_ADD(&forceBuffers[atom1+2*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force.z));
347
348
349
            unsigned int offset = atom1;
            STORE_DERIVATIVES_1
            if (atom2 < PADDED_NUM_ATOMS) {
350
351
352
                ATOMIC_ADD(&forceBuffers[atom2], (mm_ulong) realToFixedPoint(local_force[LOCAL_ID].x));
                ATOMIC_ADD(&forceBuffers[atom2+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(local_force[LOCAL_ID].y));
                ATOMIC_ADD(&forceBuffers[atom2+2*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(local_force[LOCAL_ID].z));
353
354
355
356
357
358
                offset = atom2;
                STORE_DERIVATIVES_2
            }
        }
        pos++;
    }
359
    energyBuffer[GLOBAL_ID] += energy;
360
    SAVE_PARAM_DERIVS
361
}