BondedUtilities.cpp 10.1 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
9
 * Portions copyright (c) 2011-2023 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * Authors: 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/>.      *
 * -------------------------------------------------------------------------- */

27
28
#include "openmm/common/BondedUtilities.h"
#include "openmm/common/ComputeContext.h"
29
30
31
32
33
34
#include "openmm/OpenMMException.h"
#include <iostream>

using namespace OpenMM;
using namespace std;

35
BondedUtilities::BondedUtilities(ComputeContext& context) : context(context), numForceBuffers(0), maxBonds(0), allGroups(0), hasInitializedKernels(false) {
36
37
}

38
void BondedUtilities::addInteraction(const vector<vector<int> >& atoms, const string& source, int group) {
39
40
41
42
    if (atoms.size() > 0) {
        forceAtoms.push_back(atoms);
        forceSource.push_back(source);
        forceGroup.push_back(group);
peastman's avatar
peastman committed
43
        allGroups |= 1<<group;
44
45
46
    }
}

47
48
string BondedUtilities::addArgument(ArrayInterface& data, const string& type) {
    arguments.push_back(&data);
49
50
51
52
    argTypes.push_back(type);
    return "customArg"+context.intToString(arguments.size());
}

53
string BondedUtilities::addEnergyParameterDerivative(const string& param) {
54
55
56
57
58
59
60
61
62
63
64
65
    // See if the parameter has already been added.
    
    int index;
    for (index = 0; index < energyParameterDerivatives.size(); index++)
        if (param == energyParameterDerivatives[index])
            break;
    if (index == energyParameterDerivatives.size())
        energyParameterDerivatives.push_back(param);
    context.addEnergyParameterDerivative(param);
    return string("energyParamDeriv")+context.intToString(index);
}

66
void BondedUtilities::addPrefixCode(const string& source) {
67
68
69
    for (int i = 0; i < (int) prefixCode.size(); i++)
        if (prefixCode[i] == source)
            return;
70
71
72
    prefixCode.push_back(source);
}

73
void BondedUtilities::initialize(const System& system) {
74
    int numForces = forceAtoms.size();
75
76
    hasInteractions = (numForces > 0);
    if (!hasInteractions)
77
78
79
80
81
82
83
84
        return;
    
    // Build the lists of atom indices.
    
    atomIndices.resize(numForces);
    for (int i = 0; i < numForces; i++) {
        int numBonds = forceAtoms[i].size();
        int numAtoms = forceAtoms[i][0].size();
85
        int numArrays = (numAtoms+3)/4;
86
        int startAtom = 0;
87
88
        atomIndices[i].resize(numArrays);
        for (int j = 0; j < numArrays; j++) {
89
            int width = min(numAtoms-startAtom, 4);
90
            int paddedWidth = (width == 3 ? 4 : width);
91
            vector<unsigned int> indexVec(paddedWidth*numBonds);
92
93
            for (int bond = 0; bond < numBonds; bond++) {
                for (int atom = 0; atom < width; atom++)
94
                    indexVec[bond*paddedWidth+atom] = forceAtoms[i][bond][startAtom+atom];
95
            }
96
97
            atomIndices[i][j].initialize(context, numBonds, 4*paddedWidth, "bondedIndices");
            atomIndices[i][j].upload(&indexVec[0]);
98
99
100
101
102
103
104
105
106
            startAtom += width;
        }
    }

    // Create the kernel.

    stringstream s;
    for (int i = 0; i < (int) prefixCode.size(); i++)
        s<<prefixCode[i];
107
    s<<"KERNEL void computeBondedForces(GLOBAL mm_ulong* RESTRICT forceBuffer, GLOBAL mixed* RESTRICT energyBuffer, GLOBAL const real4* RESTRICT posq, int groups, real4 periodicBoxSize, real4 invPeriodicBoxSize, real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ";
108
109
    for (int force = 0; force < numForces; force++) {
        for (int i = 0; i < (int) atomIndices[force].size(); i++) {
Peter Eastman's avatar
Peter Eastman committed
110
            int indexWidth = atomIndices[force][i].getElementSize()/4;
111
112
            string indexType = (indexWidth == 1 ? "unsigned int" : "uint"+context.intToString(indexWidth));
            s<<", GLOBAL const "<<indexType<<"* RESTRICT atomIndices"<<force<<"_"<<i;
113
114
115
        }
    }
    for (int i = 0; i < (int) arguments.size(); i++)
116
        s<<", GLOBAL "<<argTypes[i]<<"* customArg"<<(i+1);
117
    if (energyParameterDerivatives.size() > 0)
118
        s<<", GLOBAL mixed* RESTRICT energyParamDerivs";
119
    s<<") {\n";
120
    s<<"mixed energy = 0;\n";
121
122
    for (int i = 0; i < energyParameterDerivatives.size(); i++)
        s<<"mixed energyParamDeriv"<<i<<" = 0;\n";
123
124
    for (int force = 0; force < numForces; force++)
        s<<createForceSource(force, forceAtoms[force].size(), forceAtoms[force][0].size(), forceGroup[force], forceSource[force]);
125
    s<<"energyBuffer[GLOBAL_ID] += energy;\n";
126
127
128
129
130
    const vector<string>& allParamDerivNames = context.getEnergyParamDerivNames();
    int numDerivs = allParamDerivNames.size();
    for (int i = 0; i < energyParameterDerivatives.size(); i++)
        for (int index = 0; index < numDerivs; index++)
            if (allParamDerivNames[index] == energyParameterDerivatives[i])
131
                s<<"energyParamDerivs[(GLOBAL_ID)*"<<numDerivs<<"+"<<index<<"] += energyParamDeriv"<<i<<";\n";
132
133
134
    s<<"}\n";
    map<string, string> defines;
    defines["PADDED_NUM_ATOMS"] = context.intToString(context.getPaddedNumAtoms());
135
136
    ComputeProgram program = context.compileProgram(s.str(), defines);
    kernel = program->createKernel("computeBondedForces");
137
138
139
140
    forceAtoms.clear();
    forceSource.clear();
}

