customGBValueN2_nvidia.cl 10.8 KB
Newer Older
1
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
2
3
4
#define TILE_SIZE 32

/**
5
6
7
8
9
10
11
12
13
14
15
 * Mark that a block in the value buffer is in use.
 */
void reserveBuffer(unsigned int block, __global unsigned int* forceBufferFlags) {
    if ((get_local_id(0)&(TILE_SIZE-1)) == 0)
        while (atom_cmpxchg(&forceBufferFlags[block+NUM_BLOCKS*get_group_id(0)], 0, 1) != 0)
            ;
    mem_fence(CLK_GLOBAL_MEM_FENCE);
}

/**
 * Mark that a block in the value buffer is no longer in use.
16
 */
17
18
19
20
21
void releaseBuffer(unsigned int block, __global unsigned int* forceBufferFlags) {
    mem_fence(CLK_GLOBAL_MEM_FENCE);
    if ((get_local_id(0)&(TILE_SIZE-1)) == 0)
        forceBufferFlags[block+NUM_BLOCKS*get_group_id(0)] = 0;
}
22

23
24
25
26
/**
 * Compute a value based on pair interactions.
 */
__kernel void computeN2Value(__global float4* posq, __local float4* local_posq, __global unsigned int* exclusions,
27
        __global unsigned int* exclusionIndices, __global unsigned int* exclusionRowIndices, __global float* global_value, __local float* local_value,
28
        __local float* tempBuffer, __global unsigned int* forceBufferFlags,
29
#ifdef USE_CUTOFF
30
        __global ushort2* tiles, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize, unsigned int maxTiles, __global unsigned int* interactionFlags
31
32
33
34
35
36
#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;
37
38
39
40
41
#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
42
43
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
44
#endif
45
46
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
47
48
    __local unsigned int exclusionRange[2*WARPS_PER_GROUP];
    __local int exclusionIndex[WARPS_PER_GROUP];
49
50
51

    while (pos < end) {
        // Extract the coordinates of this tile
52
        unsigned int x, y;
53
#ifdef USE_CUTOFF
54
55
56
57
        if (numTiles <= maxTiles) {
            ushort2 tileIndices = tiles[pos];
            x = tileIndices.x;
            y = tileIndices.y;
58
        }
59
        else
60
#endif
61
62
63
        {
            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);
64
65
            if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
                y += (x < y ? -1 : 1);
66
67
68
                x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
            }
        }
69
70
71
        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;
72
        unsigned int atom1 = x*TILE_SIZE + tgx;
73
74
75
        float value = 0.0f;
        float4 posq1 = posq[atom1];
        LOAD_ATOM1_PARAMETERS
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

        // Locate the exclusion data for this tile.

#ifdef USE_EXCLUSIONS
        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);
#else
        bool hasExclusions = false;
#endif
91
92
93
        if (x == y) {
            // This tile is on the diagonal.

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

            // Write results
138
139
140

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
141
            global_value[offset] += value;
142
            releaseBuffer(x, forceBufferFlags);
143
144
145
146
147
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
148
                unsigned int j = y*TILE_SIZE + tgx;
149
                local_posq[get_local_id(0)] = posq[j];
150
                const unsigned int localAtomIndex = get_local_id(0);
151
152
153
154
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
            local_value[get_local_id(0)] = 0.0f;
#ifdef USE_CUTOFF
155
            unsigned int flags = (numTiles <= maxTiles ? interactionFlags[pos] : 0xFFFFFFFF);
156
157
158
159
160
161
162
163
164
165
166
167
168
            if (!hasExclusions && flags != 0xFFFFFFFF) {
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

                    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);
#ifdef USE_PERIODIC
169
170
171
                            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;
172
173
174
175
#endif
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                            float tempValue1 = 0.0f;
                            float tempValue2 = 0.0f;
176
                            if (r2 < CUTOFF_SQUARED) {
177
                                float r = SQRT(r2);
178
                                LOAD_ATOM2_PARAMETERS
179
                                atom2 = y*TILE_SIZE+j;
180
181
182
183
                                if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                                    COMPUTE_VALUE
                                }
                                value += tempValue1;
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
                            }
                            tempBuffer[get_local_id(0)] = tempValue2;

                            // Sum the forces on atom2.

                            if (tgx % 2 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+1];
                            if (tgx % 4 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+2];
                            if (tgx % 8 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+4];
                            if (tgx % 16 == 0)
                                tempBuffer[get_local_id(0)] += tempBuffer[get_local_id(0)+8];
                            if (tgx == 0)
                                local_value[tbx+j] += tempBuffer[get_local_id(0)] + tempBuffer[get_local_id(0)+16];
                        }
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

#ifdef USE_EXCLUSIONS
209
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[localGroupIndex]+tgx] : 0xFFFFFFFF);
210
211
212
213
214
215
216
217
218
219
220
                excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
#endif
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
#ifdef USE_EXCLUSIONS
                    bool isExcluded = !(excl & 0x1);
#endif
                    int atom2 = tbx+tj;
                    float4 posq2 = local_posq[atom2];
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
#ifdef USE_PERIODIC
221
222
223
                    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;
224
225
#endif
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
226
227
228
#ifdef USE_CUTOFF
                    if (r2 < CUTOFF_SQUARED) {
#endif
229
                    float r = SQRT(r2);
230
                    LOAD_ATOM2_PARAMETERS
231
                    atom2 = y*TILE_SIZE+tj;
232
233
234
235
236
237
238
239
240
241
242
                    float tempValue1 = 0.0f;
                    float tempValue2 = 0.0f;
#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[tbx+tj] += tempValue2;
243
244
245
#ifdef USE_CUTOFF
                    }
#endif
246
247
248
249
250
251
252
253
#ifdef USE_EXCLUSIONS
                    excl >>= 1;
#endif
                    tj = (tj + 1) & (TILE_SIZE - 1);
                }
            }

            // Write results
254
255
256

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
257
            global_value[offset1] += value;
258
259
260
            releaseBuffer(x, forceBufferFlags);
            reserveBuffer(y, forceBufferFlags);
            unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
261
            global_value[offset2] += local_value[get_local_id(0)];
262
            releaseBuffer(y, forceBufferFlags);
263
        }
264
        lasty = y;
265
266
267
        pos++;
    }
}