"...amoeba/platforms/cuda/src/kernels/hippoComputeField.cu" did not exist on "5281aba48570043248d1a33dc79c1688b8ac9044"
gbsaObc_default.cl 23.9 KB
Newer Older
1
2
3
4
#ifdef SUPPORTS_64_BIT_ATOMICS
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
#endif
5
6
#define TILE_SIZE 32

7
typedef struct {
8
    real x, y, z;
9
    float radius, scaledRadius;
Peter Eastman's avatar
Peter Eastman committed
10
} AtomData1;
11

12
13
14
15
/**
 * Compute the Born sum.
 */

16
__kernel __attribute__((reqd_work_group_size(FORCE_WORK_GROUP_SIZE, 1, 1)))
17
18
19
20
void computeBornSum(
#ifdef SUPPORTS_64_BIT_ATOMICS
        __global long* restrict global_bornSum,
#else
21
        __global real* restrict global_bornSum,
22
#endif
23
        __global const real4* restrict posq, __global const float2* restrict global_params,
24
#ifdef USE_CUTOFF
25
        __global const ushort2* restrict tiles, __global const unsigned int* restrict interactionCount, real4 periodicBoxSize, real4 invPeriodicBoxSize, unsigned int maxTiles) {
26
27
28
29
30
#else
        unsigned int numTiles) {
#endif
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
31
32
    unsigned int pos = get_group_id(0)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/get_num_groups(0);
    unsigned int end = (get_group_id(0)+1)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/get_num_groups(0);
33
#else
34
35
    unsigned int pos = get_group_id(0)*numTiles/get_num_groups(0);
    unsigned int end = (get_group_id(0)+1)*numTiles/get_num_groups(0);
36
#endif
37
    unsigned int lasty = 0xFFFFFFFF;
38
    __local AtomData1 localData[TILE_SIZE];
39
40
    __local real localBornSum[FORCE_WORK_GROUP_SIZE];
    __local real localTemp[TILE_SIZE];
41
42
43

    while (pos < end) {
        // Extract the coordinates of this tile
44
        unsigned int x, y;
45
#ifdef USE_CUTOFF
46
        if (numTiles <= maxTiles) {
47
48
49
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
50
        }
51
        else
52
#endif
53
54
55
        {
            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);
56
57
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
58
59
60
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
61
62
        unsigned int baseLocalAtom = (get_local_id(0) < TILE_SIZE ? 0 : TILE_SIZE/2);
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
63
        unsigned int localForceOffset = get_local_id(0) & ~(TILE_SIZE-1);
64
        unsigned int atom1 = x*TILE_SIZE + tgx;
65
66
        real bornSum = 0.0f;
        real4 posq1 = posq[atom1];
67
68
69
70
        float2 params1 = global_params[atom1];
        if (x == y) {
            // This tile is on the diagonal.

71
72
73
74
75
76
77
            if (get_local_id(0) < TILE_SIZE) {
                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)].radius = params1.x;
                localData[get_local_id(0)].scaledRadius = params1.y;
            }
78
79
            barrier(CLK_LOCAL_MEM_FENCE);
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
80
                real4 delta = (real4) (localData[baseLocalAtom+j].x-posq1.x, localData[baseLocalAtom+j].y-posq1.y, localData[baseLocalAtom+j].z-posq1.z, 0);
81
#ifdef USE_PERIODIC
82
83
84
                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;
85
#endif
86
87
88
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                real invR = RSQRT(r2);
                real r = RECIP(invR);
89
                float2 params2 = (float2) (localData[baseLocalAtom+j].radius, localData[baseLocalAtom+j].scaledRadius);
90
                real rScaledRadiusJ = r+params2.y;
91
#ifdef USE_CUTOFF
92
                unsigned int includeInteraction = (atom1 < NUM_ATOMS && y*TILE_SIZE+baseLocalAtom+j < NUM_ATOMS && r2 < CUTOFF_SQUARED && (j+baseLocalAtom != tgx) && (params1.x < rScaledRadiusJ));
93
#else
94
                unsigned int includeInteraction = (atom1 < NUM_ATOMS && y*TILE_SIZE+baseLocalAtom+j < NUM_ATOMS && (j+baseLocalAtom != tgx) && (params1.x < rScaledRadiusJ));
95
#endif
96
97
98
99
100
                real l_ij = RECIP(max((real) params1.x, fabs(r-params2.y)));
                real u_ij = RECIP(rScaledRadiusJ);
                real l_ij2 = l_ij*l_ij;
                real u_ij2 = u_ij*u_ij;
                real ratio = LOG(u_ij * RECIP(l_ij));
101
102
103
                bornSum += (includeInteraction ? 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) : (real) 0);
                bornSum += (includeInteraction && params1.x < params2.y-r ? 2.0f*(RECIP(params1.x)-l_ij) : (real) 0);
