Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
7e29671a
Commit
7e29671a
authored
Feb 13, 2015
by
Peter Eastman
Browse files
Created plugin for CUDA runtime compiler
parent
26790496
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
492 additions
and
15 deletions
+492
-15
platforms/cuda/include/CudaContext.h
platforms/cuda/include/CudaContext.h
+3
-1
platforms/cuda/include/CudaKernels.h
platforms/cuda/include/CudaKernels.h
+24
-3
platforms/cuda/src/CudaContext.cpp
platforms/cuda/src/CudaContext.cpp
+44
-11
plugins/cudacompiler/CMakeLists.txt
plugins/cudacompiler/CMakeLists.txt
+109
-0
plugins/cudacompiler/include/internal/windowsExportCudaCompiler.h
...cudacompiler/include/internal/windowsExportCudaCompiler.h
+41
-0
plugins/cudacompiler/src/CudaCompilerKernelFactory.cpp
plugins/cudacompiler/src/CudaCompilerKernelFactory.cpp
+62
-0
plugins/cudacompiler/src/CudaCompilerKernelFactory.h
plugins/cudacompiler/src/CudaCompilerKernelFactory.h
+50
-0
plugins/cudacompiler/src/CudaCompilerKernels.cpp
plugins/cudacompiler/src/CudaCompilerKernels.cpp
+98
-0
plugins/cudacompiler/src/CudaCompilerKernels.h
plugins/cudacompiler/src/CudaCompilerKernels.h
+61
-0
No files found.
platforms/cuda/include/CudaContext.h
View file @
7e29671a
...
...
@@ -41,6 +41,7 @@
#include <vector_functions.h>
#include "windowsExportCuda.h"
#include "CudaPlatform.h"
#include "openmm/Kernel.h"
typedef
unsigned
int
tileflags
;
...
...
@@ -565,7 +566,7 @@ private:
int
paddedNumAtoms
;
int
numAtomBlocks
;
int
numThreadBlocks
;
bool
useBlockingSync
,
useDoublePrecision
,
useMixedPrecision
,
contextIsValid
,
atomsWereReordered
,
boxIsTriclinic
;
bool
useBlockingSync
,
useDoublePrecision
,
useMixedPrecision
,
contextIsValid
,
atomsWereReordered
,
boxIsTriclinic
,
hasCompilerKernel
;
std
::
string
compiler
,
tempDir
,
cacheDir
,
gpuArchitecture
;
float4
periodicBoxVecXFloat
,
periodicBoxVecYFloat
,
periodicBoxVecZFloat
,
periodicBoxSizeFloat
,
invPeriodicBoxSizeFloat
;
double4
periodicBoxVecX
,
periodicBoxVecY
,
periodicBoxVecZ
,
periodicBoxSize
,
invPeriodicBoxSize
;
...
...
@@ -602,6 +603,7 @@ private:
CudaBondedUtilities
*
bonded
;
CudaNonbondedUtilities
*
nonbonded
;
WorkThread
*
thread
;
Kernel
compilerKernel
;
};
struct
CudaContext
::
Molecule
{
...
...
platforms/cuda/include/CudaKernels.h
View file @
7e29671a
...
...
@@ -38,6 +38,27 @@
namespace
OpenMM
{
/**
* This abstract class defines an interface for code that can compile CUDA kernels. This allows a plugin to take advantage of runtime compilation
* when running on recent versions of CUDA.
*/
class
CudaCompilerKernel
:
public
KernelImpl
{
public:
static
std
::
string
Name
()
{
return
"CudaCompilerKernel"
;
}
CudaCompilerKernel
(
std
::
string
name
,
const
Platform
&
platform
)
:
KernelImpl
(
name
,
platform
)
{
}
/**
* Compile a kernel to PTX.
*
* @param source the source code for the kernel
* @param options the flags to be passed to the compiler
* @param cu the CudaContext for which the kernel is being compiled
*/
virtual
std
::
string
createModule
(
const
std
::
string
&
source
,
const
std
::
string
&
flags
,
CudaContext
&
cu
)
=
0
;
};
/**
* This kernel is invoked at the beginning and end of force and energy computations. It gives the
* Platform a chance to clear buffers and do other initialization at the beginning, and to do any
...
...
@@ -591,9 +612,9 @@ private:
int
getKeySize
()
const
{
return
4
;}
const
char
*
getDataType
()
const
{
return
"int2"
;}
const
char
*
getKeyType
()
const
{
return
"int"
;}
const
char
*
getMinKey
()
const
{
return
"
INT_MIN
"
;}
const
char
*
getMaxKey
()
const
{
return
"
INT_MAX
"
;}
const
char
*
getMaxValue
()
const
{
return
"make_int2(
INT_MAX, INT_MAX
)"
;}
const
char
*
getMinKey
()
const
{
return
"
(-2147483647-1)
"
;}
const
char
*
getMaxKey
()
const
{
return
"
2147483647
"
;}
const
char
*
getMaxValue
()
const
{
return
"make_int2(
2147483647, 2147483647
)"
;}
const
char
*
getSortKey
()
const
{
return
"value.y"
;}
};
class
PmeIO
;
...
...
platforms/cuda/src/CudaContext.cpp
View file @
7e29671a
...
...
@@ -33,6 +33,7 @@
#include "CudaBondedUtilities.h"
#include "CudaForceInfo.h"
#include "CudaIntegrationUtilities.h"
#include "CudaKernels.h"
#include "CudaKernelSources.h"
#include "CudaNonbondedUtilities.h"
#include "SHA1.h"
...
...
@@ -73,9 +74,16 @@ bool CudaContext::hasInitializedCuda = false;
CudaContext
::
CudaContext
(
const
System
&
system
,
int
deviceIndex
,
bool
useBlockingSync
,
const
string
&
precision
,
const
string
&
compiler
,
const
string
&
tempDir
,
const
std
::
string
&
hostCompiler
,
CudaPlatform
::
PlatformData
&
platformData
)
:
system
(
system
),
currentStream
(
0
),
time
(
0.0
),
platformData
(
platformData
),
stepCount
(
0
),
computeForceCount
(
0
),
stepsSinceReorder
(
99999
),
contextIsValid
(
false
),
atomsWereReordered
(
false
),
pinnedBuffer
(
NULL
),
posq
(
NULL
),
posqCorrection
(
NULL
),
velm
(
NULL
),
force
(
NULL
),
energyBuffer
(
NULL
),
integration
(
NULL
),
expression
(
NULL
),
bonded
(
NULL
),
nonbonded
(
NULL
),
thread
(
NULL
)
{
time
(
0.0
),
platformData
(
platformData
),
stepCount
(
0
),
computeForceCount
(
0
),
stepsSinceReorder
(
99999
),
contextIsValid
(
false
),
atomsWereReordered
(
false
),
hasCompilerKernel
(
false
),
pinnedBuffer
(
NULL
),
posq
(
NULL
),
posqCorrection
(
NULL
),
velm
(
NULL
),
force
(
NULL
),
energyBuffer
(
NULL
),
integration
(
NULL
),
expression
(
NULL
),
bonded
(
NULL
),
nonbonded
(
NULL
),
thread
(
NULL
)
{
this
->
compiler
=
"
\"
"
+
compiler
+
"
\"
"
;
try
{
compilerKernel
=
platformData
.
context
->
getPlatform
().
createKernel
(
CudaCompilerKernel
::
Name
(),
*
platformData
.
context
);
hasCompilerKernel
=
true
;
}
catch
(...)
{
// The runtime compiler plugin isn't available.
}
if
(
hostCompiler
.
size
()
>
0
)
this
->
compiler
=
compiler
+
" --compiler-bindir "
+
hostCompiler
;
if
(
!
hasInitializedCuda
)
{
...
...
@@ -508,7 +516,7 @@ CUmodule CudaContext::createModule(const string source, const map<string, string
if
(
cuModuleLoad
(
&
module
,
cacheFile
.
str
().
c_str
())
==
CUDA_SUCCESS
)
return
module
;
//
Write out the source to a
temporary file.
//
Select names for the various
temporary file
s
.
stringstream
tempFileName
;
tempFileName
<<
"openmmTempKernel"
<<
this
;
// Include a pointer to this context as part of the filename to avoid collisions.
...
...
@@ -520,6 +528,30 @@ CUmodule CudaContext::createModule(const string source, const map<string, string
string
inputFile
=
(
tempDir
+
tempFileName
.
str
()
+
".cu"
);
string
outputFile
=
(
tempDir
+
tempFileName
.
str
()
+
".ptx"
);
string
logFile
=
(
tempDir
+
tempFileName
.
str
()
+
".log"
);
int
res
=
0
;
// If the runtime compiler plugin is available, use it.
if
(
hasCompilerKernel
)
{
string
ptx
=
compilerKernel
.
getAs
<
CudaCompilerKernel
>
().
createModule
(
src
.
str
(),
"-arch=compute_"
+
gpuArchitecture
+
" "
+
options
,
*
this
);
// If possible, write the PTX out to a temporary file so we can cache it for later use.
try
{
ofstream
out
(
outputFile
.
c_str
());
out
<<
ptx
;
out
.
close
();
}
catch
(...)
{
// An error occurred. Possibly we don't have permission to write to the tmep directory. Just try to load the module directly.
CHECK_RESULT2
(
cuModuleLoadDataEx
(
&
module
,
&
ptx
[
0
],
0
,
NULL
,
NULL
),
"Error loading CUDA module"
);
return
module
;
}
}
else
{
// Write out the source to a temporary file.
ofstream
out
(
inputFile
.
c_str
());
out
<<
src
.
str
();
out
.
close
();
...
...
@@ -532,8 +564,9 @@ CUmodule CudaContext::createModule(const string source, const map<string, string
int
res
=
compileInWindows
(
command
);
#else
string
command
=
compiler
+
" --ptx --machine "
+
bits
+
" -arch=sm_"
+
gpuArchitecture
+
" -o
\"
"
+
outputFile
+
"
\"
"
+
options
+
"
\"
"
+
inputFile
+
"
\"
2>
\"
"
+
logFile
+
"
\"
"
;
int
res
=
std
::
system
(
command
.
c_str
());
res
=
std
::
system
(
command
.
c_str
());
#endif
}
try
{
if
(
res
!=
0
)
{
// Load the error log.
...
...
plugins/cudacompiler/CMakeLists.txt
0 → 100644
View file @
7e29671a
#---------------------------------------------------
# OpenMM CUDA RPMD Integrator
#
# Creates OpenMMCudaCompiler library,.
#
# Windows:
# OpenMMCudaCompiler.dll
# OpenMMCudaCompiler.lib
# Unix:
# libOpenMMCudaCompiler.so
#----------------------------------------------------
# The source is organized into subdirectories, but we handle them all from
# this CMakeLists file rather than letting CMake visit them as SUBDIRS.
SET
(
OPENMM_SOURCE_SUBDIRS .
)
# Collect up information about the version of the OpenMM library we're building
# and make it available to the code so it can be built into the binaries.
SET
(
OPENMMCUDACOMPILER_LIBRARY_NAME OpenMMCudaCompiler
)
SET
(
SHARED_TARGET
${
OPENMMCUDACOMPILER_LIBRARY_NAME
}
)
# These are all the places to search for header files which are
# to be part of the API.
SET
(
API_INCLUDE_DIRS
)
# start empty
FOREACH
(
subdir
${
OPENMM_SOURCE_SUBDIRS
}
)
# append
SET
(
API_INCLUDE_DIRS
${
API_INCLUDE_DIRS
}
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
subdir
}
/include
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
subdir
}
/include/internal
)
ENDFOREACH
(
subdir
)
# We'll need both *relative* path names, starting with their API_INCLUDE_DIRS,
# and absolute pathnames.
SET
(
API_REL_INCLUDE_FILES
)
# start these out empty
SET
(
API_ABS_INCLUDE_FILES
)
FOREACH
(
dir
${
API_INCLUDE_DIRS
}
)
FILE
(
GLOB fullpaths
${
dir
}
/*.h
)
# returns full pathnames
SET
(
API_ABS_INCLUDE_FILES
${
API_ABS_INCLUDE_FILES
}
${
fullpaths
}
)
FOREACH
(
pathname
${
fullpaths
}
)
GET_FILENAME_COMPONENT
(
filename
${
pathname
}
NAME
)
SET
(
API_REL_INCLUDE_FILES
${
API_REL_INCLUDE_FILES
}
${
dir
}
/
${
filename
}
)
ENDFOREACH
(
pathname
)
ENDFOREACH
(
dir
)
# collect up source files
SET
(
SOURCE_FILES
)
# empty
SET
(
SOURCE_INCLUDE_FILES
)
FOREACH
(
subdir
${
OPENMM_SOURCE_SUBDIRS
}
)
FILE
(
GLOB_RECURSE src_files
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
subdir
}
/src/*.cpp
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
subdir
}
/src/*.c
)
FILE
(
GLOB incl_files
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
subdir
}
/src/*.h
)
SET
(
SOURCE_FILES
${
SOURCE_FILES
}
${
src_files
}
)
#append
SET
(
SOURCE_INCLUDE_FILES
${
SOURCE_INCLUDE_FILES
}
${
incl_files
}
)
INCLUDE_DIRECTORIES
(
BEFORE
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
subdir
}
/include
)
ENDFOREACH
(
subdir
)
INCLUDE_DIRECTORIES
(
BEFORE
${
CMAKE_CURRENT_SOURCE_DIR
}
/src
)
INCLUDE_DIRECTORIES
(
BEFORE
${
CMAKE_SOURCE_DIR
}
/platforms/cuda/include
)
INCLUDE_DIRECTORIES
(
${
CUDA_TOOLKIT_INCLUDE
}
)
GET_FILENAME_COMPONENT
(
CUDA_LIB_DIR
"
${
CUDA_cufft_LIBRARY
}
"
PATH
)
FIND_LIBRARY
(
CUDA_nvrtc_LIBRARY nvrtc
"
${
CUDA_LIB_DIR
}
"
)
# Build the shared plugin library.
IF
(
OPENMM_BUILD_SHARED_LIB
)
ADD_LIBRARY
(
${
SHARED_TARGET
}
SHARED
${
SOURCE_FILES
}
${
SOURCE_INCLUDE_FILES
}
${
API_INCLUDE_FILES
}
)
TARGET_LINK_LIBRARIES
(
${
SHARED_TARGET
}
${
OPENMM_LIBRARY_NAME
}
${
CUDA_nvrtc_LIBRARY
}
)
TARGET_LINK_LIBRARIES
(
${
SHARED_TARGET
}
${
OPENMM_LIBRARY_NAME
}
CUDA
)
SET_TARGET_PROPERTIES
(
${
SHARED_TARGET
}
PROPERTIES COMPILE_FLAGS
"
${
EXTRA_COMPILE_FLAGS
}
-DOPENMM_CUDACOMPILER_BUILDING_SHARED_LIBRARY"
)
IF
(
APPLE
)
SET_TARGET_PROPERTIES
(
${
SHARED_TARGET
}
PROPERTIES LINK_FLAGS
"
${
EXTRA_COMPILE_FLAGS
}
-F/Library/Frameworks -framework CUDA"
)
ELSE
(
APPLE
)
SET_TARGET_PROPERTIES
(
${
SHARED_TARGET
}
PROPERTIES LINK_FLAGS
"
${
EXTRA_COMPILE_FLAGS
}
"
)
ENDIF
(
APPLE
)
INSTALL_TARGETS
(
/lib/plugins RUNTIME_DIRECTORY /lib/plugins
${
SHARED_TARGET
}
)
ENDIF
(
OPENMM_BUILD_SHARED_LIB
)
# Build the static plugin library.
IF
(
OPENMM_BUILD_STATIC_LIB
)
ADD_LIBRARY
(
${
STATIC_TARGET
}
STATIC
${
SOURCE_FILES
}
${
SOURCE_INCLUDE_FILES
}
${
API_INCLUDE_FILES
}
)
TARGET_LINK_LIBRARIES
(
${
STATIC_TARGET
}
${
OPENMM_LIBRARY_NAME
}
_static
${
CUDA_nvrtc_LIBRARY
}
)
TARGET_LINK_LIBRARIES
(
${
STATIC_TARGET
}
${
OPENMM_LIBRARY_NAME
}
CUDA
)
SET_TARGET_PROPERTIES
(
${
STATIC_TARGET
}
PROPERTIES COMPILE_FLAGS
"
${
EXTRA_COMPILE_FLAGS
}
-DOPENMM_CUDACOMPILER_BUILDING_STATIC_LIBRARY"
)
IF
(
APPLE
)
SET_TARGET_PROPERTIES
(
${
STATIC_TARGET
}
PROPERTIES LINK_FLAGS
"
${
EXTRA_COMPILE_FLAGS
}
-F/Library/Frameworks -framework CUDA"
)
ELSE
(
APPLE
)
SET_TARGET_PROPERTIES
(
${
STATIC_TARGET
}
PROPERTIES LINK_FLAGS
"
${
EXTRA_COMPILE_FLAGS
}
"
)
ENDIF
(
APPLE
)
INSTALL_TARGETS
(
/lib/plugins RUNTIME_DIRECTORY /lib/plugins
${
STATIC_TARGET
}
)
ENDIF
(
OPENMM_BUILD_STATIC_LIB
)
INSTALL
(
TARGETS
${
SHARED_TARGET
}
DESTINATION
${
CMAKE_INSTALL_PREFIX
}
/lib/plugins
)
# Ensure that links to the main CUDA library will be resolved.
IF
(
APPLE
)
ENDIF
(
OPENMM_BUILD_SHARED_LIB
)
SET
(
CUDA_LIBRARY libOpenMMCUDA.dylib
)
INSTALL
(
CODE
"EXECUTE_PROCESS(COMMAND install_name_tool -change
${
CUDA_LIBRARY
}
@loader_path/
${
CUDA_LIBRARY
}
${
CMAKE_INSTALL_PREFIX
}
/lib/plugins/lib
${
SHARED_TARGET
}
.dylib)"
)
ENDIF
(
OPENMM_BUILD_SHARED_LIB
)
ENDIF
(
APPLE
)
plugins/cudacompiler/include/internal/windowsExportCudaCompiler.h
0 → 100644
View file @
7e29671a
#ifndef OPENMM_WINDOWSEXPORTCUDACOMPILER_H_
#define OPENMM_WINDOWSEXPORTCUDACOMPILER_H_
/*
* Shared libraries are messy in Visual Studio. We have to distinguish three
* cases:
* (1) this header is being used to build the OpenMM shared library
* (dllexport)
* (2) this header is being used by a *client* of the OpenMM shared
* library (dllimport)
* (3) we are building the OpenMM static library, or the client is
* being compiled with the expectation of linking with the
* OpenMM static library (nothing special needed)
* In the CMake script for building this library, we define one of the symbols
* OPENMM_CUDACOMPILER_BUILDING_{SHARED|STATIC}_LIBRARY
* Client code normally has no special symbol defined, in which case we'll
* assume it wants to use the shared library. However, if the client defines
* the symbol OPENMM_USE_STATIC_LIBRARIES we'll suppress the dllimport so
* that the client code can be linked with static libraries. Note that
* the client symbol is not library dependent, while the library symbols
* affect only the OpenMM library, meaning that other libraries can
* be clients of this one. However, we are assuming all-static or all-shared.
*/
#ifdef _MSC_VER
// We don't want to hear about how sprintf is "unsafe".
#pragma warning(disable:4996)
// Keep MS VC++ quiet about lack of dll export of private members.
#pragma warning(disable:4251)
#if defined(OPENMM_CUDACOMPILER_BUILDING_SHARED_LIBRARY)
#define OPENMM_EXPORT_CUDACOMPILER __declspec(dllexport)
#elif defined(OPENMM_CUDACOMPILER_BUILDING_STATIC_LIBRARY) || defined(OPENMM_CUDACOMPILER_USE_STATIC_LIBRARIES)
#define OPENMM_EXPORT_CUDACOMPILER
#else
#define OPENMM_EXPORT_CUDACOMPILER __declspec(dllimport) // i.e., a client of a shared library
#endif
#else
#define OPENMM_EXPORT_CUDACOMPILER // Linux, Mac
#endif
#endif // OPENMM_WINDOWSEXPORTCUDACOMPILER_H_
plugins/cudacompiler/src/CudaCompilerKernelFactory.cpp
0 → 100644
View file @
7e29671a
/* -------------------------------------------------------------------------- *
* OpenMMCudaCompiler *
* -------------------------------------------------------------------------- *
* 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) 2015 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 "CudaCompilerKernelFactory.h"
#include "CudaCompilerKernels.h"
#include "internal/windowsExportCudaCompiler.h"
#include "openmm/internal/ContextImpl.h"
#include "openmm/OpenMMException.h"
#include <cstdio>
using
namespace
OpenMM
;
extern
"C"
OPENMM_EXPORT_CUDACOMPILER
void
registerKernelFactories
()
{
try
{
Platform
&
platform
=
Platform
::
getPlatformByName
(
"CUDA"
);
CudaCompilerKernelFactory
*
factory
=
new
CudaCompilerKernelFactory
();
platform
.
registerKernelFactory
(
CudaCompilerKernel
::
Name
(),
factory
);
}
catch
(
std
::
exception
ex
)
{
// Ignore
}
}
#ifdef OPENMM_CUDACOMPILER_BUILDING_STATIC_LIBRARY
extern
"C"
void
registerCudaCompilerKernelFactories
()
{
registerKernelFactories
();
}
#else
extern
"C"
OPENMM_EXPORT_CUDACOMPILER
void
registerCudaCompilerKernelFactories
()
{
registerKernelFactories
();
}
extern
"C"
OPENMM_EXPORT_CUDACOMPILER
void
registerPlatforms
()
{
}
#endif
KernelImpl
*
CudaCompilerKernelFactory
::
createKernelImpl
(
std
::
string
name
,
const
Platform
&
platform
,
ContextImpl
&
context
)
const
{
if
(
name
==
CudaCompilerKernel
::
Name
())
return
new
CudaRuntimeCompilerKernel
(
name
,
platform
);
throw
OpenMMException
((
std
::
string
(
"Tried to create kernel with illegal kernel name '"
)
+
name
+
"'"
).
c_str
());
}
plugins/cudacompiler/src/CudaCompilerKernelFactory.h
0 → 100644
View file @
7e29671a
#ifndef OPENMM_CPUCUDACOMPILERKERNELFACTORY_H_
#define OPENMM_CPUCUDACOMPILERKERNELFACTORY_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) 2015 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
#include "openmm/KernelFactory.h"
namespace
OpenMM
{
/**
* This KernelFactory creates kernels for the CUDA runtime compiler.
*/
class
CudaCompilerKernelFactory
:
public
KernelFactory
{
public:
KernelImpl
*
createKernelImpl
(
std
::
string
name
,
const
Platform
&
platform
,
ContextImpl
&
context
)
const
;
};
}
// namespace OpenMM
#endif
/*OPENMM_CPUCUDACOMPILERKERNELFACTORY_H_*/
plugins/cudacompiler/src/CudaCompilerKernels.cpp
0 → 100644
View file @
7e29671a
/* -------------------------------------------------------------------------- *
* 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) 2015 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
#include "CudaCompilerKernels.h"
#include "openmm/OpenMMException.h"
#include <sstream>
#include <nvrtc.h>
using
namespace
OpenMM
;
using
namespace
std
;
#define CHECK_RESULT(result, prefix) \
if (result != NVRTC_SUCCESS) { \
stringstream m; \
m<<prefix<<": "<<getErrorString(result)<<" ("<<result<<")"<<" at "<<__FILE__<<":"<<__LINE__; \
throw OpenMMException(m.str());\
}
static
string
getErrorString
(
nvrtcResult
result
)
{
switch
(
result
)
{
case
NVRTC_SUCCESS
:
return
"NVRTC_SUCCESS"
;
case
NVRTC_ERROR_OUT_OF_MEMORY
:
return
"NVRTC_ERROR_OUT_OF_MEMORY"
;
case
NVRTC_ERROR_PROGRAM_CREATION_FAILURE
:
return
"NVRTC_ERROR_PROGRAM_CREATION_FAILURE"
;
case
NVRTC_ERROR_INVALID_INPUT
:
return
"NVRTC_ERROR_INVALID_INPUT"
;
case
NVRTC_ERROR_INVALID_PROGRAM
:
return
"NVRTC_ERROR_INVALID_PROGRAM"
;
case
NVRTC_ERROR_INVALID_OPTION
:
return
"NVRTC_ERROR_INVALID_OPTION"
;
case
NVRTC_ERROR_COMPILATION
:
return
"NVRTC_ERROR_COMPILATION"
;
case
NVRTC_ERROR_BUILTIN_OPERATION_FAILURE
:
return
"NVRTC_ERROR_BUILTIN_OPERATION_FAILURE"
;
}
return
"NVRTC error"
;
}
string
CudaRuntimeCompilerKernel
::
createModule
(
const
string
&
source
,
const
string
&
flags
,
CudaContext
&
cu
)
{
// Split the command line flags into an array of options.
stringstream
flagsStream
(
flags
);
string
flag
;
vector
<
string
>
splitFlags
;
while
(
flagsStream
>>
flag
)
splitFlags
.
push_back
(
flag
);
int
numOptions
=
splitFlags
.
size
();
vector
<
const
char
*>
options
(
numOptions
);
for
(
int
i
=
0
;
i
<
numOptions
;
i
++
)
options
[
i
]
=
&
splitFlags
[
i
][
0
];
// Compile the program to PTX.
nvrtcProgram
program
;
CHECK_RESULT
(
nvrtcCreateProgram
(
&
program
,
source
.
c_str
(),
""
,
0
,
NULL
,
NULL
),
"Error creating program"
);
try
{
nvrtcResult
result
=
nvrtcCompileProgram
(
program
,
options
.
size
(),
&
options
[
0
]);
if
(
result
!=
NVRTC_SUCCESS
)
{
size_t
logSize
;
nvrtcGetProgramLogSize
(
program
,
&
logSize
);
vector
<
char
>
log
(
logSize
);
nvrtcGetProgramLog
(
program
,
&
log
[
0
]);
throw
OpenMMException
(
"Error compiling program: "
+
string
(
&
log
[
0
]));
}
size_t
ptxSize
;
nvrtcGetPTXSize
(
program
,
&
ptxSize
);
vector
<
char
>
ptx
(
ptxSize
);
nvrtcGetPTX
(
program
,
&
ptx
[
0
]);
nvrtcDestroyProgram
(
&
program
);
return
string
(
&
ptx
[
0
]);
}
catch
(...)
{
nvrtcDestroyProgram
(
&
program
);
throw
;
}
}
plugins/cudacompiler/src/CudaCompilerKernels.h
0 → 100644
View file @
7e29671a
#ifndef OPENMM_CUDACOMPILER_KERNELS_H_
#define OPENMM_CUDACOMPILER_KERNELS_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) 2015 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
#include "internal/windowsExportCudaCompiler.h"
#include "CudaKernels.h"
#include <string>
namespace
OpenMM
{
/**
* This kernel is a compiler for CUDA kernels based on the runtime compilation feature
* introduced in CUDA 7.
*/
class
OPENMM_EXPORT_CUDACOMPILER
CudaRuntimeCompilerKernel
:
public
CudaCompilerKernel
{
public:
CudaRuntimeCompilerKernel
(
std
::
string
name
,
const
Platform
&
platform
)
:
CudaCompilerKernel
(
name
,
platform
)
{
}
/**
* Compile a kernel to PTX.
*
* @param source the source code for the kernel
* @param options the flags to be passed to the compiler
* @param cu the CudaContext for which the kernel is being compiled
*/
std
::
string
createModule
(
const
std
::
string
&
source
,
const
std
::
string
&
flags
,
CudaContext
&
cu
);
};
}
// namespace OpenMM
#endif
/*OPENMM_CUDACOMPILER_KERNELS_H_*/
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment