nonbonded_default.cl 9.57 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
Peter Eastman's avatar
Peter Eastman committed
18
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global unsigned int* interactionFlags
19
20
21
22
23
24
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
25
26
    unsigned int pos = get_group_id(0)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/get_num_groups(0);
    unsigned int end = (get_group_id(0)+1)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/get_num_groups(0);
27
#else
28
29
    unsigned int pos = get_group_id(0)*numTiles/get_num_groups(0);
    unsigned int end = (get_group_id(0)+1)*numTiles/get_num_groups(0);
30
#endif
31
32
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
33
34
    __local unsigned int exclusionRange[2];
    __local int exclusionIndex[1];
35
36
37

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

        // 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
77
78
79
        if (x == y) {
            // This tile is on the diagonal.

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

            // Sum the forces and write results.

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

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

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

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

            // Sum the forces and write results.

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