gbsaObc_nvidia.cl 24.8 KB
Newer Older
1
#define TILE_SIZE 32
2

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

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

17
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
18
void computeBornSum(__global float* global_bornSum, __global float4* posq, __global float2* global_params, __local AtomData* localData, __local float* tempBuffer,
19
#ifdef USE_CUTOFF
20
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, __global unsigned int* interactionFlags) {
21
22
23
#else
        unsigned int numTiles) {
#endif
24
25
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
26
27
28
29
30
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
    unsigned int pos = warp*(numTiles > MAX_TILES ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
    unsigned int end = (warp+1)*(numTiles > MAX_TILES ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
#else
31
32
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
33
#endif
34
35
36
37
38
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
        // Extract the coordinates of this tile
39
        unsigned int x, y;
40
#ifdef USE_CUTOFF
41
42
43
44
        if (numTiles <= MAX_TILES) {
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
45
        }
46
        else
47
#endif
48
49
50
51
52
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);
            if (x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y++;
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
56
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
57
        unsigned int tbx = get_local_id(0) - tgx;
58
        unsigned int atom1 = x*TILE_SIZE + tgx;
59
        float bornSum = 0.0f;
60
61
        float4 posq1 = posq[atom1];
        float2 params1 = global_params[atom1];
62
63
64
        if (x == y) {
            // This tile is on the diagonal.

65
66
67
68
69
70
            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;
71
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
72
                float4 delta = (float4) (localData[tbx+j].x-posq1.x, localData[tbx+j].y-posq1.y, localData[tbx+j].z-posq1.z, 0.0f);
73
#ifdef USE_PERIODIC
74
75
76
                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;
77
78
79
#endif
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
80
                if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
81
#else
82
                if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS) {
83
#endif
84
85
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
86
                    float2 params2 = (float2) (localData[tbx+j].radius, localData[tbx+j].scaledRadius);
87
88
                    float rScaledRadiusJ = r+params2.y;
                    if ((j != tgx) && (params1.x < rScaledRadiusJ)) {
Peter Eastman's avatar
Peter Eastman committed
89
90
                        float l_ij = RECIP(max(params1.x, fabs(r-params2.y)));
                        float u_ij = RECIP(rScaledRadiusJ);
91
92
                        float l_ij2 = l_ij*l_ij;
                        float u_ij2 = u_ij*u_ij;
93
                        float ratio = LOG(u_ij * RECIP(l_ij));
94
95
96
                        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)
97
                            bornSum += 2.0f*(RECIP(params1.x)-l_ij);
98
99
100
101
102
103
                    }
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
104
            unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
105
#else
106
            unsigned int offset = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
107
108
109
110
111
112
113
#endif
            global_bornSum[offset] += bornSum;
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
114
                unsigned int j = y*TILE_SIZE + tgx;
115
116
117
118
119
120
121
122
                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;
123
            }
124
            localData[get_local_id(0)].bornSum = 0.0f;
125
#ifdef USE_CUTOFF
126
            unsigned int flags = (numTiles <= MAX_TILES ? interactionFlags[pos] : 0xFFFFFFFF);
127
            if (flags != 0xFFFFFFFF && false) { // TODO: Fix this: should be checking for exclusions
128
129
130
131
132
133
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

134
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
135
                        if ((flags&(1<<j)) != 0) {
136
                            float4 delta = (float4) (localData[tbx+j].x-posq1.x, localData[tbx+j].y-posq1.y, localData[tbx+j].z-posq1.z, 0.0f);
137
#ifdef USE_PERIODIC
138
139
140
                            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;
141
142
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
Peter Eastman's avatar
Peter Eastman committed
143
                            tempBuffer[get_local_id(0)] = 0.0f;
144
#ifdef USE_CUTOFF
145
                            if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
146
#else
147
                            if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS) {
148
#endif
149
150
                                float invR = RSQRT(r2);
                                float r = RECIP(invR);
151
                                float2 params2 = (float2) (localData[tbx+j].radius, localData[tbx+j].scaledRadius);
152
                                float rScaledRadiusJ = r+params2.y;
Peter Eastman's avatar
Peter Eastman committed
153
                                if (params1.x < rScaledRadiusJ) {
Peter Eastman's avatar
Peter Eastman committed
154
155
                                    float l_ij = RECIP(max(params1.x, fabs(r-params2.y)));
                                    float u_ij = RECIP(rScaledRadiusJ);
156
157
                                    float l_ij2 = l_ij*l_ij;
                                    float u_ij2 = u_ij*u_ij;
158
                                    float ratio = LOG(u_ij * RECIP(l_ij));
159
160
161
                                    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)
162
                                        bornSum += 2.0f*(RECIP(params1.x)-l_ij);
163
164
                                }
                                float rScaledRadiusI = r+params1.y;
Peter Eastman's avatar
Peter Eastman committed
165
                                if (params2.x < rScaledRadiusI) {
Peter Eastman's avatar
Peter Eastman committed
166
167
                                    float l_ij = RECIP(max(params2.x, fabs(r-params1.y)));
                                    float u_ij = RECIP(rScaledRadiusI);
168
169
                                    float l_ij2 = l_ij*l_ij;
                                    float u_ij2 = u_ij*u_ij;
170
                                    float ratio = LOG(u_ij * RECIP(l_ij));
171
172
173
                                    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)
174
                                        term += 2.0f*(RECIP(params2.x)-l_ij);
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
                                    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)
190
                                localData[tbx+j].bornSum += tempBuffer[get_local_id(0)] + tempBuffer[get_local_id(0)+16];
191
192
193
194
195
196
197
198
199
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

200
201
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
202
                    float4 delta = (float4) (localData[tbx+tj].x-posq1.x, localData[tbx+tj].y-posq1.y, localData[tbx+tj].z-posq1.z, 0.0f);
203
#ifdef USE_PERIODIC
204
205
206
                    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;
207
208
209
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
210
                    if (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
211
#else
212
                    if (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS) {
213
#endif
214
215
                        float invR = RSQRT(r2);
                        float r = RECIP(invR);
216
                        float2 params2 = (float2) (localData[tbx+tj].radius, localData[tbx+tj].scaledRadius);
217
                        float rScaledRadiusJ = r+params2.y;
Peter Eastman's avatar
Peter Eastman committed
218
                        if (params1.x < rScaledRadiusJ) {
Peter Eastman's avatar
Peter Eastman committed
219
220
                            float l_ij = RECIP(max(params1.x, fabs(r-params2.y)));
                            float u_ij = RECIP(rScaledRadiusJ);
221
222
                            float l_ij2 = l_ij*l_ij;
                            float u_ij2 = u_ij*u_ij;
223
                            float ratio = LOG(u_ij * RECIP(l_ij));
224
225
226
                            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)
227
                                bornSum += 2.0f*(RECIP(params1.x)-l_ij);
228
229
                        }
                        float rScaledRadiusI = r+params1.y;
Peter Eastman's avatar
Peter Eastman committed
230
                        if (params2.x < rScaledRadiusI) {
Peter Eastman's avatar
Peter Eastman committed
231
232
                            float l_ij = RECIP(max(params2.x, fabs(r-params1.y)));
                            float u_ij = RECIP(rScaledRadiusI);
233
234
                            float l_ij2 = l_ij*l_ij;
                            float u_ij2 = u_ij*u_ij;
235
                            float ratio = LOG(u_ij * RECIP(l_ij));
236
237
238
                            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)
239
                                term += 2.0f*(RECIP(params2.x)-l_ij);
240
                            localData[tbx+tj].bornSum += term;
241
242
                        }
                    }
243
                    tj = (tj + 1) & (TILE_SIZE - 1);
244
245
246
247
248
249
                }
            }

            // Write results
            float4 of;
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
250
251
            unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
