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

4
5
6
7
8
9
10
11
12
13
typedef struct {
    float x, y, z;
    float q;
    float fx, fy, fz, fw;
    float radius, scaledRadius;
    float bornSum;
    float bornRadius;
    float bornForce;
} AtomData;

14
/**
15
16
17
18
19
20
21
22
23
24
25
 * 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.
26
 */
27
28
29
30
31
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;
}
32

33
34
35
36
37
/**
 * Compute the Born sum.
 */
__kernel void computeBornSum(__global float* global_bornSum, __global float4* posq, __global float2* global_params,
        __local AtomData* localData, __local float* tempBuffer,  __global unsigned int* forceBufferFlags,
38
#ifdef USE_CUTOFF
39
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global unsigned int* interactionFlags) {
40
41
42
#else
        unsigned int numTiles) {
#endif
43
44
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
45
46
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
47
48
    unsigned int pos = warp*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
    unsigned int end = (warp+1)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
49
#else
50
51
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
52
#endif
53
54
55
56
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
        // Extract the coordinates of this tile
57
        unsigned int x, y;
58
#ifdef USE_CUTOFF
59
        if (numTiles <= maxTiles) {
60
61
62
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
63
        }
64
        else
65
#endif
66
67
68
        {
            y = (unsigned 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);
69
70
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
71
72
73
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
74
75
76
        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;
77
        unsigned int atom1 = x*TILE_SIZE + tgx;
78
        float bornSum = 0.0f;
79
80
        float4 posq1 = posq[atom1];
        float2 params1 = global_params[atom1];
81
82
83
        if (x == y) {
            // This tile is on the diagonal.

84
85
86
87
88
89
            localData[get_local_id(0)].x = posq1.x;
            localData[get_local_id(0)].y = posq1.y;
            localData[get_local_id(0)].z = posq1.z;
            localData[get_local_id(0)].q = posq1.w;
            localData[get_local_id(0)].radius = params1.x;
            localData[get_local_id(0)].scaledRadius = params1.y;
90
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
91
                float4 delta = (float4) (localData[tbx+j].x-posq1.x, localData[tbx+j].y-posq1.y, localData[tbx+j].z-posq1.z, 0.0f);
92
#ifdef USE_PERIODIC
93
94
95
                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;
96
97
98
#endif
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
99
                if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
100
#else
101
                if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS) {
102
#endif
103
104
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
105
                    float2 params2 = (float2) (localData[tbx+j].radius, localData[tbx+j].scaledRadius);
106
107
                    float rScaledRadiusJ = r+params2.y;
                    if ((j != tgx) && (params1.x < rScaledRadiusJ)) {
Peter Eastman's avatar
Peter Eastman committed
108
109
                        float l_ij = RECIP(max(params1.x, fabs(r-params2.y)));
                        float u_ij = RECIP(rScaledRadiusJ);
110
111
                        float l_ij2 = l_ij*l_ij;
                        float u_ij2 = u_ij*u_ij;
112
                        float ratio = LOG(u_ij * RECIP(l_ij));
113
114
115
                        bornSum += l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                         (0.25f*params2.y*params2.y*invR)*(l_ij2-u_ij2);
                        if (params1.x < params2.x-r)
116
                            bornSum += 2.0f*(RECIP(params1.x)-l_ij);
117
118
119
120
121
                    }
                }
            }

            // Write results
122
123
124

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
125
            global_bornSum[offset] += bornSum;
126
            releaseBuffer(x, forceBufferFlags);
127
128
129
130
131
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
132
                unsigned int j = y*TILE_SIZE + tgx;
133
134
135
136
137
138
139
140
                float4 tempPosq = posq[j];
                localData[get_local_id(0)].x = tempPosq.x;
                localData[get_local_id(0)].y = tempPosq.y;
                localData[get_local_id(0)].z = tempPosq.z;
                localData[get_local_id(0)].q = tempPosq.w;
                float2 tempParams = global_params[j];
                localData[get_local_id(0)].radius = tempParams.x;
                localData[get_local_id(0)].scaledRadius = tempParams.y;
141
            }
142
            localData[get_local_id(0)].bornSum = 0.0f;
143
#ifdef USE_CUTOFF
144
            unsigned int flags = (numTiles <= maxTiles ? interactionFlags[pos] : 0xFFFFFFFF);
