nonbonded_default.cl 11.1 KB
Newer Older
1
#define TILE_SIZE 32
2

3
4
5
6
7
8
9
10
// Cannot use float3 as OpenCL defines it to be 4 DWORD aligned. This would
// cause every element of array to have DWORD of padding to make it 4 DWORD
// aligned which wastes space and causes LDS bank conflicts as stride is no
// longer odd DWORDS.
typedef struct {
    float x, y, z;
} UnalignedFloat3;

11
typedef struct {
12
13
14
    float x, y, z;
    float q;
    float fx, fy, fz;
15
    ATOM_PARAMETER_DATA
16
17
18
#ifndef PARAMETER_SIZE_IS_EVEN
    float padding;
#endif
19
20
} AtomData;

21
22
23
24
/**
 * Compute nonbonded interactions.
 */

25
__kernel __attribute__((reqd_work_group_size(FORCE_WORK_GROUP_SIZE, 1, 1)))
26
void computeNonbonded(__global float4* restrict forceBuffers, __global float* restrict energyBuffer, __global const float4* restrict posq, __global const unsigned int* restrict exclusions,
27
        __global const unsigned int* restrict exclusionIndices, __global const unsigned int* restrict exclusionRowIndices,
28
        unsigned int startTileIndex, unsigned int endTileIndex,
29
#ifdef USE_CUTOFF
30
        __global const ushort2* restrict tiles, __global const unsigned int* restrict interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global const unsigned int* restrict interactionFlags
31
32
33
34
35
36
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
37
38
    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));
39
#else
40
41
    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);
42
#endif
43
44
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
45
46
47
    __local AtomData localData[TILE_SIZE];
    __local UnalignedFloat3 localForce[FORCE_WORK_GROUP_SIZE];
#ifdef USE_EXCLUSIONS
48
49
    __local unsigned int exclusionRange[2];
    __local int exclusionIndex[1];
