OpenCLBondedUtilities.cpp 9.93 KB
Newer Older
Peter Eastman's avatar
Peter Eastman committed
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-2022 Stanford University and the Authors.      *
Peter Eastman's avatar
Peter Eastman committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * 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/>.      *
 * -------------------------------------------------------------------------- */

#include "OpenCLBondedUtilities.h"
28
#include "OpenCLContext.h"
Peter Eastman's avatar
Peter Eastman committed
29
30
31
32
33
34
35
36
#include "OpenCLExpressionUtilities.h"
#include "openmm/OpenMMException.h"
#include "OpenCLNonbondedUtilities.h"
#include <iostream>

using namespace OpenMM;
using namespace std;

37
OpenCLBondedUtilities::OpenCLBondedUtilities(OpenCLContext& context) : context(context), maxBonds(0), allGroups(0), hasInitializedKernels(false) {
Peter Eastman's avatar
Peter Eastman committed
38
39
}

40
void OpenCLBondedUtilities::addInteraction(const vector<vector<int> >& atoms, const string& source, int group) {
Peter Eastman's avatar
Peter Eastman committed
41
42
43
    if (atoms.size() > 0) {
        forceAtoms.push_back(atoms);
        forceSource.push_back(source);
44
        forceGroup.push_back(group);
peastman's avatar
peastman committed
45
        allGroups |= 1<<group;
Peter Eastman's avatar
Peter Eastman committed
46
        int width = 1;
47
        while (width < (int) atoms[0].size())
Peter Eastman's avatar
Peter Eastman committed
48
49
50
51
52
            width *= 2;
        indexWidth.push_back(width);
    }
}

53
string OpenCLBondedUtilities::addArgument(cl::Memory& data, const string& type) {
Peter Eastman's avatar
Peter Eastman committed
54
55
    arguments.push_back(&data);
    argTypes.push_back(type);
56
    return "customArg"+context.intToString(arguments.size());
Peter Eastman's avatar
Peter Eastman committed
57
58
}

59
60
61
62
string OpenCLBondedUtilities::addArgument(ArrayInterface& data, const string& type) {
    return addArgument(context.unwrap(data).getDeviceBuffer(), type);
}

63
64
65
66
67
68
69
70
71
72
73
74
75
string OpenCLBondedUtilities::addEnergyParameterDerivative(const string& param) {
    // 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);
}

76
void OpenCLBondedUtilities::addPrefixCode(const string& source) {
77
78
79
    for (int i = 0; i < (int) prefixCode.size(); i++)
        if (prefixCode[i] == source)
            return;
80
81
82
    prefixCode.push_back(source);
}

Peter Eastman's avatar
Peter Eastman committed
83
84
85
86
87
void OpenCLBondedUtilities::initialize(const System& system) {
    int numForces = forceAtoms.size();
    if (numForces == 0)
        return;
    
88
    // Build the lists of atom indices.
Peter Eastman's avatar
Peter Eastman committed
89
    
peastman's avatar
peastman committed
90
    atomIndices.resize(numForces);
Peter Eastman's avatar
Peter Eastman committed
91
92
93
94
95
96
97
98
99
    for (int i = 0; i < numForces; i++) {
        int numBonds = forceAtoms[i].size();
        int numAtoms = forceAtoms[i][0].size();
        int width = indexWidth[i];
        vector<cl_uint> indexVec(width*numBonds);
        for (int bond = 0; bond < numBonds; bond++) {
            for (int atom = 0; atom < numAtoms; atom++)
                indexVec[bond*width+atom] = forceAtoms[i][bond][atom];
        }
peastman's avatar
peastman committed
100
101
        atomIndices[i].initialize<cl_uint>(context, indexVec.size(), "bondedIndices");
        atomIndices[i].upload(indexVec);
Peter Eastman's avatar
Peter Eastman committed
102
103
    }

104
    // Create the kernel.
Peter Eastman's avatar
Peter Eastman committed
105

106
107
108
    stringstream s;
    for (int i = 0; i < (int) prefixCode.size(); i++)
        s<<prefixCode[i];
109
    s<<"__kernel void computeBondedForces(__global unsigned long* restrict forceBuffers, __global mixed* restrict energyBuffer, __global const real4* restrict posq, int groups, real4 periodicBoxSize, real4 invPeriodicBoxSize, real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ";
110
111
112
    for (int force = 0; force < numForces; force++) {
        string indexType = "uint"+(indexWidth[force] == 1 ? "" : context.intToString(indexWidth[force]));
        s<<", __global const "<<indexType<<"* restrict atomIndices"<<force;
Peter Eastman's avatar
Peter Eastman committed
113
    }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    for (int i = 0; i < (int) arguments.size(); i++)
        s<<", __global "<<argTypes[i]<<"* customArg"<<(i+1);
    if (energyParameterDerivatives.size() > 0)
        s<<", __global mixed* restrict energyParamDerivs";
    s<<") {\n";
    s<<"mixed energy = 0;\n";
    for (int i = 0; i < energyParameterDerivatives.size(); i++)
        s<<"mixed energyParamDeriv"<<i<<" = 0;\n";
    for (int force = 0; force < numForces; force++)
        s<<createForceSource(force, forceAtoms[force].size(), forceAtoms[force][0].size(), forceGroup[force], forceSource[force]);
    s<<"energyBuffer[get_global_id(0)] += energy;\n";
    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])
                s<<"energyParamDerivs[get_global_id(0)*"<<numDerivs<<"+"<<index<<"] += energyParamDeriv"<<i<<";\n";
    s<<"}\n";
    map<string, string> defines;
    defines["PADDED_NUM_ATOMS"] = context.intToString(context.getPaddedNumAtoms());
    cl::Program program = context.createProgram(s.str(), defines);
    kernel = cl::Kernel(program, "computeBondedForces");