104
105
106
107
108
            }

            // Sum the forces and write results.

            if (get_local_id(0) >= TILE_SIZE)
109
                localTemp[tgx] = bornSum;
110
111
            barrier(CLK_LOCAL_MEM_FENCE);
            if (get_local_id(0) < TILE_SIZE) {
112
113
114
115
#ifdef SUPPORTS_64_BIT_ATOMICS
                const unsigned int offset = x*TILE_SIZE + tgx;
                atom_add(&global_bornSum[offset], (long) ((bornSum + localTemp[tgx])*0xFFFFFFFF));
#else
116
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
117
                const unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
118
#else
119
120
121
                const unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
#endif
                global_bornSum[offset] += bornSum + localTemp[tgx];
122
123
#endif
            }
124
            // barrier not required here as localTemp is not accessed before encountering another barrier.
125
126
127
128
129
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y && get_local_id(0) < TILE_SIZE) {
130
                unsigned int j = y*TILE_SIZE + tgx;
131
                real4 tempPosq = posq[j];
132
133
134
                localData[get_local_id(0)].x = tempPosq.x;
                localData[get_local_id(0)].y = tempPosq.y;
                localData[get_local_id(0)].z = tempPosq.z;
135
                float2 tempParams = global_params[j];
136
137
                localData[get_local_id(0)].radius = tempParams.x;
                localData[get_local_id(0)].scaledRadius = tempParams.y;
138
            }
139
            localBornSum[get_local_id(0)] = 0.0f;
140
141
142
143
            barrier(CLK_LOCAL_MEM_FENCE);

            // Compute the full set of interactions in this tile.

144
            unsigned int tj = (tgx+baseLocalAtom) & (TILE_SIZE-1);
145
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
146
                real4 delta = (real4) (localData[tj].x-posq1.x, localData[tj].y-posq1.y, localData[tj].z-posq1.z, 0);
147
#ifdef USE_PERIODIC
148
149
150
                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;
151
#endif
152
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
153
#ifdef USE_CUTOFF
154
                unsigned int includeInteraction = (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS && r2 < CUTOFF_SQUARED);
155
#else
156
                unsigned int includeInteraction = (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS);
157
#endif
158
159
                real invR = RSQRT(r2);
                real r = RECIP(invR);
160
                float2 params2 = (float2) (localData[tj].radius, localData[tj].scaledRadius);
161
                real rScaledRadiusJ = r+params2.y;
162
                {
163
164
165
166
167
                    real l_ij = RECIP(max((real) params1.x, fabs(r-params2.y)));
                    real u_ij = RECIP(rScaledRadiusJ);
                    real l_ij2 = l_ij*l_ij;
                    real u_ij2 = u_ij*u_ij;
                    real ratio = LOG(u_ij * RECIP(l_ij));
168
                    unsigned int includeTerm = (includeInteraction && params1.x < rScaledRadiusJ);
169
170
171
                    bornSum += (includeTerm ? 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) : (real) 0);
                    bornSum += (includeTerm && params1.x < params2.y-r ? 2.0f*(RECIP(params1.x)-l_ij) : (real) 0);
172
                }
173
                real rScaledRadiusI = r+params1.y;