141
string BondedUtilities::createForceSource(int forceIndex, int numBonds, int numAtoms, int group, const string& computeForce) {
142
    maxBonds = max(maxBonds, numBonds);
143
144
    string suffix1[] = {""};
    string suffix4[] = {".x", ".y", ".z", ".w"};
145
146
    stringstream s;
    s<<"if ((groups&"<<(1<<group)<<") != 0)\n";
147
    s<<"for (unsigned int index = GLOBAL_ID; index < "<<numBonds<<"; index += GLOBAL_SIZE) {\n";
148
149
    int startAtom = 0;
    for (int i = 0; i < (int) atomIndices[forceIndex].size(); i++) {
Peter Eastman's avatar
Peter Eastman committed
150
        int indexWidth = atomIndices[forceIndex][i].getElementSize()/4;
151
152
        string* suffix = (indexWidth == 1 ? suffix1 : suffix4);
        string indexType = (indexWidth == 1 ? "unsigned int" : "uint"+context.intToString(indexWidth));
153
        s<<"    "<<indexType<<" atoms"<<i<<" = atomIndices"<<forceIndex<<"_"<<i<<"[index];\n";
154
155
        int atomsToLoad = min(indexWidth, numAtoms-startAtom);
        for (int j = 0; j < atomsToLoad; j++) {
156
            s<<"    unsigned int atom"<<(startAtom+j+1)<<" = atoms"<<i<<suffix[j]<<";\n";
157
            s<<"    real4 pos"<<(startAtom+j+1)<<" = posq[atom"<<(startAtom+j+1)<<"];\n";
158
159
160
161
162
        }
        startAtom += indexWidth;
    }
    s<<computeForce<<"\n";
    for (int i = 0; i < numAtoms; i++) {
163
164
165
166
        s<<"    ATOMIC_ADD(&forceBuffer[atom"<<(i+1)<<"], (mm_ulong) realToFixedPoint(force"<<(i+1)<<".x));\n";
        s<<"    ATOMIC_ADD(&forceBuffer[atom"<<(i+1)<<"+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force"<<(i+1)<<".y));\n";
        s<<"    ATOMIC_ADD(&forceBuffer[atom"<<(i+1)<<"+PADDED_NUM_ATOMS*2], (mm_ulong) realToFixedPoint(force"<<(i+1)<<".z));\n";
        s<<"    MEM_FENCE;\n";
167
168
169
170
171
    }
    s<<"}\n";
    return s.str();
}

172
void BondedUtilities::computeInteractions(int groups) {
peastman's avatar
peastman committed
173
174
    if ((groups&allGroups) == 0)
        return;
175
176
    if (!hasInitializedKernels) {
        hasInitializedKernels = true;
177
178
179
180
181
        kernel->addArg(context.getLongForceBuffer());
        kernel->addArg(context.getEnergyBuffer());
        kernel->addArg(context.getPosq());
        for (int i = 0; i < 6; i++)
            kernel->addArg();
182
183
        for (int i = 0; i < (int) atomIndices.size(); i++)
            for (int j = 0; j < (int) atomIndices[i].size(); j++)
184
                kernel->addArg(atomIndices[i][j]);
185
        for (int i = 0; i < (int) arguments.size(); i++)
186
            kernel->addArg(*arguments[i]);
187
        if (energyParameterDerivatives.size() > 0)
188
            kernel->addArg(context.getEnergyParamDerivBuffer());
189
    }
190
191
    if (!hasInteractions)
        return;
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    kernel->setArg(3, groups);
    Vec3 a, b, c;
    context.getPeriodicBoxVectors(a, b, c);
    if (context.getUseDoublePrecision()) {
        kernel->setArg(4, mm_double4(a[0], b[1], c[2], 0.0));
        kernel->setArg(5, mm_double4(1.0/a[0], 1.0/b[1], 1.0/c[2], 0.0));
        kernel->setArg(6, mm_double4(a[0], a[1], a[2], 0.0));
        kernel->setArg(7, mm_double4(b[0], b[1], b[2], 0.0));
        kernel->setArg(8, mm_double4(c[0], c[1], c[2], 0.0));
    }
    else {
        kernel->setArg(4, mm_float4((float) a[0], (float) b[1], (float) c[2], 0.0f));
        kernel->setArg(5, mm_float4(1.0f/(float) a[0], 1.0f/(float) b[1], 1.0f/(float) c[2], 0.0f));
        kernel->setArg(6, mm_float4((float) a[0], (float) a[1], (float) a[2], 0.0f));
        kernel->setArg(7, mm_float4((float) b[0], (float) b[1], (float) b[2], 0.0f));
        kernel->setArg(8, mm_float4((float) c[0], (float) c[1], (float) c[2], 0.0f));
    }
    kernel->execute(maxBonds);
210
}