"platforms/cuda-old/src/kernels/kApplyConstraints.cu" did not exist on "74a8266f4fbe2ac74c41ef84b47b57245e729020"
customGBValueN2.cc 12 KB
Newer Older
1
2
3
/**
 * Compute a value based on pair interactions.
 */
4
KERNEL void computeN2Value(GLOBAL const real4* RESTRICT posq, GLOBAL const unsigned int* RESTRICT exclusions,
5
        GLOBAL const int2* exclusionTiles,
6
        GLOBAL mm_ulong* RESTRICT global_value,
7
#ifdef USE_CUTOFF
8
9
10
        GLOBAL const int* RESTRICT tiles, GLOBAL const unsigned int* RESTRICT interactionCount, real4 periodicBoxSize, real4 invPeriodicBoxSize,
        real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ, unsigned int maxTiles, GLOBAL const real4* RESTRICT blockCenter,
        GLOBAL const real4* RESTRICT blockSize, GLOBAL const int* RESTRICT interactingAtoms
11
12
13
14
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
15
16
17
18
19
20
21
    const unsigned int totalWarps = GLOBAL_SIZE/TILE_SIZE;
    const unsigned int warp = GLOBAL_ID/TILE_SIZE;
    const unsigned int tgx = LOCAL_ID & (TILE_SIZE-1);
    const unsigned int tbx = LOCAL_ID - tgx;
    LOCAL real3 local_pos[LOCAL_BUFFER_SIZE];
    LOCAL real local_value[LOCAL_BUFFER_SIZE];
    ATOM_PARAMETER_DATA
22
23
24

    // First loop: process tiles that contain exclusions.
    
25
26
    const int firstExclusionTile = FIRST_EXCLUSION_TILE+warp*(LAST_EXCLUSION_TILE-FIRST_EXCLUSION_TILE)/totalWarps;
    const int lastExclusionTile = FIRST_EXCLUSION_TILE+(warp+1)*(LAST_EXCLUSION_TILE-FIRST_EXCLUSION_TILE)/totalWarps;
27
    for (int pos = firstExclusionTile; pos < lastExclusionTile; pos++) {
28
        const int2 tileIndices = exclusionTiles[pos];
29
30
31
32
        const unsigned int x = tileIndices.x;
        const unsigned int y = tileIndices.y;
        real value = 0;
        unsigned int atom1 = x*TILE_SIZE + tgx;
33
        real3 pos1 = trimTo3(posq[atom1]);
34
35
36
37
38
39
40
        LOAD_ATOM1_PARAMETERS
#ifdef USE_EXCLUSIONS
        unsigned int excl = exclusions[pos*TILE_SIZE+tgx];
#endif
        if (x == y) {
            // This tile is on the diagonal.

41
42
            const unsigned int localAtomIndex = LOCAL_ID;
            local_pos[localAtomIndex] = pos1;
43
44
45
46
            LOAD_LOCAL_PARAMETERS_FROM_1
            SYNC_WARPS;
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
                int atom2 = tbx+j;
47
48
                real3 pos2 = local_pos[atom2];
                real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
49
#ifdef USE_PERIODIC
50
                APPLY_PERIODIC_TO_DELTA(delta)
51
52
53
54
55
56
#endif
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
                if (r2 < CUTOFF_SQUARED) {
#endif
                    real invR = RSQRT(r2);
peastman's avatar
peastman committed
57
                    real r = r2*invR;
58
59
60
61
62
63
64
65
66
67
68
69
70
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y*TILE_SIZE+j;
                    real tempValue1 = 0;
                    real tempValue2 = 0;
#ifdef USE_EXCLUSIONS
                    bool isExcluded = (atom1 >= NUM_ATOMS || atom2 >= NUM_ATOMS || !(excl & 0x1));
                    if (!isExcluded && atom1 != atom2) {
#else
                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS && atom1 != atom2) {
#endif
                        COMPUTE_VALUE
                    }
                    value += tempValue1;
71
                    ADD_TEMP_DERIVS1
72
73
74
75
76
77
78
79
80
81
82
83
#ifdef USE_CUTOFF
                }
#endif
#ifdef USE_EXCLUSIONS
                excl >>= 1;
#endif
                SYNC_WARPS;
            }
        }
        else {
            // This is an off-diagonal tile.

84
            const unsigned int localAtomIndex = LOCAL_ID;
85
            unsigned int j = y*TILE_SIZE + tgx;
86
            local_pos[localAtomIndex] = trimTo3(posq[j]);
87
88
89
90
91
92
93
94
95
            LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            local_value[localAtomIndex] = 0;
            SYNC_WARPS;
#ifdef USE_EXCLUSIONS
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
#endif
            unsigned int tj = tgx;
            for (j = 0; j < TILE_SIZE; j++) {
                int atom2 = tbx+tj;
96
97
                real3 pos2 = local_pos[atom2];
                real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
98
#ifdef USE_PERIODIC
99
                APPLY_PERIODIC_TO_DELTA(delta)
100
101
102
103
104
105
#endif
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
                if (r2 < CUTOFF_SQUARED) {
#endif
                    real invR = RSQRT(r2);
peastman's avatar
peastman committed
106
                    real r = r2*invR;
107
108
109
110
111
112
113
114
115
116
117
118
119
120
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y*TILE_SIZE+tj;
                    real tempValue1 = 0;
                    real tempValue2 = 0;
#ifdef USE_EXCLUSIONS
                    bool isExcluded = (atom1 >= NUM_ATOMS || atom2 >= NUM_ATOMS || !(excl & 0x1));
                    if (!isExcluded) {
#else
                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
#endif
                        COMPUTE_VALUE
                    }
                    value += tempValue1;
                    local_value[tbx+tj] += tempValue2;
121
122
                    ADD_TEMP_DERIVS1
                    ADD_TEMP_DERIVS2
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifdef USE_CUTOFF
                }
#endif
#ifdef USE_EXCLUSIONS
                excl >>= 1;
#endif
                tj = (tj + 1) & (TILE_SIZE - 1);
                SYNC_WARPS;
            }
        }

        // Write results.

136
        unsigned int offset1 = x*TILE_SIZE + tgx;
137
        ATOMIC_ADD(&global_value[offset1], (mm_ulong) realToFixedPoint(value));
138
        STORE_PARAM_DERIVS1
139
        if (x != y) {
140
            unsigned int offset2 = y*TILE_SIZE + tgx;
141
            ATOMIC_ADD(&global_value[offset2], (mm_ulong) realToFixedPoint(local_value[LOCAL_ID]));
142
            STORE_PARAM_DERIVS2
143
144
145
146
147
148
149
150
        }
    }

    // Second loop: tiles without exclusions, either from the neighbor list (with cutoff) or just enumerating all
    // of them (no cutoff).