174
                {
175
176
177
178
179
180
                    real l_ij = RECIP(max((real) params2.x, fabs(r-params1.y)));
                    real u_ij = RECIP(rScaledRadiusI);
                    real l_ij2 = l_ij*l_ij;
                    real u_ij2 = u_ij*u_ij;
                    real ratio = LOG(u_ij * RECIP(l_ij));
                    real term = l_ij - u_ij + 0.25f*r*(u_ij2-l_ij2) + (0.50f*invR*ratio) +
181
                                     (0.25f*params1.y*params1.y*invR)*(l_ij2-u_ij2);
182
183
                    term += (params2.x < params1.y-r ? 2.0f*(RECIP(params2.x)-l_ij) : (real) 0);
                    localBornSum[tj+localForceOffset] += (includeInteraction && params2.x < rScaledRadiusI ? term : (real) 0);
184
185
                }
                barrier(CLK_LOCAL_MEM_FENCE);
186
                tj = (tj+1) & (TILE_SIZE-1);
187
188
189
190
191
            }

            // Sum the forces and write results.

            if (get_local_id(0) >= TILE_SIZE)
192
                localTemp[tgx] = bornSum;
193
194
            barrier(CLK_LOCAL_MEM_FENCE);
            if (get_local_id(0) < TILE_SIZE) {
195
196
197
198
199
200
#ifdef SUPPORTS_64_BIT_ATOMICS
                const unsigned int offset1 = x*TILE_SIZE + tgx;
                const unsigned int offset2 = y*TILE_SIZE + tgx;
                atom_add(&global_bornSum[offset1], (long) ((bornSum + localTemp[tgx])*0xFFFFFFFF));
                atom_add(&global_bornSum[offset2], (long) ((localBornSum[get_local_id(0)] + localBornSum[get_local_id(0)+TILE_SIZE])*0xFFFFFFFF));
#else
201
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
202
203
                const unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
                const unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
204
#else
205
206
                const unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                const unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
207
#endif
208
                // Do both loads before both stores to minimize store-load waits.
209
210
                real sum1 = global_bornSum[offset1];
                real sum2 = global_bornSum[offset2];
211
212
                sum1 += bornSum + localTemp[tgx];
                sum2 += localBornSum[get_local_id(0)] + localBornSum[get_local_id(0)+TILE_SIZE];
213
214
                global_bornSum[offset1] = sum1;
                global_bornSum[offset2] = sum2;
215
#endif
216
            }
217
            barrier(CLK_LOCAL_MEM_FENCE);
218
        }
219
        lasty = y;
220
221
222
223
        pos++;
    }
}

224
typedef struct {
225
226
    real x, y, z, w;
    real padding;
227
228
} PaddedUnalignedFloat4;

Peter Eastman's avatar
Peter Eastman committed
229
typedef struct {
230
231
232
233
    real x, y, z;
    real q;
    real bornRadius;
    real temp_x, temp_y, temp_z, temp_w;
Peter Eastman's avatar
Peter Eastman committed
234
235
} AtomData2;

236
237
238
239
/**
 * First part of computing the GBSA interaction.
 */

