"vscode:/vscode.git/clone" did not exist on "4096000e34ded704355c55fb2b3e1b0b3cb6b3ba"
CpuNeighborList.cpp 24.5 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
9
 * Portions copyright (c) 2013-2015 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

32
#include "CpuNeighborList.h"
33
#include "openmm/internal/hardware.h"
34
#include "openmm/internal/vectorize.h"
35
#include "hilbert.h"
36
#include <algorithm>
37
38
39
40
41
42
43
44
45
46
47
#include <set>
#include <map>
#include <cmath>

using namespace std;

namespace OpenMM {

class VoxelIndex 
{
public:
48
    VoxelIndex() : y(0), z(0) {
49
    }
50
    VoxelIndex(int y, int z) : y(y), z(z) {
51
    }
52
    int y;
53
    int z;
54
55
};

56
57
58
59
60
/**
 * This data structure organizes the particles spatially.  It divides them into bins along the x and y axes,
 * then sorts each bin along the z axis so ranges can be identified quickly with a binary search.
 */
class CpuNeighborList::Voxels {
61
public:
62
63
64
65
66
67
68
69
70
71
72
    Voxels(int blockSize, float vsy, float vsz, float miny, float maxy, float minz, float maxz, const RealVec* periodicBoxVectors, bool usePeriodic) :
            blockSize(blockSize), voxelSizeY(vsy), voxelSizeZ(vsz), miny(miny), maxy(maxy), minz(minz), maxz(maxz), periodicBoxVectors(periodicBoxVectors), usePeriodic(usePeriodic) {
        periodicBoxSize[0] = (float) periodicBoxVectors[0][0];
        periodicBoxSize[1] = (float) periodicBoxVectors[1][1];
        periodicBoxSize[2] = (float) periodicBoxVectors[2][2];
        recipBoxSize[0] = (float) (1/periodicBoxVectors[0][0]);
        recipBoxSize[1] = (float) (1/periodicBoxVectors[1][1]);
        recipBoxSize[2] = (float) (1/periodicBoxVectors[2][2]);
        triclinic = (periodicBoxVectors[0][1] != 0.0 || periodicBoxVectors[0][2] != 0.0 ||
                     periodicBoxVectors[1][0] != 0.0 || periodicBoxVectors[1][2] != 0.0 ||
                     periodicBoxVectors[2][0] != 0.0 || periodicBoxVectors[2][1] != 0.0);
73
        if (usePeriodic) {
74
75
76
77
            ny = (int) floorf(periodicBoxVectors[1][1]/voxelSizeY+0.5f);
            nz = (int) floorf(periodicBoxVectors[2][2]/voxelSizeZ+0.5f);
            voxelSizeY = periodicBoxVectors[1][1]/ny;
            voxelSizeZ = periodicBoxVectors[2][2]/nz;
78
79
80
        }
        else {
            ny = max(1, (int) floorf((maxy-miny)/voxelSizeY+0.5f));
81
            nz = max(1, (int) floorf((maxz-minz)/voxelSizeZ+0.5f));
82
83
            if (maxy > miny)
                voxelSizeY = (maxy-miny)/ny;
84
85
            if (maxz > minz)
                voxelSizeZ = (maxz-minz)/nz;
86
        }
87
88
89
90
        bins.resize(ny);
        for (int i = 0; i < ny; i++) {
            bins[i].resize(nz);
            for (int j = 0; j < nz; j++)
91
                bins[i][j].resize(0);
92
93
94
        }
    }

95
96
97
98
    /**
     * Insert a particle into the voxel data structure.
     */
    void insert(const int& atom, const float* location) {
99
        VoxelIndex voxelIndex = getVoxelIndex(location);
100
        bins[voxelIndex.y][voxelIndex.z].push_back(make_pair(location[0], atom));
101
102
103
    }
    
