nonbonded_nvidia.cl 8.35 KB
Newer Older
1
#define TILE_SIZE 32
2
const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
3
4
5
6
7

/**
 * Compute nonbonded interactions.
 */

8
9
__kernel void computeNonbonded(__global float4* forceBuffers, __global float* energyBuffer, __global float4* posq, __global unsigned int* exclusions,
        __global unsigned int* exclusionIndices, __local float4* local_posq, __local float4* local_force, __local float4* tempBuffer, __global unsigned int* tiles,
10
#ifdef USE_CUTOFF
11
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount
12
13
#else
        unsigned int numTiles
14
#endif
15
        PARAMETER_ARGUMENTS) {
16
17
18
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
19
20
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
21
22
23
24
25
26
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;

    while (pos < end) {
27
        // Extract the coordinates of this tile
28
        unsigned int x = tiles[pos];
29
        unsigned int y = ((x >> 2) & 0x7fff)*TILE_SIZE;
30
        bool hasExclusions = (x & 0x1);
31
32
        x = (x>>17)*TILE_SIZE;
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
33
        unsigned int tbx = get_local_id(0) - 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/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-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 < TILE_SIZE; j++) {
49
#ifdef USE_EXCLUSIONS
50
                bool isExcluded = !(excl & 0x1);
51
#endif
52
53
54
                int atom2 = tbx+j;
                float4 posq2 = local_posq[atom2];
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
55
#ifdef USE_PERIODIC
56
57
58
                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;
59
#endif
60
61
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                float r = sqrt(r2);
62
                float invR = 1.0f/r;
63
64
                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/TILE_SIZE)*PADDED_NUM_ATOMS;
77
#else
78
            unsigned int offset = x + tgx + warp*PADDED_NUM_ATOMS;
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
            if (!hasExclusions && flags != 0xFFFFFFFF) {
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

100
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
101
102
                        if ((flags&(1<<j)) != 0) {
                            bool isExcluded = false;
103
104
105
                            int atom2 = tbx+j;
                            float4 posq2 = local_posq[atom2];
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
106
#ifdef USE_PERIODIC
107
108
109
                            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;
110
#endif
111
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
112
113
                            float invR = native_rsqrt(r2);
                            float r = native_recip(invR);
114
115
                            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/TILE_SIZE;
                unsigned int yi = y/TILE_SIZE;
                unsigned int tile = xi+yi*PADDED_NUM_ATOMS/TILE_SIZE-yi*(yi+1)/2;
148
#ifdef USE_EXCLUSIONS
149
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
150
                excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
151
#endif
152
                unsigned int tj = tgx;
153
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
154
#ifdef USE_EXCLUSIONS
155
                    bool isExcluded = !(excl & 0x1);
156
#endif
157
158
159
                    int atom2 = tbx+tj;
                    float4 posq2 = local_posq[atom2];
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
160
#ifdef USE_PERIODIC
161
162
163
                    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;
164
#endif
165
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
166
167
                    float invR = native_rsqrt(r2);
                    float r = native_recip(invR);
168
169
                    LOAD_ATOM2_PARAMETERS
                    atom2 = y+tj;
170
171
172
                    float dEdR = 0.0f;
                    float tempEnergy = 0.0f;
                    COMPUTE_INTERACTION
173
174
		    energy += tempEnergy;
                    delta.xyz *= dEdR;
175
                    force.xyz -= delta.xyz;
176
                    local_force[tbx+tj].xyz += delta.xyz;
177
                    excl >>= 1;
178
                    tj = (tj + 1) & (TILE_SIZE - 1);
179
180
181
182
                }
            }

            // Write results
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
#else
187
188
            unsigned int offset1 = x + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + warp*PADDED_NUM_ATOMS;
189
#endif
190
191
            forceBuffers[offset1].xyz += force.xyz;
            forceBuffers[offset2].xyz += local_force[get_local_id(0)].xyz;
192
193
194
195
196
197
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}