240
__kernel __attribute__((reqd_work_group_size(FORCE_WORK_GROUP_SIZE, 1, 1)))
241
242
243
244
void computeGBSAForce1(
#ifdef SUPPORTS_64_BIT_ATOMICS
        __global long* restrict forceBuffers, __global long* restrict global_bornForce,
#else
245
        __global real4* restrict forceBuffers, __global real* restrict global_bornForce,
246
#endif
247
        __global real* restrict energyBuffer, __global const real4* restrict posq, __global const real* restrict global_bornRadii,
248
#ifdef USE_CUTOFF
249
        __global const ushort2* restrict tiles, __global const unsigned int* restrict interactionCount, real4 periodicBoxSize, real4 invPeriodicBoxSize, unsigned int maxTiles) {
250
251
252
253
254
#else
        unsigned int numTiles) {
#endif
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
255
256
    unsigned int pos = get_group_id(0)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/get_num_groups(0);
    unsigned int end = (get_group_id(0)+1)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/get_num_groups(0);
257
#else
258
259
    unsigned int pos = get_group_id(0)*numTiles/get_num_groups(0);
    unsigned int end = (get_group_id(0)+1)*numTiles/get_num_groups(0);
260
#endif
261
    real energy = 0.0f;
262
    unsigned int lasty = 0xFFFFFFFF;
263
264
    __local AtomData2 localData[TILE_SIZE];
    __local PaddedUnalignedFloat4 localForce[FORCE_WORK_GROUP_SIZE];
265
266
267

    while (pos < end) {
        // Extract the coordinates of this tile
268
        unsigned int x, y;
269
#ifdef USE_CUTOFF
270
        if (numTiles <= maxTiles) {
271
272
273
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
274
        }
275
        else
276
#endif
277
278
279
        {
            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);
280
281
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
282
283
284
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
285
286
        unsigned int baseLocalAtom = (get_local_id(0) < TILE_SIZE ? 0 : TILE_SIZE/2);
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
287
        unsigned int localForceOffset = get_local_id(0) & ~(TILE_SIZE-1);
288
        unsigned int atom1 = x*TILE_SIZE + tgx;
289
290
291
        real4 force = 0.0f;
        real4 posq1 = posq[atom1];
        real bornRadius1 = global_bornRadii[atom1];
292
293
294
        if (x == y) {
            // This tile is on the diagonal.

295
296
297
298
299
300
301
            if (get_local_id(0) < TILE_SIZE) {
                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;
            }
302
303
            barrier(CLK_LOCAL_MEM_FENCE);
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
304
                unsigned int includeInteraction = (atom1 < NUM_ATOMS && y*TILE_SIZE+baseLocalAtom+j < NUM_ATOMS);
305
306
                real4 posq2 = (real4) (localData[baseLocalAtom+j].x, localData[baseLocalAtom+j].y, localData[baseLocalAtom+j].z, localData[baseLocalAtom+j].q);
                real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
307
#ifdef USE_PERIODIC
308
309
310
                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;
311
#endif
312
313
314
315
316
317
318
319
320
321
322
323
324
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                real invR = RSQRT(r2);
                real r = RECIP(invR);
                real bornRadius2 = localData[baseLocalAtom+j].bornRadius;
                real alpha2_ij = bornRadius1*bornRadius2;
                real D_ij = r2*RECIP(4.0f*alpha2_ij);
                real expTerm = EXP(-D_ij);
                real denominator2 = r2 + alpha2_ij*expTerm;
                real denominator = SQRT(denominator2);
                real tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                real Gpol = tempEnergy*RECIP(denominator2);
                real dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                real dEdR = Gpol*(1.0f - 0.25f*expTerm);
325
#ifdef USE_CUTOFF
326
327
328
                dEdR = (r2 > CUTOFF_SQUARED ? (real) 0 : dEdR);
                tempEnergy = (r2 > CUTOFF_SQUARED ? (real) 0 : tempEnergy);
                dGpol_dalpha2_ij = (r2 > CUTOFF_SQUARED ? (real) 0 : dGpol_dalpha2_ij);
329
#endif
330
331
332
                force.w += (includeInteraction ? dGpol_dalpha2_ij*bornRadius2 : (real) 0);
                energy += (includeInteraction ? 0.5f*tempEnergy : (real) 0);
                delta.xyz *= (includeInteraction ? dEdR : (real) 0);
333
                force.xyz -= delta.xyz;
334
335
336
337
            }

            // Sum the forces and write results.

338
339
340
341
342
343
            if (get_local_id(0) >= TILE_SIZE) {
                localData[tgx].temp_x = force.x;
                localData[tgx].temp_y = force.y;
                localData[tgx].temp_z = force.z;
                localData[tgx].temp_w = force.w;
            }
344
345
            barrier(CLK_LOCAL_MEM_FENCE);
            if (get_local_id(0) < TILE_SIZE) {
346
347
348
349
350
351
352
#ifdef SUPPORTS_64_BIT_ATOMICS
                const unsigned int offset = x*TILE_SIZE + tgx;
                atom_add(&forceBuffers[offset], (long) ((force.x + localData[tgx].temp_x)*0xFFFFFFFF));
                atom_add(&forceBuffers[offset+PADDED_NUM_ATOMS], (long) ((force.y + localData[tgx].temp_y)*0xFFFFFFFF));
                atom_add(&forceBuffers[offset+2*PADDED_NUM_ATOMS], (long) ((force.z + localData[tgx].temp_z)*0xFFFFFFFF));
                atom_add(&global_bornForce[offset], (long) ((force.w + localData[tgx].temp_w)*0xFFFFFFFF));
#else
353
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
354
                const unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
355
#else
356
                const unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
357
#endif
358
359
360
                // Cheaper to load/store real4 than real3. Do all loads before all stores to minimize store-load waits.
                real4 sum = forceBuffers[offset];
                real global_sum = global_bornForce[offset];
361
362
363
364
                sum.x += force.x + localData[tgx].temp_x;
                sum.y += force.y + localData[tgx].temp_y;
                sum.z += force.z + localData[tgx].temp_z;
                global_sum += force.w + localData[tgx].temp_w;
365
366
                forceBuffers[offset] = sum;
                global_bornForce[offset] = global_sum;
367
#endif
368
            }
369
            // barrier not required here as localData[*]/temp_* is not accessed before encountering another barrier.
370
371
372
373
374
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y && get_local_id(0) < TILE_SIZE) {
375
                unsigned int j = y*TILE_SIZE + tgx;
376
                real4 tempPosq = posq[j];
377
378
379
380
381
                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];
382
            }
