gbsaObc_nvidia.cl 23.6 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 void computeBornSum(__global float* global_bornSum, __global float4* posq, __global float2* global_params, __local AtomData* localData, __local float* tempBuffer, __global unsigned int* tiles,
18
#ifdef USE_CUTOFF
19
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount) {
20
21
22
23
24
25
#else
        unsigned int numTiles) {
#endif
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
26
27
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
28
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
        unsigned int x = tiles[pos];
36
37
38
        unsigned int y = ((x >> 2) & 0x7fff)*TILE_SIZE;
        x = (x>>17)*TILE_SIZE;
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
39
        unsigned int tbx = get_local_id(0) - tgx;
40
        unsigned int atom1 = x + tgx;
41
        float bornSum = 0.0f;
42
43
        float4 posq1 = posq[atom1];
        float2 params1 = global_params[atom1];
44
45
46
        if (x == y) {
            // This tile is on the diagonal.

47
48
49
50
51
52
            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;
53
54
55
            unsigned int xi = x/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-xi*(xi+1)/2;
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
56
                float4 delta = (float4) (localData[tbx+j].x-posq1.x, localData[tbx+j].y-posq1.y, localData[tbx+j].z-posq1.z, 0.0f);
57
#ifdef USE_PERIODIC
58
59
60
                delta.x -= floor(delta.x*INV_PERIODIC_BOX_SIZE_X+0.5f)*PERIODIC_BOX_SIZE_X;
                delta.y -= floor(delta.y*INV_PERIODIC_BOX_SIZE_Y+0.5f)*PERIODIC_BOX_SIZE_Y;
                delta.z -= floor(delta.z*INV_PERIODIC_BOX_SIZE_Z+0.5f)*PERIODIC_BOX_SIZE_Z;
61
62
63
#endif
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
64
                if (atom1 < NUM_ATOMS && y+j < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
65
#else
66
                if (atom1 < NUM_ATOMS && y+j < NUM_ATOMS) {
67
#endif
68
69
                    float invR = native_rsqrt(r2);
                    float r = native_recip(invR);
70
                    float2 params2 = (float2) (localData[tbx+j].radius, localData[tbx+j].scaledRadius);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
                    float rScaledRadiusJ = r+params2.y;
                    if ((j != tgx) && (params1.x < rScaledRadiusJ)) {
                        float l_ij = 1.0f/max(params1.x, fabs(r-params2.y));
                        float u_ij = 1.0f/rScaledRadiusJ;
                        float l_ij2 = l_ij*l_ij;
                        float u_ij2 = u_ij*u_ij;
                        float ratio = log(u_ij / l_ij);
                        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)
                            bornSum += 2.0f*(1.0f/params1.x-l_ij);
                    }
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
88
            unsigned int offset = x + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
89
#else
90
            unsigned int offset = x + tgx + warp*PADDED_NUM_ATOMS;
91
92
93
94
95
96
97
98
#endif
            global_bornSum[offset] += bornSum;
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
                unsigned int j = y + tgx;
99
100
101
102
103
104
105
106
                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;
107
            }
108
            localData[get_local_id(0)].bornSum = 0.0f;
109
110
111
112
113
114
115
116
117
#ifdef USE_CUTOFF
            unsigned int flags = interactionFlags[pos];
            if (flags != 0xFFFFFFFF) {
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

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

184
185
186
187
188
                unsigned int xi = x/TILE_SIZE;
                unsigned int yi = y/TILE_SIZE;
                unsigned int tile = xi+yi*PADDED_NUM_ATOMS/TILE_SIZE-yi*(yi+1)/2;
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
189
                    float4 delta = (float4) (localData[tbx+tj].x-posq1.x, localData[tbx+tj].y-posq1.y, localData[tbx+tj].z-posq1.z, 0.0f);
190
#ifdef USE_PERIODIC
191
192
193
                    delta.x -= floor(delta.x*INV_PERIODIC_BOX_SIZE_X+0.5f)*PERIODIC_BOX_SIZE_X;
                    delta.y -= floor(delta.y*INV_PERIODIC_BOX_SIZE_Y+0.5f)*PERIODIC_BOX_SIZE_Y;
                    delta.z -= floor(delta.z*INV_PERIODIC_BOX_SIZE_Z+0.5f)*PERIODIC_BOX_SIZE_Z;
194
195
196
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
197
                    if (atom1 < NUM_ATOMS && y+tj < NUM_ATOMS && r2 < CUTOFF_SQUARED) {
198
#else
199
                    if (atom1 < NUM_ATOMS && y+tj < NUM_ATOMS) {
200
#endif
201
202
                        float invR = native_rsqrt(r2);
                        float r = native_recip(invR);
203
                        float2 params2 = (float2) (localData[tbx+tj].radius, localData[tbx+tj].scaledRadius);
204
                        float rScaledRadiusJ = r+params2.y;
Peter Eastman's avatar
Peter Eastman committed
205
                        if (params1.x < rScaledRadiusJ) {
206
207
208
209
210
211
212
213
214
215
216
                            float l_ij = 1.0f/max(params1.x, fabs(r-params2.y));
                            float u_ij = 1.0f/rScaledRadiusJ;
                            float l_ij2 = l_ij*l_ij;
                            float u_ij2 = u_ij*u_ij;
                            float ratio = log(u_ij / l_ij);
                            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)
                                bornSum += 2.0f*(1.0f/params1.x-l_ij);
                        }
                        float rScaledRadiusI = r+params1.y;
Peter Eastman's avatar
Peter Eastman committed
217
                        if (params2.x < rScaledRadiusI) {
218
                            float l_ij = 1.0f/max(params2.x, fabs(r-params1.y));
Peter Eastman's avatar
Peter Eastman committed
219
                            float u_ij = 1.0f/rScaledRadiusI;
220
221
222
223
224
225
226
                            float l_ij2 = l_ij*l_ij;
                            float u_ij2 = u_ij*u_ij;
                            float ratio = log(u_ij / l_ij);
                            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)
                                term += 2.0f*(1.0f/params2.x-l_ij);
227
                            localData[tbx+tj].bornSum += term;
228
229
                        }
                    }
230
                    tj = (tj + 1) & (TILE_SIZE - 1);
231
232
233
234
235
236
                }
            }

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

