nonbonded_nvidia.cl 11.1 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, __global unsigned int* tiles,
17
#ifdef USE_CUTOFF
18
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize
19
20
#else
        unsigned int numTiles
21
#endif
22
        PARAMETER_ARGUMENTS) {
23
24
25
#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
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
32
33
    __local unsigned int exclusionRange[4];
    __local int exclusionIndex[2];
34
35

    while (pos < end) {
36
        // Extract the coordinates of this tile
37
#ifdef USE_CUTOFF
38
        unsigned int x = tiles[pos];
39
40
41
42
43
44
45
46
47
48
        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
49
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
50
        unsigned int tbx = get_local_id(0) - tgx;
51
        unsigned int atom1 = x*TILE_SIZE + tgx;
52
        float4 force = 0.0f;
53
        float4 posq1 = posq[atom1];
54
        LOAD_ATOM1_PARAMETERS
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

        // 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
71
        if (x == y) {
72
            // This tile is on the diagonal.
73

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

            // Write results
117
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
118
            unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
119
#else
120
            unsigned int offset = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
121
#endif
122
            forceBuffers[offset].xyz += force.xyz;
123
124
        }
        else {
125
126
            // This is an off-diagonal tile.

127
            if (lasty != y) {
128
                unsigned int j = y*TILE_SIZE + tgx;
129
                float4 tempPosq = posq[j];
130
131
132
133
                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;
134
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
135
            }
136
137
138
            localData[get_local_id(0)].fx = 0.0f;
            localData[get_local_id(0)].fy = 0.0f;
            localData[get_local_id(0)].fz = 0.0f;
139
#ifdef USE_CUTOFF
140
            unsigned int flags = interactionFlags[pos];
141
            if (!hasExclusions && flags != 0xFFFFFFFF) {
142
143
144
145
146
147
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

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

182
                            // Sum the forces on atom2.
183
184
185
186
187
188
189
190
191

                            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;
192
                            if (tgx == 0) {
193
194
195
                                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;
196
                            }
197
198
199
200
                        }
                    }
                }
            }
201
            else
202
203
#endif
            {
204
205
                // Compute the full set of interactions in this tile.

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

            // Write results
255
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
256
257
            unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
258
#else
259
260
            unsigned int offset1 = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
261
#endif
262
            forceBuffers[offset1].xyz += force.xyz;
263
            forceBuffers[offset2] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0.0f);
264
265
266
267
268
269
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}