customNonbondedGroups.cc 8.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
typedef struct {
    real x, y, z;
    real q;
    real fx, fy, fz;
    ATOM_PARAMETER_DATA
#ifndef PARAMETER_SIZE_IS_EVEN
    real padding;
#endif
} AtomData;

11
12
13
14
/**
 * Find the maximum of a value across all threads in a warp, and return that to
 * every thread.
 */
15
16
17
18
19
20
DEVICE int reduceMax(int val, LOCAL_ARG int* temp) {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700
    // CUDA lets us do this slightly more efficiently by using shuffle operations.
    for (int mask = 16; mask > 0; mask /= 2)
        val = max(val, __shfl_xor_sync(0xffffffff, val, mask));
    return val;
21
22
23
24
#elif defined(USE_HIP)
    for (int mask = 16; mask > 0; mask /= 2)
        val = max(val, __shfl_xor(val, mask, 32));
    return val;
25
26
27
#else
    int indexInWarp = LOCAL_ID%32;
    temp[LOCAL_ID] = val;
28
29
    SYNC_WARPS;
    for (int offset = 16; offset > 0; offset /= 2) {
peastman's avatar
peastman committed
30
        if (indexInWarp < offset)
31
            temp[LOCAL_ID] = max(temp[LOCAL_ID], temp[LOCAL_ID+offset]);
32
33
        SYNC_WARPS;
    }
34
35
    return temp[LOCAL_ID-indexInWarp];
#endif
36
37
}

