nonbonded_cpu.cl 11.2 KB
Newer Older
1
2
3
#define TILE_SIZE 32

typedef struct {
4
5
6
    real x, y, z;
    real q;
    real fx, fy, fz;
7
8
9
10
11
12
13
    ATOM_PARAMETER_DATA
} AtomData;

/**
 * Compute nonbonded interactions.
 */

14
__kernel void computeNonbonded(__global real4* restrict forceBuffers, __global real* restrict energyBuffer, __global const real4* restrict posq, __global const unsigned int* restrict exclusions,
15
        __global const unsigned int* restrict exclusionIndices, __global const unsigned int* restrict exclusionRowIndices,
16
        unsigned int startTileIndex, unsigned int endTileIndex,
17
#ifdef USE_CUTOFF
18
        __global const ushort2* restrict tiles, __global const unsigned int* restrict interactionCount, real4 periodicBoxSize, real4 invPeriodicBoxSize, unsigned int maxTiles, __global const unsigned int* restrict interactionFlags
19
20
21
22
23
24
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
25
26
    unsigned int pos = (numTiles > maxTiles ? startTileIndex+get_group_id(0)*(endTileIndex-startTileIndex)/get_num_groups(0) : get_group_id(0)*numTiles/get_num_groups(0));
    unsigned int end = (numTiles > maxTiles ? startTileIndex+(get_group_id(0)+1)*(endTileIndex-startTileIndex)/get_num_groups(0) : (get_group_id(0)+1)*numTiles/get_num_groups(0));
27
#else
28
29
    unsigned int pos = startTileIndex+get_group_id(0)*numTiles/get_num_groups(0);
    unsigned int end = startTileIndex+(get_group_id(0)+1)*numTiles/get_num_groups(0);
30
#endif
31
    real energy = 0;
32
    unsigned int lasty = 0xFFFFFFFF;
33
    __local AtomData localData[TILE_SIZE];
34
35
36
37
38
39
40
41
42
43
44
45
46

    while (pos < end) {
        // Extract the coordinates of this tile
        unsigned int x, y;
#ifdef USE_CUTOFF
        if (numTiles <= maxTiles) {
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
        }
        else
#endif
        {
Peter Eastman's avatar
Peter Eastman committed
47
            y = (unsigned int) floor(NUM_BLOCKS+0.5f-SQRT((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
48
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
49
50
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }

        // Locate the exclusion data for this tile.

#ifdef USE_EXCLUSIONS
        unsigned int exclusionStart = exclusionRowIndices[x];
        unsigned int exclusionEnd = exclusionRowIndices[x+1];
        int exclusionIndex = -1;
        for (int i = exclusionStart; i < exclusionEnd; i++)
            if (exclusionIndices[i] == y) {
                exclusionIndex = i*TILE_SIZE;
                break;
            }
        bool hasExclusions = (exclusionIndex > -1);
#endif

        // Load the data for this tile if we don't already have it cached.

        if (lasty != y) {
            for (int localAtomIndex = 0; localAtomIndex < TILE_SIZE; localAtomIndex++) {
                unsigned int j = y*TILE_SIZE + localAtomIndex;
74
                real4 tempPosq = posq[j];
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
                localData[localAtomIndex].x = tempPosq.x;
                localData[localAtomIndex].y = tempPosq.y;
                localData[localAtomIndex].z = tempPosq.z;
                localData[localAtomIndex].q = tempPosq.w;
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
        }
        if (x == y) {
            // This tile is on the diagonal.

            for (unsigned int tgx = 0; tgx < TILE_SIZE; tgx++) {
#ifdef USE_EXCLUSIONS
                unsigned int excl = exclusions[exclusionIndex+tgx];
#endif
                unsigned int atom1 = x*TILE_SIZE+tgx;
90
91
                real4 force = 0;
                real4 posq1 = posq[atom1];
92
93
94
95
96
                LOAD_ATOM1_PARAMETERS
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
#ifdef USE_EXCLUSIONS
                    bool isExcluded = !(excl & 0x1);
#endif
97
98
                    real4 posq2 = (real4) (localData[j].x, localData[j].y, localData[j].z, localData[j].q);
                    real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
99
#ifdef USE_PERIODIC
100
                    delta.xyz -= floor(delta.xyz*invPeriodicBoxSize.xyz+0.5f)*periodicBoxSize.xyz;
101
#endif
102
                    real r2 = dot(delta.xyz, delta.xyz);
103
104
105
#ifdef USE_CUTOFF
                    if (r2 < CUTOFF_SQUARED) {
#endif
106
107
                    real invR = RSQRT(r2);
                    real r = RECIP(invR);
108
109
110
111
                    unsigned int atom2 = j;
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y*TILE_SIZE+j;
#ifdef USE_SYMMETRIC
112
                    real dEdR = 0;
113
#else
114
115
                    real4 dEdR1 = (real4) 0;
                    real4 dEdR2 = (real4) 0;
116
#endif
117
                    real tempEnergy = 0;
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
                    COMPUTE_INTERACTION
                    energy += 0.5f*tempEnergy;
#ifdef USE_SYMMETRIC
                    force.xyz -= delta.xyz*dEdR;
#else
                    force.xyz -= dEdR1.xyz;
#endif
#ifdef USE_CUTOFF
                    }
#endif
                    excl >>= 1;
                }

                // Write results.

                unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                forceBuffers[offset].xyz = forceBuffers[offset].xyz+force.xyz;
            }
        }
        else {
            // This is an off-diagonal tile.

            for (int tgx = 0; tgx < TILE_SIZE; tgx++) {
141
142
143
                localData[tgx].fx = 0;
                localData[tgx].fy = 0;
                localData[tgx].fz = 0;
144
145
            }
#ifdef USE_CUTOFF
146
147
148
149
            unsigned int flags1 = (numTiles <= maxTiles ? interactionFlags[2*pos] : 0xFFFFFFFF);
            unsigned int flags2 = (numTiles <= maxTiles ? interactionFlags[2*pos+1] : 0xFFFFFFFF);
            if (!hasExclusions && (flags1 != 0xFFFFFFFF || flags2 != 0xFFFFFFFF)) {
                // Compute only a subset of the interactions in this tile.
150

151
152
                for (unsigned int tgx = 0; tgx < TILE_SIZE; tgx++) {
                    if ((flags2&(1<<tgx)) != 0) {
153
                        unsigned int atom1 = x*TILE_SIZE+tgx;
154
155
                        real4 force = 0;
                        real4 posq1 = posq[atom1];
156
157
                        LOAD_ATOM1_PARAMETERS
                        for (unsigned int j = 0; j < TILE_SIZE; j++) {
158
                            if ((flags1&(1<<j)) != 0) {
159
                                bool isExcluded = false;
160
161
                                real4 posq2 = (real4) (localData[j].x, localData[j].y, localData[j].z, localData[j].q);
                                real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
162
#ifdef USE_PERIODIC
163
                                delta.xyz -= floor(delta.xyz*invPeriodicBoxSize.xyz+0.5f)*periodicBoxSize.xyz;
164
#endif
165
                                real r2 = dot(delta.xyz, delta.xyz);
166
                                if (r2 < CUTOFF_SQUARED) {
167
168
                                    real invR = RSQRT(r2);
                                    real r = RECIP(invR);
169
170
171
                                    unsigned int atom2 = j;
                                    LOAD_ATOM2_PARAMETERS
                                    atom2 = y*TILE_SIZE+j;
172
#ifdef USE_SYMMETRIC
173
                                    real dEdR = 0;
174
#else
175
176
                                    real4 dEdR1 = (real4) 0;
                                    real4 dEdR2 = (real4) 0;
177
#endif
178
                                    real tempEnergy = 0;
179
180
                                    COMPUTE_INTERACTION
                                    energy += tempEnergy;
181
#ifdef USE_SYMMETRIC
182
183
184
185
186
                                    delta.xyz *= dEdR;
                                    force.xyz -= delta.xyz;
                                    localData[j].fx += delta.x;
                                    localData[j].fy += delta.y;
                                    localData[j].fz += delta.z;
187
#else
188
189
190
191
                                    force.xyz -= dEdR1.xyz;
                                    localData[j].fx += dEdR2.x;
                                    localData[j].fy += dEdR2.y;
                                    localData[j].fz += dEdR2.z;
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#endif
                                }
                            }
                        }

                        // Write results for atom1.

                        unsigned int offset = atom1 + get_group_id(0)*PADDED_NUM_ATOMS;
                        forceBuffers[offset].xyz = forceBuffers[offset].xyz+force.xyz;
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

                for (unsigned int tgx = 0; tgx < TILE_SIZE; tgx++) {
                    unsigned int atom1 = x*TILE_SIZE+tgx;
211
212
                    real4 force = 0;
                    real4 posq1 = posq[atom1];
213
214
215
216
217
218
219
220
                    LOAD_ATOM1_PARAMETERS
#ifdef USE_EXCLUSIONS
                    unsigned int excl = (hasExclusions ? exclusions[exclusionIndex+tgx] : 0xFFFFFFFF);
#endif
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
#ifdef USE_EXCLUSIONS
                        bool isExcluded = !(excl & 0x1);
#endif
221
222
                        real4 posq2 = (real4) (localData[j].x, localData[j].y, localData[j].z, localData[j].q);
                        real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
223
#ifdef USE_PERIODIC
224
                        delta.xyz -= floor(delta.xyz*invPeriodicBoxSize.xyz+0.5f)*periodicBoxSize.xyz;
225
#endif
226
                        real r2 = dot(delta.xyz, delta.xyz);
227
228
229
#ifdef USE_CUTOFF
                        if (r2 < CUTOFF_SQUARED) {
#endif
230
231
                        real invR = RSQRT(r2);
                        real r = RECIP(invR);
232
233
234
235
                        unsigned int atom2 = j;
                        LOAD_ATOM2_PARAMETERS
                        atom2 = y*TILE_SIZE+j;
#ifdef USE_SYMMETRIC
236
                        real dEdR = 0;
237
#else
238
239
                        real4 dEdR1 = (real4) 0;
                        real4 dEdR2 = (real4) 0;
240
#endif
241
                        real tempEnergy = 0;
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
                        COMPUTE_INTERACTION
                        energy += tempEnergy;
#ifdef USE_SYMMETRIC
                        delta.xyz *= dEdR;
                        force.xyz -= delta.xyz;
                        localData[j].fx += delta.x;
                        localData[j].fy += delta.y;
                        localData[j].fz += delta.z;
#else
                        force.xyz -= dEdR1.xyz;
                        localData[j].fx += dEdR2.x;
                        localData[j].fy += dEdR2.y;
                        localData[j].fz += dEdR2.z;
#endif
#ifdef USE_CUTOFF
                        }
#endif
#ifdef USE_EXCLUSIONS
                        excl >>= 1;
#endif
                    }

                   // Write results for atom1.

                    unsigned int offset = atom1 + get_group_id(0)*PADDED_NUM_ATOMS;
                    forceBuffers[offset].xyz = forceBuffers[offset].xyz+force.xyz;
                }
            }

            // Write results.

            for (int tgx = 0; tgx < TILE_SIZE; tgx++) {
                unsigned int offset = y*TILE_SIZE+tgx + get_group_id(0)*PADDED_NUM_ATOMS;
275
                real4 f = forceBuffers[offset];
276
277
278
279
280
281
282
283
284
285
286
                f.x += localData[tgx].fx;
                f.y += localData[tgx].fy;
                f.z += localData[tgx].fz;
                forceBuffers[offset] = f;
            }
        }
        lasty = y;
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}