    /**
104
     * Sort the particles in each voxel by x coordinate.
105
106
     */
    void sortItems() {
107
108
        for (int i = 0; i < ny; i++)
            for (int j = 0; j < nz; j++)
109
                sort(bins[i][j].begin(), bins[i][j].end());
110
    }
111
    
112
    /**
113
     * Find the index of the first particle in voxel (y,z) whose x coordinate in >= the specified value.
114
     */
115
116
    int findLowerBound(int y, int z, double x) const {
        const vector<pair<float, int> >& bin = bins[y][z];
117
118
119
120
        int lower = 0;
        int upper = bin.size();
        while (lower < upper) {
            int middle = (lower+upper)/2;
121
            if (bin[middle].first < x)
122
123
124
125
126
127
128
129
                lower = middle+1;
            else
                upper = middle;
        }
        return lower;
    }
    
    /**
130
     * Find the index of the first particle in voxel (y,z) whose x coordinate in greater than the specified value.
131
     */
132
133
    int findUpperBound(int y, int z, double x) const {
        const vector<pair<float, int> >& bin = bins[y][z];
134
135
136
137
        int lower = 0;
        int upper = bin.size();
        while (lower < upper) {
            int middle = (lower+upper)/2;
138
            if (bin[middle].first > x)
139
140
141
142
143
144
                upper = middle;
            else
                lower = middle+1;
        }
        return upper;
    }
145

146
147
148
    /**
     * Get the voxel index containing a particular location.
     */
149
    VoxelIndex getVoxelIndex(const float* location) const {
150
        float yperiodic, zperiodic;
151
        if (!usePeriodic) {
152
            yperiodic = location[1]-miny;
153
            zperiodic = location[2]-minz;
154
155
        }
        else {
156
157
158
159
160
            float scale2 = floorf(location[2]*recipBoxSize[2]);
            yperiodic = location[1]-periodicBoxVectors[2][1]*scale2;
            zperiodic = location[2]-periodicBoxVectors[2][2]*scale2;
            float scale1 = floorf(yperiodic*recipBoxSize[1]);
            yperiodic -= periodicBoxVectors[1][0]*scale1;
161
        }
162
        int y = min(ny-1, int(floorf(yperiodic / voxelSizeY)));
163
        int z = min(nz-1, int(floorf(zperiodic / voxelSizeZ)));
164
        
165
        return VoxelIndex(y, z);
166
    }
167
168
        
