nonbonded.cl 8.23 KB
Newer Older
1
2
3
4
5
6
const unsigned int TileSize = 32;

/**
 * Compute nonbonded interactions.
 */

7
__kernel void computeNonbonded(int numAtoms, int paddedNumAtoms, __global float4* forceBuffers, __global float* energyBuffer, __global float4* posq,
8
        __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
        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;
34
        unsigned int atom1 = x + tgx;
35
        float4 force = 0.0f;
36
        float4 posq1 = posq[atom1];
37
        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
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                float r = sqrt(r2);
                float invR = 1.0f / r;
61
62
63
64
                int atom2 = tbx+j;
                float4 posq2 = local_posq[atom2];
                LOAD_ATOM2_PARAMETERS
                atom2 = y+j;
65
66
67
                float dEdR = 0.0f;
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
68
69
                energy += 0.5f*tempEnergy;
                delta.xyz *= dEdR;
70
                force.xyz -= delta.xyz;
71
                excl >>= 1;
72
73
74
            }

            // Write results
75
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
76
            unsigned int offset = x + tgx + (x/TileSize)*paddedNumAtoms;
77
78
#else
            unsigned int offset = x + tgx + warp*paddedNumAtoms;
79
#endif
80
            forceBuffers[offset].xyz += force.xyz;
81
82
        }
        else {
83
84
            // This is an off-diagonal tile.

85
86
87
            if (lasty != y) {
                unsigned int j = y + tgx;
                local_posq[get_local_id(0)] = posq[j];
88
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
89
90
91
            }
            local_force[get_local_id(0)] = 0.0f;
#ifdef USE_CUTOFF
92
            unsigned int flags = interactionFlags[pos];
93
94
95
96
97
98
99
100
101
102
            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;
103
                            float4 delta = (float4) (local_posq[tbx+j].xyz - posq1.xyz, 0.0f);
104
105
106
107
108
#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
109
110
111
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                            float r = sqrt(r2);
                            float invR = 1.0f / r;
112
113
114
115
                            int atom2 = tbx+j;
                            float4 posq2 = local_posq[atom2];
                            LOAD_ATOM2_PARAMETERS
                            atom2 = y+j;
116
117
118
                            float dEdR = 0.0f;
                            float tempEnergy = 0.0f;
                            COMPUTE_INTERACTION
119
120
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
121
                            force.xyz -= delta.xyz;
122
123
                            tempBuffer[get_local_id(0)] = delta;

124
                            // Sum the forces on atom2.
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

                            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;
                        }
                    }
                }
            }
140
            else
141
142
#endif
            {
143
144
                // Compute the full set of interactions in this tile.

145
146
147
                unsigned int xi = x/TileSize;
                unsigned int yi = y/TileSize;
                unsigned int tile = xi+yi*paddedNumAtoms/TileSize-yi*(yi+1)/2;
148
#ifdef USE_EXCLUSIONS
149
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
150
                excl = (excl >> tgx) | (excl << (TileSize - tgx));
151
#endif
152
                for (unsigned int j = 0; j < TileSize; j++) {
153
#ifdef USE_EXCLUSIONS
154
                    bool isExcluded = !(excl & 0x1);
155
#endif
156
                    float4 delta = (float4) (local_posq[tbx+tj].xyz - posq1.xyz, 0.0f);
157
158
159
160
161
#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
162
163
164
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                    float r = sqrt(r2);
                    float invR = 1.0f / r;
165
166
167
168
                    int atom2 = tbx+tj;
                    float4 posq2 = local_posq[atom2];
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y+tj;
169
170
171
                    float dEdR = 0.0f;
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
172
173
		    energy += tempEnergy;
                    delta.xyz *= dEdR;
174
                    force.xyz -= delta.xyz;
175
                    local_force[tbx+tj].xyz += delta.xyz;
176
                    excl >>= 1;
177
178
179
180
181
                    tj = (tj + 1) & (TileSize - 1);
                }
            }

            // Write results
182
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
183
184
            unsigned int offset1 = x + tgx + (y/TileSize)*paddedNumAtoms;
            unsigned int offset2 = y + tgx + (x/TileSize)*paddedNumAtoms;
185
#else
186
187
188
            unsigned int offset1 = x + tgx + warp*paddedNumAtoms;
            unsigned int offset2 = y + tgx + warp*paddedNumAtoms;
#endif
189
190
            forceBuffers[offset1].xyz += force.xyz;
            forceBuffers[offset2].xyz += local_force[get_local_id(0)].xyz;
191
192
193
194
195
196
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}