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

/**
 * Compute nonbonded interactions.
 */

7
__kernel void computeNonbonded(int numTiles, int paddedNumAtoms,
8
        __global float4* forceBuffers, __global float* energyBuffer, __global float4* posq, __global unsigned int* tiles,
9
        __global unsigned int* exclusions,  __global unsigned int* exclusionIndices, __local float4* local_posq, __local float4* local_force
10
#ifdef USE_CUTOFF
11
        , float cutoffSquared, float4 periodicBoxSize, __global unsigned int* interactionFlags, __local float4* tempBuffer
12
#endif
13
        PARAMETER_ARGUMENTS) {
14
15
16
17
18
19
20
21
    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) {
22
        // Extract the coordinates of this tile
23
24
25
26
27
28
29
30
        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;
31
32
33
        float4 force = 0.0f;
        float4 posq1 = posq[i];
        LOAD_ATOM1_PARAMETERS
34
        if (x == y) {
35
            // This tile is on the diagonal.
36

37
38
            local_posq[get_local_id(0)] = posq1;
            local_sigmaEpsilon[get_local_id(0)] = sigmaEpsilon1;
39
40
41
42
43
            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);
44
                float4 delta = (float4) (local_posq[tbx+j].xyz - posq1.xyz, 0.0f);
45
46
47
48
49
#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
50
51
52
53
54
55
56
57
                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
58
59
60
61
62
63
64
65
66
67
#ifdef USE_CUTOFF
                if (isExcluded || r2 > cutoffSquared) {
#else
                if (isExcluded) {
#endif
                    dEdR = 0.0f;
                    tempEnergy  = 0.0f;
                }
                energy += 0.5f*tempEnergy;
                delta.xyz *= dEdR;
68
                force.xyz -= delta.xyz;
69
70
71
72
73
                excl >>= 1;
            }

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

89
90
91
            if (lasty != y) {
                unsigned int j = y + tgx;
                local_posq[get_local_id(0)] = posq[j];
92
                local_sigmaEpsilon[get_local_id(0)] = global_sigmaEpsilon[j];
93
94
95
            }
            local_force[get_local_id(0)] = 0.0f;
#ifdef USE_CUTOFF
96
            unsigned int flags = interactionFlags[pos];
97
98
99
100
101
102
103
104
105
106
            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;
107
                            float4 delta = (float4) (local_posq[tbx+j].xyz - posq1.xyz, 0.0f);
108
109
110
111
112
#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
113
114
115
116
117
118
119
120
                            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
121
122
123
124
125
126
127
128
#ifdef USE_CUTOFF
                            if (r2 > cutoffSquared) {
                                dEdR = 0.0f;
				tempEnergy = 0.0f;
                            }
#endif
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
129
                            force.xyz -= delta.xyz;
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                            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;
                        }
                    }
                }
            }
148
            else
149
150
#endif
            {
151
152
                // Compute the full set of interactions in this tile.

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

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