customGBEnergyN2_cpu.cl 10.3 KB
Newer Older
1
2
3
4
5
6
7
8
#define TILE_SIZE 32
#define STORE_DERIVATIVE_1(INDEX) derivBuffers##INDEX[offset1] += deriv##INDEX##_1;
#define STORE_DERIVATIVE_2(INDEX) derivBuffers##INDEX[offset2] += local_deriv##INDEX[tgx];

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

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

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

        // Locate the exclusion data for this tile.

#ifdef USE_EXCLUSIONS
        unsigned int exclusionStart = exclusionRowIndices[x];
        unsigned int exclusionEnd = exclusionRowIndices[x+1];
        int exclusionIndex = -1;
        for (int i = exclusionStart; i < exclusionEnd; i++)
            if (exclusionIndices[i] == y) {
                exclusionIndex = i*TILE_SIZE;
                break;
            }
        bool hasExclusions = (exclusionIndex > -1);
#else
        bool hasExclusions = false;
#endif

        // Load the data for this tile if we don't already have it cached.

        if (lasty != y) {
            for (int localAtomIndex = 0; localAtomIndex < TILE_SIZE; localAtomIndex++) {
                unsigned int j = y*TILE_SIZE + localAtomIndex;
                local_posq[localAtomIndex] = posq[j];
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
        }
        if (x == y) {
            // This tile is on the diagonal.

            for (unsigned int tgx = 0; tgx < TILE_SIZE; tgx++) {
#ifdef USE_EXCLUSIONS
                unsigned int excl = exclusions[exclusionIndex+tgx];
#endif
                unsigned int atom1 = x*TILE_SIZE+tgx;
82
                real4 force = 0;
83
                DECLARE_ATOM1_DERIVATIVES
84
                real4 posq1 = posq[atom1];
85
86
87
88
89
                LOAD_ATOM1_PARAMETERS
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
#ifdef USE_EXCLUSIONS
                    bool isExcluded = !(excl & 0x1);
#endif
90
91
                    real4 posq2 = local_posq[j];
                    real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
92
93
94
#ifdef USE_PERIODIC
                    delta.xyz -= floor(delta.xyz*invPeriodicBoxSize.xyz+0.5f)*periodicBoxSize.xyz;
#endif
95
                    real r2 = dot(delta.xyz, delta.xyz);
96
97
98
#ifdef USE_CUTOFF
                    if (r2 < CUTOFF_SQUARED) {
#endif
99
100
                    real invR = RSQRT(r2);
                    real r = RECIP(invR);
101
102
103
                    unsigned int atom2 = j;
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y*TILE_SIZE+j;
104
105
                    real dEdR = 0;
                    real tempEnergy = 0;
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                    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
                }

                // Write results

                unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                forceBuffers[offset1].xyz += force.xyz;
                STORE_DERIVATIVES_1
            }
        }
        else {
            // This is an off-diagonal tile.

            for (int localAtomIndex = 0; localAtomIndex < TILE_SIZE; localAtomIndex++) {
132
                local_force[localAtomIndex] = 0;
133
134
135
136
137
138
139
140
141
142
143
                CLEAR_LOCAL_DERIVATIVES
            }
#if defined(USE_CUTOFF) && defined(USE_EXCLUSIONS)
            unsigned int flags1 = (numTiles <= maxTiles ? interactionFlags[2*pos] : 0xFFFFFFFF);
            unsigned int flags2 = (numTiles <= maxTiles ? interactionFlags[2*pos+1] : 0xFFFFFFFF);
            if (!hasExclusions && (flags1 != 0xFFFFFFFF || flags2 != 0xFFFFFFFF)) {
                // Compute only a subset of the interactions in this tile.

                for (unsigned int tgx = 0; tgx < TILE_SIZE; tgx++) {
                    if ((flags2&(1<<tgx)) != 0) {
                        unsigned int atom1 = x*TILE_SIZE+tgx;
144
                        real value = 0;
145
                        DECLARE_ATOM1_DERIVATIVES
146
                        real4 posq1 = posq[atom1];
147
148
149
                        LOAD_ATOM1_PARAMETERS
                        for (unsigned int j = 0; j < TILE_SIZE; j++) {
                            if ((flags&(1<<j)) != 0) {
150
151
                                real4 posq2 = local_posq[j];
                                real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
152
153
154
#ifdef USE_PERIODIC
                                delta.xyz -= floor(delta.xyz*invPeriodicBoxSize.xyz+0.5f)*periodicBoxSize.xyz;
#endif
155
                                real r2 = dot(delta.xyz, delta.xyz);
156
                                if (r2 < CUTOFF_SQUARED) {
157
158
                                    real invR = RSQRT(r2);
                                    real r = RECIP(invR);
159
160
161
                                    unsigned int atom2 = j;
                                    LOAD_ATOM2_PARAMETERS
                                    atom2 = y*TILE_SIZE+j;
162
163
                                    real dEdR = 0;
                                    real tempEnergy = 0;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
                                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                                        COMPUTE_INTERACTION
                                        dEdR /= -r;
                                    }
                                    energy += tempEnergy;
                                    delta.xyz *= dEdR;
                                    force.xyz -= delta.xyz;
                                    atom2 = j;
                                    local_force[atom2].xyz += delta.xyz;
                                    RECORD_DERIVATIVE_2
                                }
                            }
                        }

                        // Write results for atom1.

                        unsigned int offset = atom1 + get_group_id(0)*PADDED_NUM_ATOMS;
                        global_value[offset] += value;
                    }
                }
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

                for (unsigned int tgx = 0; tgx < TILE_SIZE; tgx++) {
                    unsigned int atom1 = x*TILE_SIZE+tgx;
192
                    real4 force = 0;
193
                    DECLARE_ATOM1_DERIVATIVES
194
                    real4 posq1 = posq[atom1];
195
196
197
198
199
200
201
202
                    LOAD_ATOM1_PARAMETERS
#ifdef USE_EXCLUSIONS
                    unsigned int excl = (hasExclusions ? exclusions[exclusionIndex+tgx] : 0xFFFFFFFF);
#endif
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
#ifdef USE_EXCLUSIONS
                        bool isExcluded = !(excl & 0x1);
#endif
203
204
                        real4 posq2 = local_posq[j];
                        real4 delta = (real4) (posq2.xyz - posq1.xyz, 0);
205
206
207
#ifdef USE_PERIODIC
                        delta.xyz -= floor(delta.xyz*invPeriodicBoxSize.xyz+0.5f)*periodicBoxSize.xyz;
#endif
208
                        real r2 = dot(delta.xyz, delta.xyz);
209
210
211
#ifdef USE_CUTOFF
                        if (r2 < CUTOFF_SQUARED) {
#endif
212
213
                        real invR = RSQRT(r2);
                        real r = RECIP(invR);
214
215
216
                        unsigned int atom2 = j;
                        LOAD_ATOM2_PARAMETERS
                        atom2 = y*TILE_SIZE+j;
217
218
                        real dEdR = 0;
                        real tempEnergy = 0;
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                        if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                            COMPUTE_INTERACTION
                            dEdR /= -r;
                        }
                        energy += tempEnergy;
                        delta.xyz *= dEdR;
                        force.xyz -= delta.xyz;
                        atom2 = j;
                        local_force[atom2].xyz += delta.xyz;
                        RECORD_DERIVATIVE_2
#ifdef USE_CUTOFF
                        }
#endif
#ifdef USE_EXCLUSIONS
                        excl >>= 1;
#endif
                    }

                    // Write results for atom1.

                    unsigned int offset1 = atom1 + get_group_id(0)*PADDED_NUM_ATOMS;
                    forceBuffers[offset1].xyz += force.xyz;
                    STORE_DERIVATIVES_1
                }
            }

            // Write results

            for (int tgx = 0; tgx < TILE_SIZE; tgx++) {
                unsigned int offset2 = y*TILE_SIZE+tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                forceBuffers[offset2].xyz += local_force[tgx].xyz;
                STORE_DERIVATIVES_2
            }
        }
        lasty = y;
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}