Commit 6bbef600 authored by Peter Eastman's avatar Peter Eastman
Browse files

OpenCL kernels are now stored into a C++ class at build time rather than...

OpenCL kernels are now stored into a C++ class at build time rather than installing them as text files.
parent f38f956d
......@@ -82,8 +82,12 @@ ENDFOREACH(subdir)
INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/src)
# Install kernel files that will be loaded at runtime.
FILE(GLOB OPENCL_KERNELS ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/*.cl src/kernels/*.h)
INSTALL_FILES(/lib/plugins/opencl FILES ${OPENCL_KERNELS})
# Set variables needed for encoding kernel sources into a C++ class
SET(CL_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
SET(CL_KERNELS_CPP ${CMAKE_CURRENT_BINARY_DIR}/src/OpenCLKernelSources.cpp)
SET(CL_KERNELS_H ${CMAKE_CURRENT_BINARY_DIR}/src/OpenCLKernelSources.h)
SET(SOURCE_FILES ${SOURCE_FILES} ${CL_KERNELS_CPP} ${CL_KERNELS_H})
INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_BINARY_DIR}/src)
SUBDIRS (sharedTarget)
FILE(GLOB OPENCL_KERNELS ${CL_SOURCE_DIR}/kernels/*.cl)
SET(CL_FILE_DECLARATIONS)
SET(CL_FILE_DEFINITIONS)
CONFIGURE_FILE(${CL_SOURCE_DIR}/OpenCLKernelSources.cpp.in ${CL_KERNELS_CPP})
FOREACH(file ${OPENCL_KERNELS})
# Load the file contents and process it.
FILE(STRINGS ${file} file_content NEWLINE_CONSUME)
STRING(REPLACE \\\n \\\\\n file_content "${file_content}")
STRING(REPLACE \n \\n file_content "${file_content}")
STRING(REPLACE \" \\\" file_content "${file_content}")
# Determine a name for the variable that will contain this file's contents
FILE(RELATIVE_PATH filename ${CL_SOURCE_DIR}/kernels ${file})
STRING(LENGTH ${filename} filename_length)
MATH(EXPR filename_length ${filename_length}-3)
STRING(SUBSTRING ${filename} 0 ${filename_length} variable_name)
# Record the variable declaration and definition.
SET(CL_FILE_DECLARATIONS ${CL_FILE_DECLARATIONS}static\ const\ std::string\ ${variable_name};\n)
FILE(APPEND ${CL_KERNELS_CPP} const\ string\ OpenCLKernelSources::${variable_name}\ =\ \"${file_content}\"\;\n)
ENDFOREACH(file)
CONFIGURE_FILE(${CL_SOURCE_DIR}/OpenCLKernelSources.h.in ${CL_KERNELS_H})
......@@ -4,6 +4,13 @@
INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/../FindOpenCL.cmake)
INCLUDE_DIRECTORIES(${OPENCL_INCLUDE_DIR})
FILE(GLOB OPENCL_KERNELS ${CL_SOURCE_DIR}/kernels/*.cl)
ADD_CUSTOM_COMMAND(OUTPUT ${CL_KERNELS_CPP} ${CL_KERNELS_H}
COMMAND ${CMAKE_COMMAND}
ARGS -D CL_SOURCE_DIR=${CL_SOURCE_DIR} -D CL_KERNELS_CPP=${CL_KERNELS_CPP} -D CL_KERNELS_H=${CL_KERNELS_H} -P /Users/peastman/workspace/openmm/platforms/opencl/EncodeCLFiles.cmake
DEPENDS ${OPENCL_KERNELS}
)
SET_SOURCE_FILES_PROPERTIES(${CL_KERNELS_CPP} ${CL_KERNELS_H} PROPERTIES GENERATED TRUE)
ADD_LIBRARY(${SHARED_TARGET} SHARED ${SOURCE_FILES} ${SOURCE_INCLUDE_FILES} ${API_ABS_INCLUDE_FILES})
TARGET_LINK_LIBRARIES(${SHARED_TARGET} debug ${OPENMM_LIBRARY_NAME}_d optimized ${OPENMM_LIBRARY_NAME} ${OPENCL_LIBRARIES})
......
......@@ -25,12 +25,13 @@
*/
#include "OpenCLCompact.h"
#include "OpenCLKernelSources.h"
using namespace OpenMM;
OpenCLCompact::OpenCLCompact(OpenCLContext& context) : context(context), dgBlockCounts(NULL) {
dgBlockCounts = new OpenCLArray<cl_uint>(context, context.getNumThreadBlocks(), "dgBlockCounts");
cl::Program program = context.createProgram(context.loadSourceFromFile("compact.cl"));
cl::Program program = context.createProgram(OpenCLKernelSources::compact);
countKernel = cl::Kernel(program, "countElts");
moveValidKernel = cl::Kernel(program, "moveValidElementsStaged");
}
......
......@@ -28,6 +28,7 @@
#include "OpenCLArray.h"
#include "OpenCLForceInfo.h"
#include "OpenCLIntegrationUtilities.h"
#include "OpenCLKernelSources.h"
#include "OpenCLNonbondedUtilities.h"
#include "hilbert.h"
#include "openmm/Platform.h"
......@@ -43,11 +44,10 @@ using namespace std;
OpenCLContext::OpenCLContext(int numParticles, int deviceIndex) : time(0.0), stepCount(0), computeForceCount(0) {
try {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
cl_context_properties cprops[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[0](), 0 };
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
cl_context_properties cprops[] = {CL_CONTEXT_PLATFORM, (cl_context_properties) platforms[0](), 0};
context = cl::Context(CL_DEVICE_TYPE_ALL, cprops);
vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
const int minThreadBlockSize = 32;
if (deviceIndex < 0 || deviceIndex >= (int) devices.size()) {
......@@ -93,7 +93,7 @@ OpenCLContext::OpenCLContext(int numParticles, int deviceIndex) : time(0.0), ste
// Create utility kernels that are used in multiple places.
utilities = createProgram(loadSourceFromFile("utilities.cl"));
utilities = createProgram(OpenCLKernelSources::utilities);
clearBufferKernel = cl::Kernel(utilities, "clearBuffer");
reduceFloat4Kernel = cl::Kernel(utilities, "reduceFloat4Buffer");
}
......@@ -143,10 +143,6 @@ void OpenCLContext::addForce(OpenCLForceInfo* force) {
}
string OpenCLContext::loadSourceFromFile(const string& filename) const {
return loadSourceFromFile(filename, map<string, string>());
}
string OpenCLContext::loadSourceFromFile(const string& filename, const std::map<std::string, std::string>& replacements) const {
ifstream file((Platform::getDefaultPluginsDirectory()+"/opencl/"+filename).c_str());
if (!file.is_open())
throw OpenMMException("Unable to load kernel: "+filename);
......@@ -158,15 +154,24 @@ string OpenCLContext::loadSourceFromFile(const string& filename, const std::map<
kernel += '\n';
}
file.close();
return kernel;
}
string OpenCLContext::loadSourceFromFile(const string& filename, const std::map<std::string, std::string>& replacements) const {
return replaceStrings(loadSourceFromFile(filename), replacements);
}
string OpenCLContext::replaceStrings(const string& input, const std::map<std::string, std::string>& replacements) const {
string result = input;
for (map<string, string>::const_iterator iter = replacements.begin(); iter != replacements.end(); iter++) {
int index = -1;
do {
index = kernel.find(iter->first);
if (index != kernel.npos)
kernel.replace(index, iter->first.size(), iter->second);
} while (index != kernel.npos);
index = result.find(iter->first);
if (index != result.npos)
result.replace(index, iter->first.size(), iter->second);
} while (index != result.npos);
}
return kernel;
return result;
}
cl::Program OpenCLContext::createProgram(const string source) {
......
......@@ -192,6 +192,14 @@ public:
* @param replacements a set of strings that should be replaced with new strings wherever they appear in the
*/
std::string loadSourceFromFile(const std::string& filename, const std::map<std::string, std::string>& replacements) const;
/**
* Replace all occurance of a list of substrings.
*
* @param input a string to process
* @param replacements a set of strings that should be replaced with new strings wherever they appear in the input string
* @return a new string produced by performing the replacements
*/
std::string replaceStrings(const std::string& input, const std::map<std::string, std::string>& replacements) const;
/**
* Create an OpenCL Program from source code.
*/
......
......@@ -26,7 +26,7 @@
#include "OpenCLFFT3D.h"
#include "OpenCLExpressionUtilities.h"
#include "OpenCLExpressionUtilities.h"
#include "OpenCLKernelSources.h"
#include "../src/SimTKUtilities/SimTKOpenMMRealType.h"
#include <map>
#include <sstream>
......@@ -177,7 +177,7 @@ cl::Kernel OpenCLFFT3D::createKernel(int xsize, int ysize, int zsize, int xmult,
replacements["ZMULT"] = OpenCLExpressionUtilities::intToString(zmult);
replacements["M_PI"] = OpenCLExpressionUtilities::doubleToString(M_PI);
replacements["COMPUTE_FFT"] = source.str();
cl::Program program = context.createProgram(context.loadSourceFromFile("fft.cl", replacements));
cl::Program program = context.createProgram(context.replaceStrings(OpenCLKernelSources::fft, replacements));
cl::Kernel kernel(program, "execFFT");
kernel.setArg(2, xsize*sizeof(mm_float2), NULL);
kernel.setArg(3, xsize*sizeof(mm_float2), NULL);
......
......@@ -26,6 +26,7 @@
#include "OpenCLIntegrationUtilities.h"
#include "OpenCLArray.h"
#include "OpenCLKernelSources.h"
#include <cmath>
#include <cstdlib>
#include <map>
......@@ -78,9 +79,9 @@ OpenCLIntegrationUtilities::OpenCLIntegrationUtilities(OpenCLContext& context, c
// Create kernels for enforcing constraints.
cl::Program settleProgram = context.createProgram(context.loadSourceFromFile("settle.cl"));
cl::Program settleProgram = context.createProgram(OpenCLKernelSources::settle);
settleKernel = cl::Kernel(settleProgram, "applySettle");
cl::Program shakeProgram = context.createProgram(context.loadSourceFromFile("shakeHydrogens.cl"));
cl::Program shakeProgram = context.createProgram(OpenCLKernelSources::shakeHydrogens);
shakeKernel = cl::Kernel(shakeProgram, "applyShakeToHydrogens");
// Record the set of constraints and how many constraints each atom is involved in.
......@@ -319,7 +320,7 @@ void OpenCLIntegrationUtilities::initRandomNumberGenerator(unsigned int randomNu
// Create the kernel.
cl::Program randomProgram = context.createProgram(context.loadSourceFromFile("random.cl"));
cl::Program randomProgram = context.createProgram(OpenCLKernelSources::random);
randomKernel = cl::Kernel(randomProgram, "generateRandomNumbers");
}
......
/* -------------------------------------------------------------------------- *
* 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) 2010 Stanford University and the Authors. *
* 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 "OpenCLKernelSources.h"
using namespace OpenMM;
using namespace std;
#ifndef OPENMM_OPENCLKERNELSOURCES_H_
#define OPENMM_OPENCLKERNELSOURCES_H_
/* -------------------------------------------------------------------------- *
* 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) 2010 Stanford University and the Authors. *
* 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 <string>
namespace OpenMM {
/**
* This class is a central holding place for the source code of OpenCL kernels.
* The CMake build script inserts declarations into it based on the .cl files in the
* kernels subfolder.
*/
class OpenCLKernelSources {
public:
@CL_FILE_DECLARATIONS@
};
} // namespace OpenMM
#endif /*OPENMM_OPENCLKERNELSOURCES_H_*/
......@@ -33,6 +33,7 @@
#include "OpenCLExpressionUtilities.h"
#include "OpenCLIntegrationUtilities.h"
#include "OpenCLNonbondedUtilities.h"
#include "OpenCLKernelSources.h"
#include "lepton/Parser.h"
#include "lepton/ParsedExpression.h"
#include "../src/SimTKUtilities/SimTKOpenMMRealType.h"
......@@ -222,7 +223,7 @@ void OpenCLCalcHarmonicBondForceKernel::initialize(const System& system, const H
for (int i = 0; i < (int) forceBufferCounter.size(); i++)
maxBuffers = max(maxBuffers, forceBufferCounter[i]);
cl.addForce(new OpenCLBondForceInfo(maxBuffers, force));
cl::Program program = cl.createProgram(cl.loadSourceFromFile("harmonicBondForce.cl"));
cl::Program program = cl.createProgram(OpenCLKernelSources::harmonicBondForce);
kernel = cl::Kernel(program, "calcHarmonicBondForce");
}
......@@ -358,7 +359,7 @@ void OpenCLCalcCustomBondForceKernel::initialize(const System& system, const Cus
map<string, string> replacements;
replacements["COMPUTE_FORCE"] = compute.str();
replacements["EXTRA_ARGUMENTS"] = extraArguments;
cl::Program program = cl.createProgram(cl.loadSourceFromFile("customBondForce.cl", replacements));
cl::Program program = cl.createProgram(cl.replaceStrings(OpenCLKernelSources::customBondForce, replacements));
kernel = cl::Kernel(program, "computeCustomBondForces");
}
......@@ -458,7 +459,7 @@ void OpenCLCalcHarmonicAngleForceKernel::initialize(const System& system, const
for (int i = 0; i < (int) forceBufferCounter.size(); i++)
maxBuffers = max(maxBuffers, forceBufferCounter[i]);
cl.addForce(new OpenCLAngleForceInfo(maxBuffers, force));
cl::Program program = cl.createProgram(cl.loadSourceFromFile("harmonicAngleForce.cl"));
cl::Program program = cl.createProgram(OpenCLKernelSources::harmonicAngleForce);
kernel = cl::Kernel(program, "calcHarmonicAngleForce");
}
......@@ -542,7 +543,7 @@ void OpenCLCalcPeriodicTorsionForceKernel::initialize(const System& system, cons
for (int i = 0; i < (int) forceBufferCounter.size(); i++)
maxBuffers = max(maxBuffers, forceBufferCounter[i]);
cl.addForce(new OpenCLPeriodicTorsionForceInfo(maxBuffers, force));
cl::Program program = cl.createProgram(cl.loadSourceFromFile("periodicTorsionForce.cl"));
cl::Program program = cl.createProgram(OpenCLKernelSources::periodicTorsionForce);
kernel = cl::Kernel(program, "calcPeriodicTorsionForce");
}
......@@ -626,7 +627,7 @@ void OpenCLCalcRBTorsionForceKernel::initialize(const System& system, const RBTo
for (int i = 0; i < (int) forceBufferCounter.size(); i++)
maxBuffers = max(maxBuffers, forceBufferCounter[i]);
cl.addForce(new OpenCLRBTorsionForceInfo(maxBuffers, force));
cl::Program program = cl.createProgram(cl.loadSourceFromFile("rbTorsionForce.cl"));
cl::Program program = cl.createProgram(OpenCLKernelSources::rbTorsionForce);
kernel = cl::Kernel(program, "calcRBTorsionForce");
}
......@@ -774,7 +775,7 @@ void OpenCLCalcNonbondedForceKernel::initialize(const System& system, const Nonb
replacements["RECIPROCAL_BOX_SIZE_Z"] = doubleToString(2.0*M_PI/boxVectors[2][2]);
replacements["RECIPROCAL_COEFFICIENT"] = doubleToString(ONE_4PI_EPS0*4*M_PI/(boxVectors[0][0]*boxVectors[1][1]*boxVectors[2][2]));
replacements["EXP_COEFFICIENT"] = doubleToString(-1.0/(4.0*alpha*alpha));
cl::Program program = cl.createProgram(cl.loadSourceFromFile("ewald.cl"), replacements);
cl::Program program = cl.createProgram(OpenCLKernelSources::ewald, replacements);
ewaldSumsKernel = cl::Kernel(program, "calculateEwaldCosSinSums");
ewaldForcesKernel = cl::Kernel(program, "calculateEwaldForces");
cosSinSums = new OpenCLArray<mm_float2>(cl, (2*kmaxx-1)*(2*kmaxy-1)*(2*kmaxz-1), "cosSinSums");
......@@ -784,7 +785,7 @@ void OpenCLCalcNonbondedForceKernel::initialize(const System& system, const Nonb
// Add the interaction to the default nonbonded kernel.
string source = cl.loadSourceFromFile("coulombLennardJones.cl", defines);
string source = cl.replaceStrings(OpenCLKernelSources::coulombLennardJones, defines);
cl.getNonbondedUtilities().addInteraction(useCutoff, usePeriodic, true, force.getCutoffDistance(), exclusionList, source);
if (hasLJ)
cl.getNonbondedUtilities().addParameter(OpenCLNonbondedUtilities::ParameterInfo("sigmaEpsilon", "float2", sizeof(cl_float2), sigmaEpsilon->getDeviceBuffer()));
......@@ -818,7 +819,7 @@ void OpenCLCalcNonbondedForceKernel::initialize(const System& system, const Nonb
}
if (usePeriodic)
defines["USE_PERIODIC"] = "1";
cl::Program program = cl.createProgram(cl.loadSourceFromFile("nonbondedExceptions.cl"), defines);
cl::Program program = cl.createProgram(OpenCLKernelSources::nonbondedExceptions, defines);
exceptionsKernel = cl::Kernel(program, "computeNonbondedExceptions");
}
......@@ -997,7 +998,7 @@ void OpenCLCalcCustomNonbondedForceKernel::initialize(const System& system, cons
compute << OpenCLExpressionUtilities::createExpressions(forceExpressions, variables, functionDefinitions, prefix+"temp", prefix+"functionParams");
map<string, string> replacements;
replacements["COMPUTE_FORCE"] = compute.str();
string source = cl.loadSourceFromFile("customNonbonded.cl", replacements);
string source = cl.replaceStrings(OpenCLKernelSources::customNonbonded, replacements);
cl.getNonbondedUtilities().addInteraction(useCutoff, usePeriodic, true, force.getCutoffDistance(), exclusionList, source);
for (int i = 0; i < (int) params->getBuffers().size(); i++) {
const OpenCLNonbondedUtilities::ParameterInfo& buffer = params->getBuffers()[i];
......@@ -1079,7 +1080,7 @@ void OpenCLCalcGBSAOBCForceKernel::initialize(const System& system, const GBSAOB
prefactor = -ONE_4PI_EPS0*((1.0/force.getSoluteDielectric())-(1.0/force.getSolventDielectric()));
bool useCutoff = (force.getNonbondedMethod() != GBSAOBCForce::NoCutoff);
bool usePeriodic = (force.getNonbondedMethod() != GBSAOBCForce::NoCutoff && force.getNonbondedMethod() != GBSAOBCForce::CutoffNonPeriodic);
string source = cl.loadSourceFromFile("gbsaObc2.cl");
string source = OpenCLKernelSources::gbsaObc2;
nb.addInteraction(useCutoff, usePeriodic, false, force.getCutoffDistance(), vector<vector<int> >(), source);
nb.addParameter(OpenCLNonbondedUtilities::ParameterInfo("obcParams", "float2", sizeof(cl_float2), params->getDeviceBuffer()));;
nb.addParameter(OpenCLNonbondedUtilities::ParameterInfo("bornForce", "float", sizeof(cl_float), bornForce->getDeviceBuffer()));;
......@@ -1106,8 +1107,8 @@ void OpenCLCalcGBSAOBCForceKernel::executeForces(ContextImpl& context) {
defines["PREFACTOR"] = doubleToString(prefactor);
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
defines["PADDED_NUM_ATOMS"] = intToString(cl.getPaddedNumAtoms());
string filename = (cl.getSIMDWidth() == 32 ? "gbsaObc_nvidia.cl" : "gbsaObc_default.cl");
cl::Program program = cl.createProgram(cl.loadSourceFromFile(filename), defines);
string file = (cl.getSIMDWidth() == 32 ? OpenCLKernelSources::gbsaObc_nvidia : OpenCLKernelSources::gbsaObc_default);
cl::Program program = cl.createProgram(file, defines);
computeBornSumKernel = cl::Kernel(program, "computeBornSum");
computeBornSumKernel.setArg<cl::Buffer>(0, bornSum->getDeviceBuffer());
computeBornSumKernel.setArg(1, OpenCLContext::ThreadBlockSize*sizeof(cl_float), NULL);
......@@ -1145,7 +1146,7 @@ void OpenCLCalcGBSAOBCForceKernel::executeForces(ContextImpl& context) {
force1Kernel.setArg<cl::Buffer>(10, nb.getTiles().getDeviceBuffer());
force1Kernel.setArg<cl_uint>(11, nb.getTiles().getSize());
}
program = cl.createProgram(cl.loadSourceFromFile("gbsaObcReductions.cl"), defines);
program = cl.createProgram(OpenCLKernelSources::gbsaObcReductions, defines);
reduceBornSumKernel = cl::Kernel(program, "reduceBornSum");
reduceBornSumKernel.setArg<cl_int>(0, cl.getPaddedNumAtoms());
reduceBornSumKernel.setArg<cl_int>(1, nb.getNumForceBuffers());
......@@ -1408,8 +1409,8 @@ void OpenCLCalcCustomGBForceKernel::initialize(const System& system, const Custo
defines["CUTOFF_SQUARED"] = doubleToString(force.getCutoffDistance()*force.getCutoffDistance());
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
defines["PADDED_NUM_ATOMS"] = intToString(cl.getPaddedNumAtoms());
string filename = (cl.getSIMDWidth() == 32 ? "customGBValueN2_nvidia.cl" : "customGBValueN2_default.cl");
cl::Program program = cl.createProgram(cl.loadSourceFromFile(filename, replacements), defines);
string file = (cl.getSIMDWidth() == 32 ? OpenCLKernelSources::customGBValueN2_nvidia : OpenCLKernelSources::customGBValueN2_default);
cl::Program program = cl.createProgram(cl.replaceStrings(file, replacements), defines);
pairValueKernel = cl::Kernel(program, "computeN2Value");
}
{
......@@ -1450,7 +1451,7 @@ void OpenCLCalcCustomGBForceKernel::initialize(const System& system, const Custo
replacements["COMPUTE_VALUES"] = reductionSource.str();
map<string, string> defines;
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
cl::Program program = cl.createProgram(cl.loadSourceFromFile("customGBValuePerParticle.cl", replacements), defines);
cl::Program program = cl.createProgram(cl.replaceStrings(OpenCLKernelSources::customGBValuePerParticle, replacements), defines);
perParticleValueKernel = cl::Kernel(program, "computePerParticleValues");
}
{
......@@ -1556,8 +1557,8 @@ void OpenCLCalcCustomGBForceKernel::initialize(const System& system, const Custo
defines["CUTOFF_SQUARED"] = doubleToString(force.getCutoffDistance()*force.getCutoffDistance());
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
defines["PADDED_NUM_ATOMS"] = intToString(cl.getPaddedNumAtoms());
string filename = (cl.getSIMDWidth() == 32 ? "customGBEnergyN2_nvidia.cl" : "customGBEnergyN2_default.cl");
cl::Program program = cl.createProgram(cl.loadSourceFromFile(filename, replacements), defines);
string file = (cl.getSIMDWidth() == 32 ? OpenCLKernelSources::customGBEnergyN2_nvidia : OpenCLKernelSources::customGBEnergyN2_default);
cl::Program program = cl.createProgram(cl.replaceStrings(file, replacements), defines);
pairEnergyKernel = cl::Kernel(program, "computeN2Energy");
}
{
......@@ -1612,7 +1613,7 @@ void OpenCLCalcCustomGBForceKernel::initialize(const System& system, const Custo
replacements["COMPUTE_ENERGY"] = compute.str();
map<string, string> defines;
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
cl::Program program = cl.createProgram(cl.loadSourceFromFile("customGBEnergyPerParticle.cl", replacements), defines);
cl::Program program = cl.createProgram(cl.replaceStrings(OpenCLKernelSources::customGBEnergyPerParticle, replacements), defines);
perParticleEnergyKernel = cl::Kernel(program, "computePerParticleEnergy");
}
{
......@@ -1673,7 +1674,7 @@ void OpenCLCalcCustomGBForceKernel::initialize(const System& system, const Custo
}
map<string, string> replacements;
replacements["COMPUTE_FORCE"] = chainSource.str();
string source = cl.loadSourceFromFile("customGBChainRule.cl", replacements);
string source = cl.replaceStrings(OpenCLKernelSources::customGBChainRule, replacements);
cl.getNonbondedUtilities().addInteraction(useCutoff, usePeriodic, true, force.getCutoffDistance(), exclusionList, source);
for (int i = 0; i < (int) params->getBuffers().size(); i++) {
const OpenCLNonbondedUtilities::ParameterInfo& buffer = params->getBuffers()[i];
......@@ -1944,7 +1945,7 @@ void OpenCLCalcCustomExternalForceKernel::initialize(const System& system, const
map<string, string> replacements;
replacements["COMPUTE_FORCE"] = compute.str();
replacements["EXTRA_ARGUMENTS"] = extraArguments;
cl::Program program = cl.createProgram(cl.loadSourceFromFile("customExternalForce.cl", replacements));
cl::Program program = cl.createProgram(cl.replaceStrings(OpenCLKernelSources::customExternalForce, replacements));
kernel = cl::Kernel(program, "computeCustomExternalForces");
}
......@@ -1988,7 +1989,7 @@ OpenCLIntegrateVerletStepKernel::~OpenCLIntegrateVerletStepKernel() {
void OpenCLIntegrateVerletStepKernel::initialize(const System& system, const VerletIntegrator& integrator) {
cl.initialize(system);
cl::Program program = cl.createProgram(cl.loadSourceFromFile("verlet.cl"));
cl::Program program = cl.createProgram(OpenCLKernelSources::verlet);
kernel1 = cl::Kernel(program, "integrateVerletPart1");
kernel2 = cl::Kernel(program, "integrateVerletPart2");
prevStepSize = -1.0;
......@@ -2052,7 +2053,7 @@ void OpenCLIntegrateLangevinStepKernel::initialize(const System& system, const L
map<string, string> defines;
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
defines["PADDED_NUM_ATOMS"] = intToString(cl.getPaddedNumAtoms());
cl::Program program = cl.createProgram(cl.loadSourceFromFile("langevin.cl"), defines);
cl::Program program = cl.createProgram(OpenCLKernelSources::langevin, defines);
kernel1 = cl::Kernel(program, "integrateLangevinPart1");
kernel2 = cl::Kernel(program, "integrateLangevinPart2");
kernel3 = cl::Kernel(program, "integrateLangevinPart3");
......@@ -2189,7 +2190,7 @@ void OpenCLIntegrateBrownianStepKernel::initialize(const System& system, const B
cl.getIntegrationUtilities().initRandomNumberGenerator(integrator.getRandomNumberSeed());
map<string, string> defines;
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
cl::Program program = cl.createProgram(cl.loadSourceFromFile("brownian.cl"), defines);
cl::Program program = cl.createProgram(OpenCLKernelSources::brownian, defines);
kernel1 = cl::Kernel(program, "integrateBrownianPart1");
kernel2 = cl::Kernel(program, "integrateBrownianPart2");
prevStepSize = -1.0;
......@@ -2244,7 +2245,7 @@ OpenCLIntegrateVariableVerletStepKernel::~OpenCLIntegrateVariableVerletStepKerne
void OpenCLIntegrateVariableVerletStepKernel::initialize(const System& system, const VariableVerletIntegrator& integrator) {
cl.initialize(system);
cl::Program program = cl.createProgram(cl.loadSourceFromFile("verlet.cl"));
cl::Program program = cl.createProgram(OpenCLKernelSources::verlet);
kernel1 = cl::Kernel(program, "integrateVerletPart1");
kernel2 = cl::Kernel(program, "integrateVerletPart2");
selectSizeKernel = cl::Kernel(program, "selectVerletStepSize");
......@@ -2319,7 +2320,7 @@ void OpenCLIntegrateVariableLangevinStepKernel::initialize(const System& system,
map<string, string> defines;
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
defines["PADDED_NUM_ATOMS"] = intToString(cl.getPaddedNumAtoms());
cl::Program program = cl.createProgram(cl.loadSourceFromFile("langevin.cl"), defines);
cl::Program program = cl.createProgram(OpenCLKernelSources::langevin, defines);
kernel1 = cl::Kernel(program, "integrateLangevinPart1");
kernel2 = cl::Kernel(program, "integrateLangevinPart2");
kernel3 = cl::Kernel(program, "integrateLangevinPart3");
......@@ -2414,7 +2415,7 @@ void OpenCLApplyAndersenThermostatKernel::initialize(const System& system, const
map<string, string> defines;
defines["NUM_ATOMS"] = intToString(cl.getNumAtoms());
defines["PADDED_NUM_ATOMS"] = intToString(cl.getPaddedNumAtoms());
cl::Program program = cl.createProgram(cl.loadSourceFromFile("andersenThermostat.cl"), defines);
cl::Program program = cl.createProgram(OpenCLKernelSources::andersenThermostat, defines);
kernel = cl::Kernel(program, "applyAndersenThermostat");
}
......@@ -2467,7 +2468,7 @@ void OpenCLRemoveCMMotionKernel::initialize(const System& system, const CMMotion
totalMass += system.getParticleMass(i);
map<string, string> defines;
defines["INVERSE_TOTAL_MASS"] = doubleToString(1.0/totalMass);
cl::Program program = cl.createProgram(cl.loadSourceFromFile("removeCM.cl"), defines);
cl::Program program = cl.createProgram(OpenCLKernelSources::removeCM, defines);
kernel1 = cl::Kernel(program, "calcCenterOfMassMomentum");
kernel1.setArg<cl_int>(0, numAtoms);
kernel1.setArg<cl::Buffer>(1, cl.getVelm().getDeviceBuffer());
......
......@@ -27,6 +27,7 @@
#include "OpenCLNonbondedUtilities.h"
#include "OpenCLArray.h"
#include "OpenCLCompact.h"
#include "OpenCLKernelSources.h"
#include <map>
using namespace OpenMM;
......@@ -233,7 +234,7 @@ void OpenCLNonbondedUtilities::initialize(const System& system) {
defines["USE_OUTPUT_BUFFER_PER_BLOCK"] = "1";
if (usePeriodic)
defines["USE_PERIODIC"] = "1";
cl::Program interactingBlocksProgram = context.createProgram(context.loadSourceFromFile("findInteractingBlocks.cl"), defines);
cl::Program interactingBlocksProgram = context.createProgram(OpenCLKernelSources::findInteractingBlocks, defines);
findBlockBoundsKernel = cl::Kernel(interactingBlocksProgram, "findBlockBounds");
findBlockBoundsKernel.setArg<cl_int>(0, context.getNumAtoms());
findBlockBoundsKernel.setArg<mm_float4>(1, periodicBoxSize);
......@@ -367,8 +368,8 @@ cl::Kernel OpenCLNonbondedUtilities::createInteractionKernel(const string& sourc
padded << context.getPaddedNumAtoms();
defines["NUM_ATOMS"] = natom.str();
defines["PADDED_NUM_ATOMS"] = padded.str();
string filename = (context.getSIMDWidth() == 32 ? "nonbonded_nvidia.cl" : "nonbonded_default.cl");
cl::Program program = context.createProgram(context.loadSourceFromFile(filename, replacements), defines);
string file = (context.getSIMDWidth() == 32 ? OpenCLKernelSources::nonbonded_nvidia : OpenCLKernelSources::nonbonded_default);
cl::Program program = context.createProgram(context.replaceStrings(file, replacements), defines);
cl::Kernel kernel(program, "computeNonbonded");
// Set arguments to the Kernel.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment