nonbonded_nvidia.cl 11.5 KB
Newer Older
1
#define TILE_SIZE 32
2

3
typedef struct {
4
5
6
    float x, y, z;
    float q;
    float fx, fy, fz;
7
8
9
    ATOM_PARAMETER_DATA
} AtomData;

10
11
12
13
/**
 * Compute nonbonded interactions.
 */

14
15
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
void computeNonbonded(__global float4* forceBuffers, __global float* energyBuffer, __global float4* posq, __global unsigned int* exclusions,
16
        __global unsigned int* exclusionIndices, __global unsigned int* exclusionRowIndices, __local AtomData* localData, __local float4* tempBuffer,
17
#ifdef USE_CUTOFF
18
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, __global unsigned int* interactionFlags
19
20
#else
        unsigned int numTiles
21
#endif
22
        PARAMETER_ARGUMENTS) {
23
24
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
25
26
27
28
29
#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
30
31
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
32
#endif
33
34
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
35
36
    __local unsigned int exclusionRange[4];
    __local int exclusionIndex[2];
37
38

    while (pos < end) {
39
        // Extract the coordinates of this tile
40
        unsigned int x, y;
41
#ifdef USE_CUTOFF
42
43
44
45
        if (numTiles <= MAX_TILES) {
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
46
        }
47
        else
48
#endif
49
50
51
52
53
54
55
56
        {
            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);
            }
        }
57
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
58
        unsigned int tbx = get_local_id(0) - tgx;
59
        unsigned int atom1 = x*TILE_SIZE + tgx;
60
        float4 force = 0.0f;
61
        float4 posq1 = posq[atom1];
62
        LOAD_ATOM1_PARAMETERS
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

        // Locate the exclusion data for this tile.

#ifdef USE_EXCLUSIONS
        int localGroupIndex = get_local_id(0)/TILE_SIZE;
        if (tgx < 2)
            exclusionRange[2*localGroupIndex+tgx] = exclusionRowIndices[x+tgx];
        if (tgx == 0)
            exclusionIndex[localGroupIndex] = -1;
        for (int i = exclusionRange[2*localGroupIndex]+tgx; i < exclusionRange[2*localGroupIndex+1]; i += TILE_SIZE)
            if (exclusionIndices[i] == y)
                exclusionIndex[localGroupIndex] = i*TILE_SIZE;
        bool hasExclusions = (exclusionIndex[localGroupIndex] > -1);
#else
        bool hasExclusions = false;
#endif
79
        if (x == y) {
80
            // This tile is on the diagonal.
81

82
83
84
85
            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;
86
87
            LOAD_LOCAL_PARAMETERS_FROM_1
#ifdef USE_EXCLUSIONS
88
            unsigned int excl = exclusions[exclusionIndex[localGroupIndex]+tgx];
89
#endif
90
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
91
#ifdef USE_EXCLUSIONS
92
                bool isExcluded = !(excl & 0x1);
93
#endif
94
95
                int atom2 = tbx+j;
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
96
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
97
#ifdef USE_PERIODIC
98
99
100
                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;
101
#endif
102
103
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                float r = sqrt(r2);
Peter Eastman's avatar
Peter Eastman committed
104
                float invR = RECIP(r);
105
                LOAD_ATOM2_PARAMETERS
106
                atom2 = y*TILE_SIZE+j;
107
#ifdef USE_SYMMETRIC
108
                float dEdR = 0.0f;
109
110
111
112
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
113
114
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
115
                energy += 0.5f*tempEnergy;
116
117
118
119
120
#ifdef USE_SYMMETRIC
                force.xyz -= delta.xyz*dEdR;
#else
                force.xyz -= dEdR1.xyz;
#endif
121
                excl >>= 1;
122
123
124
            }

            // Write results
125
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
126
            unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
127
#else
128
            unsigned int offset = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
129
#endif
130
            forceBuffers[offset].xyz += force.xyz;
131
132
        }
        else {
133
134
            // This is an off-diagonal tile.

135
            if (lasty != y) {
136
                unsigned int j = y*TILE_SIZE + tgx;
137
                float4 tempPosq = posq[j];
138
139
140
141
                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;
142
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
143
            }
144
145
146
            localData[get_local_id(0)].fx = 0.0f;
            localData[get_local_id(0)].fy = 0.0f;
            localData[get_local_id(0)].fz = 0.0f;
147
#ifdef USE_CUTOFF
148
            unsigned int flags = (numTiles <= MAX_TILES ? interactionFlags[pos] : 0xFFFFFFFF);
149
            if (!hasExclusions && flags != 0xFFFFFFFF) {
150
151
152
153
154
155
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

156
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
157
158
                        if ((flags&(1<<j)) != 0) {
                            bool isExcluded = false;
159
160
                            int atom2 = tbx+j;
                            float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
161
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
162
#ifdef USE_PERIODIC
163
164
165
                            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;
166
#endif
167
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
168
169
                            float invR = RSQRT(r2);
                            float r = RECIP(invR);
170
                            LOAD_ATOM2_PARAMETERS
171
                            atom2 = y*TILE_SIZE+j;
172
#ifdef USE_SYMMETRIC
173
                            float dEdR = 0.0f;
174
175
176
177
#else
                            float4 dEdR1 = (float4) 0.0f;
                            float4 dEdR2 = (float4) 0.0f;
#endif
178
179
                            float tempEnergy = 0.0f;
                            COMPUTE_INTERACTION
180
			    energy += tempEnergy;
181
#ifdef USE_SYMMETRIC
182
                            delta.xyz *= dEdR;
183
                            force.xyz -= delta.xyz;
184
                            tempBuffer[get_local_id(0)] = delta;
185
186
187
188
#else
                            force.xyz -= dEdR1.xyz;
                            tempBuffer[get_local_id(0)] = dEdR2;
#endif
189

190
                            // Sum the forces on atom2.
191
192
193
194
195
196
197
198
199

                            if (tgx % 2 == 0)
                                tempBuffer[get_local_id(0)].xyz += tempBuffer[get_local_id(0)+1].xyz;
                            if (tgx % 4 == 0)
                                tempBuffer[get_local_id(0)].xyz += tempBuffer[get_local_id(0)+2].xyz;
                            if (tgx % 8 == 0)
                                tempBuffer[get_local_id(0)].xyz += tempBuffer[get_local_id(0)+4].xyz;
                            if (tgx % 16 == 0)
                                tempBuffer[get_local_id(0)].xyz += tempBuffer[get_local_id(0)+8].xyz;
200
                            if (tgx == 0) {
201
202
203
                                localData[tbx+j].fx += tempBuffer[get_local_id(0)].x + tempBuffer[get_local_id(0)+16].x;
                                localData[tbx+j].fy += tempBuffer[get_local_id(0)].y + tempBuffer[get_local_id(0)+16].y;
                                localData[tbx+j].fz += tempBuffer[get_local_id(0)].z + tempBuffer[get_local_id(0)+16].z;
204
                            }
205
206
207
208
                        }
                    }
                }
            }
209
            else
210
211
#endif
            {
212
213
                // Compute the full set of interactions in this tile.

214
#ifdef USE_EXCLUSIONS
215
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[localGroupIndex]+tgx] : 0xFFFFFFFF);
216
                excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
217
#endif
218
                unsigned int tj = tgx;
219
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
220
#ifdef USE_EXCLUSIONS
221
                    bool isExcluded = !(excl & 0x1);
222
#endif
223
224
                    int atom2 = tbx+tj;
                    float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
225
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
226
#ifdef USE_PERIODIC
227
228
229
                    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;
230
#endif
231
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
232
233
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
234
                    LOAD_ATOM2_PARAMETERS
235
                    atom2 = y*TILE_SIZE+tj;
236
#ifdef USE_SYMMETRIC
237
                    float dEdR = 0.0f;
238
239
240
241
#else
                    float4 dEdR1 = (float4) 0.0f;
                    float4 dEdR2 = (float4) 0.0f;
#endif
242
243
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
244
		    energy += tempEnergy;
245
#ifdef USE_SYMMETRIC
246
                    delta.xyz *= dEdR;
247
                    force.xyz -= delta.xyz;
248
249
250
                    localData[tbx+tj].fx += delta.x;
                    localData[tbx+tj].fy += delta.y;
                    localData[tbx+tj].fz += delta.z;
251
252
253
254
255
256
#else
                    force.xyz -= dEdR1.xyz;
                    localData[tbx+tj].fx += dEdR2.x;
                    localData[tbx+tj].fy += dEdR2.y;
                    localData[tbx+tj].fz += dEdR2.z;
#endif
257
                    excl >>= 1;
258
                    tj = (tj + 1) & (TILE_SIZE - 1);
259
260
261
262
                }
            }

            // Write results
263
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
264
265
            unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
266
#else
267
268
            unsigned int offset1 = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
269
#endif
270
            forceBuffers[offset1].xyz += force.xyz;
271
            forceBuffers[offset2] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0.0f);
272
        }
273
        lasty = y;
274
275
276
277
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}