customGBEnergyN2_nvidia.cl 8.89 KB
Newer Older
1
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
2
#define TILE_SIZE 32
3
4
#define STORE_DERIVATIVE_1(INDEX) derivBuffers##INDEX[offset1] += deriv##INDEX##_1;
#define STORE_DERIVATIVE_2(INDEX) derivBuffers##INDEX[offset2] += local_deriv##INDEX[get_local_id(0)];
5
6

/**
7
 * Mark that a block in the force buffer is in use.
8
 */
9
10
11
12
13
14
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);
}
15

16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * Mark that a block in the force buffer is no longer in use.
 */
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;
}

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

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

        // 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
93
94
95
        if (x == y) {
            // This tile is on the diagonal.

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

            // Write results
139
140
141

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
142
143
            forceBuffers[offset1].xyz += force.xyz;
            STORE_DERIVATIVES_1
144
            releaseBuffer(x, forceBufferFlags);
145
146
147
148
        }
        else {
            // This is an off-diagonal tile.

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

#ifdef USE_EXCLUSIONS
168
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[localGroupIndex]+tgx] : 0xFFFFFFFF);
169
170
171
172
173
174
175
176
177
178
179
                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
180
181
182
                    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;
183
#endif
184
185
186
187
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
                    if (r2 < CUTOFF_SQUARED) {
#endif
188
                    float r = SQRT(r2);
189
                    LOAD_ATOM2_PARAMETERS
190
                    atom2 = y*TILE_SIZE+tj;
191
192
193
194
                    float dEdR = 0.0f;
                    float tempEnergy = 0.0f;
                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                        COMPUTE_INTERACTION
195
                        dEdR /= -r;
196
197
198
199
                    }
		    energy += tempEnergy;
                    delta.xyz *= dEdR;
                    force.xyz -= delta.xyz;
200
201
                    atom2 = tbx+tj;
                    local_force[atom2].xyz += delta.xyz;
202
                    RECORD_DERIVATIVE_2
203
204
205
#ifdef USE_CUTOFF
                    }
#endif
206
207
208
209
210
211
212
213
#ifdef USE_EXCLUSIONS
                    excl >>= 1;
#endif
                    tj = (tj + 1) & (TILE_SIZE - 1);
                }
            }

            // Write results
214
215
216

            reserveBuffer(x, forceBufferFlags);
            unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
217
            forceBuffers[offset1].xyz += force.xyz;
218
            STORE_DERIVATIVES_1
219
220
221
222
            releaseBuffer(x, forceBufferFlags);
            reserveBuffer(y, forceBufferFlags);
            unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
            forceBuffers[offset2].xyz += local_force[get_local_id(0)].xyz;
223
            STORE_DERIVATIVES_2
224
            releaseBuffer(y, forceBufferFlags);
225
        }
226
        lasty = y;
227
228
229
230
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}