"...cuda-old/src/kernels/kCalculateAmoebaCudaFixedEField.h" did not exist on "f7f79b0402057533abf1c3c41a1d06f562a69b12"
nonbonded_nvidia.cl 16.3 KB
Newer Older
1
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
2
3
4
#ifdef SUPPORTS_64_BIT_ATOMICS
#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
#endif
5
#define TILE_SIZE 32
6

7
typedef struct {
8
9
10
    float x, y, z;
    float q;
    float fx, fy, fz;
11
12
13
    ATOM_PARAMETER_DATA
} AtomData;

14
15
16
/**
 * Compute nonbonded interactions.
 */
17
18
19
20
21
22
23
__kernel void computeNonbonded(
#ifdef SUPPORTS_64_BIT_ATOMICS
        __global long* forceBuffers,
#else
        __global float4* forceBuffers,
#endif
        __global float* energyBuffer, __global float4* posq, __global unsigned int* exclusions,
24
        __global unsigned int* exclusionIndices, __global unsigned int* exclusionRowIndices, __local AtomData* localData, __local float* tempBuffer,
25
        unsigned int startTileIndex, unsigned int endTileIndex,
26
#ifdef USE_CUTOFF
27
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global unsigned int* interactionFlags
28
29
#else
        unsigned int numTiles
30
#endif
31
        PARAMETER_ARGUMENTS) {
32
33
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
34
35
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
36
37
    unsigned int pos = (numTiles > maxTiles ? startTileIndex+warp*(endTileIndex-startTileIndex)/totalWarps : warp*numTiles/totalWarps);
    unsigned int end = (numTiles > maxTiles ? startTileIndex+(warp+1)*(endTileIndex-startTileIndex)/totalWarps : (warp+1)*numTiles/totalWarps);
38
#else
39
40
    unsigned int pos = startTileIndex+warp*numTiles/totalWarps;
    unsigned int end = startTileIndex+(warp+1)*numTiles/totalWarps;
41
#endif
42
43
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
44
45
    __local unsigned int exclusionRange[2*WARPS_PER_GROUP];
    __local int exclusionIndex[WARPS_PER_GROUP];
46
47
48
    __local int2* reservedBlocks = (__local int2*) exclusionRange;
    
    do {
49
        // Extract the coordinates of this tile
50
51
52
        const unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
        const unsigned int tbx = get_local_id(0) - tgx;
        const unsigned int localGroupIndex = get_local_id(0)/TILE_SIZE;
53
        unsigned int x, y;
54
55
        float4 force = 0.0f;
        if (pos < end) {
56
#ifdef USE_CUTOFF
57
58
59
60
61
62
            if (numTiles <= maxTiles) {
                ushort2 tileIndices = tiles[pos];
                x = tileIndices.x;
                y = tileIndices.y;
            }
            else
63
#endif
64
65
            {
                y = (unsigned int) floor(NUM_BLOCKS+0.5f-SQRT((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
66
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
67
68
69
70
                if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                    y += (x < y ? -1 : 1);
                    x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
                }
71
            }
72
73
74
            unsigned int atom1 = x*TILE_SIZE + tgx;
            float4 posq1 = posq[atom1];
            LOAD_ATOM1_PARAMETERS
75

76
            // Locate the exclusion data for this tile.
77
78

#ifdef USE_EXCLUSIONS
79
80
81
82
83
84
85
86
            if (tgx < 2)
                exclusionRange[2*localGroupIndex+tgx] = exclusionRowIndices[x+tgx];
            if (tgx == 0)
                exclusionIndex[localGroupIndex] = -1;
            for (int i = exclusionRange[2*localGroupIndex]+tgx; i < exclusionRange[2*localGroupIndex+1]; i += TILE_SIZE)
                if (exclusionIndices[i] == y)
                    exclusionIndex[localGroupIndex] = i*TILE_SIZE;
            bool hasExclusions = (exclusionIndex[localGroupIndex] > -1);
87
#else
88
            bool hasExclusions = false;
89
#endif
90
91
92
93
            if (pos >= end)
                ; // This warp is done.
            else if (x == y) {
                // This tile is on the diagonal.
94

95
96
97
98
99
100
                const unsigned int localAtomIndex = get_local_id(0);
                localData[localAtomIndex].x = posq1.x;
                localData[localAtomIndex].y = posq1.y;
                localData[localAtomIndex].z = posq1.z;
                localData[localAtomIndex].q = posq1.w;
                LOAD_LOCAL_PARAMETERS_FROM_1
101
#ifdef USE_EXCLUSIONS
102
                unsigned int excl = exclusions[exclusionIndex[localGroupIndex]+tgx];
103
#endif
104
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
105
#ifdef USE_EXCLUSIONS
106
                    bool isExcluded = !(excl & 0x1);
107
#endif
108
109
110
                    int atom2 = tbx+j;
                    float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
111
#ifdef USE_PERIODIC
112
113
114
                    delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                    delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                    delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
115
#endif
116
117
118
119
120
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y*TILE_SIZE+j;
121
#ifdef USE_SYMMETRIC
122
                    float dEdR = 0.0f;
123
#else
124
125
                    float4 dEdR1 = (float4) 0.0f;
                    float4 dEdR2 = (float4) 0.0f;
126
#endif
127
128
129
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
                    energy += 0.5f*tempEnergy;
130
#ifdef USE_SYMMETRIC
131
                    force.xyz -= delta.xyz*dEdR;
132
#else
133
                    force.xyz -= dEdR1.xyz;
134
#endif
135
136
137
138
#ifdef USE_EXCLUSIONS
                    excl >>= 1;
#endif
                }
139
            }
140
141
            else {
                // This is an off-diagonal tile.
142

143
144
145
146
147
148
149
150
151
                const unsigned int localAtomIndex = get_local_id(0);
                if (lasty != y) {
                    unsigned int j = y*TILE_SIZE + tgx;
                    float4 tempPosq = posq[j];
                    localData[localAtomIndex].x = tempPosq.x;
                    localData[localAtomIndex].y = tempPosq.y;
                    localData[localAtomIndex].z = tempPosq.z;
                    localData[localAtomIndex].q = tempPosq.w;
                    LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
152
                }
153
154
155
156
157
158
159
160
161
162
163
                localData[localAtomIndex].fx = 0.0f;
                localData[localAtomIndex].fy = 0.0f;
                localData[localAtomIndex].fz = 0.0f;
#ifdef USE_CUTOFF
                unsigned int flags = (numTiles <= maxTiles ? interactionFlags[pos] : 0xFFFFFFFF);
                if (!hasExclusions && flags != 0xFFFFFFFF) {
                    if (flags == 0) {
                        // No interactions in this tile.
                    }
                    else {
                        // Compute only a subset of the interactions in this tile.
164

165
166
167
168
                        for (unsigned int j = 0; j < TILE_SIZE; j++) {
                            if ((flags&(1<<j)) != 0) {
                                bool isExcluded = false;
                                int atom2 = tbx+j;
169
170
171
172
173
174
175
176
                                int bufferIndex = 3*get_local_id(0);
#ifdef USE_SYMMETRIC
                                float dEdR = 0.0f;
#else
                                float4 dEdR1 = (float4) 0.0f;
                                float4 dEdR2 = (float4) 0.0f;
#endif
                                float tempEnergy = 0.0f;
177
178
                                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
                                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
179
#ifdef USE_PERIODIC
180
181
182
                                delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                                delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                                delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
183
#endif
184
                                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
185
186
187
188
189
190
191
192
193
194
195
#ifdef USE_CUTOFF
                                if (r2 < CUTOFF_SQUARED) {
#endif
                                    float invR = RSQRT(r2);
                                    float r = RECIP(invR);
                                    LOAD_ATOM2_PARAMETERS
                                    atom2 = y*TILE_SIZE+j;
                                    COMPUTE_INTERACTION
                                    energy += tempEnergy;
#ifdef USE_CUTOFF
                                }
196
197
#endif
#ifdef USE_SYMMETRIC
198
199
200
201
202
                                delta.xyz *= dEdR;
                                force.xyz -= delta.xyz;
                                tempBuffer[bufferIndex] = delta.x;
                                tempBuffer[bufferIndex+1] = delta.y;
                                tempBuffer[bufferIndex+2] = delta.z;
203
#else
204
205
206
207
                                force.xyz -= dEdR1.xyz;
                                tempBuffer[bufferIndex] = dEdR2.x;
                                tempBuffer[bufferIndex+1] = dEdR2.y;
                                tempBuffer[bufferIndex+2] = dEdR2.z;
208
#endif
209

210
                                // Sum the forces on atom2.
211

212
                                if (tgx % 4 == 0) {
213
214
215
                                    tempBuffer[bufferIndex] += tempBuffer[bufferIndex+3]+tempBuffer[bufferIndex+6]+tempBuffer[bufferIndex+9];
                                    tempBuffer[bufferIndex+1] += tempBuffer[bufferIndex+4]+tempBuffer[bufferIndex+7]+tempBuffer[bufferIndex+10];
                                    tempBuffer[bufferIndex+2] += tempBuffer[bufferIndex+5]+tempBuffer[bufferIndex+8]+tempBuffer[bufferIndex+11];
216
217
                                }
                                if (tgx == 0) {
218
219
220
                                    localData[tbx+j].fx += tempBuffer[bufferIndex]+tempBuffer[bufferIndex+12]+tempBuffer[bufferIndex+24]+tempBuffer[bufferIndex+36]+tempBuffer[bufferIndex+48]+tempBuffer[bufferIndex+60]+tempBuffer[bufferIndex+72]+tempBuffer[bufferIndex+84];
                                    localData[tbx+j].fy += tempBuffer[bufferIndex+1]+tempBuffer[bufferIndex+13]+tempBuffer[bufferIndex+25]+tempBuffer[bufferIndex+37]+tempBuffer[bufferIndex+49]+tempBuffer[bufferIndex+61]+tempBuffer[bufferIndex+73]+tempBuffer[bufferIndex+85];
                                    localData[tbx+j].fz += tempBuffer[bufferIndex+2]+tempBuffer[bufferIndex+14]+tempBuffer[bufferIndex+26]+tempBuffer[bufferIndex+38]+tempBuffer[bufferIndex+50]+tempBuffer[bufferIndex+62]+tempBuffer[bufferIndex+74]+tempBuffer[bufferIndex+86];
221
                                }
222
                            }
223
224
225
                        }
                    }
                }
226
                else
227
#endif
228
229
                {
                    // Compute the full set of interactions in this tile.
230

231
#ifdef USE_EXCLUSIONS
232
233
                    unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[localGroupIndex]+tgx] : 0xFFFFFFFF);
                    excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
234
#endif
235
236
                    unsigned int tj = tgx;
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
237
#ifdef USE_EXCLUSIONS
238
                        bool isExcluded = !(excl & 0x1);
239
#endif
240
241
242
                        int atom2 = tbx+tj;
                        float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
                        float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
243
#ifdef USE_PERIODIC
244
245
246
                        delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                        delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                        delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
247
#endif
248
                        float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
249
250
251
252
253
254
255
#ifdef USE_CUTOFF
                        if (r2 < CUTOFF_SQUARED) {
#endif
                            float invR = RSQRT(r2);
                            float r = RECIP(invR);
                            LOAD_ATOM2_PARAMETERS
                            atom2 = y*TILE_SIZE+tj;
256
#ifdef USE_SYMMETRIC
257
                            float dEdR = 0.0f;
258
#else
259
260
                            float4 dEdR1 = (float4) 0.0f;
                            float4 dEdR2 = (float4) 0.0f;
261
#endif
262
263
264
                            float tempEnergy = 0.0f;
                            COMPUTE_INTERACTION
                            energy += tempEnergy;
265
#ifdef USE_SYMMETRIC
266
267
268
269
270
                            delta.xyz *= dEdR;
                            force.xyz -= delta.xyz;
                            localData[tbx+tj].fx += delta.x;
                            localData[tbx+tj].fy += delta.y;
                            localData[tbx+tj].fz += delta.z;
271
#else
272
273
274
275
276
277
278
                            force.xyz -= dEdR1.xyz;
                            localData[tbx+tj].fx += dEdR2.x;
                            localData[tbx+tj].fy += dEdR2.y;
                            localData[tbx+tj].fz += dEdR2.z;
#endif
#ifdef USE_CUTOFF
                        }
279
#endif
280
#ifdef USE_EXCLUSIONS
281
                        excl >>= 1;
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
282
#endif
283
284
                        tj = (tj + 1) & (TILE_SIZE - 1);
                    }
285
286
                }
            }
287
288
289
290
291
        }
        
        // Write results.  We need to coordinate between warps to make sure no two of them
        // ever try to write to the same piece of memory at the same time.
        
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#ifdef SUPPORTS_64_BIT_ATOMICS
        if (pos < end) {
            const unsigned int offset = x*TILE_SIZE + tgx;
            atom_add(&forceBuffers[offset], (long) (force.x*0xFFFFFFFF));
            atom_add(&forceBuffers[offset+PADDED_NUM_ATOMS], (long) (force.y*0xFFFFFFFF));
            atom_add(&forceBuffers[offset+2*PADDED_NUM_ATOMS], (long) (force.z*0xFFFFFFFF));
        }
        if (pos < end && x != y) {
            const unsigned int offset = y*TILE_SIZE + tgx;
            atom_add(&forceBuffers[offset], (long) (localData[get_local_id(0)].fx*0xFFFFFFFF));
            atom_add(&forceBuffers[offset+PADDED_NUM_ATOMS], (long) (localData[get_local_id(0)].fy*0xFFFFFFFF));
            atom_add(&forceBuffers[offset+2*PADDED_NUM_ATOMS], (long) (localData[get_local_id(0)].fz*0xFFFFFFFF));
        }
