"plugins/drude/serialization/vscode:/vscode.git/clone" did not exist on "f6431a42ac7597f33d017a585854bea0159f3b85"
nonbonded_default.cl 9.08 KB
Newer Older
1
#define TILE_SIZE 32
2

3
typedef struct {
4
5
6
    float x, y, z;
    float q;
    float fx, fy, fz;
7
8
9
    ATOM_PARAMETER_DATA
} AtomData;

10
11
12
13
/**
 * Compute nonbonded interactions.
 */

14
15
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
void computeNonbonded(__global float4* forceBuffers, __global float* energyBuffer, __global float4* posq, __global unsigned int* exclusions,
16
        __global unsigned int* exclusionIndices, __global unsigned int* exclusionRowIndices, __local AtomData* localData, __local float4* tempBuffer,
17
#ifdef USE_CUTOFF
18
        __global ushort2* tiles, __global unsigned int* interactionFlags, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize
19
20
21
22
23
24
25
26
27
28
29
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
    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);
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
30
31
    __local unsigned int exclusionRange[2];
    __local int exclusionIndex[1];
32
33
34

    while (pos < end) {
        // Extract the coordinates of this tile
35
#ifdef USE_CUTOFF
36
37
38
        ushort2 tileIndices = tiles[pos];
        unsigned int x = tileIndices.x;
        unsigned int y = tileIndices.y;
39
40
41
42
43
44
45
46
#else
        unsigned int y = (unsigned int) floor(NUM_BLOCKS+0.5f-sqrt((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
        unsigned int x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
        if (x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
            y++;
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
        }
#endif
47
48
49
        unsigned int baseLocalAtom = (get_local_id(0) < TILE_SIZE ? 0 : TILE_SIZE/2);
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
        unsigned int forceBufferOffset = (tgx < TILE_SIZE/2 ? 0 : TILE_SIZE);
50
        unsigned int atom1 = x*TILE_SIZE + tgx;
51
52
53
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        LOAD_ATOM1_PARAMETERS
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

        // Locate the exclusion data for this tile.

#ifdef USE_EXCLUSIONS
        if (get_local_id(0) < 2)
            exclusionRange[get_local_id(0)] = exclusionRowIndices[x+get_local_id(0)];
        if (tgx == 0)
            exclusionIndex[0] = -1;
        barrier(CLK_LOCAL_MEM_FENCE);
        for (int i = exclusionRange[0]+tgx; i < exclusionRange[1]; i += TILE_SIZE)
            if (exclusionIndices[i] == y)
                exclusionIndex[0] = i*TILE_SIZE;
        barrier(CLK_LOCAL_MEM_FENCE);
        bool hasExclusions = (exclusionIndex[0] > -1);
#endif
69
70
71
        if (x == y) {
            // This tile is on the diagonal.

72
73
74
75
            localData[get_local_id(0)].x = posq1.x;
            localData[get_local_id(0)].y = posq1.y;
            localData[get_local_id(0)].z = posq1.z;
            localData[get_local_id(0)].q = posq1.w;
76
77
78
            LOAD_LOCAL_PARAMETERS_FROM_1
            barrier(CLK_LOCAL_MEM_FENCE);
#ifdef USE_EXCLUSIONS
79
            unsigned int excl = exclusions[exclusionIndex[0]+tgx] >> baseLocalAtom;
80
#endif
81
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
82
83
84
#ifdef USE_EXCLUSIONS
                bool isExcluded = !(excl & 0x1);
#endif
85
86
                int atom2 = baseLocalAtom+j;
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
87
88
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
#ifdef USE_PERIODIC
89
90
91
                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;
92
93
#endif
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
94
95
                float invR = RSQRT(r2);
                float r = RECIP(invR);
96
                LOAD_ATOM2_PARAMETERS
97
                atom2 = y*TILE_SIZE+baseLocalAtom+j;
98
#ifdef USE_SYMMETRIC
99
                float dEdR = 0.0f;
100
101
102
103
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
104
105
106
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += 0.5f*tempEnergy;
107
108
109
110
111
#ifdef USE_SYMMETRIC
                force.xyz -= delta.xyz*dEdR;
#else
                force.xyz -= dEdR1.xyz;
#endif
112
113
114
115
116
                excl >>= 1;
            }

            // Sum the forces and write results.

117
            if (get_local_id(0) >= TILE_SIZE)
118
119
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
120
            if (get_local_id(0) < TILE_SIZE) {
121
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
122
                unsigned int offset = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
123
#else
124
                unsigned int offset = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
125
#endif
126
                forceBuffers[offset].xyz = forceBuffers[offset].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
127
128
129
130
131
            }
        }
        else {
            // This is an off-diagonal tile.

132
            if (lasty != y && get_local_id(0) < TILE_SIZE) {
133
                unsigned int j = y*TILE_SIZE + tgx;
134
                float4 tempPosq = posq[j];
135
136
137
138
                localData[get_local_id(0)].x = tempPosq.x;
                localData[get_local_id(0)].y = tempPosq.y;
                localData[get_local_id(0)].z = tempPosq.z;
                localData[get_local_id(0)].q = tempPosq.w;
139
140
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
141
142
143
            localData[get_local_id(0)].fx = 0.0f;
            localData[get_local_id(0)].fy = 0.0f;
            localData[get_local_id(0)].fz = 0.0f;
144
145
            barrier(CLK_LOCAL_MEM_FENCE);

146
147
            // Compute the full set of interactions in this tile.

148
#ifdef USE_EXCLUSIONS
149
            unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[0]+tgx] : 0xFFFFFFFF);
150
151
            excl = (excl >> baseLocalAtom) & 0xFFFF;
            excl += excl << 16;
152
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
153
#endif
154
155
            unsigned int tj = tgx%(TILE_SIZE/2);
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
156
#ifdef USE_EXCLUSIONS
157
                bool isExcluded = !(excl & 0x1);
158
#endif
159
160
                int atom2 = baseLocalAtom+tj;
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
161
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
162
#ifdef USE_PERIODIC
163
164
165
                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;
166
#endif
167
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
168
169
                float invR = RSQRT(r2);
                float r = RECIP(invR);
170
                LOAD_ATOM2_PARAMETERS
171
                atom2 = y*TILE_SIZE+baseLocalAtom+tj;
172
#ifdef USE_SYMMETRIC
173
                float dEdR = 0.0f;
174
175
176
177
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
178
179
180
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += tempEnergy;
181
#ifdef USE_SYMMETRIC
182
183
                delta.xyz *= dEdR;
                force.xyz -= delta.xyz;
184
185
186
                localData[baseLocalAtom+tj+forceBufferOffset].fx += delta.x;
                localData[baseLocalAtom+tj+forceBufferOffset].fy += delta.y;
                localData[baseLocalAtom+tj+forceBufferOffset].fz += delta.z;
187
188
189
190
191
192
#else
                force.xyz -= dEdR1.xyz;
                localData[baseLocalAtom+tj+forceBufferOffset].fx += dEdR2.x;
                localData[baseLocalAtom+tj+forceBufferOffset].fy += dEdR2.y;
                localData[baseLocalAtom+tj+forceBufferOffset].fz += dEdR2.z;
#endif
193
194
195
                barrier(CLK_LOCAL_MEM_FENCE);
                excl >>= 1;
                tj = (tj+1)%(TILE_SIZE/2);
196
197
198
199
            }

            // Sum the forces and write results.

200
            if (get_local_id(0) >= TILE_SIZE)
201
202
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
203
            if (get_local_id(0) < TILE_SIZE) {
204
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
205
206
                unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
                unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
207
#else
208
209
                unsigned int offset1 = x*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                unsigned int offset2 = y*TILE_SIZE + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
210
#endif
211
                forceBuffers[offset1].xyz = forceBuffers[offset1].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
212
                float4 sum = (float4) (localData[get_local_id(0)].fx+localData[get_local_id(0)+TILE_SIZE].fx, localData[get_local_id(0)].fy+localData[get_local_id(0)+TILE_SIZE].fy, localData[get_local_id(0)].fz+localData[get_local_id(0)+TILE_SIZE].fz, 0.0f);
213
                forceBuffers[offset2].xyz = forceBuffers[offset2].xyz+sum.xyz;
214
215
            }
        }
216
        lasty = y;
217
218
219
220
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}