nonbonded_nvidia.cl 10.3 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, __local AtomData* localData, __local float4* tempBuffer, __global unsigned int* tiles,
17
#ifdef USE_CUTOFF
18
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount
19
20
#else
        unsigned int numTiles
21
#endif
22
        PARAMETER_ARGUMENTS) {
23
24
25
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
26
27
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
28
29
30
31
32
33
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
34
        // Extract the coordinates of this tile
35
        unsigned int x = tiles[pos];
36
        unsigned int y = ((x >> 2) & 0x7fff)*TILE_SIZE;
37
        bool hasExclusions = (x & 0x1);
38
39
        x = (x>>17)*TILE_SIZE;
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
40
        unsigned int tbx = get_local_id(0) - tgx;
41
        unsigned int atom1 = x + tgx;
42
        float4 force = 0.0f;
43
        float4 posq1 = posq[atom1];
44
        LOAD_ATOM1_PARAMETERS
45
        if (x == y) {
46
            // This tile is on the diagonal.
47

48
49
50
51
            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;
52
            LOAD_LOCAL_PARAMETERS_FROM_1
53
54
            unsigned int xi = x/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-xi*(xi+1)/2;
55
#ifdef USE_EXCLUSIONS
56
            unsigned int excl = exclusions[exclusionIndices[tile]+tgx];
57
#endif
58
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
59
#ifdef USE_EXCLUSIONS
60
                bool isExcluded = !(excl & 0x1);
61
#endif
62
63
                int atom2 = tbx+j;
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
64
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
65
#ifdef USE_PERIODIC
66
67
68
                delta.x -= floor(delta.x*INV_PERIODIC_BOX_SIZE_X+0.5f)*PERIODIC_BOX_SIZE_X;
                delta.y -= floor(delta.y*INV_PERIODIC_BOX_SIZE_Y+0.5f)*PERIODIC_BOX_SIZE_Y;
                delta.z -= floor(delta.z*INV_PERIODIC_BOX_SIZE_Z+0.5f)*PERIODIC_BOX_SIZE_Z;
69
#endif
70
71
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                float r = sqrt(r2);
72
                float invR = 1.0f/r;
73
                LOAD_ATOM2_PARAMETERS
74
                atom2 = y+j;
75
#ifdef USE_SYMMETRIC
76
                float dEdR = 0.0f;
77
78
79
80
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
81
82
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
83
                energy += 0.5f*tempEnergy;
84
85
86
87
88
#ifdef USE_SYMMETRIC
                force.xyz -= delta.xyz*dEdR;
#else
                force.xyz -= dEdR1.xyz;
#endif
89
                excl >>= 1;
90
91
92
            }

            // Write results
93
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
94
            unsigned int offset = x + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
95
#else
96
            unsigned int offset = x + tgx + warp*PADDED_NUM_ATOMS;
97
#endif
98
            forceBuffers[offset].xyz += force.xyz;
99
100
        }
        else {
101
102
            // This is an off-diagonal tile.

103
104
            if (lasty != y) {
                unsigned int j = y + tgx;
105
                float4 tempPosq = posq[j];
106
107
108
109
                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;
110
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
111
            }
112
113
114
            localData[get_local_id(0)].fx = 0.0f;
            localData[get_local_id(0)].fy = 0.0f;
            localData[get_local_id(0)].fz = 0.0f;
115
#ifdef USE_CUTOFF
116
            unsigned int flags = interactionFlags[pos];
117
            if (!hasExclusions && flags != 0xFFFFFFFF) {
118
119
120
121
122
123
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

124
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
125
126
                        if ((flags&(1<<j)) != 0) {
                            bool isExcluded = false;
127
128
                            int atom2 = tbx+j;
                            float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
129
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
130
#ifdef USE_PERIODIC
131
132
133
                            delta.x -= floor(delta.x*INV_PERIODIC_BOX_SIZE_X+0.5f)*PERIODIC_BOX_SIZE_X;
                            delta.y -= floor(delta.y*INV_PERIODIC_BOX_SIZE_Y+0.5f)*PERIODIC_BOX_SIZE_Y;
                            delta.z -= floor(delta.z*INV_PERIODIC_BOX_SIZE_Z+0.5f)*PERIODIC_BOX_SIZE_Z;
134
#endif
135
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
136
137
                            float invR = RSQRT(r2);
                            float r = RECIP(invR);
138
                            LOAD_ATOM2_PARAMETERS
139
                            atom2 = y+j;
140
#ifdef USE_SYMMETRIC
141
                            float dEdR = 0.0f;
142
143
144
145
#else
                            float4 dEdR1 = (float4) 0.0f;
                            float4 dEdR2 = (float4) 0.0f;
#endif
146
147
                            float tempEnergy = 0.0f;
                            COMPUTE_INTERACTION
148
			    energy += tempEnergy;
149
#ifdef USE_SYMMETRIC
150
                            delta.xyz *= dEdR;
151
                            force.xyz -= delta.xyz;
152
                            tempBuffer[get_local_id(0)] = delta;
153
154
155
156
#else
                            force.xyz -= dEdR1.xyz;
                            tempBuffer[get_local_id(0)] = dEdR2;
#endif
157

158
                            // Sum the forces on atom2.
159
160
161
162
163
164
165
166
167

                            if (tgx % 2 == 0)
                                tempBuffer[get_local_id(0)].xyz += tempBuffer[get_local_id(0)+1].xyz;
                            if (tgx % 4 == 0)
                                tempBuffer[get_local_id(0)].xyz += tempBuffer[get_local_id(0)+2].xyz;
                            if (tgx % 8 == 0)
                                tempBuffer[get_local_id(0)].xyz += tempBuffer[get_local_id(0)+4].xyz;
                            if (tgx % 16 == 0)
                                tempBuffer[get_local_id(0)].xyz += tempBuffer[get_local_id(0)+8].xyz;
168
                            if (tgx == 0) {
169
170
171
                                localData[tbx+j].fx += tempBuffer[get_local_id(0)].x + tempBuffer[get_local_id(0)+16].x;
                                localData[tbx+j].fy += tempBuffer[get_local_id(0)].y + tempBuffer[get_local_id(0)+16].y;
                                localData[tbx+j].fz += tempBuffer[get_local_id(0)].z + tempBuffer[get_local_id(0)+16].z;
172
                            }
173
174
175
176
                        }
                    }
                }
            }
