nonbonded_nvidia.cl 13.6 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
15
16
17
18
19
20
21
22
 * Mark that a block in the force buffer is in use.
 */
void reserveBuffer(unsigned int block, __global unsigned int* forceBufferFlags) {
    if ((get_local_id(0)&(TILE_SIZE-1)) == 0)
        while (atom_cmpxchg(&forceBufferFlags[block+NUM_BLOCKS*get_group_id(0)], 0, 1) != 0)
            ;
    mem_fence(CLK_GLOBAL_MEM_FENCE);
}

/**
 * Mark that a block in the force buffer is no longer in use.
23
 */
24
25
26
27
28
void releaseBuffer(unsigned int block, __global unsigned int* forceBufferFlags) {
    mem_fence(CLK_GLOBAL_MEM_FENCE);
    if ((get_local_id(0)&(TILE_SIZE-1)) == 0)
        forceBufferFlags[block+NUM_BLOCKS*get_group_id(0)] = 0;
}
29

30
31
32
33
/**
 * Compute nonbonded interactions.
 */
__kernel void computeNonbonded(__global float4* forceBuffers, __global float* energyBuffer, __global float4* posq, __global unsigned int* exclusions,
34
        __global unsigned int* exclusionIndices, __global unsigned int* exclusionRowIndices, __local AtomData* localData, __local float* tempBuffer,
35
        unsigned int startTileIndex, unsigned int endTileIndex, __global unsigned int* forceBufferFlags,
36
#ifdef USE_CUTOFF
37
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global unsigned int* interactionFlags
38
39
#else
        unsigned int numTiles
40
#endif
41
        PARAMETER_ARGUMENTS) {
42
43
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
44
45
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
46
47
    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);
48
#else
49
50
    unsigned int pos = startTileIndex+warp*numTiles/totalWarps;
    unsigned int end = startTileIndex+(warp+1)*numTiles/totalWarps;
51
#endif
52
53
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
54
55
    __local unsigned int exclusionRange[2*WARPS_PER_GROUP];
    __local int exclusionIndex[WARPS_PER_GROUP];
56
57

    while (pos < end) {
58
        // Extract the coordinates of this tile
59
        unsigned int x, y;
60
#ifdef USE_CUTOFF
61
        if (numTiles <= maxTiles) {
62
63
64
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
65
        }
66
        else
67
#endif
68
        {
Peter Eastman's avatar
Peter Eastman committed
69
            y = (unsigned int) floor(NUM_BLOCKS+0.5f-SQRT((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
70
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
71
72
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
73
74
75
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
76
77
78
        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;
79
        unsigned int atom1 = x*TILE_SIZE + tgx;
80
        float4 force = 0.0f;
81
        float4 posq1 = posq[atom1];
82
        LOAD_ATOM1_PARAMETERS
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

        // Locate the exclusion data for this tile.

#ifdef USE_EXCLUSIONS
        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);
#else
        bool hasExclusions = false;
#endif
98
        if (x == y) {
99
            // This tile is on the diagonal.
100

101
102
103
104
105
            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;
106
107
            LOAD_LOCAL_PARAMETERS_FROM_1
#ifdef USE_EXCLUSIONS
108
            unsigned int excl = exclusions[exclusionIndex[localGroupIndex]+tgx];
109
#endif
110
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
111
#ifdef USE_EXCLUSIONS
112
                bool isExcluded = !(excl & 0x1);
113
#endif
114
115
                int atom2 = tbx+j;
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
116
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
117
#ifdef USE_PERIODIC
118
119
120
                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;
121
#endif
122
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
Peter Eastman's avatar
Peter Eastman committed
123
124
                float invR = RSQRT(r2);
                float r = RECIP(invR);
125
                LOAD_ATOM2_PARAMETERS
126
                atom2 = y*TILE_SIZE+j;
127
#ifdef USE_SYMMETRIC
128
                float dEdR = 0.0f;
129
130
131
132
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
133
134
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
135
                energy += 0.5f*tempEnergy;
136
137
138
139
140
#ifdef USE_SYMMETRIC
                force.xyz -= delta.xyz*dEdR;
#else
                force.xyz -= dEdR1.xyz;
#endif
141
                excl >>= 1;
142
143
144
            }

            // Write results
145
146
147

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
148
            forceBuffers[offset].xyz += force.xyz;
149
            releaseBuffer(x, forceBufferFlags);
150
151
        }
        else {
152
153
            // This is an off-diagonal tile.

154
            const unsigned int localAtomIndex = get_local_id(0);
155
            if (lasty != y) {
156
                unsigned int j = y*TILE_SIZE + tgx;
157
                float4 tempPosq = posq[j];
158
159
160
161
                localData[localAtomIndex].x = tempPosq.x;
                localData[localAtomIndex].y = tempPosq.y;
                localData[localAtomIndex].z = tempPosq.z;
                localData[localAtomIndex].q = tempPosq.w;
162
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
163
            }
164
165
166
            localData[localAtomIndex].fx = 0.0f;
            localData[localAtomIndex].fy = 0.0f;
            localData[localAtomIndex].fz = 0.0f;
167
#ifdef USE_CUTOFF
168
            unsigned int flags = (numTiles <= maxTiles ? interactionFlags[pos] : 0xFFFFFFFF);
169
            if (!hasExclusions && flags != 0xFFFFFFFF) {
170
171
172
173
174
175
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

176
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
177
178
                        if ((flags&(1<<j)) != 0) {
                            bool isExcluded = false;
179
180
                            int atom2 = tbx+j;
                            float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
181
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
182
#ifdef USE_PERIODIC
183
184
185
                            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;
186
#endif
187
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
188
189
                            float invR = RSQRT(r2);
                            float r = RECIP(invR);
190
                            LOAD_ATOM2_PARAMETERS
191
                            atom2 = y*TILE_SIZE+j;
192
#ifdef USE_SYMMETRIC
193
                            float dEdR = 0.0f;
194
195
196
197
#else
                            float4 dEdR1 = (float4) 0.0f;
                            float4 dEdR2 = (float4) 0.0f;
#endif
198
199
                            float tempEnergy = 0.0f;
                            COMPUTE_INTERACTION
200
			    energy += tempEnergy;
201
                            int bufferIndex = 3*get_local_id(0);
202
#ifdef USE_SYMMETRIC
203
                            delta.xyz *= dEdR;
204
                            force.xyz -= delta.xyz;
205
206
207
                            tempBuffer[bufferIndex] = delta.x;
                            tempBuffer[bufferIndex+1] = delta.y;
                            tempBuffer[bufferIndex+2] = delta.z;
208
209
#else
                            force.xyz -= dEdR1.xyz;
210
211
212
                            tempBuffer[bufferIndex] = dEdR2.x;
                            tempBuffer[bufferIndex+1] = dEdR2.y;
                            tempBuffer[bufferIndex+2] = dEdR2.z;
213
#endif
214

215
                            // Sum the forces on atom2.
216

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
                            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];
                            }
237
                            if (tgx == 0) {
238
239
240
                                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];
241
                            }
242
243
244
245
                        }
                    }
                }
            }