252
#else
253
254
            unsigned int offset1 = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
255
256
#endif
            global_bornSum[offset1] += bornSum;
257
            global_bornSum[offset2] += localData[get_local_id(0)].bornSum;
258
        }
259
        lasty = y;
260
261
262
263
        pos++;
    }
}

264
265
266
267
/**
 * First part of computing the GBSA interaction.
 */

268
269
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
void computeGBSAForce1(__global float4* forceBuffers, __global float* energyBuffer,
270
        __global float4* posq, __global float* global_bornRadii,
271
        __global float* global_bornForce, __local AtomData* localData, __local float4* tempBuffer,
272
#ifdef USE_CUTOFF
273
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, __global unsigned int* interactionFlags) {
274
275
276
#else
        unsigned int numTiles) {
#endif
277
278
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
279
280
281
282
283
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
    unsigned int pos = warp*(numTiles > MAX_TILES ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
    unsigned int end = (warp+1)*(numTiles > MAX_TILES ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
#else
284
285
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
286
#endif
287
288
289
290
291
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
        // Extract the coordinates of this tile
292
        unsigned int x, y;
293
#ifdef USE_CUTOFF
294
295
296
297
        if (numTiles <= MAX_TILES) {
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
298
        }
299
        else
300
#endif
301
302
303
304
305
306
307
308
        {
            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);
            if (x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y++;
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
309
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
310
        unsigned int tbx = get_local_id(0) - tgx;
311
        unsigned int atom1 = x*TILE_SIZE + tgx;
312
313
314
315
316
317
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        float bornRadius1 = global_bornRadii[atom1];
        if (x == y) {
            // This tile is on the diagonal.

318
319
320
321
322
            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;
323
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
324
                if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS) {
325
                    float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
326
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
327
#ifdef USE_PERIODIC
328
329
330
                    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;
331
332
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
333
334
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
335
                    float bornRadius2 = localData[tbx+j].bornRadius;
336
                    float alpha2_ij = bornRadius1*bornRadius2;
337
                    float D_ij = r2*RECIP(4.0f*alpha2_ij);
338
                    float expTerm = EXP(-D_ij);
339
                    float denominator2 = r2 + alpha2_ij*expTerm;
340
                    float denominator = SQRT(denominator2);
341
342
                    float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                    float Gpol = tempEnergy*RECIP(denominator2);
343
344
345
346
                    float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                    force.w += dGpol_dalpha2_ij*bornRadius2;
                    float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
347
                    if (r2 > CUTOFF_SQUARED) {
348
349
350
351
352
353
354
355
356
357
358
359
                        dEdR = 0.0f;
                        tempEnergy  = 0.0f;
                    }
#endif
                    energy += 0.5f*tempEnergy;
                    delta.xyz *= dEdR;
                    force.xyz -= delta.xyz;
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
360
            unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
361
#else
362
            unsigned int offset = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
363
364
#endif
            forceBuffers[offset].xyz += force.xyz;
Peter Eastman's avatar
Peter Eastman committed
365
            global_bornForce[offset] += force.w;
366
367
368
369
370
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
371
                unsigned int j = y*TILE_SIZE + tgx;
372
373
374
375
376
377
                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];
378
            }
379
380
381
382
            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;
383
#ifdef USE_CUTOFF
384
            unsigned int flags = (numTiles <= MAX_TILES ? interactionFlags[pos] : 0xFFFFFFFF);
385
            if (flags != 0xFFFFFFFF && false) { // TODO: Fix this: should be checking for exclusions
386
387
388
389
390
391
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

392
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
393
                        if ((flags&(1<<j)) != 0) {
394
                            float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
395
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
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
401
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
402
403
                            float invR = RSQRT(r2);
                            float r = RECIP(invR);
404
                            float bornRadius2 = localData[tbx+j].bornRadius;
405
                            float alpha2_ij = bornRadius1*bornRadius2;
406
                            float D_ij = r2*RECIP(4.0f*alpha2_ij);
407
                            float expTerm = EXP(-D_ij);
408
                            float denominator2 = r2 + alpha2_ij*expTerm;
409
                            float denominator = SQRT(denominator2);
410
411
                            float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                            float Gpol = tempEnergy*RECIP(denominator2);
412
413
414
415
                            float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                            force.w += dGpol_dalpha2_ij*bornRadius2;
                            float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
416
                            if (atom1 >= NUM_ATOMS || y*TILE_SIZE+j >= NUM_ATOMS || r2 > CUTOFF_SQUARED) {
417
#else
418
                            if (atom1 >= NUM_ATOMS || y*TILE_SIZE+j >= NUM_ATOMS) {
419
420
421
422
423
424
425
#endif
                                dEdR = 0.0f;
				tempEnergy = 0.0f;
                            }
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
                            force.xyz -= delta.xyz;
Peter Eastman's avatar
Peter Eastman committed
426
                            tempBuffer[get_local_id(0)] = (float4) (delta.xyz, dGpol_dalpha2_ij*bornRadius1);
427
428
429
430
431
432
433
434
435
436
437

                            // 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];
438
439
440
441
442
443
444
                            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;
                            }
445
446
447
448
449
450
451
452
453
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

454
455
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
456
                    if (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS) {
457
                        float4 posq2 = (float4) (localData[tbx+tj].x, localData[tbx+tj].y, localData[tbx+tj].z, localData[tbx+tj].q);
458
                        float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
459
#ifdef USE_PERIODIC
460
461
462
                        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;
463
464
#endif
                        float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
465
466
                        float invR = RSQRT(r2);
                        float r = RECIP(invR);
467
                        float bornRadius2 = localData[tbx+tj].bornRadius;
468
                        float alpha2_ij = bornRadius1*bornRadius2;
469
                        float D_ij = r2*RECIP(4.0f*alpha2_ij);
470
                        float expTerm = EXP(-D_ij);
471
                        float denominator2 = r2 + alpha2_ij*expTerm;
472
                        float denominator = SQRT(denominator2);
473
474
                        float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                        float Gpol = tempEnergy*RECIP(denominator2);
475
476
477
478
                        float dGpol_dalpha2_ij = -0.5f*Gpol*expTerm*(1.0f+D_ij);
                        force.w += dGpol_dalpha2_ij*bornRadius2;
                        float dEdR = Gpol*(1.0f - 0.25f*expTerm);
#ifdef USE_CUTOFF
479
                        if (r2 > CUTOFF_SQUARED) {
480
481
482
483
484
485
486
                            dEdR = 0.0f;
                            tempEnergy  = 0.0f;
                        }
#endif
                        energy += tempEnergy;
                        delta.xyz *= dEdR;
                        force.xyz -= delta.xyz;
487
488
489
490
                        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;
491
                    }
492
                    tj = (tj + 1) & (TILE_SIZE - 1);
493
494
495
496
497
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
498
499
            unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
500
#else
501
502
            unsigned int offset1 = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
503
504
#endif
            forceBuffers[offset1].xyz += force.xyz;
505
            forceBuffers[offset2] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0);
Peter Eastman's avatar
Peter Eastman committed
506
            global_bornForce[offset1] += force.w;
507
            global_bornForce[offset2] += localData[get_local_id(0)].fw;
508
        }
509
        lasty = y;
510
511
512
513
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}