gbsaObc_nvidia.cl 24.1 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 unsigned int* tiles, __global unsigned int* interactionFlags, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize) {
21
22
23
24
25
26
#else
        unsigned int numTiles) {
#endif
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
27
28
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
29
30
31
32
33
34
35
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
        // Extract the coordinates of this tile
36
#ifdef USE_CUTOFF
37
        unsigned int x = tiles[pos];
38
39
40
41
42
43
44
45
46
47
        unsigned int y = ((x >> 2) & 0x7fff);
        x = (x>>17);
#else
        unsigned int y = (unsigned int) floor(NUM_BLOCKS+0.5f-sqrt((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
        unsigned int 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);
        }
#endif
48
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
49
        unsigned int tbx = get_local_id(0) - tgx;
50
        unsigned int atom1 = x*TILE_SIZE + tgx;
51
        float bornSum = 0.0f;
52
53
        float4 posq1 = posq[atom1];
        float2 params1 = global_params[atom1];
54
55
56
        if (x == y) {
            // This tile is on the diagonal.

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

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
96
            unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
97
#else
98
            unsigned int offset = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
99
100
101
102
103
104
105
#endif
            global_bornSum[offset] += bornSum;
        }
        else {
            // This is an off-diagonal tile.

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

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

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

            // Write results
            float4 of;
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
242
243
            unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
244
#else
245
246
            unsigned int offset1 = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
247
248
#endif
            global_bornSum[offset1] += bornSum;
249
            global_bornSum[offset2] += localData[get_local_id(0)].bornSum;
250
251
252
253
254
255
            lasty = y;
        }
        pos++;
    }
}

256
257
258
259
/**
 * First part of computing the GBSA interaction.
 */