38
39
40
41
KERNEL void computeInteractionGroups(
        GLOBAL mm_ulong* RESTRICT forceBuffers,
        GLOBAL mixed* RESTRICT energyBuffer, GLOBAL const real4* RESTRICT posq, GLOBAL const int4* RESTRICT groupData,
        GLOBAL const int* RESTRICT numGroupTiles, int useNeighborList,
42
        real4 periodicBoxSize, real4 invPeriodicBoxSize, real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ
43
        PARAMETER_ARGUMENTS) {
44
45
46
47
    const unsigned int totalWarps = GLOBAL_SIZE/TILE_SIZE;
    const unsigned int warp = GLOBAL_ID/TILE_SIZE; // global warpIndex
    const unsigned int tgx = LOCAL_ID & (TILE_SIZE-1); // index within the warp
    const unsigned int tbx = LOCAL_ID - tgx;           // block warpIndex
48
    mixed energy = 0;
49
    INIT_DERIVATIVES
50
51
    LOCAL AtomData localData[LOCAL_MEMORY_SIZE];
    LOCAL int reductionBuffer[LOCAL_MEMORY_SIZE];
52

53
54
55
    mm_ulong wl = warp;
    const unsigned int startTile = (unsigned int) (useNeighborList ? wl*numGroupTiles[0]/totalWarps : FIRST_TILE+wl*(LAST_TILE-FIRST_TILE)/totalWarps);
    const unsigned int endTile = (unsigned int) (useNeighborList ? (wl+1)*numGroupTiles[0]/totalWarps : FIRST_TILE+(wl+1)*(LAST_TILE-FIRST_TILE)/totalWarps);
56
57
58
59
60
61
62
63
64
    for (int tile = startTile; tile < endTile; tile++) {
        const int4 atomData = groupData[TILE_SIZE*tile+tgx];
        const int atom1 = atomData.x;
        const int atom2 = atomData.y;
        const int rangeStart = atomData.z&0xFFFF;
        const int rangeEnd = (atomData.z>>16)&0xFFFF;
        const int exclusions = atomData.w;
        real4 posq1 = posq[atom1];
        LOAD_ATOM1_PARAMETERS
65
        real3 force = make_real3(0);
66
        real4 posq2 = posq[atom2];
67
68
69
70
        localData[LOCAL_ID].x = posq2.x;
        localData[LOCAL_ID].y = posq2.y;
        localData[LOCAL_ID].z = posq2.z;
        localData[LOCAL_ID].q = posq2.w;
71
        LOAD_LOCAL_PARAMETERS
72
73
74
        localData[LOCAL_ID].fx = 0.0f;
        localData[LOCAL_ID].fy = 0.0f;
        localData[LOCAL_ID].fz = 0.0f;
75
        int tj = tgx;
76
        int rangeStop = rangeStart + reduceMax(rangeEnd-rangeStart, reductionBuffer);
77
        SYNC_WARPS;
78
79
        for (int j = rangeStart; j < rangeStop; j++) {
            if (j < rangeEnd) {
peastman's avatar
peastman committed
80
81
                bool isExcluded = (((exclusions>>tj)&1) == 0);
                int localIndex = tbx+tj;
82
83
                posq2 = make_real4(localData[localIndex].x, localData[localIndex].y, localData[localIndex].z, localData[localIndex].q);
                real3 delta = make_real3(posq2.x-posq1.x, posq2.y-posq1.y, posq2.z-posq1.z);
84
#ifdef USE_PERIODIC
85
                APPLY_PERIODIC_TO_DELTA(delta)
86
#endif
peastman's avatar
peastman committed
87
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
88
#ifdef USE_CUTOFF
peastman's avatar
peastman committed
89
                if (!isExcluded && r2 < CUTOFF_SQUARED) {
90
#endif
peastman's avatar
peastman committed
91
                    real invR = RSQRT(r2);
peastman's avatar
peastman committed
92
                    real r = r2*invR;
peastman's avatar
peastman committed
93
94
95
                    LOAD_ATOM2_PARAMETERS
                    real dEdR = 0.0f;
                    real tempEnergy = 0.0f;
96
                    const real interactionScale = 1.0f;
peastman's avatar
peastman committed
97
98
99
                    COMPUTE_INTERACTION
                    energy += tempEnergy;
                    delta *= dEdR;
100
101
102
                    force.x -= delta.x;
                    force.y -= delta.y;
                    force.z -= delta.z;
peastman's avatar
peastman committed
103
104
105
                    localData[localIndex].fx += delta.x;
                    localData[localIndex].fy += delta.y;
                    localData[localIndex].fz += delta.z;
106
#ifdef USE_CUTOFF
peastman's avatar
peastman committed
107
                }
108
#endif
109
                tj = (tj == rangeEnd-1 ? rangeStart : tj+1);
peastman's avatar
peastman committed
110
            }
111
112
113
            SYNC_WARPS;
        }
        if (exclusions != 0) {
114
115
116
            ATOMIC_ADD(&forceBuffers[atom1], (mm_ulong) realToFixedPoint(force.x));
            ATOMIC_ADD(&forceBuffers[atom1+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force.y));
            ATOMIC_ADD(&forceBuffers[atom1+2*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force.z));
117
        }
118
119
120
        ATOMIC_ADD(&forceBuffers[atom2], (mm_ulong) realToFixedPoint(localData[LOCAL_ID].fx));
        ATOMIC_ADD(&forceBuffers[atom2+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(localData[LOCAL_ID].fy));
        ATOMIC_ADD(&forceBuffers[atom2+2*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(localData[LOCAL_ID].fz));
121
        SYNC_WARPS;
122
    }
123
    energyBuffer[GLOBAL_ID] += energy;
124
    SAVE_DERIVATIVES
125
}
126
127
128
129
130

/**
 * If the neighbor list needs to be rebuilt, reset the number of tiles to 0.  This is
 * executed by a single thread.
 */
131
KERNEL void prepareToBuildNeighborList(GLOBAL int* RESTRICT rebuildNeighborList, GLOBAL int* RESTRICT numGroupTiles) {
132
133
134
135
136
137
138
139
    if (rebuildNeighborList[0] == 1)
        numGroupTiles[0] = 0;
}

/**
 * Filter the list of tiles to include only ones that have interactions within the
 * padded cutoff.
 */
140
141
KERNEL void buildNeighborList(GLOBAL int* RESTRICT rebuildNeighborList, GLOBAL int* RESTRICT numGroupTiles,
        GLOBAL const real4* RESTRICT posq, GLOBAL const int4* RESTRICT groupData, GLOBAL int4* RESTRICT filteredGroupData,
142
143
144
145
146
147
148
        real4 periodicBoxSize, real4 invPeriodicBoxSize, real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ) {
    
    // If the neighbor list doesn't need to be rebuilt on this step, return immediately.
    
    if (rebuildNeighborList[0] == 0)
        return;

149
150
151
152
153
154
155
156
157
    const unsigned int totalWarps = GLOBAL_SIZE/TILE_SIZE;
    const unsigned int warp = GLOBAL_ID/TILE_SIZE; // global warpIndex
    const unsigned int local_warp = LOCAL_ID/TILE_SIZE; // local warpIndex
    const unsigned int tgx = LOCAL_ID & (TILE_SIZE-1); // index within the warp
    const unsigned int tbx = LOCAL_ID - tgx;           // block warpIndex
    LOCAL real4 localPos[LOCAL_MEMORY_SIZE];
    LOCAL volatile bool anyInteraction[WARPS_IN_BLOCK];
    LOCAL volatile int tileIndex[WARPS_IN_BLOCK];
    LOCAL int reductionBuffer[LOCAL_MEMORY_SIZE];
158

159
160
    const unsigned int startTile = (unsigned int) (warp*(mm_ulong)NUM_TILES/totalWarps);
    const unsigned int endTile = (unsigned int) ((warp+1)*(mm_ulong)NUM_TILES/totalWarps);
161
162
163
164
165
166
167
168
    for (int tile = startTile; tile < endTile; tile++) {
        const int4 atomData = groupData[TILE_SIZE*tile+tgx];
        const int atom1 = atomData.x;
        const int atom2 = atomData.y;
        const int rangeStart = atomData.z&0xFFFF;
        const int rangeEnd = (atomData.z>>16)&0xFFFF;
        const int exclusions = atomData.w;
        real4 posq1 = posq[atom1];
169
        localPos[LOCAL_ID] = posq[atom2];
170
171
172
        if (tgx == 0)
            anyInteraction[local_warp] = false;
        int tj = tgx;
173
        int rangeStop = rangeStart + reduceMax(rangeEnd-rangeStart, reductionBuffer);
174
        SYNC_WARPS;
175
        for (int j = rangeStart; j < rangeStop && !anyInteraction[local_warp]; j++) {
peastman's avatar
peastman committed
176
            SYNC_WARPS;
177
            if (j < rangeEnd && tj < rangeEnd) {
178
179
                bool isExcluded = (((exclusions>>tj)&1) == 0);
                int localIndex = tbx+tj;
180
                real3 delta = make_real3(localPos[localIndex].x-posq1.x, localPos[localIndex].y-posq1.y, localPos[localIndex].z-posq1.z);
181
182
183
184
185
186
187
188
189
190
191
#ifdef USE_PERIODIC
                APPLY_PERIODIC_TO_DELTA(delta)
#endif
                real r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                if (!isExcluded && r2 < PADDED_CUTOFF_SQUARED)
                    anyInteraction[local_warp] = true;
            }
            tj = (tj == rangeEnd-1 ? rangeStart : tj+1);
            SYNC_WARPS;
        }
        if (anyInteraction[local_warp]) {
peastman's avatar
peastman committed
192
            SYNC_WARPS;
193
            if (tgx == 0)
194
                tileIndex[local_warp] = ATOMIC_ADD(numGroupTiles, 1);
195
196
197
198
199
            SYNC_WARPS;
            filteredGroupData[TILE_SIZE*tileIndex[local_warp]+tgx] = atomData;
        }
    }
}