nonbonded_default.cl 9.79 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
        unsigned int startTileIndex, unsigned int endTileIndex,
18
#ifdef USE_CUTOFF
Peter Eastman's avatar
Peter Eastman committed
19
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global unsigned int* 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
35
    __local unsigned int exclusionRange[2];
    __local int exclusionIndex[1];
36
37
38

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

        // 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)];
        if (tgx == 0)
            exclusionIndex[0] = -1;
        barrier(CLK_LOCAL_MEM_FENCE);
        for (int i = exclusionRange[0]+tgx; i < exclusionRange[1]; i += TILE_SIZE)
            if (exclusionIndices[i] == y)
                exclusionIndex[0] = i*TILE_SIZE;
        barrier(CLK_LOCAL_MEM_FENCE);
        bool hasExclusions = (exclusionIndex[0] > -1);
#endif
78
79
80
        if (x == y) {
            // This tile is on the diagonal.

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

            // Sum the forces and write results.

127
            if (get_local_id(0) >= TILE_SIZE)
128
129
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
130
            if (get_local_id(0) < TILE_SIZE) {
131
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
132
                unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
133
#else
134
                unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
135
#endif
136
                forceBuffers[offset].xyz = forceBuffers[offset].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
137
138
139
140
141
            }
        }
        else {
            // This is an off-diagonal tile.

142
143
            const unsigned int localAtomIndex = get_local_id(0);
            if (lasty != y && localAtomIndex < TILE_SIZE) {
144
                unsigned int j = y*TILE_SIZE + tgx;
145
                float4 tempPosq = posq[j];
146
147
148
149
                localData[localAtomIndex].x = tempPosq.x;
                localData[localAtomIndex].y = tempPosq.y;
                localData[localAtomIndex].z = tempPosq.z;
                localData[localAtomIndex].q = tempPosq.w;
150
151
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
152
153
154
            localData[localAtomIndex].fx = 0.0f;
            localData[localAtomIndex].fy = 0.0f;
            localData[localAtomIndex].fz = 0.0f;
155
156
            barrier(CLK_LOCAL_MEM_FENCE);

157
158
            // Compute the full set of interactions in this tile.

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

            // Sum the forces and write results.

213
            if (get_local_id(0) >= TILE_SIZE)
214
215
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
216
            if (get_local_id(0) < TILE_SIZE) {
217
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
218
219
                unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
                unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
220
#else
221
222
                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;
223
#endif
224
                forceBuffers[offset1].xyz = forceBuffers[offset1].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
225
                float4 sum = (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);
226
                forceBuffers[offset2].xyz = forceBuffers[offset2].xyz+sum.xyz;
227
228
            }
        }
229
        lasty = y;
230
231
232
233
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}