customGBEnergyN2_default.cl 11.2 KB
Newer Older
1
#define TILE_SIZE 32
2
3
#ifdef SUPPORTS_64_BIT_ATOMICS
#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
4
5
#define STORE_DERIVATIVE_1(INDEX) atom_add(&derivBuffers[offset1+(INDEX-1)*PADDED_NUM_ATOMS], (long) (deriv##INDEX##_1*0x100000000));
#define STORE_DERIVATIVE_2(INDEX) atom_add(&derivBuffers[offset2+(INDEX-1)*PADDED_NUM_ATOMS], (long) (local_deriv##INDEX[get_local_id(0)]*0x100000000));
6
#else
7
8
#define STORE_DERIVATIVE_1(INDEX) derivBuffers##INDEX[offset1] += deriv##INDEX##_1+tempDerivBuffer##INDEX[get_local_id(0)+TILE_SIZE];
#define STORE_DERIVATIVE_2(INDEX) derivBuffers##INDEX[offset2] += local_deriv##INDEX[get_local_id(0)]+local_deriv##INDEX[get_local_id(0)+TILE_SIZE];
9
#endif
10
11
12
13
14

/**
 * Compute a force based on pair interactions.
 */

15
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
16
17
18
19
void computeN2Energy(
#ifdef SUPPORTS_64_BIT_ATOMICS
        __global long* restrict forceBuffers,
#else
20
        __global real4* restrict forceBuffers,
21
#endif
22
        __global real* restrict energyBuffer, __local real4* restrict local_force,
23
        __global const real4* restrict posq, __local real4* restrict local_posq, __global const unsigned int* restrict exclusions, __global const unsigned int* restrict exclusionIndices,
24
        __global const unsigned int* restrict exclusionRowIndices, __local real4* restrict tempForceBuffer,
25
#ifdef USE_CUTOFF
26
        __global const ushort2* restrict tiles, __global const unsigned int* restrict interactionCount, real4 periodicBoxSize, real4 invPeriodicBoxSize, unsigned int maxTiles
27
28
29
30
31
32
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
33
34
35
    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);
#else
36
37
    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);
38
#endif
39
    real energy = 0;
40
    unsigned int lasty = 0xFFFFFFFF;
41
42
    __local unsigned int exclusionRange[2];
    __local int exclusionIndex[1];
43
44
45
46
    DECLARE_TEMP_BUFFERS

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

        // 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
87
88
89
        if (x == y) {
            // This tile is on the diagonal.

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

            // Sum the forces and write results.

            if (get_local_id(0) >= TILE_SIZE) {
                tempForceBuffer[get_local_id(0)] = force;
                SET_TEMP_BUFFERS
            }
            barrier(CLK_LOCAL_MEM_FENCE);
            if (get_local_id(0) < TILE_SIZE) {
142
143
#ifdef SUPPORTS_64_BIT_ATOMICS
                const unsigned int offset1 = x*TILE_SIZE + tgx;
144
145
146
                atom_add(&forceBuffers[offset1], (long) ((force.x + tempForceBuffer[get_local_id(0)+TILE_SIZE].x)*0x100000000));
                atom_add(&forceBuffers[offset1+PADDED_NUM_ATOMS], (long) ((force.y + tempForceBuffer[get_local_id(0)+TILE_SIZE].y)*0x100000000));
                atom_add(&forceBuffers[offset1+2*PADDED_NUM_ATOMS], (long) ((force.z + tempForceBuffer[get_local_id(0)+TILE_SIZE].z)*0x100000000));
147
#else
148
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
149
                const unsigned int offset1 = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
150
#else
151
152
153
                const unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
#endif
                forceBuffers[offset1].xyz += force.xyz + tempForceBuffer[get_local_id(0)+TILE_SIZE].xyz;
154
155
156
157
158
159
160
#endif
                STORE_DERIVATIVES_1
            }
        }
        else {
            // This is an off-diagonal tile.

161
            const unsigned int localAtomIndex = get_local_id(0);
162
            if (lasty != y && get_local_id(0) < TILE_SIZE) {
163
                unsigned int j = y*TILE_SIZE + tgx;
164
                local_posq[localAtomIndex] = posq[j];
165
166
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
167
            local_force[localAtomIndex] = 0;
168
            CLEAR_LOCAL_DERIVATIVES
169
170
171
172
173
            barrier(CLK_LOCAL_MEM_FENCE);

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

#ifdef USE_EXCLUSIONS
174
            unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[0]+tgx] : 0xFFFFFFFF);