251
252
253
254
/**
 * First part of computing the GBSA interaction.
 */

255
__kernel void computeGBSAForce1(__global float4* forceBuffers, __global float* energyBuffer,
256
257
        __global float4* posq, __global float* global_bornRadii,
        __global float* global_bornForce, __local AtomData* localData, __local float4* tempBuffer, __global unsigned int* tiles,
258
#ifdef USE_CUTOFF
259
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount) {
260
261
262
263
264
265
#else
        unsigned int numTiles) {
#endif
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
266
267
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
268
269
270
271
272
273
274
275
    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
        unsigned int x = tiles[pos];
276
277
278
        unsigned int y = ((x >> 2) & 0x7fff)*TILE_SIZE;
        x = (x>>17)*TILE_SIZE;
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
279
280
281
282
283
284
285
286
        unsigned int tbx = get_local_id(0) - tgx;
        unsigned int atom1 = x + tgx;
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        float bornRadius1 = global_bornRadii[atom1];
        if (x == y) {
            // This tile is on the diagonal.

287
288
289
290
291
            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;
292
293
294
            unsigned int xi = x/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-xi*(xi+1)/2;
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
295
                if (atom1 < NUM_ATOMS && y+j < NUM_ATOMS) {
296
                    float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
297
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
298
#ifdef USE_PERIODIC
299
300
301
                    delta.x -= floor(delta.x*INV_PERIODIC_BOX_SIZE_X+0.5f)*PERIODIC_BOX_SIZE_X;
                    delta.y -= floor(delta.y*INV_PERIODIC_BOX_SIZE_Y+0.5f)*PERIODIC_BOX_SIZE_Y;
                    delta.z -= floor(delta.z*INV_PERIODIC_BOX_SIZE_Z+0.5f)*PERIODIC_BOX_SIZE_Z;
302
303
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
304
305
                    float invR = native_rsqrt(r2);
                    float r = native_recip(invR);
306
                    float bornRadius2 = localData[tbx+j].bornRadius;
307
308
309
310
                    float alpha2_ij = bornRadius1*bornRadius2;
                    float D_ij = r2/(4.0f*alpha2_ij);
                    float expTerm = exp(-D_ij);
                    float denominator2 = r2 + alpha2_ij*expTerm;
311
                    float denominator = native_sqrt(denominator2);
312
                    float tempEnergy = (PREFACTOR*posq1.w*posq2.w)/denominator;
313
314
315
316
317
                    float Gpol = tempEnergy/denominator2;
                    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
318
                    if (r2 > CUTOFF_SQUARED) {
319
320
321
322
323
324
325
326
327
328
329
330
                        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
331
            unsigned int offset = x + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
332
#else
333
            unsigned int offset = x + tgx + warp*PADDED_NUM_ATOMS;
334
335
#endif
            forceBuffers[offset].xyz += force.xyz;
Peter Eastman's avatar
Peter Eastman committed
336
            global_bornForce[offset] += force.w;
337
338
339
340
341
342
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
                unsigned int j = y + tgx;
343
344
345
346
347
348
                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];
349
            }
350
351
352
353
            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;
354
355
356
357
358
359
360
361
362
#ifdef USE_CUTOFF
            unsigned int flags = interactionFlags[pos];
            if (flags != 0xFFFFFFFF) {
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

363
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
364
                        if ((flags&(1<<j)) != 0) {
365
                            float4 posq2 = (float4) (localData[tbx+j].x, localData[tbx+j].y, localData[tbx+j].z, localData[tbx+j].q);
366
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
367
#ifdef USE_PERIODIC
368
369
370
                            delta.x -= floor(delta.x*INV_PERIODIC_BOX_SIZE_X+0.5f)*PERIODIC_BOX_SIZE_X;
                            delta.y -= floor(delta.y*INV_PERIODIC_BOX_SIZE_Y+0.5f)*PERIODIC_BOX_SIZE_Y;
                            delta.z -= floor(delta.z*INV_PERIODIC_BOX_SIZE_Z+0.5f)*PERIODIC_BOX_SIZE_Z;
371
372
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
373
374
                            float invR = native_rsqrt(r2);
                            float r = native_recip(invR);
375
                            float bornRadius2 = localData[tbx+j].bornRadius;
376
377
378
379
                            float alpha2_ij = bornRadius1*bornRadius2;
                            float D_ij = r2/(4.0f*alpha2_ij);
                            float expTerm = exp(-D_ij);
                            float denominator2 = r2 + alpha2_ij*expTerm;
380
                            float denominator = native_sqrt(denominator2);
381
                            float tempEnergy = (PREFACTOR*posq1.w*posq2.w)/denominator;
382
383
384
385
386
                            float Gpol = tempEnergy/denominator2;
                            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
387
                            if (atom1 >= NUM_ATOMS || y+j >= NUM_ATOMS || r2 > CUTOFF_SQUARED) {
388
#else
389
                            if (atom1 >= NUM_ATOMS || y+j >= NUM_ATOMS) {
390
391
392
393
394
395
396
#endif
                                dEdR = 0.0f;
				tempEnergy = 0.0f;
                            }
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
                            force.xyz -= delta.xyz;
Peter Eastman's avatar
Peter Eastman committed
397
                            tempBuffer[get_local_id(0)] = (float4) (delta.xyz, dGpol_dalpha2_ij*bornRadius1);
398
399
400
401
402
403
404
405
406
407
408

                            // 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];
409
410
411
412
413
414
415
                            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;
                            }
416
417
418
419
420
421
422
423
424
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

425
426
427
428
429
                unsigned int xi = x/TILE_SIZE;
                unsigned int yi = y/TILE_SIZE;
                unsigned int tile = xi+yi*PADDED_NUM_ATOMS/TILE_SIZE-yi*(yi+1)/2;
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
430
                    if (atom1 < NUM_ATOMS && y+tj < NUM_ATOMS) {
431
                        float4 posq2 = (float4) (localData[tbx+tj].x, localData[tbx+tj].y, localData[tbx+tj].z, localData[tbx+tj].q);
432
                        float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
433
#ifdef USE_PERIODIC
434
435
436
                        delta.x -= floor(delta.x*INV_PERIODIC_BOX_SIZE_X+0.5f)*PERIODIC_BOX_SIZE_X;
                        delta.y -= floor(delta.y*INV_PERIODIC_BOX_SIZE_Y+0.5f)*PERIODIC_BOX_SIZE_Y;
                        delta.z -= floor(delta.z*INV_PERIODIC_BOX_SIZE_Z+0.5f)*PERIODIC_BOX_SIZE_Z;
437
438
#endif
                        float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
439
440
                        float invR = native_rsqrt(r2);
                        float r = native_recip(invR);
441
                        float bornRadius2 = localData[tbx+tj].bornRadius;
442
443
444
445
                        float alpha2_ij = bornRadius1*bornRadius2;
                        float D_ij = r2/(4.0f*alpha2_ij);
                        float expTerm = exp(-D_ij);
                        float denominator2 = r2 + alpha2_ij*expTerm;
446
                        float denominator = native_sqrt(denominator2);
447
                        float tempEnergy = (PREFACTOR*posq1.w*posq2.w)/denominator;
448
449
450
451
452
                        float Gpol = tempEnergy/denominator2;
                        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
453
                        if (r2 > CUTOFF_SQUARED) {
454
455
456
457
458
459
460
                            dEdR = 0.0f;
                            tempEnergy  = 0.0f;
                        }
#endif
                        energy += tempEnergy;
                        delta.xyz *= dEdR;
                        force.xyz -= delta.xyz;
461
462
463
464
                        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;
465
                    }
466
                    tj = (tj + 1) & (TILE_SIZE - 1);
467
468
469
470
471
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
472
473
            unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
474
#else
475
476
            unsigned int offset1 = x + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + warp*PADDED_NUM_ATOMS;
477
478
#endif
            forceBuffers[offset1].xyz += force.xyz;
479
            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
480
            global_bornForce[offset1] += force.w;
481
            global_bornForce[offset2] += localData[get_local_id(0)].fw;
482
483
484
485
486
487
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}