nonbonded.cl 9.25 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
            local_posq[get_local_id(0)] = posq1;
42
            LOAD_LOCAL_PARAMETERS_FROM_1
43
44
            unsigned int xi = x/TileSize;
            unsigned int tile = xi+xi*paddedNumAtoms/TileSize-xi*(xi+1)/2;
45
#ifdef USE_EXCLUSIONS
46
            unsigned int excl = exclusions[exclusionIndices[tile]+tgx];
47
#endif
48
            for (unsigned int j = 0; j < TileSize; j++) {
49
#ifdef USE_EXCLUSIONS
50
                bool isExcluded = !(excl & 0x1);
51
#endif
52
                float4 delta = (float4) (local_posq[tbx+j].xyz - posq1.xyz, 0.0f);
53
54
55
56
57
#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
58
59
60
61
62
63
64
65
                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
66
67
68
#if defined USE_CUTOFF || defined USE_EXCLUSIONS
    #if defined USE_CUTOFF && defined USE_EXCLUSIONS
                excl >>= 1;
69
                if (isExcluded || r2 > cutoffSquared) {
70
71
72
73
    #elif defined USE_CUTOFF
                if (r2 > cutoffSquared) {
    #elif defined USE_EXCLUSIONS
                excl >>= 1;
74
                if (isExcluded) {
75
    #endif
76
77
78
                    dEdR = 0.0f;
                    tempEnergy  = 0.0f;
                }
79
#endif
80
81
                energy += 0.5f*tempEnergy;
                delta.xyz *= dEdR;
82
                force.xyz -= delta.xyz;
83
84
85
86
            }

            // Write results
            float4 of;
87
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
88
            unsigned int offset = x + tgx + (x/TileSize)*paddedNumAtoms;
89
90
#else
            unsigned int offset = x + tgx + warp*paddedNumAtoms;
91
#endif
92
            of = forceBuffers[offset];
93
            of.xyz += force.xyz;
94
            forceBuffers[offset] = of;
95
96
        }
        else {
97
98
            // This is an off-diagonal tile.

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

163
164
165
                unsigned int xi = x/TileSize;
                unsigned int yi = y/TileSize;
                unsigned int tile = xi+yi*paddedNumAtoms/TileSize-yi*(yi+1)/2;
166
#ifdef USE_EXCLUSIONS
167
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
168
                excl = (excl >> tgx) | (excl << (TileSize - tgx));
169
#endif
170
                for (unsigned int j = 0; j < TileSize; j++) {
171
#ifdef USE_EXCLUSIONS
172
                    bool isExcluded = !(excl & 0x1);
173
#endif
174
                    float4 delta = (float4) (local_posq[tbx+tj].xyz - posq1.xyz, 0.0f);
175
176
177
178
179
#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
180
181
182
183
184
185
186
187
                    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
188
189
190
#if defined USE_CUTOFF || defined USE_EXCLUSIONS
    #if defined USE_CUTOFF && defined USE_EXCLUSIONS
                    excl >>= 1;
191
                    if (isExcluded || r2 > cutoffSquared) {
192
193
194
195
    #elif defined USE_CUTOFF
                    if (r2 > cutoffSquared) {
    #elif defined USE_EXCLUSIONS
                    excl >>= 1;
196
                    if (isExcluded) {
197
    #endif
198
199
200
                        dEdR = 0.0f;
			tempEnergy  = 0.0f;
                    }
201
#endif
202
203
		    energy += tempEnergy;
                    delta.xyz *= dEdR;
204
                    force.xyz -= delta.xyz;
205
206
207
208
209
210
211
                    local_force[tbx+tj].xyz += delta.xyz;
                    tj = (tj + 1) & (TileSize - 1);
                }
            }

            // Write results
            float4 of;
212
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
213
214
            unsigned int offset1 = x + tgx + (y/TileSize)*paddedNumAtoms;
            unsigned int offset2 = y + tgx + (x/TileSize)*paddedNumAtoms;
215
#else
216
217
218
219
            unsigned int offset1 = x + tgx + warp*paddedNumAtoms;
            unsigned int offset2 = y + tgx + warp*paddedNumAtoms;
#endif
            of = forceBuffers[offset1];
220
            of.xyz += force.xyz;
221
222
            forceBuffers[offset1] = of;
            of = forceBuffers[offset2];
223
            of.xyz += local_force[get_local_id(0)].xyz;
224
            forceBuffers[offset2] = of;
225
226
227
228
229
230
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}