nonbonded_default.cl 8.27 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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifdef USE_CUTOFF
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
    unsigned int pos = get_group_id(0)*numTiles/get_num_groups(0);
    unsigned int end = (get_group_id(0)+1)*numTiles/get_num_groups(0);
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

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

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
52
            LOAD_LOCAL_PARAMETERS_FROM_1
            barrier(CLK_LOCAL_MEM_FENCE);
53
54
            unsigned int xi = x/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-xi*(xi+1)/2;
55
56
57
#ifdef USE_EXCLUSIONS
            unsigned int excl = exclusions[exclusionIndices[tile]+tgx] >> baseLocalAtom;
#endif
58
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
59
60
61
#ifdef USE_EXCLUSIONS
                bool isExcluded = !(excl & 0x1);
#endif
62
63
                int atom2 = baseLocalAtom+j;
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
64
65
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
#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
70
#endif
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
71
72
                float invR = RSQRT(r2);
                float r = RECIP(invR);
73
                LOAD_ATOM2_PARAMETERS
74
                atom2 = y+baseLocalAtom+j;
75
#ifdef USE_SYMMETRIC
76
                float dEdR = 0.0f;
77
78
79
80
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
81
82
83
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += 0.5f*tempEnergy;
84
85
86
87
88
#ifdef USE_SYMMETRIC
                force.xyz -= delta.xyz*dEdR;
#else
                force.xyz -= dEdR1.xyz;
#endif
89
90
91
92
93
                excl >>= 1;
            }

            // Sum the forces and write results.

94
            if (get_local_id(0) >= TILE_SIZE)
95
96
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
97
            if (get_local_id(0) < TILE_SIZE) {
98
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
99
                unsigned int offset = x + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
100
101
102
#else
                unsigned int offset = x + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
#endif
103
                forceBuffers[offset].xyz = forceBuffers[offset].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
104
105
106
107
108
            }
        }
        else {
            // This is an off-diagonal tile.

109
            if (lasty != y && get_local_id(0) < TILE_SIZE) {
110
                unsigned int j = y + tgx;
111
                float4 tempPosq = posq[j];
112
113
114
115
                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;
116
117
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
118
119
120
            localData[get_local_id(0)].fx = 0.0f;
            localData[get_local_id(0)].fy = 0.0f;
            localData[get_local_id(0)].fz = 0.0f;
121
122
            barrier(CLK_LOCAL_MEM_FENCE);

123
124
125
126
127
            // Compute the full set of interactions in this tile.

            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;
128
#ifdef USE_EXCLUSIONS
129
130
131
            unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
            excl >>= baseLocalAtom;
132
#endif
133
134
            unsigned int tj = tgx%(TILE_SIZE/2);
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
135
#ifdef USE_EXCLUSIONS
136
                bool isExcluded = !(excl & 0x1);
137
#endif
138
139
                int atom2 = baseLocalAtom+tj;
                float4 posq2 = (float4) (localData[atom2].x, localData[atom2].y, localData[atom2].z, localData[atom2].q);
140
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
141
#ifdef USE_PERIODIC
142
143
144
                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;
145
#endif
146
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
147
148
                float invR = RSQRT(r2);
                float r = RECIP(invR);
149
                LOAD_ATOM2_PARAMETERS
150
                atom2 = y+baseLocalAtom+tj;
151
#ifdef USE_SYMMETRIC
152
                float dEdR = 0.0f;
153
154
155
156
#else
                float4 dEdR1 = (float4) 0.0f;
                float4 dEdR2 = (float4) 0.0f;
#endif
157
158
159
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += tempEnergy;
160
#ifdef USE_SYMMETRIC
161
162
                delta.xyz *= dEdR;
                force.xyz -= delta.xyz;
163
164
165
                localData[baseLocalAtom+tj+forceBufferOffset].fx += delta.x;
                localData[baseLocalAtom+tj+forceBufferOffset].fy += delta.y;
                localData[baseLocalAtom+tj+forceBufferOffset].fz += delta.z;
166
167
168
169
170
171
#else
                force.xyz -= dEdR1.xyz;
                localData[baseLocalAtom+tj+forceBufferOffset].fx += dEdR2.x;
                localData[baseLocalAtom+tj+forceBufferOffset].fy += dEdR2.y;
                localData[baseLocalAtom+tj+forceBufferOffset].fz += dEdR2.z;
#endif
172
173
174
                barrier(CLK_LOCAL_MEM_FENCE);
                excl >>= 1;
                tj = (tj+1)%(TILE_SIZE/2);
175
176
177
178
            }

            // Sum the forces and write results.

179
            if (get_local_id(0) >= TILE_SIZE)
180
181
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
182
            if (get_local_id(0) < TILE_SIZE) {
183
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
184
185
                unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
                unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
186
187
188
189
#else
                unsigned int offset1 = x + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
                unsigned int offset2 = y + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
#endif
190
                forceBuffers[offset1].xyz = forceBuffers[offset1].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
191
                float4 sum = (float4) (localData[get_local_id(0)].fx+localData[get_local_id(0)+TILE_SIZE].fx, localData[get_local_id(0)].fy+localData[get_local_id(0)+TILE_SIZE].fy, localData[get_local_id(0)].fz+localData[get_local_id(0)+TILE_SIZE].fz, 0.0f);
192
                forceBuffers[offset2].xyz = forceBuffers[offset2].xyz+sum.xyz;
193
194
195
196
197
198
199
            }
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}