"plugins/drude/platforms/cuda/CMakeLists.txt" did not exist on "18c9a78a8a9950be32af8021986d619afab3e017"
nonbonded_nvidia.cl 8.25 KB
Newer Older
1
#define TILE_SIZE 32
2
3
4
5
6

/**
 * Compute nonbonded interactions.
 */

7
8
__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,
9
#ifdef USE_CUTOFF
10
        __global unsigned int* interactionFlags, __global unsigned int* interactionCount
11
12
#else
        unsigned int numTiles
13
#endif
14
        PARAMETER_ARGUMENTS) {
15
16
17
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
18
19
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
20
21
22
23
24
25
    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
        unsigned int x = tiles[pos];
28
        unsigned int y = ((x >> 2) & 0x7fff)*TILE_SIZE;
29
        bool hasExclusions = (x & 0x1);
30
31
        x = (x>>17)*TILE_SIZE;
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
32
        unsigned int tbx = get_local_id(0) - tgx;
33
        unsigned int atom1 = x + tgx;
34
        float4 force = 0.0f;
35
        float4 posq1 = posq[atom1];
36
        LOAD_ATOM1_PARAMETERS
37
        if (x == y) {
38
            // This tile is on the diagonal.
39

40
            local_posq[get_local_id(0)] = posq1;
41
            LOAD_LOCAL_PARAMETERS_FROM_1
42
43
            unsigned int xi = x/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-xi*(xi+1)/2;
44
#ifdef USE_EXCLUSIONS
45
            unsigned int excl = exclusions[exclusionIndices[tile]+tgx];
46
#endif
47
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
48
#ifdef USE_EXCLUSIONS
49
                bool isExcluded = !(excl & 0x1);
50
#endif
51
52
53
                int atom2 = tbx+j;
                float4 posq2 = local_posq[atom2];
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
54
#ifdef USE_PERIODIC
55
56
57
                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;
58
#endif
59
60
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
                float r = sqrt(r2);
61
                float invR = 1.0f/r;
62
63
                LOAD_ATOM2_PARAMETERS
                atom2 = y+j;
64
65
66
                float dEdR = 0.0f;
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
67
68
                energy += 0.5f*tempEnergy;
                delta.xyz *= dEdR;
69
                force.xyz -= delta.xyz;
70
                excl >>= 1;
71
72
73
            }

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

84
85
86
            if (lasty != y) {
                unsigned int j = y + tgx;
                local_posq[get_local_id(0)] = posq[j];
87
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
88
89
90
            }
            local_force[get_local_id(0)] = 0.0f;
#ifdef USE_CUTOFF
91
            unsigned int flags = interactionFlags[pos];
92
93
94
95
96
97
98
            if (!hasExclusions && flags != 0xFFFFFFFF) {
                if (flags == 0) {
                    // No interactions in this tile.
                }
                else {
                    // Compute only a subset of the interactions in this tile.

99
                    for (unsigned int j = 0; j < TILE_SIZE; j++) {
100
101
                        if ((flags&(1<<j)) != 0) {
                            bool isExcluded = false;
102
103
104
                            int atom2 = tbx+j;
                            float4 posq2 = local_posq[atom2];
                            float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
105
#ifdef USE_PERIODIC
106
107
108
                            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;
109
#endif
110
                            float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
111
112
                            float invR = native_rsqrt(r2);
                            float r = native_recip(invR);
113
114
                            LOAD_ATOM2_PARAMETERS
                            atom2 = y+j;
115
116
117
                            float dEdR = 0.0f;
                            float tempEnergy = 0.0f;
                            COMPUTE_INTERACTION
118
119
			    energy += tempEnergy;
                            delta.xyz *= dEdR;
120
                            force.xyz -= delta.xyz;
121
122
                            tempBuffer[get_local_id(0)] = delta;

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

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

144
145
146
                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;
147
#ifdef USE_EXCLUSIONS
148
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
149
                excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
150
#endif
151
                unsigned int tj = tgx;
152
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
153
#ifdef USE_EXCLUSIONS
154
                    bool isExcluded = !(excl & 0x1);
155
#endif
156
157
158
                    int atom2 = tbx+tj;
                    float4 posq2 = local_posq[atom2];
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
159
#ifdef USE_PERIODIC
160
161
162
                    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;
163
#endif
164
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
165
166
                    float invR = native_rsqrt(r2);
                    float r = native_recip(invR);
167
168
                    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
                    tj = (tj + 1) & (TILE_SIZE - 1);
178
179
180
181
                }
            }

            // Write results
182
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
183
184
            unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
185
#else
186
187
            unsigned int offset1 = x + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y + tgx + warp*PADDED_NUM_ATOMS;
188
#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;
}