246
            else
247
248
#endif
            {
249
250
                // Compute the full set of interactions in this tile.

251
#ifdef USE_EXCLUSIONS
252
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[localGroupIndex]+tgx] : 0xFFFFFFFF);
253
                excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
254
#endif
255
                unsigned int tj = tgx;
256
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
257
#ifdef USE_EXCLUSIONS
258
                    bool isExcluded = !(excl & 0x1);
259
#endif
260
261
                    int atom2 = tbx+tj;
                    float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
262
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
263
#ifdef USE_PERIODIC
264
265
266
                    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;
267
#endif
268
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
269
270
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
271
                    LOAD_ATOM2_PARAMETERS
272
                    atom2 = y*TILE_SIZE+tj;
273
#ifdef USE_SYMMETRIC
274
                    float dEdR = 0.0f;
275
276
277
278
#else
                    float4 dEdR1 = (float4) 0.0f;
                    float4 dEdR2 = (float4) 0.0f;
#endif
279
280
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
281
		    energy += tempEnergy;
282
#ifdef USE_SYMMETRIC
283
                    delta.xyz *= dEdR;
284
                    force.xyz -= delta.xyz;
285
286
287
                    localData[tbx+tj].fx += delta.x;
                    localData[tbx+tj].fy += delta.y;
                    localData[tbx+tj].fz += delta.z;
288
289
290
291
292
293
#else
                    force.xyz -= dEdR1.xyz;
                    localData[tbx+tj].fx += dEdR2.x;
                    localData[tbx+tj].fy += dEdR2.y;
                    localData[tbx+tj].fz += dEdR2.z;
#endif
294
#ifdef USE_EXCLUSIONS
295
                    excl >>= 1;
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
296
#endif
297
                    tj = (tj + 1) & (TILE_SIZE - 1);
298
299
300
301
                }
            }

            // Write results
302
303
304

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
305
            forceBuffers[offset1].xyz += force.xyz;
306
307
308
            releaseBuffer(x, forceBufferFlags);
            reserveBuffer(y, forceBufferFlags);
            unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
309
            forceBuffers[offset2] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0.0f);
310
            releaseBuffer(y, forceBufferFlags);
311
        }
312
        lasty = y;
313
314
315
316
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}