145
            if (flags != 0xFFFFFFFF && false) { // TODO: Fix this: should be checking for exclusions
146
147
148
149
150
151
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

152
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
153
                        if ((flags&(1<<j)) != 0) {
154
                            float4 delta = (float4) (localData[tbx+j].x-posq1.x, localData[tbx+j].y-posq1.y, localData[tbx+j].z-posq1.z, 0.0f);
155
#ifdef USE_PERIODIC
156
157
158
                            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;
159
160
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
Peter Eastman's avatar
Peter Eastman committed
161
                            tempBuffer[get_local_id(0)] = 0.0f;
162
#ifdef USE_CUTOFF
163
                            if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
164
#else
165
                            if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS) {
166
#endif
167
168
                                float invR = RSQRT(r2);
                                float r = RECIP(invR);
169
                                float2 params2 = (float2) (localData[tbx+j].radius, localData[tbx+j].scaledRadius);
170
                                float rScaledRadiusJ = r+params2.y;
Peter Eastman's avatar
Peter Eastman committed
171
                                if (params1.x < rScaledRadiusJ) {
Peter Eastman's avatar
Peter Eastman committed
172
173
                                    float l_ij = RECIP(max(params1.x, fabs(r-params2.y)));
                                    float u_ij = RECIP(rScaledRadiusJ);
174
175
                                    float l_ij2 = l_ij*l_ij;
                                    float u_ij2 = u_ij*u_ij;
176
                                    float ratio = LOG(u_ij * RECIP(l_ij));
177
178
179
                                    bornSum += l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                                     (0.25f*params2.y*params2.y*invR)*(l_ij2-u_ij2);
                                    if (params1.x < params2.x-r)
180
                                        bornSum += 2.0f*(RECIP(params1.x)-l_ij);
181
182
                                }
                                float rScaledRadiusI = r+params1.y;
Peter Eastman's avatar
Peter Eastman committed
183
                                if (params2.x < rScaledRadiusI) {
Peter Eastman's avatar
Peter Eastman committed
184
185
                                    float l_ij = RECIP(max(params2.x, fabs(r-params1.y)));
                                    float u_ij = RECIP(rScaledRadiusI);
186
187
                                    float l_ij2 = l_ij*l_ij;
                                    float u_ij2 = u_ij*u_ij;
188
                                    float ratio = LOG(u_ij * RECIP(l_ij));
189
190
191
                                    float term = l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                                     (0.25f*params1.y*params1.y*invR)*(l_ij2-u_ij2);
                                    if (params2.x < params1.x-r)
192
                                        term += 2.0f*(RECIP(params2.x)-l_ij);
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                                    tempBuffer[get_local_id(0)] = term;
                                }
                            }

                            // Sum the forces on atom j.

                            if (tgx % 2 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+1];
                            if (tgx % 4 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+2];
                            if (tgx % 8 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+4];
                            if (tgx % 16 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+8];
                            if (tgx == 0)
208
                                localData[tbx+j].bornSum += tempBuffer[get_local_id(0)] + tempBuffer[get_local_id(0)+16];
209
210
211
212
213
214
215
216
217
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

218
219
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
220
                    float4 delta = (float4) (localData[tbx+tj].x-posq1.x, localData[tbx+tj].y-posq1.y, localData[tbx+tj].z-posq1.z, 0.0f);
221
#ifdef USE_PERIODIC
222
223
224
                    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;
225
226
227
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
228
                    if (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
229
#else
230
                    if (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS) {
231
#endif
232
233
                        float invR = RSQRT(r2);
                        float r = RECIP(invR);
234
                        float2 params2 = (float2) (localData[tbx+tj].radius, localData[tbx+tj].scaledRadius);
235
                        float rScaledRadiusJ = r+params2.y;
Peter Eastman's avatar
Peter Eastman committed
236
                        if (params1.x < rScaledRadiusJ) {
Peter Eastman's avatar
Peter Eastman committed
237
238
                            float l_ij = RECIP(max(params1.x, fabs(r-params2.y)));
                            float u_ij = RECIP(rScaledRadiusJ);
239
240
                            float l_ij2 = l_ij*l_ij;
                            float u_ij2 = u_ij*u_ij;
241
                            float ratio = LOG(u_ij * RECIP(l_ij));
242
243
244
                            bornSum += l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                             (0.25f*params2.y*params2.y*invR)*(l_ij2-u_ij2);
                            if (params1.x < params2.x-r)
245
                                bornSum += 2.0f*(RECIP(params1.x)-l_ij);
246
247
                        }
                        float rScaledRadiusI = r+params1.y;
Peter Eastman's avatar
Peter Eastman committed
248
                        if (params2.x < rScaledRadiusI) {
Peter Eastman's avatar
Peter Eastman committed
249
250
                            float l_ij = RECIP(max(params2.x, fabs(r-params1.y)));
                            float u_ij = RECIP(rScaledRadiusI);
251
252
                            float l_ij2 = l_ij*l_ij;
                            float u_ij2 = u_ij*u_ij;
253
                            float ratio = LOG(u_ij * RECIP(l_ij));
254
255
256
                            float term = l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
                                             (0.25f*params1.y*params1.y*invR)*(l_ij2-u_ij2);
                            if (params2.x < params1.x-r)
257
                                term += 2.0f*(RECIP(params2.x)-l_ij);
258
                            localData[tbx+tj].bornSum += term;
259
260
                        }
                    }
261
                    tj = (tj + 1) & (TILE_SIZE - 1);
262
263
264
265
                }
            }

            // Write results
