customGBEnergyN2_nvidia.cl 7.65 KB
Newer Older
1
#define TILE_SIZE 32
2
3
#define STORE_DERIVATIVE_1(INDEX) derivBuffers##INDEX[offset1] += deriv##INDEX##_1;
#define STORE_DERIVATIVE_2(INDEX) derivBuffers##INDEX[offset2] += local_deriv##INDEX[get_local_id(0)];
4
5
6
7
8

/**
 * Compute a force based on pair interactions.
 */

9
10
__kernel __attribute__((reqd_work_group_size(WORK_GROUP_SIZE, 1, 1)))
void computeN2Energy(__global float4* forceBuffers, __global float* energyBuffer, __local float4* local_force,
11
	__global float4* posq, __local float4* local_posq, __global unsigned int* exclusions, __global unsigned int* exclusionIndices,
12
        __global unsigned int* exclusionRowIndices, __local float4* tempBuffer,
13
#ifdef USE_CUTOFF
14
        __global unsigned int* tiles, __global unsigned int* interactionFlags, __global unsigned int* interactionCount, float4 periodicBoxSize, float4 invPeriodicBoxSize
15
16
17
18
19
20
21
22
23
24
25
26
27
#else
        unsigned int numTiles
#endif
        PARAMETER_ARGUMENTS) {
#ifdef USE_CUTOFF
    unsigned int numTiles = interactionCount[0];
#endif
    unsigned int totalWarps = get_global_size(0)/TILE_SIZE;
    unsigned int warp = get_global_id(0)/TILE_SIZE;
    unsigned int pos = warp*numTiles/totalWarps;
    unsigned int end = (warp+1)*numTiles/totalWarps;
    float energy = 0.0f;
    unsigned int lasty = 0xFFFFFFFF;
28
29
    __local unsigned int exclusionRange[4];
    __local int exclusionIndex[2];
30
31
32

    while (pos < end) {
        // Extract the coordinates of this tile
33
#ifdef USE_CUTOFF
34
        unsigned int x = tiles[pos];
35
36
37
38
39
40
41
42
43
44
        unsigned int y = ((x >> 2) & 0x7fff);
        x = (x>>17);
#else
        unsigned int y = (unsigned int) floor(NUM_BLOCKS+0.5f-sqrt((NUM_BLOCKS+0.5f)*(NUM_BLOCKS+0.5f)-2*pos));
        unsigned int x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
        if (x >= NUM_BLOCKS) { // Occasionally happens due to roundoff error.
            y++;
            x = (pos-y*NUM_BLOCKS+y*(y+1)/2);
        }
#endif
45
46
        unsigned int tgx = get_local_id(0) & (TILE_SIZE-1);
        unsigned int tbx = get_local_id(0) - tgx;
47
        unsigned int atom1 = x*TILE_SIZE + tgx;
48
49
50
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        LOAD_ATOM1_PARAMETERS
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

        // Locate the exclusion data for this tile.

#ifdef USE_EXCLUSIONS
        int localGroupIndex = get_local_id(0)/TILE_SIZE;
        if (tgx < 2)
            exclusionRange[2*localGroupIndex+tgx] = exclusionRowIndices[x+tgx];
        if (tgx == 0)
            exclusionIndex[localGroupIndex] = -1;
        for (int i = exclusionRange[2*localGroupIndex]+tgx; i < exclusionRange[2*localGroupIndex+1]; i += TILE_SIZE)
            if (exclusionIndices[i] == y)
                exclusionIndex[localGroupIndex] = i*TILE_SIZE;
        bool hasExclusions = (exclusionIndex[localGroupIndex] > -1);
#else
        bool hasExclusions = false;
#endif
67
68
69
70
71
72
        if (x == y) {
            // This tile is on the diagonal.

            local_posq[get_local_id(0)] = posq1;
            LOAD_LOCAL_PARAMETERS_FROM_1
#ifdef USE_EXCLUSIONS
73
            unsigned int excl = exclusions[exclusionIndex[localGroupIndex]+tgx];
74
75
76
77
78
79
80
81
82
#endif
            for (unsigned int j = 0; j < TILE_SIZE; j++) {
#ifdef USE_EXCLUSIONS
                bool isExcluded = !(excl & 0x1);
#endif
                int atom2 = tbx+j;
                float4 posq2 = local_posq[atom2];
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
#ifdef USE_PERIODIC
83
84
85
                delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
86
#endif
87
88
89
90
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
                if (r2 < CUTOFF_SQUARED) {
#endif
91
                float r = SQRT(r2);
92
                LOAD_ATOM2_PARAMETERS
93
                atom2 = y*TILE_SIZE+j;
94
95
96
97
                float dEdR = 0.0f;
                float tempEnergy = 0.0f;
                if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS && atom1 != atom2) {
                    COMPUTE_INTERACTION
98
                    dEdR /= -r;
99
100
101
102
                }
                energy += 0.5f*tempEnergy;
                delta.xyz *= dEdR;
                force.xyz -= delta.xyz;
103
104
105
#ifdef USE_CUTOFF
                }
#endif
106
107
108
109
110
111
112
#ifdef USE_EXCLUSIONS
                excl >>= 1;
#endif
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
113
            unsigned int offset1 = x*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
114
#else
115
            unsigned int offset1 = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
116
#endif
117
118
            forceBuffers[offset1].xyz += force.xyz;
            STORE_DERIVATIVES_1
119
120
121
122
123
        }
        else {
            // This is an off-diagonal tile.

            if (lasty != y) {
124
                unsigned int j = y*TILE_SIZE + tgx;
125
126
127
128
                local_posq[get_local_id(0)] = posq[j];
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
            local_force[get_local_id(0)] = 0.0f;
129
            CLEAR_LOCAL_DERIVATIVES
130
131
#ifdef USE_CUTOFF
            unsigned int flags = interactionFlags[pos];
132
133
            if (!hasExclusions && flags == 0) {
                // No interactions in this tile.
134
135
136
137
138
139
140
            }
            else
#endif
            {
                // Compute the full set of interactions in this tile.

#ifdef USE_EXCLUSIONS
141
                unsigned int excl = (hasExclusions ? exclusions[exclusionIndex[localGroupIndex]+tgx] : 0xFFFFFFFF);
142
143
144
145
146
147
148
149
150
151
152
                excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
#endif
                unsigned int tj = tgx;
                for (unsigned int j = 0; j < TILE_SIZE; j++) {
#ifdef USE_EXCLUSIONS
                    bool isExcluded = !(excl & 0x1);
#endif
                    int atom2 = tbx+tj;
                    float4 posq2 = local_posq[atom2];
                    float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
#ifdef USE_PERIODIC
153
154
155
                    delta.x -= floor(delta.x*invPeriodicBoxSize.x+0.5f)*periodicBoxSize.x;
                    delta.y -= floor(delta.y*invPeriodicBoxSize.y+0.5f)*periodicBoxSize.y;
                    delta.z -= floor(delta.z*invPeriodicBoxSize.z+0.5f)*periodicBoxSize.z;
156
#endif
157
158
159
160
                    float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
#ifdef USE_CUTOFF
                    if (r2 < CUTOFF_SQUARED) {
#endif
161
                    float r = SQRT(r2);
162
                    LOAD_ATOM2_PARAMETERS
163
                    atom2 = y*TILE_SIZE+tj;
164
165
166
167
                    float dEdR = 0.0f;
                    float tempEnergy = 0.0f;
                    if (atom1 < NUM_ATOMS && atom2 < NUM_ATOMS) {
                        COMPUTE_INTERACTION
168
                        dEdR /= -r;
169
170
171
172
                    }
		    energy += tempEnergy;
                    delta.xyz *= dEdR;
                    force.xyz -= delta.xyz;
173
174
                    atom2 = tbx+tj;
                    local_force[atom2].xyz += delta.xyz;
175
                    RECORD_DERIVATIVE_2
176
177
178
#ifdef USE_CUTOFF
                    }
#endif
179
180
181
182
183
184
185
186
187
#ifdef USE_EXCLUSIONS
                    excl >>= 1;
#endif
                    tj = (tj + 1) & (TILE_SIZE - 1);
                }
            }

            // Write results
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
188
189
            unsigned int offset1 = x*TILE_SIZE + tgx + y*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + x*PADDED_NUM_ATOMS;
190
#else
191
192
            unsigned int offset1 = x*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
            unsigned int offset2 = y*TILE_SIZE + tgx + warp*PADDED_NUM_ATOMS;
193
194
195
#endif
            forceBuffers[offset1].xyz += force.xyz;
            forceBuffers[offset2].xyz += local_force[get_local_id(0)].xyz;
196
197
            STORE_DERIVATIVES_1
            STORE_DERIVATIVES_2
198
199
200
201
202
203
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}