customGBValueN2_nvidia.cl 13 KB
Newer Older
1
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
2
3
4
#ifdef SUPPORTS_64_BIT_ATOMICS
#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
#endif
5
6
#define TILE_SIZE 32

7
8
9
/**
 * Compute a value based on pair interactions.
 */
10
11
__kernel void computeN2Value(__global const float4* restrict posq, __local float4* restrict local_posq, __global const unsigned int* restrict exclusions,
        __global const unsigned int* restrict exclusionIndices, __global const unsigned int* restrict exclusionRowIndices,
12
#ifdef SUPPORTS_64_BIT_ATOMICS
13
        __global long* restrict global_value,
14
#else
15
        __global float* restrict global_value,
16
#endif
17
        __local float* restrict local_value, __local float* restrict tempBuffer,
18
#ifdef USE_CUTOFF
19
        __global const ushort2* restrict tiles, __global const unsigned int* restrict interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global const unsigned int* restrict interactionFlags
20
21
22
23
24
25
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
26
27
28
29
30
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
    unsigned int pos = warp*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
    unsigned int end = (warp+1)*(numTiles > maxTiles ? NUM_BLOCKS*(NUM_BLOCKS+1)/2 : numTiles)/totalWarps;
#else
31
32
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
33
#endif
34
35
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
36
37
    __local unsigned int exclusionRange[2*WARPS_PER_GROUP];
    __local int exclusionIndex[WARPS_PER_GROUP];
38
39
40
    __local int2* reservedBlocks = (__local int2*) exclusionRange;
    
    do {
41
        // Extract the coordinates of this tile
42
43
44
        const unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
        const unsigned int tbx = get_local_id(0) - tgx;
        const unsigned int localGroupIndex = get_local_id(0)/TILE_SIZE;
45
        unsigned int x, y;
46
47
        float value = 0.0f;
        if (pos < end) {
48
#ifdef USE_CUTOFF
49
50
51
52
53
54
            if (numTiles <= maxTiles) {
                ushort2 tileIndices = tiles[pos];
                x = tileIndices.x;
                y = tileIndices.y;
            }
            else
55
#endif
56
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
61
62
                if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                    y += (x < y ? -1 : 1);
                    x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
                }
63
            }
64
65
66
            unsigned int atom1 = x*TILE_SIZE + tgx;
            float4 posq1 = posq[atom1];
            LOAD_ATOM1_PARAMETERS
67

68
            // Locate the exclusion data for this tile.
69
70

#ifdef USE_EXCLUSIONS
71
72
73
74
75
76
77
78
            if (tgx < 2)
                exclusionRange[2*localGroupIndex+tgx] = exclusionRowIndices[x+tgx];
            if (tgx == 0)
                exclusionIndex[localGroupIndex] = -1;
            for (int i = exclusionRange[2*localGroupIndex]+tgx; i < exclusionRange[2*localGroupIndex+1]; i += TILE_SIZE)
                if (exclusionIndices[i] == y)
                    exclusionIndex[localGroupIndex] = i*TILE_SIZE;
            bool hasExclusions = (exclusionIndex[localGroupIndex] > -1);
79
#else
80
            bool hasExclusions = false;
81
#endif
82
83
84
85
            if (pos >= end)
                ; // This warp is done.
            else if (x == y) {
                // This tile is on the diagonal.
86

87
88
89
                const unsigned int localAtomIndex = get_local_id(0);
                local_posq[localAtomIndex] = posq1;
                LOAD_LOCAL_PARAMETERS_FROM_1
90
#ifdef USE_EXCLUSIONS
91
                unsigned int excl = exclusions[exclusionIndex[localGroupIndex]+tgx];
92
#endif
93
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
94
#ifdef USE_EXCLUSIONS
95
                    bool isExcluded = !(excl & 0x1);
96
#endif
97
98
99
                    int atom2 = tbx+j;
                    float4 posq2 = local_posq[atom2];
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
100
#ifdef USE_PERIODIC
101
102
103
                    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;
104
#endif
105
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
106
#ifdef USE_CUTOFF
107
                    if (r2 < CUTOFF_SQUARED) {
108
#endif
109
110
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
111
112
113
114
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y*TILE_SIZE+j;
                    float tempValue1 = 0.0f;
                    float tempValue2 = 0.0f;
115
#ifdef USE_EXCLUSIONS
116
                    if (!isExcluded && atom1 < NUM_ATOMS && atom2 < NUM_ATOMS && atom1 != atom2) {
117
#else
118
                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS && atom1 != atom2) {
119
#endif
120
121
122
                        COMPUTE_VALUE
                    }
                    value += tempValue1;
123
#ifdef USE_CUTOFF
124
                    }
125
#endif
126
#ifdef USE_EXCLUSIONS
127
                    excl >>= 1;
128
#endif
129
                }
130
            }
