kCalculateAmoebaCudaWcaDispersion.h 7.79 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Scott Le Grand, Peter Eastman                                     *
 * Contributors:                                                              *
 *                                                                            *
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

__global__
#if (__CUDA_ARCH__ >= 200)
Peter Eastman's avatar
Peter Eastman committed
29
__launch_bounds__(384, 1)
30
#elif (__CUDA_ARCH__ >= 120)
Peter Eastman's avatar
Peter Eastman committed
31
__launch_bounds__(192, 1)
Mark Friedrichs's avatar
Mark Friedrichs committed
32
#else
Peter Eastman's avatar
Peter Eastman committed
33
__launch_bounds__(64, 1)
Mark Friedrichs's avatar
Mark Friedrichs committed
34
#endif
35
36

void METHOD_NAME(kCalculateAmoebaWcaDispersion, _kernel)( unsigned int* workUnit ){
Mark Friedrichs's avatar
Mark Friedrichs committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

    extern __shared__ WcaDispersionParticle sA[];

    unsigned int totalWarps      = gridDim.x*blockDim.x/GRID;
    unsigned int warp            = (blockIdx.x*blockDim.x+threadIdx.x)/GRID;
    unsigned int numWorkUnits    = cSim.pInteractionCount[0];
    unsigned int pos             = warp*numWorkUnits/totalWarps;
    unsigned int end             = (warp+1)*numWorkUnits/totalWarps;
    unsigned int lasty           = 0xFFFFFFFF;

    float4 jCoord;
    float jRadius;
    float jEpsilon;
    float totalEnergy            = 0.0f;

    while (pos < end)
    {

        unsigned int x;
        unsigned int y;
        bool bExclusionFlag;

        // Extract cell coordinates

        decodeCell( workUnit[pos], &x, &y, &bExclusionFlag );

        unsigned int tgx                 = threadIdx.x & (GRID - 1);
        unsigned int tbx                 = threadIdx.x - tgx;
        unsigned int tj                  = tgx;

        WcaDispersionParticle*  psA      = &sA[tbx];
        unsigned int atomI               = x + tgx;
69
70
71
        float4 iCoord                    = cSim.pPosq[atomI];
        float iRadius                    = cAmoebaSim.pWcaDispersionRadiusEpsilon[atomI].x;
        float iEpsilon                   = cAmoebaSim.pWcaDispersionRadiusEpsilon[atomI].y;
Mark Friedrichs's avatar
Mark Friedrichs committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

        float forceSum[3];

        float emixo,emixh;
        float rmixo,rmixh;

        float emjxo,emjxh;
        float rmjxo,rmjxh;

        calculateWcaDispersionInit_kernel( iRadius,   iEpsilon,
                                           &rmixo,    &rmixh,
                                           &emixo,    &emixh );

        forceSum[0]                      = 0.0f;
        forceSum[1]                      = 0.0f;
        forceSum[2]                      = 0.0f;

        // load coordinates, charge, ...

        if (lasty != y)
        {
93
            loadWcaDispersionShared( &(sA[threadIdx.x]), (y+tgx), cSim.pPosq, cAmoebaSim.pWcaDispersionRadiusEpsilon );
Mark Friedrichs's avatar
Mark Friedrichs committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

        }

       // zero shared fields

        zeroWcaDispersionSharedForce( &(sA[threadIdx.x]) );

        for (unsigned int j = 0; j < GRID; j++)
        {

            float ijForce[3];

            // load coords, charge, ...

            loadWcaDispersionData( &(psA[tj]), &jCoord, &jRadius, &jEpsilon ); 

            // calculate force

            float energy;
            calculateWcaDispersionPairIxn_kernel( iCoord, jCoord,
                                                  iRadius,jRadius,
                                                  rmixo,  rmixh,
                                                  emixo,  emixh,
117
                                                  ijForce, &energy);
Mark Friedrichs's avatar
Mark Friedrichs committed
118

119
            if( (atomI != (y+tj)) && (atomI < cSim.atoms) && ((y+tj) < cSim.atoms) ){
Mark Friedrichs's avatar
Mark Friedrichs committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
       
                // add to field at atomI the field due atomJ's dipole

                forceSum[0]              += ijForce[0];
                forceSum[1]              += ijForce[1];
                forceSum[2]              += ijForce[2];
    
                // add to field at atomJ the field due atomI's dipole

                psA[tj].force[0]         -= ijForce[0];
                psA[tj].force[1]         -= ijForce[1];
                psA[tj].force[2]         -= ijForce[2];

                totalEnergy              += (x == y) ? 0.5f*energy : energy;
            }

            calculateWcaDispersionInit_kernel( jRadius,   jEpsilon,
                                               &rmjxo,    &rmjxh,
                                               &emjxo,    &emjxh );

            calculateWcaDispersionPairIxn_kernel( jCoord, iCoord,
                                                  jRadius,iRadius,
                                                  rmjxo,  rmjxh,
                                                  emjxo,  emjxh,
144
                                                  ijForce, &energy);
Mark Friedrichs's avatar
Mark Friedrichs committed
145

146
            if( (atomI != (y+tj)) && (atomI < cSim.atoms) && ((y+tj) < cSim.atoms) ){
Mark Friedrichs's avatar
Mark Friedrichs committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
       
                // add to field at atomI the field due atomJ's dipole

                forceSum[0]              -= ijForce[0];
                forceSum[1]              -= ijForce[1];
                forceSum[2]              -= ijForce[2];
    
                // add to field at atomJ the field due atomI's dipole

                psA[tj].force[0]         += ijForce[0];
                psA[tj].force[1]         += ijForce[1];
                psA[tj].force[2]         += ijForce[2];

                totalEnergy              += (x == y) ? 0.5f*energy : energy;
            }

            tj  = (tj + 1) & (GRID - 1);

        }

        // Write results

#ifdef USE_OUTPUT_BUFFER_PER_WARP
170
171
        unsigned int offset                 = (x + tgx + warp*cSim.paddedNumberOfAtoms);
        add3dArrayToFloat4( offset, forceSum,  cSim.pForce4);
Mark Friedrichs's avatar
Mark Friedrichs committed
172

173
        // include diagonal only once
Mark Friedrichs's avatar
Mark Friedrichs committed
174

175
176
177
178
        if( x != y ){
            offset                              = (y + tgx + warp*cSim.paddedNumberOfAtoms);
            add3dArrayToFloat4( offset, sA[threadIdx.x].force, cSim.pForce4);
        }
Mark Friedrichs's avatar
Mark Friedrichs committed
179
180

#else
181
182
        unsigned int offset                 = (x + tgx + (y >> GRIDBITS) * cSim.paddedNumberOfAtoms);
        add3dArrayToFloat4( offset, forceSum,    cSim.pForce4);
Mark Friedrichs's avatar
Mark Friedrichs committed
183

184
185
186
187
188
189
        // include diagonal only once

        if( x != y ){
            offset                              = (y + tgx + (x >> GRIDBITS) * cSim.paddedNumberOfAtoms);
            add3dArrayToFloat4( offset, sA[threadIdx.x].force,    cSim.pForce4 );
        }
Mark Friedrichs's avatar
Mark Friedrichs committed
190
191
192
193
194

#endif
        lasty = y;
        pos++;
    }
195

Mark Friedrichs's avatar
Mark Friedrichs committed
196
197
198
199
200
    cSim.pEnergy[blockIdx.x * blockDim.x + threadIdx.x] -= cAmoebaSim.awater*totalEnergy;
    if( (blockIdx.x*blockDim.x + threadIdx.x) == 0 ){
        cSim.pEnergy[0] += cAmoebaSim.totalMaxWcaDispersionEnergy;
    }
}