"platforms/common/vscode:/vscode.git/clone" did not exist on "e069e1f147b9e38a141ec4b436fbe1749787cd9f"
nonbonded_default.cl 6.72 KB
Newer Older
1
#define TILE_SIZE 32
2

3
4
5
6
7
8
typedef struct {
    float4 posq;
    float4 force;
    ATOM_PARAMETER_DATA
} AtomData;

9
10
11
12
13
/**
 * Compute nonbonded interactions.
 */

__kernel void computeNonbonded(__global float4* forceBuffers, __global float* energyBuffer, __global float4* posq, __global unsigned int* exclusions,
14
        __global unsigned int* exclusionIndices, __local AtomData* localData, __local float4* tempBuffer, __global unsigned int* tiles,
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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];
32
        unsigned int y = ((x >> 2) & 0x7fff)*TILE_SIZE;
33
        bool hasExclusions = (x & 0x1);
34
35
36
37
        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);
38
39
40
41
42
43
44
        unsigned int atom1 = x + tgx;
        float4 force = 0.0f;
        float4 posq1 = posq[atom1];
        LOAD_ATOM1_PARAMETERS
        if (x == y) {
            // This tile is on the diagonal.

45
            localData[get_local_id(0)].posq = posq1;
46
47
            LOAD_LOCAL_PARAMETERS_FROM_1
            barrier(CLK_LOCAL_MEM_FENCE);
48
49
            unsigned int xi = x/TILE_SIZE;
            unsigned int tile = xi+xi*PADDED_NUM_ATOMS/TILE_SIZE-xi*(xi+1)/2;
50
51
52
#ifdef USE_EXCLUSIONS
            unsigned int excl = exclusions[exclusionIndices[tile]+tgx] >> baseLocalAtom;
#endif
53
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
54
55
56
57
#ifdef USE_EXCLUSIONS
                bool isExcluded = !(excl & 0x1);
#endif
                int atom2 = baseLocalAtom+j;
58
                float4 posq2 = localData[atom2].posq;
59
60
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
#ifdef USE_PERIODIC
61
62
63
                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;
64
65
#endif
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
66
67
                float invR = native_rsqrt(r2);
                float r = native_recip(invR);
68
69
70
71
72
73
74
75
76
77
78
79
80
                LOAD_ATOM2_PARAMETERS
                atom2 = y+baseLocalAtom+j;
                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.

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

96
            if (lasty != y && get_local_id(0) < TILE_SIZE) {
97
                unsigned int j = y + tgx;
98
                localData[get_local_id(0)].posq = posq[j];
99
100
                LOAD_LOCAL_PARAMETERS_FROM_GLOBAL
            }
101
            localData[get_local_id(0)].force = 0.0f;
102
103
            barrier(CLK_LOCAL_MEM_FENCE);

104
105
106
107
108
            // 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;
109
#ifdef USE_EXCLUSIONS
110
111
112
            unsigned int excl = (hasExclusions ? exclusions[exclusionIndices[tile]+tgx] : 0xFFFFFFFF);
            excl = (excl >> tgx) | (excl << (TILE_SIZE - tgx));
            excl >>= baseLocalAtom;
113
#endif
114
115
            unsigned int tj = tgx%(TILE_SIZE/2);
            for (unsigned int j = 0; j < TILE_SIZE/2; j++) {
116
#ifdef USE_EXCLUSIONS
117
                bool isExcluded = !(excl & 0x1);
118
#endif
119
                int atom2 = baseLocalAtom+tj;
120
                float4 posq2 = localData[atom2].posq;
121
                float4 delta = (float4) (posq2.xyz - posq1.xyz, 0.0f);
122
#ifdef USE_PERIODIC
123
124
125
                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;
126
#endif
127
                float r2 = delta.x*delta.x + delta.y*delta.y + delta.z*delta.z;
128
129
                float invR = native_rsqrt(r2);
                float r = native_recip(invR);
130
131
132
133
134
135
136
137
                LOAD_ATOM2_PARAMETERS
                atom2 = y+baseLocalAtom+tj;
                float dEdR = 0.0f;
                float tempEnergy = 0.0f;
                COMPUTE_INTERACTION
                energy += tempEnergy;
                delta.xyz *= dEdR;
                force.xyz -= delta.xyz;
138
                localData[baseLocalAtom+tj+forceBufferOffset].force.xyz += delta.xyz;
139
140
141
                barrier(CLK_LOCAL_MEM_FENCE);
                excl >>= 1;
                tj = (tj+1)%(TILE_SIZE/2);
142
143
144
145
            }

            // Sum the forces and write results.

146
            if (get_local_id(0) >= TILE_SIZE)
147
148
                tempBuffer[get_local_id(0)] = force;
            barrier(CLK_LOCAL_MEM_FENCE);
149
            if (get_local_id(0) < TILE_SIZE) {
150
#ifdef USE_OUTPUT_BUFFER_PER_BLOCK
151
152
                unsigned int offset1 = x + tgx + (y/TILE_SIZE)*PADDED_NUM_ATOMS;
                unsigned int offset2 = y + tgx + (x/TILE_SIZE)*PADDED_NUM_ATOMS;
153
154
155
156
#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
157
                forceBuffers[offset1].xyz = forceBuffers[offset1].xyz+force.xyz+tempBuffer[get_local_id(0)+TILE_SIZE].xyz;
158
                forceBuffers[offset2].xyz = forceBuffers[offset2].xyz+localData[get_local_id(0)].force.xyz+localData[get_local_id(0)+TILE_SIZE].force.xyz;
159
160
161
162
163
164
165
            }
            lasty = y;
        }
        pos++;
    }
    energyBuffer[get_global_id(0)] += energy;
}