nonbonded_default.cl 10.7 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
__kernel __attribute__((reqd_work_group_size(FORCE_WORK_GROUP_SIZE, 1, 1)))
15
void computeNonbonded(__global float4* restrict forceBuffers, __global float* restrict energyBuffer, __global const float4* restrict posq, __global const unsigned int* restrict exclusions,
16
        __global const unsigned int* restrict exclusionIndices, __global const unsigned int* restrict exclusionRowIndices, __local AtomData* restrict localData,
17
        unsigned int startTileIndex, unsigned int endTileIndex,
18
#ifdef USE_CUTOFF
19
        __global const ushort2* restrict tiles, __global const unsigned int* restrict interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global const unsigned int* restrict interactionFlags
20
21
22
23
24
25
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
26
27
    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));
28
#else
29
30
    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);
31
#endif
32
33
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
34
    __local float tempBuffer[3*(FORCE_WORK_GROUP_SIZE/2)];
35
36
    __local unsigned int exclusionRange[2];
    __local int exclusionIndex[1];
37
38
39

    while (pos < end) {
        // Extract the coordinates of this tile
40
        unsigned int x, y;
41
#ifdef USE_CUTOFF
42
        if (numTiles <= maxTiles) {
43
44
45
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
46
        }
47
        else
48
#endif
49
        {
Peter Eastman's avatar
Peter Eastman committed
50
            y = (unsigned int) floor(NUM_BLOCKS+0.5f-SQRT((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
51
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
52
53
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
54
55
56
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
57
58
59
        unsigned int baseLocalAtom = (get_local_id(0) < TILE_SIZE ? 0 : TILE_SIZE/2);
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
        unsigned int forceBufferOffset = (tgx < TILE_SIZE/2 ? 0 : TILE_SIZE);
60
        unsigned int atom1 = x*TILE_SIZE + tgx;
61
62
63
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        LOAD_ATOM1_PARAMETERS
64
65
66
67
68
69

        // Locate the exclusion data for this tile.

#ifdef USE_EXCLUSIONS
        if (get_local_id(0) < 2)
            exclusionRange[get_local_id(0)] = exclusionRowIndices[x+get_local_id(0)];
70
        if (get_local_id(0) == 0)
71
72
            exclusionIndex[0] = -1;
        barrier(CLK_LOCAL_MEM_FENCE);
73
        for (int i = exclusionRange[0]+get_local_id(0); i < exclusionRange[1]; i += TILE_SIZE)
74
75
76
77
78
            if (exclusionIndices[i] == y)
                exclusionIndex[0] = i*TILE_SIZE;
        barrier(CLK_LOCAL_MEM_FENCE);
        bool hasExclusions = (exclusionIndex[0] > -1);
#endif
79
80
81
        if (x == y) {
            // This tile is on the diagonal.

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

            // Sum the forces and write results.

128
129
130
131
132
133
            int bufferIndex = 3*tgx;
            if (get_local_id(0) >= TILE_SIZE) {
                tempBuffer[bufferIndex] = force.x;
                tempBuffer[bufferIndex+1] = force.y;
                tempBuffer[bufferIndex+2] = force.z;
            }
134
            barrier(CLK_LOCAL_MEM_FENCE);
135
            if (get_local_id(0) < TILE_SIZE) {
136
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
137
                unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
138
#else
139
                unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
140
#endif
141
                // Cheaper to load/store float4 than float3.
142
143
144
                float4 sum = forceBuffers[offset];
                sum += force + (float4) (tempBuffer[bufferIndex], tempBuffer[bufferIndex+1], tempBuffer[bufferIndex+2], 0.0f);
                forceBuffers[offset] = sum;
145
146
147
148
149
            }
        }
        else {
            // This is an off-diagonal tile.

150
151
            const unsigned int localAtomIndex = get_local_id(0);
            if (lasty != y && localAtomIndex < TILE_SIZE) {
152
                unsigned int j = y*TILE_SIZE + tgx;
153
                float4 tempPosq = posq[j];
154
155
156
157
                localData[localAtomIndex].x = tempPosq.x;
                localData[localAtomIndex].y = tempPosq.y;
                localData[localAtomIndex].z = tempPosq.z;
                localData[localAtomIndex].q = tempPosq.w;
158
159
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
160
161
162
            localData[localAtomIndex].fx = 0.0f;
            localData[localAtomIndex].fy = 0.0f;
            localData[localAtomIndex].fz = 0.0f;
163
164
            barrier(CLK_LOCAL_MEM_FENCE);

165
166
            // Compute the full set of interactions in this tile.

167
#ifdef USE_EXCLUSIONS
168
            unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[0]+tgx] : 0xFFFFFFFF);
169
170
            excl = (excl >> baseLocalAtom) & 0xFFFF;
            excl += excl << 16;
171
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
172
#endif
173
174
            unsigned int tj = tgx%(TILE_SIZE/2);
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
175
#ifdef USE_EXCLUSIONS
176
                bool isExcluded = !(excl & 0x1);
177
#endif
178
179
                int atom2 = baseLocalAtom+tj;
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
180
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
181
#ifdef USE_PERIODIC
182
183
184
                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;
185
#endif
186
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
187
188
                float invR = RSQRT(r2);
                float r = RECIP(invR);
189
                LOAD_ATOM2_PARAMETERS
190
                atom2 = y*TILE_SIZE+baseLocalAtom+tj;
191
#ifdef USE_SYMMETRIC
192
                float dEdR = 0.0f;
193
194
195
196
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
197
198
199
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += tempEnergy;
200
#ifdef USE_SYMMETRIC
201
202
                delta.xyz *= dEdR;
                force.xyz -= delta.xyz;
203
204
205
                localData[baseLocalAtom+tj+forceBufferOffset].fx += delta.x;
                localData[baseLocalAtom+tj+forceBufferOffset].fy += delta.y;
                localData[baseLocalAtom+tj+forceBufferOffset].fz += delta.z;
206
207
208
209
210
211
#else
                force.xyz -= dEdR1.xyz;
                localData[baseLocalAtom+tj+forceBufferOffset].fx += dEdR2.x;
                localData[baseLocalAtom+tj+forceBufferOffset].fy += dEdR2.y;
                localData[baseLocalAtom+tj+forceBufferOffset].fz += dEdR2.z;
#endif
212
                barrier(CLK_LOCAL_MEM_FENCE);
213
#ifdef USE_EXCLUSIONS
214
                excl >>= 1;
215
#endif
216
                tj = (tj+1)%(TILE_SIZE/2);
217
218
219
220
            }

            // Sum the forces and write results.

221
222
223
224
225
226
            int bufferIndex = 3*tgx;
            if (get_local_id(0) >= TILE_SIZE) {
                tempBuffer[bufferIndex] = force.x;
                tempBuffer[bufferIndex+1] = force.y;
                tempBuffer[bufferIndex+2] = force.z;
            }
227
            barrier(CLK_LOCAL_MEM_FENCE);
228
            if (get_local_id(0) < TILE_SIZE) {
229
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
230
231
                unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
                unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
232
#else
233
234
                unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
235
#endif
236
                // Cheaper to load/store float4 than float3. Do all loads before all stores to minimize store-load waits.
237
238
239
240
241
242
                float4 sum1 = forceBuffers[offset1];
                float4 sum2 = forceBuffers[offset2];
                sum1 += force + (float4) (tempBuffer[bufferIndex], tempBuffer[bufferIndex+1], tempBuffer[bufferIndex+2], 0.0f);
                sum2 += (float4) (localData[get_local_id(0)].fx+localData[get_local_id(0)+TILE_SIZE].fx, localData[get_local_id(0)].fy+localData[get_local_id(0)+TILE_SIZE].fy, localData[get_local_id(0)].fz+localData[get_local_id(0)+TILE_SIZE].fz, 0.0f);
                forceBuffers[offset1] = sum1;
                forceBuffers[offset2] = sum2;
243
244
            }
        }
245
        lasty = y;
246
247
248
249
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}