OpenCLKernels.cpp 5.02 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) 2008-2025 Stanford University and the Authors.      *
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 "OpenCLKernels.h"
28
#include "OpenCLForceInfo.h"
29
30
#include "openmm/Context.h"
#include "openmm/internal/ContextImpl.h"
31
#include "openmm/internal/NonbondedForceImpl.h"
32
33
#include "openmm/common/CommonKernelUtilities.h"
#include "openmm/common/ContextSelector.h"
34
#include "CommonKernelSources.h"
Peter Eastman's avatar
Peter Eastman committed
35
#include "OpenCLBondedUtilities.h"
36
#include "OpenCLExpressionUtilities.h"
37
#include "OpenCLIntegrationUtilities.h"
38
#include "OpenCLNonbondedUtilities.h"
39
#include "OpenCLKernelSources.h"
40
#include "OpenCLQueue.h"
41
42
#include "SimTKOpenMMRealType.h"
#include "SimTKOpenMMUtilities.h"
43
#include <algorithm>
44
#include <assert.h>
45
#include <cmath>
46
#include <iterator>
47
#include <set>
48
49
50

using namespace OpenMM;
using namespace std;
51

52
53
54
55
56
57
58
static void getOpenCLPmeParameters(OpenCLContext& cl, bool& usePmeQueue, bool& deviceIsCpu) {
    string vendor = cl.getDevice().getInfo<CL_DEVICE_VENDOR>();
    bool isNvidia = (vendor.size() >= 6 && vendor.substr(0, 6) == "NVIDIA");
    usePmeQueue = (!cl.getPlatformData().disablePmeStream && !cl.getPlatformData().useCpuPme && isNvidia);
    deviceIsCpu = (cl.getDevice().getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_CPU);
}

59
void OpenCLCalcForcesAndEnergyKernel::initialize(const System& system) {
60
61
}

62
void OpenCLCalcForcesAndEnergyKernel::beginComputation(ContextImpl& context, bool includeForces, bool includeEnergy, int groups) {
63
    cl.setForcesValid(true);
64
    cl.clearAutoclearBuffers();
65
    cl.updateGlobalParamValues();
peastman's avatar
peastman committed
66
67
    for (auto computation : cl.getPreComputations())
        computation->computeForceAndEnergy(includeForces, includeEnergy, groups);
68
    OpenCLNonbondedUtilities& nb = cl.getNonbondedUtilities();
69
    cl.setComputeForceCount(cl.getComputeForceCount()+1);
70
    nb.prepareInteractions(groups);
71
    map<string, double>& derivs = cl.getEnergyParamDerivWorkspace();
peastman's avatar
peastman committed
72
73
    for (auto& param : context.getParameters())
        derivs[param.first] = 0;
74
75
}

76
double OpenCLCalcForcesAndEnergyKernel::finishComputation(ContextImpl& context, bool includeForces, bool includeEnergy, int groups, bool& valid) {
77
    cl.getBondedUtilities().computeInteractions(groups);
78
    cl.getNonbondedUtilities().computeInteractions(groups, includeForces, includeEnergy);
79
    double sum = 0.0;
peastman's avatar
peastman committed
80
81
    for (auto computation : cl.getPostComputations())
        sum += computation->computeForceAndEnergy(includeForces, includeEnergy, groups);
82
    cl.reduceForces();
83
    cl.getIntegrationUtilities().distributeForcesFromVirtualSites();
Peter Eastman's avatar
Peter Eastman committed
84
85
    if (includeEnergy)
        sum += cl.reduceEnergy();
86
87
    if (!cl.getForcesValid())
        valid = false;
88
    return sum;
89
90
}

91
void OpenCLCalcNonbondedForceKernel::initialize(const System& system, const NonbondedForce& force) {
92
93
    bool usePmeQueue, deviceIsCpu;
    getOpenCLPmeParameters(cl, usePmeQueue, deviceIsCpu);
94
    commonInitialize(system, force, usePmeQueue, deviceIsCpu, true, cl.getPlatformData().useCpuPme);
95
}
96
97
98
99
100
101

void OpenCLCalcConstantPotentialForceKernel::initialize(const System& system, const ConstantPotentialForce& force) {
    bool usePmeQueue, deviceIsCpu;
    getOpenCLPmeParameters(cl, usePmeQueue, deviceIsCpu);
    commonInitialize(system, force, deviceIsCpu, true);
}