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

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

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

            // Write results
84
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
85
            unsigned int offset = x + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
86
#else
87
            unsigned int offset = x + tgx + warp*PADDED_NUM_ATOMS;
88
#endif
89
            forceBuffers[offset].xyz += force.xyz;
90
91
        }
        else {
92
93
            // This is an off-diagonal tile.

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

115
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
116
117
                        if ((flags&(1<<j)) != 0) {
                            bool isExcluded = false;
118
                            int atom2 = tbx+j;
119
                            float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
120
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
121
#ifdef USE_PERIODIC
122
123
124
                            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;
125
#endif
126
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
127
128
                            float invR = native_rsqrt(r2);
                            float r = native_recip(invR);
129
130
                            LOAD_ATOM2_PARAMETERS
                            atom2 = y+j;
131
132
133
                            float dEdR = 0.0f;
                            float tempEnergy = 0.0f;
                            COMPUTE_INTERACTION
134
135
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
136
                            force.xyz -= delta.xyz;
137
138
                            tempBuffer[get_local_id(0)] = delta;

139
                            // Sum the forces on atom2.
140
141
142
143
144
145
146
147
148

                            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;
149
150
151
152
153
                            if (tgx == 0) {
                                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;
                            }
154
155
156
157
                        }
                    }
                }
            }
158
            else
159
160
#endif
            {
161
162
                // Compute the full set of interactions in this tile.

163
164
165
                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;
166
#ifdef USE_EXCLUSIONS
167
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
168
                excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
169
#endif
170
                unsigned int tj = tgx;
171
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
172
#ifdef USE_EXCLUSIONS
173
                    bool isExcluded = !(excl & 0x1);
174
#endif
175
                    int atom2 = tbx+tj;
176
                    float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
177
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
178
#ifdef USE_PERIODIC
179
180
181
                    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;
182
#endif
183
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
184
185
                    float invR = native_rsqrt(r2);
                    float r = native_recip(invR);
186
187
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y+tj;
188
189
190
                    float dEdR = 0.0f;
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
191
192
		    energy += tempEnergy;
                    delta.xyz *= dEdR;
193
                    force.xyz -= delta.xyz;
194
195
196
                    localData[tbx+tj].fx += delta.x;
                    localData[tbx+tj].fy += delta.y;
                    localData[tbx+tj].fz += delta.z;
197
                    excl >>= 1;
198
                    tj = (tj + 1) & (TILE_SIZE - 1);
199
200
201
202
                }
            }

            // Write results
203
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
204
205
            unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
206
#else
207
208
            unsigned int offset1 = x + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + warp*PADDED_NUM_ATOMS;
209
#endif
210
            forceBuffers[offset1].xyz += force.xyz;
211
            forceBuffers[offset2] += (float4) (localData[get_local_id(0)].fx, localData[get_local_id(0)].fy, localData[get_local_id(0)].fz, 0.0f);
212
213
214
215
216
217
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}