177
            else
178
179
#endif
            {
180
181
                // Compute the full set of interactions in this tile.

182
183
184
                unsigned int xi = x/TILE_SIZE;
                unsigned int yi = y/TILE_SIZE;
                unsigned int tile = xi+yi*PADDED_NUM_ATOMS/TILE_SIZE-yi*(yi+1)/2;
185
#ifdef USE_EXCLUSIONS
186
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
187
                excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
188
#endif
189
                unsigned int tj = tgx;
190
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
191
#ifdef USE_EXCLUSIONS
192
                    bool isExcluded = !(excl & 0x1);
193
#endif
194
195
                    int atom2 = tbx+tj;
                    float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
196
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
197
#ifdef USE_PERIODIC
198
199
200
                    delta.x -= floor(delta.x*INV_PERIODIC_BOX_SIZE_X+0.5f)*PERIODIC_BOX_SIZE_X;
                    delta.y -= floor(delta.y*INV_PERIODIC_BOX_SIZE_Y+0.5f)*PERIODIC_BOX_SIZE_Y;
                    delta.z -= floor(delta.z*INV_PERIODIC_BOX_SIZE_Z+0.5f)*PERIODIC_BOX_SIZE_Z;
201
#endif
202
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
203
204
                    float invR = RSQRT(r2);
                    float r = RECIP(invR);
205
                    LOAD_ATOM2_PARAMETERS
206
                    atom2 = y+tj;
207
#ifdef USE_SYMMETRIC
208
                    float dEdR = 0.0f;
209
210
211
212
#else
                    float4 dEdR1 = (float4) 0.0f;
                    float4 dEdR2 = (float4) 0.0f;
#endif
213
214
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
215
		    energy += tempEnergy;
216
#ifdef USE_SYMMETRIC
217
                    delta.xyz *= dEdR;
218
                    force.xyz -= delta.xyz;
219
220
221
                    localData[tbx+tj].fx += delta.x;
                    localData[tbx+tj].fy += delta.y;
                    localData[tbx+tj].fz += delta.z;
222
223
224
225
226
227
#else
                    force.xyz -= dEdR1.xyz;
                    localData[tbx+tj].fx += dEdR2.x;
                    localData[tbx+tj].fy += dEdR2.y;
                    localData[tbx+tj].fz += dEdR2.z;
#endif
228
                    excl >>= 1;
229
                    tj = (tj + 1) & (TILE_SIZE - 1);
230
231
232
233
                }
            }

            // Write results
234
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
235
236
            unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
237
#else
238
239
            unsigned int offset1 = x + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + warp*PADDED_NUM_ATOMS;
240
#endif
241
            forceBuffers[offset1].xyz += force.xyz;
242
            forceBuffers[offset2] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0.0f);
243
244
245
246
247
248
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}