260
261
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
void computeGBSAForce1(__global float4* forceBuffers, __global float* energyBuffer,
262
        __global float4* posq, __global float* global_bornRadii,
263
        __global float* global_bornForce, __local AtomData* localData, __local float4* tempBuffer,
264
#ifdef USE_CUTOFF
265
        __global unsigned int* tiles, __global unsigned int* interactionFlags, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize) {
266
267
268
269
270
271
#else
        unsigned int numTiles) {
#endif
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
272
273
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
274
275
276
277
278
279
280
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
        // Extract the coordinates of this tile
281
#ifdef USE_CUTOFF
282
        unsigned int x = tiles[pos];
283
284
285
286
287
288
289
290
291
292
        unsigned int y = ((x >> 2) & 0x7fff);
        x = (x>>17);
#else
        unsigned int y = (unsigned int) floor(NUM_BLOCKS+0.5f-sqrt((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
        unsigned int 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);
        }
#endif
293
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
294
        unsigned int tbx = get_local_id(0) - tgx;
295
        unsigned int atom1 = x*TILE_SIZE + tgx;
296
297
298
299
300
301
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        float bornRadius1 = global_bornRadii[atom1];
        if (x == y) {
            // This tile is on the diagonal.

302
303
304
305
306
            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;
307
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
308
                if (atom1 < NUM_ATOMS && y*TILE_SIZE+j < NUM_ATOMS) {
309
                    float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
310
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
311
#ifdef USE_PERIODIC
312
313
314
                    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;
315
316
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
317
318
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
319
                    float bornRadius2 = localData[tbx+j].bornRadius;
320
                    float alpha2_ij = bornRadius1*bornRadius2;
321
                    float D_ij = r2*RECIP(4.0f*alpha2_ij);
322
                    float expTerm = EXP(-D_ij);
323
                    float denominator2 = r2 + alpha2_ij*expTerm;
324
                    float denominator = SQRT(denominator2);
325
326
                    float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                    float Gpol = tempEnergy*RECIP(denominator2);
327
328
329
330
                    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
331
                    if (r2 > CUTOFF_SQUARED) {
332
333
334
335
336
337
338
339
340
341
342
343
                        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
344
            unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
345
#else
346
            unsigned int offset = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
347
348
#endif
            forceBuffers[offset].xyz += force.xyz;
Peter Eastman's avatar
Peter Eastman committed
349
            global_bornForce[offset] += force.w;
350
351
352
353
354
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
355
                unsigned int j = y*TILE_SIZE + tgx;
356
357
358
359
360
361
                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];
362
            }
363
364
365
366
            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;
367
368
#ifdef USE_CUTOFF
            unsigned int flags = interactionFlags[pos];
369
            if (flags != 0xFFFFFFFF && false) { // TODO: Fix this: should be checking for exclusions
370
371
372
373
374
375
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

376
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
377
                        if ((flags&(1<<j)) != 0) {
378
                            float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
379
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
380
#ifdef USE_PERIODIC
381
382
383
                            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;
384
385
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
386
387
                            float invR = RSQRT(r2);
                            float r = RECIP(invR);
388
                            float bornRadius2 = localData[tbx+j].bornRadius;
389
                            float alpha2_ij = bornRadius1*bornRadius2;
390
                            float D_ij = r2*RECIP(4.0f*alpha2_ij);
391
                            float expTerm = EXP(-D_ij);
392
                            float denominator2 = r2 + alpha2_ij*expTerm;
393
                            float denominator = SQRT(denominator2);
394
395
                            float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                            float Gpol = tempEnergy*RECIP(denominator2);
396
397
398
399
                            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
400
                            if (atom1 >= NUM_ATOMS || y*TILE_SIZE+j >= NUM_ATOMS || r2 > CUTOFF_SQUARED) {
401
#else
402
                            if (atom1 >= NUM_ATOMS || y*TILE_SIZE+j >= NUM_ATOMS) {
403
404
405
406
407
408
409
#endif
                                dEdR = 0.0f;
				tempEnergy = 0.0f;
                            }
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
                            force.xyz -= delta.xyz;
Peter Eastman's avatar
Peter Eastman committed
410
                            tempBuffer[get_local_id(0)] = (float4) (delta.xyz, dGpol_dalpha2_ij*bornRadius1);
411
412
413
414
415
416
417
418
419
420
421

                            // 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];
422
423
424
425
426
427
428
                            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;
                            }
429
430
431
432
433
434
435
436
437
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

438
439
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
440
                    if (atom1 < NUM_ATOMS && y*TILE_SIZE+tj < NUM_ATOMS) {
441
                        float4 posq2 = (float4) (localData[tbx+tj].x, localData[tbx+tj].y, localData[tbx+tj].z, localData[tbx+tj].q);
442
                        float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
443
#ifdef USE_PERIODIC
444
445
446
                        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;
447
448
#endif
                        float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
449
450
                        float invR = RSQRT(r2);
                        float r = RECIP(invR);
451
                        float bornRadius2 = localData[tbx+tj].bornRadius;
452
                        float alpha2_ij = bornRadius1*bornRadius2;
453
                        float D_ij = r2*RECIP(4.0f*alpha2_ij);
454
                        float expTerm = EXP(-D_ij);
455
                        float denominator2 = r2 + alpha2_ij*expTerm;
456
                        float denominator = SQRT(denominator2);
457
458
                        float tempEnergy = (PREFACTOR*posq1.w*posq2.w)*RECIP(denominator);
                        float Gpol = tempEnergy*RECIP(denominator2);
459
460
461
462
                        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
463
                        if (r2 > CUTOFF_SQUARED) {
464
465
466
467
468
469
470
                            dEdR = 0.0f;
                            tempEnergy  = 0.0f;
                        }
#endif
                        energy += tempEnergy;
                        delta.xyz *= dEdR;
                        force.xyz -= delta.xyz;
471
472
473
474
                        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;
475
                    }
476
                    tj = (tj + 1) & (TILE_SIZE - 1);
477
478
479
480
481
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
482
483
            unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
484
#else
485
486
            unsigned int offset1 = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
487
488
#endif
            forceBuffers[offset1].xyz += force.xyz;
489
            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
490
            global_bornForce[offset1] += force.w;
491
            global_bornForce[offset2] += localData[get_local_id(0)].fw;
492
493
494
495
496
497
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}