nonbonded.cl 8.99 KB
Newer Older
1
2
3
4
5
6
const unsigned int TileSize = 32;

/**
 * Compute nonbonded interactions.
 */

7
8
__kernel void computeNonbonded(int paddedNumAtoms, __global float4* forceBuffers, __global float* energyBuffer, __global float4* posq,
        __global unsigned int* exclusions,  __global unsigned int* exclusionIndices, __local float4* local_posq, __local float4* local_force, __global unsigned int* tiles,
9
#ifdef USE_CUTOFF
10
11
12
        float cutoffSquared, float4 periodicBoxSize, __global unsigned int* interactionFlags, __global unsigned int* interactionCount, __local float4* tempBuffer
#else
        unsigned int numTiles
13
#endif
14
        PARAMETER_ARGUMENTS) {
15
16
17
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
18
19
20
21
22
23
24
25
    unsigned int totalWarps = get_global_size(0)/TileSize;
    unsigned int warp = get_global_id(0)/TileSize;
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
26
        // Extract the coordinates of this tile
27
28
29
30
31
32
33
34
        unsigned int x = tiles[pos];
        unsigned int y = ((x >> 2) & 0x7fff)*TileSize;
        bool hasExclusions = (x & 0x1);
        x = (x>>17)*TileSize;
        unsigned int tgx = get_local_id(0) & (TileSize-1);
        unsigned int tbx = get_local_id(0) - tgx;
        unsigned int tj = tgx;
        unsigned int i = x + tgx;
35
36
37
        float4 force = 0.0f;
        float4 posq1 = posq[i];
        LOAD_ATOM1_PARAMETERS
38
        if (x == y) {
39
            // This tile is on the diagonal.
40

41
42
            local_posq[get_local_id(0)] = posq1;
            local_sigmaEpsilon[get_local_id(0)] = sigmaEpsilon1;
43
44
45
46
47
            unsigned int xi = x/TileSize;
            unsigned int tile = xi+xi*paddedNumAtoms/TileSize-xi*(xi+1)/2;
            unsigned int excl = exclusions[exclusionIndices[tile]+tgx];
            for (unsigned int j = 0; j < TileSize; j++) {
                bool isExcluded = !(excl & 0x1);
48
                float4 delta = (float4) (local_posq[tbx+j].xyz - posq1.xyz, 0.0f);
49
50
51
52
53
#ifdef USE_PERIODIC
                delta.x -= floor(delta.x/periodicBoxSize.x+0.5f)*periodicBoxSize.x;
                delta.y -= floor(delta.y/periodicBoxSize.y+0.5f)*periodicBoxSize.y;
                delta.z -= floor(delta.z/periodicBoxSize.z+0.5f)*periodicBoxSize.z;
#endif
54
55
56
57
58
59
60
61
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                float r = sqrt(r2);
                float invR = 1.0f / r;
                float4 posq2 = local_posq[tbx+j];
                LOAD_ATOM2_PARAMETERS_J
                float dEdR = 0.0f;
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
62
63
64
65
66
67
68
69
70
71
#ifdef USE_CUTOFF
                if (isExcluded || r2 > cutoffSquared) {
#else
                if (isExcluded) {
#endif
                    dEdR = 0.0f;
                    tempEnergy  = 0.0f;
                }
                energy += 0.5f*tempEnergy;
                delta.xyz *= dEdR;
72
                force.xyz -= delta.xyz;
73
74
75
76
77
                excl >>= 1;
            }

            // Write results
            float4 of;
78
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
79
            of.xyz = force.xyz;
80
            of.w = 0.0f;
81
            unsigned int offset = x + tgx + (x/TileSize)*paddedNumAtoms;
82
            forceBuffers[offset] = of;
83
84
85
#else
            unsigned int offset = x + tgx + warp*paddedNumAtoms;
            of = forceBuffers[offset];
86
            of.xyz += force.xyz;
87
            forceBuffers[offset] = of;
88
89
90
#endif
        }
        else {
91
92
            // This is an off-diagonal tile.

93
94
95
            if (lasty != y) {
                unsigned int j = y + tgx;
                local_posq[get_local_id(0)] = posq[j];
96
                local_sigmaEpsilon[get_local_id(0)] = global_sigmaEpsilon[j];
97
98
99
            }
            local_force[get_local_id(0)] = 0.0f;
#ifdef USE_CUTOFF
100
            unsigned int flags = interactionFlags[pos];
101
102
103
104
105
106
107
108
109
110
            if (!hasExclusions && flags != 0xFFFFFFFF) {
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

                    for (unsigned int j = 0; j < TileSize; j++) {
                        if ((flags&(1<<j)) != 0) {
                            bool isExcluded = false;
111
                            float4 delta = (float4) (local_posq[tbx+j].xyz - posq1.xyz, 0.0f);
112
113
114
115
116
#ifdef USE_PERIODIC
                            delta.x -= floor(delta.x/periodicBoxSize.x+0.5f)*periodicBoxSize.x;
                            delta.y -= floor(delta.y/periodicBoxSize.y+0.5f)*periodicBoxSize.y;
                            delta.z -= floor(delta.z/periodicBoxSize.z+0.5f)*periodicBoxSize.z;
#endif
117
118
119
120
121
122
123
124
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                            float r = sqrt(r2);
                            float invR = 1.0f / r;
                            float4 posq2 = local_posq[tbx+j];
                            LOAD_ATOM2_PARAMETERS_J
                            float dEdR = 0.0f;
                            float tempEnergy = 0.0f;
                            COMPUTE_INTERACTION
125
126
127
128
129
130
131
132
#ifdef USE_CUTOFF
                            if (r2 > cutoffSquared) {
                                dEdR = 0.0f;
				tempEnergy = 0.0f;
                            }
#endif
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
133
                            force.xyz -= delta.xyz;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
                            tempBuffer[get_local_id(0)] = delta;

                            // Sum the forces on atom j.

                            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;
                            if (tgx == 0)
                                local_force[tbx+j].xyz += tempBuffer[get_local_id(0)].xyz + tempBuffer[get_local_id(0)+16].xyz;
                        }
                    }
                }
            }
152
            else
153
154
#endif
            {
155
156
                // Compute the full set of interactions in this tile.

157
158
159
                unsigned int xi = x/TileSize;
                unsigned int yi = y/TileSize;
                unsigned int tile = xi+yi*paddedNumAtoms/TileSize-yi*(yi+1)/2;
160
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
161
162
163
                excl = (excl >> tgx) | (excl << (TileSize - tgx));
                for (unsigned int j = 0; j < TileSize; j++) {
                    bool isExcluded = !(excl & 0x1);
164
                    float4 delta = (float4) (local_posq[tbx+tj].xyz - posq1.xyz, 0.0f);
165
166
167
168
169
#ifdef USE_PERIODIC
                    delta.x -= floor(delta.x/periodicBoxSize.x+0.5f)*periodicBoxSize.x;
                    delta.y -= floor(delta.y/periodicBoxSize.y+0.5f)*periodicBoxSize.y;
                    delta.z -= floor(delta.z/periodicBoxSize.z+0.5f)*periodicBoxSize.z;
#endif
170
171
172
173
174
175
176
177
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                    float r = sqrt(r2);
                    float invR = 1.0f / r;
                    float4 posq2 = local_posq[tbx+tj];
                    LOAD_ATOM2_PARAMETERS_TJ
                    float dEdR = 0.0f;
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
178
179
180
181
182
183
184
185
186
187
#ifdef USE_CUTOFF
                    if (isExcluded || r2 > cutoffSquared) {
#else
                    if (isExcluded) {
#endif
                        dEdR = 0.0f;
			tempEnergy  = 0.0f;
                    }
		    energy += tempEnergy;
                    delta.xyz *= dEdR;
188
                    force.xyz -= delta.xyz;
189
190
191
192
193
194
195
196
                    local_force[tbx+tj].xyz += delta.xyz;
                    excl >>= 1;
                    tj = (tj + 1) & (TileSize - 1);
                }
            }

            // Write results
            float4 of;
197
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
198
            of.xyz = force.xyz;
199
            of.w = 0.0f;
200
            unsigned int offset = x + tgx + (y/TileSize)*paddedNumAtoms;
201
202
            forceBuffers[offset] = of;
            of = local_force[get_local_id(0)];
203
            offset = y + tgx + (x/TileSize)*paddedNumAtoms;
204
            forceBuffers[offset] = of;
205
206
207
#else
            unsigned int offset = x + tgx + warp*paddedNumAtoms;
            of = forceBuffers[offset];
208
            of.xyz += force.xyz;
209
210
211
212
213
            forceBuffers[offset] = of;
            offset = y + tgx + warp*paddedNumAtoms;
            of = forceBuffers[offset];
            of.xyz += local_force[get_local_id(0)].xyz;
            forceBuffers[offset] = of;
214
215
216
217
218
219
220
#endif
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}