#else
306
307
308
309
310
311
312
313
314
        int writeX = (pos < end ? x : -1);
        int writeY = (pos < end && x != y ? y : -1);
        if (tgx == 0)
            reservedBlocks[localGroupIndex] = (int2)(writeX, writeY);
        bool done = false;
        int doneIndex = 0;
        int checkIndex = 0;
        while (true) {
            // See if any warp still needs to write its data.
315

316
317
318
319
320
321
322
323
324
325
326
327
328
            bool allDone = true;
            barrier(CLK_LOCAL_MEM_FENCE);
            while (doneIndex < WARPS_PER_GROUP && allDone) {
                if (reservedBlocks[doneIndex].x != -1)
                    allDone = false;
                else
                    doneIndex++;
            }
            if (allDone)
                break;
            if (!done) {
                // See whether this warp can write its data.  This requires that no previous warp
                // is trying to write to the same block of the buffer.
329

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
                bool canWrite = (writeX != -1);
                while (checkIndex < localGroupIndex && canWrite) {
                    if ((reservedBlocks[checkIndex].x == x || reservedBlocks[checkIndex].y == x) ||
                            (writeY != -1 && (reservedBlocks[checkIndex].x == y || reservedBlocks[checkIndex].y == y)))
                        canWrite = false;
                    else
                        checkIndex++;
                }
                if (canWrite) {
                    // Write the data to global memory, then mark this warp as done.

                    if (writeX > -1) {
                        const unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                        forceBuffers[offset].xyz += force.xyz;
                    }
                    if (writeY > -1) {
                        const unsigned int offset = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                        forceBuffers[offset] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0.0f);
                    }
                    done = true;
                    if (tgx == 0)
                        reservedBlocks[localGroupIndex] = (int2)(-1, -1);
                }
            }
354
        }
355
#endif
356
        lasty = y;
357
        pos++;
358
    } while (pos < end);
359
360
    energyBuffer[get_global_id(0)] += energy;
}