266
267
268

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
269
            global_bornSum[offset1] += bornSum;
270
271
272
            releaseBuffer(x, forceBufferFlags);
            reserveBuffer(y, forceBufferFlags);
            unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
273
            global_bornSum[offset2] += localData[get_local_id(0)].bornSum;
274
            releaseBuffer(y, forceBufferFlags);
275
        }
276
        lasty = y;
277
278
279
280
        pos++;
    }
}

281
282
283
284
/**
 * First part of computing the GBSA interaction.
 */

285
286
287
__kernel void computeGBSAForce1(__global float4* forceBuffers, __global float* energyBuffer,
        __global float4* posq, __global float* global_bornRadii, __global float* global_bornForce,
        __local AtomData* localData, __local float4* tempBuffer, __global unsigned int* forceBufferFlags,
288
#ifdef USE_CUTOFF
289
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global unsigned int* interactionFlags) {
290
291
292
#else
        unsigned int numTiles) {
#endif
293
294
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
295
296
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
297
298
    unsigned int pos = warp*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
    unsigned int end = (warp+1)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
299
#else
300
301
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
302
#endif
303
304
305
306
307
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
        // Extract the coordinates of this tile
308
        unsigned int x, y;
309
#ifdef USE_CUTOFF
310
        if (numTiles <= maxTiles) {
311
312
313
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
314
        }
315
        else
316
#endif
317
318
319
        {
            y = (unsigned 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);
320
321
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
322
323
324
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
325
326
327
        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;
328
        unsigned int atom1 = x*TILE_SIZE + tgx;
329
330
331
332
333
334
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        float bornRadius1 = global_bornRadii[atom1];
        if (x == y) {
            // This tile is on the diagonal.

335
336
337
338
339
            localData[get_local_id(0)].x = posq1.x;
            localData[get_local_id(0)].y = posq1.y;
            localData[get_local_id(0)].z = posq1.z;
            localData[get_local_id(0)].q = posq1.w;
            localData[get_local_id(0)].bornRadius = bornRadius1;
340
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
341
                if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS) {
342
                    float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
343
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
344
#ifdef USE_PERIODIC
345
346
347
                    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;
348
349
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
350
351
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
352
                    float bornRadius2 = localData[tbx+j].bornRadius;
353
                    float alpha2_ij = bornRadius1*bornRadius2;
354
                    float D_ij = r2*RECIP(4.0f*alpha2_ij);
355
                    float expTerm = EXP(-D_ij);
356
                    float denominator2 = r2 + alpha2_ij*expTerm;
357
                    float denominator = SQRT(denominator2);
358
359
                    float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                    float Gpol = tempEnergy*RECIP(denominator2);
360
361
362
                    float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                    float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
363
                    if (r2 > CUTOFF_SQUARED) {
364
365
                        dEdR = 0.0f;
                        tempEnergy  = 0.0f;
366
                        dGpol_dalpha2_ij = 0.0f;
367
368
                    }
#endif
369
                    force.w += dGpol_dalpha2_ij*bornRadius2;
370
371
372
373
374
375
376
                    energy += 0.5f*tempEnergy;
                    delta.xyz *= dEdR;
                    force.xyz -= delta.xyz;
                }
            }

            // Write results
377
378
379

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
380
            forceBuffers[offset].xyz += force.xyz;
Peter Eastman's avatar
Peter Eastman committed
381
            global_bornForce[offset] += force.w;
382
            releaseBuffer(x, forceBufferFlags);
383
384
385
386
387
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
388
                unsigned int j = y*TILE_SIZE + tgx;
389
390
391
392
393
394
                float4 tempPosq = posq[j];
                localData[get_local_id(0)].x = tempPosq.x;
                localData[get_local_id(0)].y = tempPosq.y;
                localData[get_local_id(0)].z = tempPosq.z;
                localData[get_local_id(0)].q = tempPosq.w;
                localData[get_local_id(0)].bornRadius = global_bornRadii[j];
395
            }
396
397
398
399
            localData[get_local_id(0)].fx = 0.0f;
            localData[get_local_id(0)].fy = 0.0f;
            localData[get_local_id(0)].fz = 0.0f;
            localData[get_local_id(0)].fw = 0.0f;
400
#ifdef USE_CUTOFF
401
            unsigned int flags = (numTiles <= maxTiles ? interactionFlags[pos] : 0xFFFFFFFF);