383
384
385
386
            localForce[get_local_id(0)].x = 0.0f;
            localForce[get_local_id(0)].y = 0.0f;
            localForce[get_local_id(0)].z = 0.0f;
            localForce[get_local_id(0)].w = 0.0f;
387
388
389
390
            barrier(CLK_LOCAL_MEM_FENCE);

            // Compute the full set of interactions in this tile.

391
            unsigned int tj = (tgx+baseLocalAtom) & (TILE_SIZE-1);
392
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
393
                unsigned int includeInteraction = (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS);
394
395
                real4 posq2 = (real4) (localData[tj].x, localData[tj].y, localData[tj].z, localData[tj].q);
                real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
396
#ifdef USE_PERIODIC
397
398
399
                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;
400
#endif
401
402
403
404
405
406
407
408
409
410
411
412
413
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                real invR = RSQRT(r2);
                real r = RECIP(invR);
                real bornRadius2 = localData[tj].bornRadius;
                real alpha2_ij = bornRadius1*bornRadius2;
                real D_ij = r2*RECIP(4.0f*alpha2_ij);
                real expTerm = EXP(-D_ij);
                real denominator2 = r2 + alpha2_ij*expTerm;
                real denominator = SQRT(denominator2);
                real tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                real Gpol = tempEnergy*RECIP(denominator2);
                real dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                real dEdR = Gpol*(1.0f - 0.25f*expTerm);
414
#ifdef USE_CUTOFF
415
416
417
                dEdR = (r2 > CUTOFF_SQUARED ? (real) 0 : dEdR);
                tempEnergy = (r2 > CUTOFF_SQUARED ? (real) 0 : tempEnergy);
                dGpol_dalpha2_ij = (r2 > CUTOFF_SQUARED ? (real) 0 : dGpol_dalpha2_ij);
418
#endif
419
420
421
                force.w += (includeInteraction ? dGpol_dalpha2_ij*bornRadius2 : (real) 0);
                energy += (includeInteraction ? tempEnergy : (real) 0);
                delta.xyz *= (includeInteraction ? dEdR : (real) 0);
422
                force.xyz -= delta.xyz;
423
424
425
                localForce[tj+localForceOffset].x += delta.x;
                localForce[tj+localForceOffset].y += delta.y;
                localForce[tj+localForceOffset].z += delta.z;
