"platforms/cuda-old/src/kernels/kCCMA.cu" did not exist on "2f1c9531539f753af002e0a901107195eaee164a"
nonbonded_nvidia.cl 9.48 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
76
77
                float dEdR = 0.0f;
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
78
79
                energy += 0.5f*tempEnergy;
                delta.xyz *= dEdR;
80
                force.xyz -= delta.xyz;
81
                excl >>= 1;
82
83
84
            }

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

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

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

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

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

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

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