50
#endif
51
52
53

    while (pos < end) {
        // Extract the coordinates of this tile
54
        unsigned int x, y;
55
#ifdef USE_CUTOFF
56
        if (numTiles <= maxTiles) {
57
58
59
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
60
        }
61
        else
62
#endif
63
        {
Peter Eastman's avatar
Peter Eastman committed
64
            y = (unsigned int) floor(NUM_BLOCKS+0.5f-SQRT((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
65
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
66
67
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
68
69
70
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
71
72
        unsigned int baseLocalAtom = (get_local_id(0) < TILE_SIZE ? 0 : TILE_SIZE/2);
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
73
        unsigned int localForceOffset = get_local_id(0) & ~(TILE_SIZE-1);
74
        unsigned int atom1 = x*TILE_SIZE + tgx;
75
76
77
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        LOAD_ATOM1_PARAMETERS
78
79
80
81
82
83

        // 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)];
84
        if (get_local_id(0) == 0)
85
86
            exclusionIndex[0] = -1;
        barrier(CLK_LOCAL_MEM_FENCE);
87
        for (int i = exclusionRange[0]+get_local_id(0); i < exclusionRange[1]; i += FORCE_WORK_GROUP_SIZE)
88
89
90
91
92
            if (exclusionIndices[i] == y)
                exclusionIndex[0] = i*TILE_SIZE;
        barrier(CLK_LOCAL_MEM_FENCE);
        bool hasExclusions = (exclusionIndex[0] > -1);
#endif
93
94
95
        if (x == y) {
            // This tile is on the diagonal.

96
97
98
99
100
101
102
103
            if (get_local_id(0) < TILE_SIZE) {
                const unsigned int localAtomIndex = tgx;
                localData[localAtomIndex].x = posq1.x;
                localData[localAtomIndex].y = posq1.y;
                localData[localAtomIndex].z = posq1.z;
                localData[localAtomIndex].q = posq1.w;
                LOAD_LOCAL_PARAMETERS_FROM_1
            }
104
105
            barrier(CLK_LOCAL_MEM_FENCE);
#ifdef USE_EXCLUSIONS
106
            unsigned int excl = exclusions[exclusionIndex[0]+tgx] >> baseLocalAtom;
107
#endif
108
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
109
110
111
#ifdef USE_EXCLUSIONS
                bool isExcluded = !(excl & 0x1);
#endif
112
                unsigned int atom2 = baseLocalAtom+j;
113
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
114
115
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
#ifdef USE_PERIODIC
116
117
118
                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;
119
120
#endif
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
121
122
                float invR = RSQRT(r2);
                float r = RECIP(invR);
123
                LOAD_ATOM2_PARAMETERS
124
                atom2 = y*TILE_SIZE+baseLocalAtom+j;
125
#ifdef USE_SYMMETRIC
126
                float dEdR = 0.0f;
127
128
129
130
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
131
132
133
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += 0.5f*tempEnergy;
134
135
136
137
138
#ifdef USE_SYMMETRIC
                force.xyz -= delta.xyz*dEdR;
#else
                force.xyz -= dEdR1.xyz;
#endif
139
140
141
142
143
                excl >>= 1;
            }

            // Sum the forces and write results.

144
            if (get_local_id(0) >= TILE_SIZE) {
145
146
147
                localData[tgx].fx = force.x;
                localData[tgx].fy = force.y;
                localData[tgx].fz = force.z;
148
            }
149
            barrier(CLK_LOCAL_MEM_FENCE);
150
            if (get_local_id(0) < TILE_SIZE) {
151
152
153
                force.x += localData[tgx].fx;
                force.y += localData[tgx].fy;
                force.z += localData[tgx].fz;
154
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
155
                unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
156
#else
157
                unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
158
#endif
159
                // Cheaper to load/store float4 than float3.
160
                float4 sum = forceBuffers[offset];
161
                sum.xyz += force.xyz;
162
                forceBuffers[offset] = sum;
163
            }
164
            // barrier not required here as localData[*].temp is not accessed before encountering another barrier.
165
166
167
168
        }
        else {
            // This is an off-diagonal tile.

169
170
            if (lasty != y && get_local_id(0) < TILE_SIZE) {
                const unsigned int localAtomIndex = tgx;
171
                unsigned int j = y*TILE_SIZE + tgx;
172
                float4 tempPosq = posq[j];
173
174
175
176
                localData[localAtomIndex].x = tempPosq.x;
                localData[localAtomIndex].y = tempPosq.y;
                localData[localAtomIndex].z = tempPosq.z;
                localData[localAtomIndex].q = tempPosq.w;
177
178
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
179
180
181
            localForce[get_local_id(0)].x = 0.0f;
            localForce[get_local_id(0)].y = 0.0f;
            localForce[get_local_id(0)].z = 0.0f;
182
183
            barrier(CLK_LOCAL_MEM_FENCE);

184
185
            // Compute the full set of interactions in this tile.

186
            unsigned int tj = (tgx+baseLocalAtom) & (TILE_SIZE-1);
187
#ifdef USE_EXCLUSIONS
188
            unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[0]+tgx] : 0xFFFFFFFF);
189
            excl = (excl >> tj) | (excl << (TILE_SIZE - tj));
190
#endif
191
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
192
#ifdef USE_EXCLUSIONS
193
                bool isExcluded = !(excl & 0x1);
194
#endif
195
                float4 posq2 = (float4) (localData[tj].x, localData[tj].y, localData[tj].z, localData[tj].q);
196
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
197
#ifdef USE_PERIODIC
198
199
200
                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;
201
#endif
202
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
203
204
                float invR = RSQRT(r2);
                float r = RECIP(invR);
205
                int atom2 = tj;
206
                LOAD_ATOM2_PARAMETERS
207
                atom2 = y*TILE_SIZE+tj;
208
#ifdef USE_SYMMETRIC
209
                float dEdR = 0.0f;
210
211
212
213
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
214
215
216
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += tempEnergy;
217
#ifdef USE_SYMMETRIC
218
219
                delta.xyz *= dEdR;
                force.xyz -= delta.xyz;
220
221
222
                localForce[tj+localForceOffset].x += delta.x;
                localForce[tj+localForceOffset].y += delta.y;
                localForce[tj+localForceOffset].z += delta.z;
223
224
#else
                force.xyz -= dEdR1.xyz;
225
226
227
                localForce[tj+localForceOffset].x += dEdR2.x;
                localForce[tj+localForceOffset].y += dEdR2.y;
                localForce[tj+localForceOffset].z += dEdR2.z;
228
#endif
229
                barrier(CLK_LOCAL_MEM_FENCE);
230
#ifdef USE_EXCLUSIONS
231
                excl >>= 1;
232
#endif
233
                tj = (tj+1) & (TILE_SIZE-1);
234
235
236
237
            }

            // Sum the forces and write results.

238
            if (get_local_id(0) >= TILE_SIZE) {
239
240
241
                localData[tgx].fx = force.x;
                localData[tgx].fy = force.y;
                localData[tgx].fz = force.z;
242
            }
243
            barrier(CLK_LOCAL_MEM_FENCE);
244
            if (get_local_id(0) < TILE_SIZE) {
245
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
246
247
                unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
                unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
248
#else
249
250
                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;
251
#endif
252
                // Cheaper to load/store float4 than float3. Do all loads before all stores to minimize store-load waits.
253
254
                float4 sum1 = forceBuffers[offset1];
                float4 sum2 = forceBuffers[offset2];
255
256
257
258
259
260
                sum1.x += localData[tgx].fx + force.x;
                sum1.y += localData[tgx].fy + force.y;
                sum1.z += localData[tgx].fz + force.z;
                sum2.x += localForce[tgx].x + localForce[tgx+TILE_SIZE].x;
                sum2.y += localForce[tgx].y + localForce[tgx+TILE_SIZE].y;
                sum2.z += localForce[tgx].z + localForce[tgx+TILE_SIZE].z;
261
262
                forceBuffers[offset1] = sum1;
                forceBuffers[offset2] = sum2;
263
            }
264
            barrier(CLK_LOCAL_MEM_FENCE);
265
        }
266
        lasty = y;
267
268
269
270
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}