Peter Eastman's avatar
Peter Eastman committed
136
137
138
139
    forceAtoms.clear();
    forceSource.clear();
}

140
string OpenCLBondedUtilities::createForceSource(int forceIndex, int numBonds, int numAtoms, int group, const string& computeForce) {
Peter Eastman's avatar
Peter Eastman committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    maxBonds = max(maxBonds, numBonds);
    int width = 1;
    while (width < numAtoms)
        width *= 2;
    string suffix1[] = {""};
    string suffix4[] = {".x", ".y", ".z", ".w"};
    string suffix16[] = {".s0", ".s1", ".s2", ".s3", ".s4", ".s5", ".s6", ".s7",
        ".s8", ".s9", ".s10", ".s11", ".s12", ".s13", ".s14", ".s15"};
    string* suffix;
    if (width == 1)
        suffix = suffix1;
    else if (width <= 4)
        suffix = suffix4;
    else
        suffix = suffix16;
156
    string indexType = "uint"+(width == 1 ? "" : context.intToString(width));
Peter Eastman's avatar
Peter Eastman committed
157
    stringstream s;
158
    s<<"if ((groups&"<<(1<<group)<<") != 0)\n";
Peter Eastman's avatar
Peter Eastman committed
159
160
161
162
    s<<"for (unsigned int index = get_global_id(0); index < "<<numBonds<<"; index += get_global_size(0)) {\n";
    s<<"    "<<indexType<<" atoms = atomIndices"<<forceIndex<<"[index];\n";
    for (int i = 0; i < numAtoms; i++) {
        s<<"    unsigned int atom"<<(i+1)<<" = atoms"<<suffix[i]<<";\n";
163
        s<<"    real4 pos"<<(i+1)<<" = posq[atom"<<(i+1)<<"];\n";
Peter Eastman's avatar
Peter Eastman committed
164
165
166
167
    }
    s<<computeForce<<"\n";
    for (int i = 0; i < numAtoms; i++) {
        s<<"    {\n";
168
169
170
        s<<"    ATOMIC_ADD(&forceBuffers[atom"<<(i+1)<<"], (mm_ulong) realToFixedPoint(force"<<(i+1)<<".x));\n";
        s<<"    ATOMIC_ADD(&forceBuffers[atom"<<(i+1)<<"+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force"<<(i+1)<<".y));\n";
        s<<"    ATOMIC_ADD(&forceBuffers[atom"<<(i+1)<<"+2*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(force"<<(i+1)<<".z));\n";
Peter Eastman's avatar
Peter Eastman committed
171
172
173
174
175
176
        s<<"    }\n";
    }
    s<<"}\n";
    return s.str();
}

177
void OpenCLBondedUtilities::computeInteractions(int groups) {
peastman's avatar
peastman committed
178
179
    if ((groups&allGroups) == 0)
        return;
Peter Eastman's avatar
Peter Eastman committed
180
181
    if (!hasInitializedKernels) {
        hasInitializedKernels = true;
182
183
184
185
186
187
188
189
190
191
192
        int index = 0;
        kernel.setArg<cl::Buffer>(index++, context.getLongForceBuffer().getDeviceBuffer());
        kernel.setArg<cl::Buffer>(index++, context.getEnergyBuffer().getDeviceBuffer());
        kernel.setArg<cl::Buffer>(index++, context.getPosq().getDeviceBuffer());
        index += 6;
        for (int j = 0; j < (int) atomIndices.size(); j++)
            kernel.setArg<cl::Buffer>(index++, atomIndices[j].getDeviceBuffer());
        for (int j = 0; j < (int) arguments.size(); j++)
            kernel.setArg<cl::Memory>(index++, *arguments[j]);
        if (energyParameterDerivatives.size() > 0)
            kernel.setArg<cl::Memory>(index++, context.getEnergyParamDerivBuffer().getDeviceBuffer());
Peter Eastman's avatar
Peter Eastman committed
193
    }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    kernel.setArg<cl_int>(3, groups);
    if (context.getUseDoublePrecision()) {
        kernel.setArg<mm_double4>(4, context.getPeriodicBoxSizeDouble());
        kernel.setArg<mm_double4>(5, context.getInvPeriodicBoxSizeDouble());
        kernel.setArg<mm_double4>(6, context.getPeriodicBoxVecXDouble());
        kernel.setArg<mm_double4>(7, context.getPeriodicBoxVecYDouble());
        kernel.setArg<mm_double4>(8, context.getPeriodicBoxVecZDouble());
    }
    else {
        kernel.setArg<mm_float4>(4, context.getPeriodicBoxSize());
        kernel.setArg<mm_float4>(5, context.getInvPeriodicBoxSize());
        kernel.setArg<mm_float4>(6, context.getPeriodicBoxVecX());
        kernel.setArg<mm_float4>(7, context.getPeriodicBoxVecY());
        kernel.setArg<mm_float4>(8, context.getPeriodicBoxVecZ());
208
    }
209
    context.executeKernel(kernel, maxBonds);
Peter Eastman's avatar
Peter Eastman committed
210
}