customGBValueN2_default.cl 9.32 KB
Newer Older
1
2
3
4
#ifdef SUPPORTS_64_BIT_ATOMICS
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
#endif
5
6
7
8
9
10
#define TILE_SIZE 32

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

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

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

        // 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
81
82
83
        if (x == y) {
            // This tile is on the diagonal.

84
85
            const unsigned int localAtomIndex = get_local_id(0);
            local_posq[localAtomIndex] = posq1;
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
91
92
93
94
95
#endif
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
#ifdef USE_EXCLUSIONS
                bool isExcluded = !(excl & 0x1);
#endif
                int atom2 = baseLocalAtom+j;
96
97
                real4 posq2 = local_posq[atom2];
                real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
98
#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
#endif
103
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
104
105
106
#ifdef USE_CUTOFF
                if (r2 < CUTOFF_SQUARED) {
#endif
107
108
                real invR = RSQRT(r2);
                real r = RECIP(invR);
109
                LOAD_ATOM2_PARAMETERS
110
                atom2 = y*TILE_SIZE+baseLocalAtom+j;
111
112
                real tempValue1 = 0;
                real tempValue2 = 0;
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifdef USE_EXCLUSIONS
                if (!isExcluded && atom1 < NUM_ATOMS && atom2 < NUM_ATOMS && atom1 != atom2) {
#else
                if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS && atom1 != atom2) {
#endif
                    COMPUTE_VALUE
                }
                value += tempValue1;
#ifdef USE_CUTOFF
                }
#endif
#ifdef USE_EXCLUSIONS
                excl >>= 1;
#endif
            }

            // Sum the values and write results.

            if (get_local_id(0) >= TILE_SIZE)
                tempBuffer[get_local_id(0)] = value;
            barrier(CLK_LOCAL_MEM_FENCE);
            if (get_local_id(0) < TILE_SIZE) {
135
136
137
138
#ifdef SUPPORTS_64_BIT_ATOMICS
                const unsigned int offset = x*TILE_SIZE + tgx;
                atom_add(&global_value[offset], (long) ((value + tempBuffer[get_local_id(0)+TILE_SIZE])*0xFFFFFFFF));
#else
139
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
140
                const unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
141
#else
142
143
144
                const unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
#endif
                global_value[offset] += value + tempBuffer[get_local_id(0)+TILE_SIZE];
145
146
147
148
149
150
151
#endif
            }
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y && get_local_id(0) < TILE_SIZE) {
152
                unsigned int j = y*TILE_SIZE + tgx;
153
                local_posq[get_local_id(0)] = posq[j];
154
                const unsigned int localAtomIndex = get_local_id(0);
155
156
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
157
            local_value[get_local_id(0)] = 0;
158
159
160
161
162
            barrier(CLK_LOCAL_MEM_FENCE);

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

#ifdef USE_EXCLUSIONS
163
            unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[0]+tgx] : 0xFFFFFFFF);
164
165
            excl = (excl >> baseLocalAtom) & 0xFFFF;
            excl += excl << 16;
166
167
168
169
170
171
172
173
            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;
174
175
                real4 posq2 = local_posq[atom2];
                real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
176
#ifdef USE_PERIODIC
177
178
179
                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;
180
#endif
181
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
182
183
184
#ifdef USE_CUTOFF
                if (r2 < CUTOFF_SQUARED) {
#endif
185
186
                real invR = RSQRT(r2);
                real r = RECIP(invR);
187
                LOAD_ATOM2_PARAMETERS
188
                atom2 = y*TILE_SIZE+baseLocalAtom+tj;
189
190
                real tempValue1 = 0;
                real tempValue2 = 0;
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifdef USE_EXCLUSIONS
                if (!isExcluded && atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
#else
                if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
#endif
                    COMPUTE_VALUE
                }
                value += tempValue1;
                local_value[baseLocalAtom+tj+valueBufferOffset] += tempValue2;
#ifdef USE_CUTOFF
                }
#endif
                barrier(CLK_LOCAL_MEM_FENCE);
#ifdef USE_EXCLUSIONS
                excl >>= 1;
#endif
                tj = (tj+1)%(TILE_SIZE/2);
            }

            // Sum the values and write results.

            if (get_local_id(0) >= TILE_SIZE)
                tempBuffer[get_local_id(0)] = value;
            barrier(CLK_LOCAL_MEM_FENCE);
            if (get_local_id(0) < TILE_SIZE) {
216
217
218
219
220
221
#ifdef SUPPORTS_64_BIT_ATOMICS
                const unsigned int offset1 = x*TILE_SIZE + tgx;
                const unsigned int offset2 = y*TILE_SIZE + tgx;
                atom_add(&global_value[offset1], (long) ((value + tempBuffer[get_local_id(0)+TILE_SIZE])*0xFFFFFFFF));
                atom_add(&global_value[offset2], (long) ((local_value[get_local_id(0)] + local_value[get_local_id(0)+TILE_SIZE])*0xFFFFFFFF));
#else
222
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
223
224
                const unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
                const unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
225
#else
226
227
228
229
230
                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;
#endif
                global_value[offset1] += value + tempBuffer[get_local_id(0)+TILE_SIZE];
                global_value[offset2] += local_value[get_local_id(0)] + local_value[get_local_id(0)+TILE_SIZE];
231
232
233
#endif
            }
        }
234
        lasty = y;
235
236
237
        pos++;
    }
}