426
                localForce[tj+localForceOffset].w += (includeInteraction ? dGpol_dalpha2_ij*bornRadius1 : (real) 0);
427
                barrier(CLK_LOCAL_MEM_FENCE);
428
                tj = (tj+1) & (TILE_SIZE-1);
429
430
431
432
            }

            // Sum the forces and write results.

433
434
435
436
437
438
            if (get_local_id(0) >= TILE_SIZE) {
                localData[tgx].temp_x = force.x;
                localData[tgx].temp_y = force.y;
                localData[tgx].temp_z = force.z;
                localData[tgx].temp_w = force.w;
            }
439
440
            barrier(CLK_LOCAL_MEM_FENCE);
            if (get_local_id(0) < TILE_SIZE) {
441
442
443
444
445
446
447
448
#ifdef SUPPORTS_64_BIT_ATOMICS
                barrier(CLK_LOCAL_MEM_FENCE);
                const unsigned int offset1 = x*TILE_SIZE + tgx;
                const unsigned int offset2 = y*TILE_SIZE + tgx;
                atom_add(&forceBuffers[offset1], (long) ((force.x + localData[tgx].temp_x)*0xFFFFFFFF));
                atom_add(&forceBuffers[offset1+PADDED_NUM_ATOMS], (long) ((force.y + localData[tgx].temp_y)*0xFFFFFFFF));
                atom_add(&forceBuffers[offset1+2*PADDED_NUM_ATOMS], (long) ((force.z + localData[tgx].temp_z)*0xFFFFFFFF));
                atom_add(&global_bornForce[offset1], (long) ((force.w + localData[tgx].temp_w)*0xFFFFFFFF));
449
450
451
452
                atom_add(&forceBuffers[offset2], (long) ((localForce[get_local_id(0)].x + localForce[get_local_id(0)+TILE_SIZE].x)*0xFFFFFFFF));
                atom_add(&forceBuffers[offset2+PADDED_NUM_ATOMS], (long) ((localForce[get_local_id(0)].y + localForce[get_local_id(0)+TILE_SIZE].y)*0xFFFFFFFF));
                atom_add(&forceBuffers[offset2+2*PADDED_NUM_ATOMS], (long) ((localForce[get_local_id(0)].z + localForce[get_local_id(0)+TILE_SIZE].z)*0xFFFFFFFF));
                atom_add(&global_bornForce[offset2], (long) ((localForce[get_local_id(0)].w + localForce[get_local_id(0)+TILE_SIZE].w)*0xFFFFFFFF));
453
#else
454
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
455
456
                const unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
                const unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
457
#else
458
459
                const unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                const unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
460
#endif
461
462
463
464
465
                // Cheaper to load/store real4 than real3. Do all loads before all stores to minimize store-load waits.
                real4 sum1 = forceBuffers[offset1];
                real4 sum2 = forceBuffers[offset2];
                real global_sum1 = global_bornForce[offset1];
                real global_sum2 = global_bornForce[offset2];
466
467
468
469
470
471
472
473
                sum1.x += force.x + localData[tgx].temp_x;
                sum1.y += force.y + localData[tgx].temp_y;
                sum1.z += force.z + localData[tgx].temp_z;
                global_sum1 += force.w + localData[tgx].temp_w;
                sum2.x += localForce[get_local_id(0)].x + localForce[get_local_id(0)+TILE_SIZE].x;
                sum2.y += localForce[get_local_id(0)].y + localForce[get_local_id(0)+TILE_SIZE].y;
                sum2.z += localForce[get_local_id(0)].z + localForce[get_local_id(0)+TILE_SIZE].z;
                global_sum2 += localForce[get_local_id(0)].w + localForce[get_local_id(0)+TILE_SIZE].w;
474
475
476
477
                forceBuffers[offset1] = sum1;
                forceBuffers[offset2] = sum2;
                global_bornForce[offset1] = global_sum1;
                global_bornForce[offset2] = global_sum2;
478
#endif
479
            }
480
            barrier(CLK_LOCAL_MEM_FENCE);
481
        }
482
        lasty = y;
483
484
485
486
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}