    void getNeighbors(vector<int>& neighbors, int blockIndex, const fvec4& blockCenter, const fvec4& blockWidth, const vector<int>& sortedAtoms, vector<char>& exclusions, float maxDistance, const vector<int>& blockAtoms, const vector<float>& blockAtomX, const vector<float>& blockAtomY, const vector<float>& blockAtomZ, const vector<float>& sortedPositions, const vector<VoxelIndex>& atomVoxelIndex) const {
169
170
        neighbors.resize(0);
        exclusions.resize(0);
171
        fvec4 boxSize(periodicBoxSize[0], periodicBoxSize[1], periodicBoxSize[2], 0);
172
173
174
175
176
177
        fvec4 invBoxSize(recipBoxSize[0], recipBoxSize[1], recipBoxSize[2], 0);
        fvec4 periodicBoxVec4[3];
        periodicBoxVec4[0] = fvec4(periodicBoxVectors[0][0], periodicBoxVectors[0][1], periodicBoxVectors[0][2], 0);
        periodicBoxVec4[1] = fvec4(periodicBoxVectors[1][0], periodicBoxVectors[1][1], periodicBoxVectors[1][2], 0);
        periodicBoxVec4[2] = fvec4(periodicBoxVectors[2][0], periodicBoxVectors[2][1], periodicBoxVectors[2][2], 0);

178
        float maxDistanceSquared = maxDistance * maxDistance;
179
180
        float refineCutoff = maxDistance-max(max(blockWidth[0], blockWidth[1]), blockWidth[2]);
        float refineCutoffSquared = refineCutoff*refineCutoff;
181

182
183
        int dIndexY = int((maxDistance+blockWidth[1])/voxelSizeY)+1; // How may voxels away do we have to look?
        int dIndexZ = int((maxDistance+blockWidth[2])/voxelSizeZ)+1;
peastman's avatar
peastman committed
184
185
        if (usePeriodic) {
            dIndexY = min(ny/2, dIndexY);
186
            dIndexZ = min(nz/2, dIndexZ);
peastman's avatar
peastman committed
187
        }
188
189
190
        float centerPos[4];
        blockCenter.store(centerPos);
        VoxelIndex centerVoxelIndex = getVoxelIndex(centerPos);
191
192
193
194
195
196
197

        // Loop over voxels along the z axis.

        int startz = centerVoxelIndex.z-dIndexZ;
        int endz = centerVoxelIndex.z+dIndexZ;
        if (usePeriodic)
            endz = min(endz, startz+nz-1);
198
        else {
199
200
            startz = max(startz, 0);
            endz = min(endz, nz-1);
201
        }
202
        int lastSortedIndex = blockSize*(blockIndex+1);
203
        VoxelIndex voxelIndex(0, 0);
204
205
        for (int z = startz; z <= endz; ++z) {
            voxelIndex.z = z;
206
            if (usePeriodic)
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
                voxelIndex.z = (z < 0 ? z+nz : (z >= nz ? z-nz : z));

            // Loop over voxels along the y axis.

            int boxz = (int) floor((float) z/nz);
            int starty = centerVoxelIndex.y-dIndexY;
            int endy = centerVoxelIndex.y+dIndexY;
            float yoffset = (float) (usePeriodic ? boxz*periodicBoxVectors[2][1] : 0);
            if (usePeriodic) {
                starty -= (int) ceil(yoffset/voxelSizeY);
                endy -= (int) floor(yoffset/voxelSizeY);
                endy = min(endy, starty+ny-1);
            }
            else {
                starty = max(starty, 0);
                endy = min(endy, ny-1);
            }
224
            for (int y = starty; y <= endy; ++y) {
225
226
227
                voxelIndex.y = y;
                if (usePeriodic)
                    voxelIndex.y = (y < 0 ? y+ny : (y >= ny ? y-ny : y));
228
229
                int boxy = (int) floor((float) y/ny);
                float xoffset = (float) (usePeriodic ? boxy*periodicBoxVectors[1][0]+boxz*periodicBoxVectors[2][0] : 0);
230
231
232
233
                
                // Identify the range of atoms within this bin we need to search.  When using periodic boundary
                // conditions, there may be two separate ranges.
                
234
235
                float minx = centerPos[0];
                float maxx = centerPos[0];
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                float offset[3] = {-xoffset, -yoffset+voxelSizeY*y+(usePeriodic ? 0.0f : miny), voxelSizeZ*z+(usePeriodic ? 0.0f : minz)};
                for (int k = 0; k < (int) blockAtoms.size(); k += 4) {
                    fvec4 dist2 = maxDistanceSquared;
                    if (y != atomVoxelIndex[k].y) {
                        fvec4 dy1 = offset[1]-fvec4(&blockAtomY[k]);
                        fvec4 dy2 = dy1+voxelSizeY;
                        if (usePeriodic) {
                            dy1 -= round(dy1*invBoxSize[1])*boxSize[1];
                            dy2 -= round(dy2*invBoxSize[1])*boxSize[1];
                        }
                        fvec4 dy = min(abs(dy1), abs(dy2));
                        dist2 -= dy*dy;
                    }
                    if (z != atomVoxelIndex[k].z) {
                        fvec4 dz1 = offset[2]-fvec4(&blockAtomZ[k]);
                        fvec4 dz2 = dz1+voxelSizeZ;
                        if (usePeriodic) {
                            dz1 -= round(dz1*invBoxSize[2])*boxSize[2];
                            dz2 -= round(dz2*invBoxSize[2])*boxSize[2];
                        }
                        fvec4 dz = min(abs(dz1), abs(dz2));
                        dist2 -= dz*dz;
258
                    }
259
260
261
262
263
                    fvec4 dist = sqrt(dist2);
                    int numToCheck = min(4, (int) (blockAtoms.size()-k));
                    for (int m = 0; m < numToCheck; m++) {
                        minx = min(minx, blockAtomX[k+m]-dist[m]-xoffset);
                        maxx = max(maxx, blockAtomX[k+m]+dist[m]-xoffset);
264
265
                    }
                }
266
                if (minx == maxx)
peastman's avatar
peastman committed
267
                    continue;
268
269
270
271
                bool needPeriodic = (centerPos[1]-blockWidth[1] < maxDistance || centerPos[1]+blockWidth[1] > periodicBoxSize[1]-maxDistance ||
                                     centerPos[2]-blockWidth[2] < maxDistance || centerPos[2]+blockWidth[2] > periodicBoxSize[2]-maxDistance ||
                                     minx < 0.0f || maxx > periodicBoxVectors[0][0]);
                int numRanges;
272
273
                int rangeStart[2];
                int rangeEnd[2];
274
                rangeStart[0] = findLowerBound(voxelIndex.y, voxelIndex.z, minx);
275
276
                if (needPeriodic) {
                    numRanges = 2;
277
                    rangeEnd[0] = findUpperBound(voxelIndex.y, voxelIndex.z, maxx);
278
279
                    if (rangeStart[0] > 0) {
                        rangeStart[1] = 0;
280
                        rangeEnd[1] = min(findUpperBound(voxelIndex.y, voxelIndex.z, maxx-periodicBoxSize[0]), rangeStart[0]);
281
282
                    }
                    else {
283
284
                        rangeStart[1] = max(findLowerBound(voxelIndex.y, voxelIndex.z, minx+periodicBoxSize[0]), rangeEnd[0]);
                        rangeEnd[1] = bins[voxelIndex.y][voxelIndex.z].size();
285
286
                    }
                }
287
288
                else {
                    numRanges = 1;
289
                    rangeEnd[0] = findUpperBound(voxelIndex.y, voxelIndex.z, maxx);
290
                }
291
                bool periodicRectangular = (needPeriodic && !triclinic);
292
293
294
295
296
                
                // Loop over atoms and check to see if they are neighbors of this block.
                
                for (int range = 0; range < numRanges; range++) {
                    for (int item = rangeStart[range]; item < rangeEnd[range]; item++) {
297
                        const int sortedIndex = bins[voxelIndex.y][voxelIndex.z][item].second;
298

peastman's avatar
peastman committed
299
                        // Avoid duplicate entries.
300
                        if (sortedIndex >= lastSortedIndex)
301
                            continue;
302
                        
303
                        fvec4 atomPos(&sortedPositions[4*sortedIndex]);
304
                        fvec4 delta = atomPos-centerPos;
305
306
                        if (periodicRectangular)
                            delta -= round(delta*invBoxSize)*boxSize;
307
308
309
310
311
                        else if (needPeriodic) {
                            delta -= periodicBoxVec4[2]*floorf(delta[2]*recipBoxSize[2]+0.5f);
                            delta -= periodicBoxVec4[1]*floorf(delta[1]*recipBoxSize[1]+0.5f);
                            delta -= periodicBoxVec4[0]*floorf(delta[0]*recipBoxSize[0]+0.5f);
                        }
312
                        delta = max(0.0f, abs(delta)-blockWidth);
313
                        float dSquared = dot3(delta, delta);
314
315
                        if (dSquared > maxDistanceSquared)
                            continue;
peastman's avatar
peastman committed
316
                        
317
318
319
320
                        if (dSquared > refineCutoffSquared) {
                            // The distance is large enough that there might not be any actual interactions.
                            // Check individual atom pairs to be sure.
                            
321
322
323
324
325
                            bool anyInteraction = false;
                            for (int k = 0; k < (int) blockAtoms.size(); k += 4) {
                                fvec4 dx = fvec4(&blockAtomX[k])-atomPos[0];
                                fvec4 dy = fvec4(&blockAtomY[k])-atomPos[1];
                                fvec4 dz = fvec4(&blockAtomZ[k])-atomPos[2];
326
                                if (periodicRectangular) {
327
328
329
                                    dx -= round(dx*invBoxSize[0])*boxSize[0];
                                    dy -= round(dy*invBoxSize[1])*boxSize[1];
                                    dz -= round(dz*invBoxSize[2])*boxSize[2];
330
                                }
331
                                else if (needPeriodic) {
332
333
334
335
336
337
338
339
340
                                    fvec4 scale3 = floor(dz*recipBoxSize[2]+0.5f);
                                    dx -= scale3*periodicBoxVectors[2][0];
                                    dy -= scale3*periodicBoxVectors[2][1];
                                    dz -= scale3*periodicBoxVectors[2][2];
                                    fvec4 scale2 = floor(dy*recipBoxSize[1]+0.5f);
                                    dx -= scale2*periodicBoxVectors[1][0];
                                    dy -= scale2*periodicBoxVectors[1][1];
                                    fvec4 scale1 = floor(dx*recipBoxSize[0]+0.5f);
                                    dx -= scale1*periodicBoxVectors[0][0];
341
                                }
342
343
344
                                fvec4 r2 = dx*dx + dy*dy + dz*dz;
                                if (any(r2 < maxDistanceSquared)) {
                                    anyInteraction = true;
345
346
347
                                    break;
                                }
                            }
348
                            if (!anyInteraction)
349
350
351
352
353
354
                                continue;
                        }
                        
                        // Add this atom to the list of neighbors.
                        
                        neighbors.push_back(sortedAtoms[sortedIndex]);
355
                        if (sortedIndex < blockSize*blockIndex)
356
                            exclusions.push_back(0);
357
358
359
360
                        else {
                            int mask = (1<<blockSize)-1;
                            exclusions.push_back(mask & (mask<<(sortedIndex-blockSize*blockIndex)));
                        }
361
362
363
364
365
366
367
                    }
                }
            }
        }
    }

private:
368
    int blockSize;
369
370
371
372
373
374
    float voxelSizeY, voxelSizeZ;
    float miny, maxy, minz, maxz;
    int ny, nz;
    float periodicBoxSize[3], recipBoxSize[3];
    bool triclinic;
    const RealVec* periodicBoxVectors;
375
    const bool usePeriodic;
peastman's avatar
peastman committed
376
    vector<vector<vector<pair<float, int> > > > bins;
377
378
};

379
class CpuNeighborList::ThreadTask : public ThreadPool::Task {
380
public:
381
382
383
384
    ThreadTask(CpuNeighborList& owner) : owner(owner) {
    }
    void execute(ThreadPool& threads, int threadIndex) {
        owner.threadComputeNeighborList(threads, threadIndex);
385
386
387
    }
    CpuNeighborList& owner;
};
388

389
CpuNeighborList::CpuNeighborList(int blockSize) : blockSize(blockSize) {
390
}
391

392
393
void CpuNeighborList::computeNeighborList(int numAtoms, const AlignedArray<float>& atomLocations, const vector<set<int> >& exclusions,
            const RealVec* periodicBoxVectors, bool usePeriodic, float maxDistance, ThreadPool& threads) {
394
    int numBlocks = (numAtoms+blockSize-1)/blockSize;
395
396
397
    blockNeighbors.resize(numBlocks);
    blockExclusions.resize(numBlocks);
    sortedAtoms.resize(numAtoms);
398
    sortedPositions.resize(4*numAtoms);
399
    
400
    // Record the parameters for the threads.
401
    
402
403
    this->exclusions = &exclusions;
    this->atomLocations = &atomLocations[0];
404
405
406
    this->periodicBoxVectors[0] = periodicBoxVectors[0];
    this->periodicBoxVectors[1] = periodicBoxVectors[1];
    this->periodicBoxVectors[2] = periodicBoxVectors[2];
407
408
409
410
411
412
413
414
    this->numAtoms = numAtoms;
    this->usePeriodic = usePeriodic;
    this->maxDistance = maxDistance;
    
    // Identify the range of atom positions along each axis.
    
    fvec4 minPos(&atomLocations[0]);
    fvec4 maxPos = minPos;
415
    for (int i = 0; i < numAtoms; i++) {
416
417
418
        fvec4 pos(&atomLocations[4*i]);
        minPos = min(minPos, pos);
        maxPos = max(maxPos, pos);
419
    }
420
421
422
423
424
425
426
427
428
429
    minx = minPos[0];
    maxx = maxPos[0];
    miny = minPos[1];
    maxy = maxPos[1];
    minz = minPos[2];
    maxz = maxPos[2];
    
    // Sort the atoms based on a Hilbert curve.
    
    atomBins.resize(numAtoms);
430
431
432
    ThreadTask task(*this);
    threads.execute(task);
    threads.waitForThreads();
433
434
    sort(atomBins.begin(), atomBins.end());

435
    // Build the voxel hash.
436

437
    float edgeSizeY, edgeSizeZ;
438
    if (!usePeriodic)
439
        edgeSizeY = edgeSizeZ = maxDistance; // TODO - adjust this as needed
440
    else {
441
442
        edgeSizeY = 0.6f*periodicBoxVectors[1][1]/floorf(periodicBoxVectors[1][1]/maxDistance);
        edgeSizeZ = 0.6f*periodicBoxVectors[2][2]/floorf(periodicBoxVectors[2][2]/maxDistance);
443
    }
444
    Voxels voxels(blockSize, edgeSizeY, edgeSizeZ, miny, maxy, minz, maxz, periodicBoxVectors, usePeriodic);
445
446
447
    for (int i = 0; i < numAtoms; i++) {
        int atomIndex = atomBins[i].second;
        sortedAtoms[i] = atomIndex;
448
449
        fvec4 atomPos(&atomLocations[4*atomIndex]);
        atomPos.store(&sortedPositions[4*i]);
450
        voxels.insert(i, &atomLocations[4*atomIndex]);
451
    }
452
453
    voxels.sortItems();
    this->voxels = &voxels;
454

455
456
    // Signal the threads to start running and wait for them to finish.
    
457
458
    threads.resumeThreads();
    threads.waitForThreads();
459
    
460
    // Add padding atoms to fill up the last block.
461
    
462
    int numPadding = numBlocks*blockSize-numAtoms;
463
    if (numPadding > 0) {
464
        char mask = ((0xFFFF-(1<<blockSize)+1) >> numPadding);
465
466
467
468
469
470
        for (int i = 0; i < numPadding; i++)
            sortedAtoms.push_back(0);
        vector<char>& exc = blockExclusions[blockExclusions.size()-1];
        for (int i = 0; i < (int) exc.size(); i++)
            exc[i] |= mask;
    }
471
472
}

473
int CpuNeighborList::getNumBlocks() const {
474
    return sortedAtoms.size()/blockSize;
475
476
}

477
478
479
480
481
482
483
484
485
486
487
488
489
const std::vector<int>& CpuNeighborList::getSortedAtoms() const {
    return sortedAtoms;
}

const std::vector<int>& CpuNeighborList::getBlockNeighbors(int blockIndex) const {
    return blockNeighbors[blockIndex];
}

const std::vector<char>& CpuNeighborList::getBlockExclusions(int blockIndex) const {
    return blockExclusions[blockIndex];
    
}

490
491
void CpuNeighborList::threadComputeNeighborList(ThreadPool& threads, int threadIndex) {
    // Compute the positions of atoms along the Hilbert curve.
492

493
494
495
496
497
498
499
500
501
502
503
504
505
    float binWidth = max(max(maxx-minx, maxy-miny), maxz-minz)/255.0f;
    float invBinWidth = 1.0f/binWidth;
    bitmask_t coords[3];
    int numThreads = threads.getNumThreads();
    for (int i = threadIndex; i < numAtoms; i += numThreads) {
        const float* pos = &atomLocations[4*i];
        coords[0] = (bitmask_t) ((pos[0]-minx)*invBinWidth);
        coords[1] = (bitmask_t) ((pos[1]-miny)*invBinWidth);
        coords[2] = (bitmask_t) ((pos[2]-minz)*invBinWidth);
        int bin = (int) hilbert_c2i(3, 8, coords);
        atomBins[i] = pair<int, int>(bin, i);
    }
    threads.syncThreads();
506

507
508
509
510
    // Compute this thread's subset of neighbors.

    int numBlocks = blockNeighbors.size();
    vector<int> blockAtoms;
511
    vector<float> blockAtomX(blockSize), blockAtomY(blockSize), blockAtomZ(blockSize);
512
    vector<VoxelIndex> atomVoxelIndex;
513
514
515
    for (int i = threadIndex; i < numBlocks; i += numThreads) {
        // Find the atoms in this block and compute their bounding box.
        
516
517
        int firstIndex = blockSize*i;
        int atomsInBlock = min(blockSize, numAtoms-firstIndex);
518
        blockAtoms.resize(atomsInBlock);
519
520
        atomVoxelIndex.resize(atomsInBlock);
        for (int j = 0; j < atomsInBlock; j++) {
521
            blockAtoms[j] = sortedAtoms[firstIndex+j];
522
523
            atomVoxelIndex[j] = voxels->getVoxelIndex(&atomLocations[4*blockAtoms[j]]);
        }
524
        fvec4 minPos(&sortedPositions[4*firstIndex]);
525
526
        fvec4 maxPos = minPos;
        for (int j = 1; j < atomsInBlock; j++) {
527
            fvec4 pos(&sortedPositions[4*(firstIndex+j)]);
528
529
            minPos = min(minPos, pos);
            maxPos = max(maxPos, pos);
530
        }
531
532
533
534
535
536
537
538
539
540
541
        for (int j = 0; j < atomsInBlock; j++) {
            blockAtomX[j] = sortedPositions[4*(firstIndex+j)];
            blockAtomY[j] = sortedPositions[4*(firstIndex+j)+1];
            blockAtomZ[j] = sortedPositions[4*(firstIndex+j)+2];
        }
        for (int j = atomsInBlock; j < blockSize; j++) {
            blockAtomX[j] = 1e10;
            blockAtomY[j] = 1e10;
            blockAtomZ[j] = 1e10;
        }
        voxels->getNeighbors(blockNeighbors[i], i, (maxPos+minPos)*0.5f, (maxPos-minPos)*0.5f, sortedAtoms, blockExclusions[i], maxDistance, blockAtoms, blockAtomX, blockAtomY, blockAtomZ, sortedPositions, atomVoxelIndex);
542

543
        // Record the exclusions for this block.
544

545
546
547
548
549
550
551
552
553
        for (int j = 0; j < atomsInBlock; j++) {
            const set<int>& atomExclusions = (*exclusions)[sortedAtoms[firstIndex+j]];
            char mask = 1<<j;
            for (int k = 0; k < (int) blockNeighbors[i].size(); k++) {
                int atomIndex = blockNeighbors[i][k];
                if (atomExclusions.find(atomIndex) != atomExclusions.end())
                    blockExclusions[i][k] |= mask;
            }
        }
554
555
556
    }
}

557
} // namespace OpenMM