nonbonded_nvidia.cl 15.4 KB
Newer Older
1
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
2
#define TILE_SIZE 32
3

4
typedef struct {
5
6
7
    float x, y, z;
    float q;
    float fx, fy, fz;
8
9
10
    ATOM_PARAMETER_DATA
} AtomData;

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

67
            // Locate the exclusion data for this tile.
68
69

#ifdef USE_EXCLUSIONS
70
71
72
73
74
75
76
77
            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);
78
#else
79
            bool hasExclusions = false;
80
#endif
81
82
83
84
            if (pos >= end)
                ; // This warp is done.
            else if (x == y) {
                // This tile is on the diagonal.
85

86
87
88
89
90
91
                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
92
#ifdef USE_EXCLUSIONS
93
                unsigned int excl = exclusions[exclusionIndex[localGroupIndex]+tgx];
94
#endif
95
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
96
#ifdef USE_EXCLUSIONS
97
                    bool isExcluded = !(excl & 0x1);
98
#endif
99
100
101
                    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);
102
#ifdef USE_PERIODIC
103
104
105
                    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;
106
#endif
107
108
109
110
111
                    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;
112
#ifdef USE_SYMMETRIC
113
                    float dEdR = 0.0f;
114
#else
115
116
                    float4 dEdR1 = (float4) 0.0f;
                    float4 dEdR2 = (float4) 0.0f;
117
#endif
118
119
120
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
                    energy += 0.5f*tempEnergy;
121
#ifdef USE_SYMMETRIC
122
                    force.xyz -= delta.xyz*dEdR;
123
#else
124
                    force.xyz -= dEdR1.xyz;
125
#endif
126
127
128
129
#ifdef USE_EXCLUSIONS
                    excl >>= 1;
#endif
                }
130
            }
131
132
            else {
                // This is an off-diagonal tile.
133

134
135
136
137
138
139
140
141
142
                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
143
                }
144
145
146
147
148
149
150
151
152
153
154
                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.
155

156
157
158
159
160
161
                        for (unsigned int j = 0; j < TILE_SIZE; j++) {
                            if ((flags&(1<<j)) != 0) {
                                bool isExcluded = false;
                                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);
162
#ifdef USE_PERIODIC
163
164
165
                                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;
166
#endif
167
168
169
170
171
                                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;
172
#ifdef USE_SYMMETRIC
173
                                float dEdR = 0.0f;
174
#else
175
176
                                float4 dEdR1 = (float4) 0.0f;
                                float4 dEdR2 = (float4) 0.0f;
177
#endif
178
179
180
181
                                float tempEnergy = 0.0f;
                                COMPUTE_INTERACTION
                                energy += tempEnergy;
                                int bufferIndex = 3*get_local_id(0);
182
#ifdef USE_SYMMETRIC
183
184
185
186
187
                                delta.xyz *= dEdR;
                                force.xyz -= delta.xyz;
                                tempBuffer[bufferIndex] = delta.x;
                                tempBuffer[bufferIndex+1] = delta.y;
                                tempBuffer[bufferIndex+2] = delta.z;
188
#else
189
190
191
192
                                force.xyz -= dEdR1.xyz;
                                tempBuffer[bufferIndex] = dEdR2.x;
                                tempBuffer[bufferIndex+1] = dEdR2.y;
                                tempBuffer[bufferIndex+2] = dEdR2.z;
193
#endif
194

195
                                // Sum the forces on atom2.
196

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
                                if (tgx % 2 == 0) {
                                    tempBuffer[bufferIndex] += tempBuffer[bufferIndex+3];
                                    tempBuffer[bufferIndex+1] += tempBuffer[bufferIndex+4];
                                    tempBuffer[bufferIndex+2] += tempBuffer[bufferIndex+5];
                                }
                                if (tgx % 4 == 0) {
                                    tempBuffer[bufferIndex] += tempBuffer[bufferIndex+6];
                                    tempBuffer[bufferIndex+1] += tempBuffer[bufferIndex+7];
                                    tempBuffer[bufferIndex+2] += tempBuffer[bufferIndex+8];
                                }
                                if (tgx % 8 == 0) {
                                    tempBuffer[bufferIndex] += tempBuffer[bufferIndex+12];
                                    tempBuffer[bufferIndex+1] += tempBuffer[bufferIndex+13];
                                    tempBuffer[bufferIndex+2] += tempBuffer[bufferIndex+14];
                                }
                                if (tgx % 16 == 0) {
                                    tempBuffer[bufferIndex] += tempBuffer[bufferIndex+24];
                                    tempBuffer[bufferIndex+1] += tempBuffer[bufferIndex+25];
                                    tempBuffer[bufferIndex+2] += tempBuffer[bufferIndex+26];
                                }
                                if (tgx == 0) {
                                    localData[tbx+j].fx += tempBuffer[bufferIndex] + tempBuffer[bufferIndex+48];
                                    localData[tbx+j].fy += tempBuffer[bufferIndex+1] + tempBuffer[bufferIndex+49];
                                    localData[tbx+j].fz += tempBuffer[bufferIndex+2] + tempBuffer[bufferIndex+50];
                                }
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
249
250
251
252
                        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+tj;
253
#ifdef USE_SYMMETRIC
254
                        float dEdR = 0.0f;
255
#else
256
257
                        float4 dEdR1 = (float4) 0.0f;
                        float4 dEdR2 = (float4) 0.0f;
258
#endif
259
260
261
                        float tempEnergy = 0.0f;
                        COMPUTE_INTERACTION
                        energy += tempEnergy;
262
#ifdef USE_SYMMETRIC
263
264
265
266
267
                        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;
268
#else
269
270
271
272
                        force.xyz -= dEdR1.xyz;
                        localData[tbx+tj].fx += dEdR2.x;
                        localData[tbx+tj].fy += dEdR2.y;
                        localData[tbx+tj].fz += dEdR2.z;
273
#endif
274
#ifdef USE_EXCLUSIONS
275
                        excl >>= 1;
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
276
#endif
277
278
                        tj = (tj + 1) & (TILE_SIZE - 1);
                    }
279
280
                }
            }
281
282
283
284
285
286
287
288
289
290
291
292
293
294
        }
        
        // 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.
        
        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.
295

296
297
298
299
300
301
302
303
304
305
306
307
308
            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.
309

310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
                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);
                }
            }
334
        }
335
        lasty = y;
336
        pos++;
337
    } while (pos < end);
338
339
    energyBuffer[get_global_id(0)] += energy;
}