nonbonded_default.cl 7.55 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
        unsigned int atom1 = x + tgx;
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
43
        __local AtomData* atom1Data = &localData[get_local_id(0)];
44
45
46
47
        LOAD_ATOM1_PARAMETERS
        if (x == y) {
            // This tile is on the diagonal.

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

            // Sum the forces and write results.

87
            if (get_local_id(0) >= TILE_SIZE)
88
89
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
90
            if (get_local_id(0) < TILE_SIZE) {
91
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
92
                unsigned int offset = x + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
93
94
95
#else
                unsigned int offset = x + tgx + get_group_id(0)*PADDED_NUM_ATOMS;
#endif
96
                forceBuffers[offset].xyz = forceBuffers[offset].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
97
98
99
100
101
            }
        }
        else {
            // This is an off-diagonal tile.

102
            if (lasty != y && get_local_id(0) < TILE_SIZE) {
103
                unsigned int j = y + tgx;
104
                float4 tempPosq = posq[j];
105
106
107
108
                atom1Data->x = tempPosq.x;
                atom1Data->y = tempPosq.y;
                atom1Data->z = tempPosq.z;
                atom1Data->q = tempPosq.w;
109
110
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
111
112
113
            atom1Data->fx = 0.0f;
            atom1Data->fy = 0.0f;
            atom1Data->fz = 0.0f;
114
115
            barrier(CLK_LOCAL_MEM_FENCE);

116
117
118
119
120
            // 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;
121
#ifdef USE_EXCLUSIONS
122
123
124
            unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
            excl >>= baseLocalAtom;
125
#endif
126
127
            unsigned int tj = tgx%(TILE_SIZE/2);
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
128
#ifdef USE_EXCLUSIONS
129
                bool isExcluded = !(excl & 0x1);
130
#endif
131
132
                __local AtomData* atom2Data = &localData[baseLocalAtom+j];
                float4 posq2 = (float4) (atom2Data->x, atom2Data->y, atom2Data->z, atom2Data->q);
133
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
134
#ifdef USE_PERIODIC
135
136
137
                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;
138
#endif
139
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
140
141
                float invR = RSQRT(r2);
                float r = RECIP(invR);
142
                LOAD_ATOM2_PARAMETERS
143
                int atom2 = y+baseLocalAtom+tj;
144
145
146
147
148
149
                float dEdR = 0.0f;
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += tempEnergy;
                delta.xyz *= dEdR;
                force.xyz -= delta.xyz;
150
151
152
                localData[baseLocalAtom+tj+forceBufferOffset].fx += delta.x;
                localData[baseLocalAtom+tj+forceBufferOffset].fy += delta.y;
                localData[baseLocalAtom+tj+forceBufferOffset].fz += delta.z;
153
154
155
                barrier(CLK_LOCAL_MEM_FENCE);
                excl >>= 1;
                tj = (tj+1)%(TILE_SIZE/2);
156
157
158
159
            }

            // Sum the forces and write results.

160
            if (get_local_id(0) >= TILE_SIZE)
161
162
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
163
            if (get_local_id(0) < TILE_SIZE) {
164
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
165
166
                unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
                unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
167
168
169
170
#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
171
                forceBuffers[offset1].xyz = forceBuffers[offset1].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
172
                float4 sum = (float4) (atom1Data->fx+localData[get_local_id(0)+TILE_SIZE].fx, atom1Data->fy+localData[get_local_id(0)+TILE_SIZE].fy, atom1Data->fz+localData[get_local_id(0)+TILE_SIZE].fz, 0.0f);
173
                forceBuffers[offset2].xyz = forceBuffers[offset2].xyz+sum.xyz;
174
175
176
177
178
179
180
            }
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}