131
132
            else {
                // This is an off-diagonal tile.
133

134
135
136
137
138
                if (lasty != y) {
                    unsigned int j = y*TILE_SIZE + tgx;
                    local_posq[get_local_id(0)] = posq[j];
                    const unsigned int localAtomIndex = get_local_id(0);
                    LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
139
                }
140
141
142
143
144
145
146
147
148
                local_value[get_local_id(0)] = 0.0f;
#ifdef USE_CUTOFF
                unsigned int flags = (numTiles <= maxTiles ? interactionFlags[pos] : 0xFFFFFFFF);
                if (!hasExclusions && flags != 0xFFFFFFFF) {
                    if (flags == 0) {
                        // No interactions in this tile.
                    }
                    else {
                        // Compute only a subset of the interactions in this tile.
149

150
151
152
153
154
                        for (unsigned int j = 0; j < TILE_SIZE; j++) {
                            if ((flags&(1<<j)) != 0) {
                                int atom2 = tbx+j;
                                float4 posq2 = local_posq[atom2];
                                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
155
#ifdef USE_PERIODIC
156
157
158
                                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;
159
#endif
160
161
162
163
                                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                                float tempValue1 = 0.0f;
                                float tempValue2 = 0.0f;
                                if (r2 < CUTOFF_SQUARED) {
164
165
                                    float invR = RSQRT(r2);
                                    float r = RECIP(invR);
166
167
168
169
170
171
                                    LOAD_ATOM2_PARAMETERS
                                    atom2 = y*TILE_SIZE+j;
                                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                                        COMPUTE_VALUE
                                    }
                                    value += tempValue1;
172
                                }
173
                                tempBuffer[get_local_id(0)] = tempValue2;
174

175
                                // Sum the forces on atom2.
176

177
                                if (tgx % 4 == 0)
178
                                    tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+1]+tempBuffer[get_local_id(0)+2]+tempBuffer[get_local_id(0)+3];
179
                                if (tgx == 0)
180
                                    local_value[tbx+j] += tempBuffer[get_local_id(0)]+tempBuffer[get_local_id(0)+4]+tempBuffer[get_local_id(0)+8]+tempBuffer[get_local_id(0)+12]+tempBuffer[get_local_id(0)+16]+tempBuffer[get_local_id(0)+20]+tempBuffer[get_local_id(0)+24]+tempBuffer[get_local_id(0)+28];
181
                            }
182
183
184
                        }
                    }
                }
185
                else
186
#endif
187
188
                {
                    // Compute the full set of interactions in this tile.
189
190

#ifdef USE_EXCLUSIONS
191
192
                    unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[localGroupIndex]+tgx] : 0xFFFFFFFF);
                    excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
193
#endif
194
195
                    unsigned int tj = tgx;
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
196
#ifdef USE_EXCLUSIONS
197
                        bool isExcluded = !(excl & 0x1);
198
#endif
199
200
201
                        int atom2 = tbx+tj;
                        float4 posq2 = local_posq[atom2];
                        float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
202
#ifdef USE_PERIODIC
203
204
205
                        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;
206
#endif
207
                        float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
208
#ifdef USE_CUTOFF
209
                        if (r2 < CUTOFF_SQUARED) {
210
#endif
211
212
                        float invR = RSQRT(r2);
                        float r = RECIP(invR);
213
214
215
216
                        LOAD_ATOM2_PARAMETERS
                        atom2 = y*TILE_SIZE+tj;
                        float tempValue1 = 0.0f;
                        float tempValue2 = 0.0f;
217
#ifdef USE_EXCLUSIONS
218
                        if (!isExcluded && atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
219
#else
220
                        if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
221
#endif
222
223
224
225
                            COMPUTE_VALUE
                        }
                        value += tempValue1;
                        local_value[tbx+tj] += tempValue2;
226
#ifdef USE_CUTOFF
227
                        }
228
#endif
229
#ifdef USE_EXCLUSIONS
230
                        excl >>= 1;
231
#endif
232
233
                        tj = (tj + 1) & (TILE_SIZE - 1);
                    }
234
235
                }
            }
236
237
238
239
240
        }
        
        // Write results.  We need to coordinate between warps to make sure no two of them
        // ever try to write to the same piece of memory at the same time.
        
241
242
243
244
245
246
247
248
249
250
#ifdef SUPPORTS_64_BIT_ATOMICS
        if (pos < end) {
            const unsigned int offset = x*TILE_SIZE + tgx;
            atom_add(&global_value[offset], (long) (value*0xFFFFFFFF));
        }
        if (pos < end && x != y) {
            const unsigned int offset = y*TILE_SIZE + tgx;
            atom_add(&global_value[offset], (long) (local_value[get_local_id(0)]*0xFFFFFFFF));
        }
#else
251
252
253
254
255
256
257
258
259
        int writeX = (pos < end ? x : -1);
        int writeY = (pos < end && x != y ? y : -1);
        if (tgx == 0)
            reservedBlocks[localGroupIndex] = (int2)(writeX, writeY);
        bool done = false;
        int doneIndex = 0;
        int checkIndex = 0;
        while (true) {
            // See if any warp still needs to write its data.
260

261
262
263
264
265
266
267
268
269
270
271
272
273
            bool allDone = true;
            barrier(CLK_LOCAL_MEM_FENCE);
            while (doneIndex < WARPS_PER_GROUP && allDone) {
                if (reservedBlocks[doneIndex].x != -1)
                    allDone = false;
                else
                    doneIndex++;
            }
            if (allDone)
                break;
            if (!done) {
                // See whether this warp can write its data.  This requires that no previous warp
                // is trying to write to the same block of the buffer.
274

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
                bool canWrite = (writeX != -1);
                while (checkIndex < localGroupIndex && canWrite) {
                    if ((reservedBlocks[checkIndex].x == x || reservedBlocks[checkIndex].y == x) ||
                            (writeY != -1 && (reservedBlocks[checkIndex].x == y || reservedBlocks[checkIndex].y == y)))
                        canWrite = false;
                    else
                        checkIndex++;
                }
                if (canWrite) {
                    // Write the data to global memory, then mark this warp as done.

                    if (writeX > -1) {
                        const unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                        global_value[offset] += value;
                    }
                    if (writeY > -1) {
                        const unsigned int offset = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                        global_value[offset] += local_value[get_local_id(0)];
                    }
                    done = true;
                    if (tgx == 0)
                        reservedBlocks[localGroupIndex] = (int2)(-1, -1);
                }
            }
299
        }
300
#endif
301
        lasty = y;
302
        pos++;
303
    } while (pos < end);
304
}