175
176
            excl = (excl >> baseLocalAtom) & 0xFFFF;
            excl += excl << 16;
177
178
179
180
181
182
183
184
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
#endif
            unsigned int tj = tgx%(TILE_SIZE/2);
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
#ifdef USE_EXCLUSIONS
                bool isExcluded = !(excl & 0x1);
#endif
                int atom2 = baseLocalAtom+tj;
185
186
                real4 posq2 = local_posq[atom2];
                real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
187
#ifdef USE_PERIODIC
188
189
190
                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;
191
#endif
192
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
193
194
195
#ifdef USE_CUTOFF
                if (r2 < CUTOFF_SQUARED) {
#endif
196
197
                real invR = RSQRT(r2);
                real r = RECIP(invR);
198
                LOAD_ATOM2_PARAMETERS
199
                atom2 = y*TILE_SIZE+baseLocalAtom+tj;
200
201
                real dEdR = 0;
                real tempEnergy = 0;
202
203
204
205
206
207
208
                if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                    COMPUTE_INTERACTION
                    dEdR /= -r;
                }
                energy += tempEnergy;
                delta.xyz *= dEdR;
                force.xyz -= delta.xyz;
209
                atom2 = baseLocalAtom+tj+forceBufferOffset;
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
                local_force[baseLocalAtom+tj+forceBufferOffset].xyz += delta.xyz;
                RECORD_DERIVATIVE_2
#ifdef USE_CUTOFF
                }
#endif
                barrier(CLK_LOCAL_MEM_FENCE);
#ifdef USE_EXCLUSIONS
                excl >>= 1;
#endif
                tj = (tj+1)%(TILE_SIZE/2);
            }

            // Sum the forces and write results.

            if (get_local_id(0) >= TILE_SIZE) {
                tempForceBuffer[get_local_id(0)] = force;
                SET_TEMP_BUFFERS
            }
            barrier(CLK_LOCAL_MEM_FENCE);
            if (get_local_id(0) < TILE_SIZE) {
230
231
232
#ifdef SUPPORTS_64_BIT_ATOMICS
                const unsigned int offset1 = x*TILE_SIZE + tgx;
                const unsigned int offset2 = y*TILE_SIZE + tgx;
233
234
235
236
237
238
                atom_add(&forceBuffers[offset1], (long) ((force.x+tempForceBuffer[get_local_id(0)+TILE_SIZE].x)*0x100000000));
                atom_add(&forceBuffers[offset1+PADDED_NUM_ATOMS], (long) ((force.y+tempForceBuffer[get_local_id(0)+TILE_SIZE].y)*0x100000000));
                atom_add(&forceBuffers[offset1+2*PADDED_NUM_ATOMS], (long) ((force.z+tempForceBuffer[get_local_id(0)+TILE_SIZE].z)*0x100000000));
                atom_add(&forceBuffers[offset2], (long) ((local_force[get_local_id(0)].x+local_force[get_local_id(0)+TILE_SIZE].x)*0x100000000));
                atom_add(&forceBuffers[offset2+PADDED_NUM_ATOMS], (long) ((local_force[get_local_id(0)].y+local_force[get_local_id(0)+TILE_SIZE].y)*0x100000000));
                atom_add(&forceBuffers[offset2+2*PADDED_NUM_ATOMS], (long) ((local_force[get_local_id(0)].z+local_force[get_local_id(0)+TILE_SIZE].z)*0x100000000));
239
#else
240
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
241
242
                const unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
                const unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
243
#else
244
245
                const unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                const unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
246
247
248
#endif
                forceBuffers[offset1].xyz += force.xyz+tempForceBuffer[get_local_id(0)+TILE_SIZE].xyz;
                forceBuffers[offset2].xyz += local_force[get_local_id(0)].xyz+local_force[get_local_id(0)+TILE_SIZE].xyz;
249
#endif
250
251
252
253
                STORE_DERIVATIVES_1
                STORE_DERIVATIVES_2
            }
        }
254
        lasty = y;
255
256
257
258
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}