402
            if (flags != 0xFFFFFFFF && false) { // TODO: Fix this: should be checking for exclusions
403
404
405
406
407
408
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

409
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
410
                        if ((flags&(1<<j)) != 0) {
411
                            float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
412
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
413
#ifdef USE_PERIODIC
414
415
416
                            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;
417
418
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
419
420
                            float invR = RSQRT(r2);
                            float r = RECIP(invR);
421
                            float bornRadius2 = localData[tbx+j].bornRadius;
422
                            float alpha2_ij = bornRadius1*bornRadius2;
423
                            float D_ij = r2*RECIP(4.0f*alpha2_ij);
424
                            float expTerm = EXP(-D_ij);
425
                            float denominator2 = r2 + alpha2_ij*expTerm;
426
                            float denominator = SQRT(denominator2);
427
428
                            float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                            float Gpol = tempEnergy*RECIP(denominator2);
429
430
431
                            float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                            float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
432
                            if (atom1 >= NUM_ATOMS || y*TILE_SIZE+j >= NUM_ATOMS || r2 > CUTOFF_SQUARED) {
433
#else
434
                            if (atom1 >= NUM_ATOMS || y*TILE_SIZE+j >= NUM_ATOMS) {
435
436
#endif
                                dEdR = 0.0f;
437
                                dGpol_dalpha2_ij = 0.0f;
438
439
440
				tempEnergy = 0.0f;
                            }
			    energy += tempEnergy;
441
                            force.w += dGpol_dalpha2_ij*bornRadius2;
442
443
                            delta.xyz *= dEdR;
                            force.xyz -= delta.xyz;
Peter Eastman's avatar
Peter Eastman committed
444
                            tempBuffer[get_local_id(0)] = (float4) (delta.xyz, dGpol_dalpha2_ij*bornRadius1);
445
446
447
448
449
450
451
452
453
454
455

                            // Sum the forces on atom j.

                            if (tgx % 2 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+1];
                            if (tgx % 4 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+2];
                            if (tgx % 8 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+4];
                            if (tgx % 16 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+8];
456
457
458
459
460
461
462
                            if (tgx == 0) {
                                float4 sum = tempBuffer[get_local_id(0)] + tempBuffer[get_local_id(0)+16];
                                localData[tbx+j].fx += sum.x;
                                localData[tbx+j].fy += sum.y;
                                localData[tbx+j].fz += sum.z;
                                localData[tbx+j].fw += sum.w;
                            }
463
464
465
466
467
468
469
470
471
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

472
473
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
474
                    if (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS) {
475
                        float4 posq2 = (float4) (localData[tbx+tj].x, localData[tbx+tj].y, localData[tbx+tj].z, localData[tbx+tj].q);
476
                        float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
477
#ifdef USE_PERIODIC
478
479
480
                        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;
481
482
#endif
                        float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
483
484
                        float invR = RSQRT(r2);
                        float r = RECIP(invR);
485
                        float bornRadius2 = localData[tbx+tj].bornRadius;
486
                        float alpha2_ij = bornRadius1*bornRadius2;
487
                        float D_ij = r2*RECIP(4.0f*alpha2_ij);
488
                        float expTerm = EXP(-D_ij);
489
                        float denominator2 = r2 + alpha2_ij*expTerm;
490
                        float denominator = SQRT(denominator2);
491
492
                        float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                        float Gpol = tempEnergy*RECIP(denominator2);
493
494
495
                        float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                        float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
496
                        if (r2 > CUTOFF_SQUARED) {
497
498
                            dEdR = 0.0f;
                            tempEnergy  = 0.0f;
499
                            dGpol_dalpha2_ij = 0.0f;
500
501
                        }
#endif
502
                        force.w += dGpol_dalpha2_ij*bornRadius2;
503
504
505
                        energy += tempEnergy;
                        delta.xyz *= dEdR;
                        force.xyz -= delta.xyz;
506
507
508
509
                        localData[tbx+tj].fx += delta.x;
                        localData[tbx+tj].fy += delta.y;
                        localData[tbx+tj].fz += delta.z;
                        localData[tbx+tj].fw += dGpol_dalpha2_ij*bornRadius1;
510
                    }
511
                    tj = (tj + 1) & (TILE_SIZE - 1);
512
513
514
515
                }
            }

            // Write results
516
517
518

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
519
            forceBuffers[offset1].xyz += force.xyz;
Peter Eastman's avatar
Peter Eastman committed
520
            global_bornForce[offset1] += force.w;
521
522
523
524
            releaseBuffer(x, forceBufferFlags);
            reserveBuffer(y, forceBufferFlags);
            unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
            forceBuffers[offset2] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0);
525
            global_bornForce[offset2] += localData[get_local_id(0)].fw;
526
            releaseBuffer(y, forceBufferFlags);
527
        }
528
        lasty = y;
529
530
531
532
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}