#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
151
152
    if (numTiles > maxTiles)
        return; // There wasn't enough memory for the neighbor list.
153
154
    int pos = (int) (warp*(numTiles > maxTiles ? NUM_BLOCKS*((mm_long)NUM_BLOCKS+1)/2 : (mm_long)numTiles)/totalWarps);
    int end = (int) ((warp+1)*(numTiles > maxTiles ? NUM_BLOCKS*((mm_long)NUM_BLOCKS+1)/2 : (mm_long)numTiles)/totalWarps);
155
#else
156
157
    int pos = (int) (warp*(mm_long)numTiles/totalWarps);
    int end = (int) ((warp+1)*(mm_long)numTiles/totalWarps);
158
159
160
#endif
    int skipBase = 0;
    int currentSkipIndex = tbx;
161
162
163
    LOCAL int atomIndices[LOCAL_BUFFER_SIZE];
    LOCAL volatile int skipTiles[LOCAL_BUFFER_SIZE];
    skipTiles[LOCAL_ID] = -1;
164
165
166
167
168
169
170

    while (pos < end) {
        real value = 0;
        bool includeTile = true;

        // Extract the coordinates of this tile.
        
171
        int x, y;
172
173
        bool singlePeriodicCopy = false;
#ifdef USE_CUTOFF
174
175
176
177
178
179
180
181
182
183
        x = tiles[pos];
        real4 blockSizeX = blockSize[x];
        singlePeriodicCopy = (0.5f*periodicBoxSize.x-blockSizeX.x >= CUTOFF &&
                              0.5f*periodicBoxSize.y-blockSizeX.y >= CUTOFF &&
                              0.5f*periodicBoxSize.z-blockSizeX.z >= CUTOFF);
#else
        y = (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);
        if (x < y || x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
            y += (x < y ? -1 : 1);
184
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
185
        }
186

187
        // Skip over tiles that have exclusions, since they were already processed.
188

189
190
        SYNC_WARPS;
        while (skipTiles[tbx+TILE_SIZE-1] < pos) {
191
            SYNC_WARPS;
192
            if (skipBase+tgx < NUM_TILES_WITH_EXCLUSIONS) {
193
                int2 tile = exclusionTiles[skipBase+tgx];
194
                skipTiles[LOCAL_ID] = tile.x + tile.y*NUM_BLOCKS - tile.y*(tile.y+1)/2;
195
            }
196
            else
197
                skipTiles[LOCAL_ID] = end;
198
199
200
            skipBase += TILE_SIZE;            
            currentSkipIndex = tbx;
            SYNC_WARPS;
201
        }
202
203
204
205
        while (skipTiles[currentSkipIndex] < pos)
            currentSkipIndex++;
        includeTile = (skipTiles[currentSkipIndex] != pos);
#endif
206
207
208
209
210
        if (includeTile) {
            unsigned int atom1 = x*TILE_SIZE + tgx;

            // Load atom data for this tile.
            
211
            real3 pos1 = trimTo3(posq[atom1]);
212
            LOAD_ATOM1_PARAMETERS
213
            const unsigned int localAtomIndex = LOCAL_ID;
214
#ifdef USE_CUTOFF
peastman's avatar
peastman committed
215
            unsigned int j = interactingAtoms[pos*TILE_SIZE+tgx];
216
217
218
#else
            unsigned int j = y*TILE_SIZE + tgx;
#endif
219
            atomIndices[LOCAL_ID] = j;
220
            if (j < PADDED_NUM_ATOMS) {
221
                local_pos[localAtomIndex] = trimTo3(posq[j]);
222
223
224
225
226
227
228
229
230
231
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
                local_value[localAtomIndex] = 0;
            }
            SYNC_WARPS;
#ifdef USE_PERIODIC
            if (singlePeriodicCopy) {
                // The box is small enough that we can just translate all the atoms into a single periodic
                // box, then skip having to apply periodic boundary conditions later.

                real4 blockCenterX = blockCenter[x];
232
233
                APPLY_PERIODIC_TO_POS_WITH_CENTER(pos1, blockCenterX)
                APPLY_PERIODIC_TO_POS_WITH_CENTER(local_pos[LOCAL_ID], blockCenterX)
234
235
236
237
                SYNC_WARPS;
                unsigned int tj = tgx;
                for (j = 0; j < TILE_SIZE; j++) {
                    int atom2 = tbx+tj;
238
239
                    real3 pos2 = local_pos[atom2];
                    real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
240
241
242
                    real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                    if (r2 < CUTOFF_SQUARED) {
                        real invR = RSQRT(r2);
peastman's avatar
peastman committed
243
                        real r = r2*invR;
244
245
246
247
248
249
250
251
252
                        LOAD_ATOM2_PARAMETERS
                        atom2 = atomIndices[tbx+tj];
                        real tempValue1 = 0;
                        real tempValue2 = 0;
                        if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                            COMPUTE_VALUE
                        }
                        value += tempValue1;
                        local_value[tbx+tj] += tempValue2;
253
254
                        ADD_TEMP_DERIVS1
                        ADD_TEMP_DERIVS2
255
256
257
258
259
260
261
262
263
                    }
                    tj = (tj + 1) & (TILE_SIZE - 1);
                    SYNC_WARPS;
                }
            }
            else
#endif
            {
                // We need to apply periodic boundary conditions separately for each interaction.
264

265
266
267
                unsigned int tj = tgx;
                for (j = 0; j < TILE_SIZE; j++) {
                    int atom2 = tbx+tj;
268
269
                    real3 pos2 = local_pos[atom2];
                    real3 delta = make_real3(pos2.x-pos1.x, pos2.y-pos1.y, pos2.z-pos1.z);
270
#ifdef USE_PERIODIC
271
                    APPLY_PERIODIC_TO_DELTA(delta)
272
273
274
275
276
277
#endif
                    real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
                    if (r2 < CUTOFF_SQUARED) {
#endif
                        real invR = RSQRT(r2);
peastman's avatar
peastman committed
278
                        real r = r2*invR;
279
280
281
282
283
284
285
286
287
                        LOAD_ATOM2_PARAMETERS
                        atom2 = atomIndices[tbx+tj];
                        real tempValue1 = 0;
                        real tempValue2 = 0;
                        if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                            COMPUTE_VALUE
                        }
                        value += tempValue1;
                        local_value[tbx+tj] += tempValue2;
288
289
                        ADD_TEMP_DERIVS1
                        ADD_TEMP_DERIVS2
290
291
292
293
294
295
296
297
298
#ifdef USE_CUTOFF
                    }
#endif
                    tj = (tj + 1) & (TILE_SIZE - 1);
                    SYNC_WARPS;
                }
            }
        
            // Write results.
299

300
#ifdef USE_CUTOFF
301
            unsigned int atom2 = atomIndices[LOCAL_ID];
302
303
304
#else
            unsigned int atom2 = y*TILE_SIZE + tgx;
#endif
305
            unsigned int offset1 = atom1;
306
            ATOMIC_ADD(&global_value[offset1], (mm_ulong) realToFixedPoint(value));
307
308
309
            STORE_PARAM_DERIVS1
            if (atom2 < PADDED_NUM_ATOMS) {
                unsigned int offset2 = atom2;
310
                ATOMIC_ADD(&global_value[offset2], (mm_ulong) realToFixedPoint(local_value[LOCAL_ID]));
311
312
                STORE_PARAM_DERIVS2
            }
313
314
315
316
        }
        pos++;
    }
}