Skip to content

Commit b41eb54

Browse files
authored
Add support for HOOMD 3 (#15)
2 parents 38fa49c + 7e18eb3 commit b41eb54

13 files changed

+246
-187
lines changed

.clang-format

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ BreakBeforeBinaryOperators: All
2525
BreakBeforeBraces: Custom
2626
IncludeBlocks: Regroup
2727
IndentWidth: 4
28+
NamespaceIndentation: None
2829
PenaltyBreakAssignment: 100
2930
SpacesBeforeTrailingComments: 2
3031
Standard: Cpp11
3132
TabWidth: 4
32-
UseTab: Never
33+
UseTab: Never

CMakeLists.txt

+4-20
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,20 @@ cmake_minimum_required(VERSION 3.16..3.24)
33
# Set-up project
44
project(dlext LANGUAGES C CXX)
55

6+
# Find HOOMD
67
set(PROJECT_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
7-
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_MODULE_PATH})
8-
9-
if(NOT HOOMD_FOUND)
10-
find_package(HOOMD 2.6.0 REQUIRED)
11-
endif()
12-
13-
if(NOT HOOMD_INSTALL_PREFIX)
14-
set(HOOMD_INSTALL_PREFIX ${HOOMD_ROOT})
15-
endif()
16-
17-
if(NOT HOOMD_LIBRARIES)
18-
set(HOOMD_LIBRARIES HOOMD::_hoomd)
19-
endif()
8+
include("${PROJECT_MODULE_PATH}/FindHOOMDTools.cmake")
209

2110
include(GNUInstallDirs)
2211
include("${PROJECT_MODULE_PATH}/FetchCPM.cmake")
2312
include("${PROJECT_MODULE_PATH}/FetchDLPack.cmake")
2413

25-
# Plugins must be built as shared libraries
26-
if(ENABLE_STATIC)
27-
message(SEND_ERROR "Plugins cannot be built against a statically compiled hoomd")
28-
endif()
29-
3014
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
3115
set(CMAKE_INSTALL_PREFIX ${HOOMD_INSTALL_PREFIX} CACHE PATH "" FORCE)
3216
endif()
3317

3418
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
3519

36-
message(STATUS "Install plugin to: " ${CMAKE_INSTALL_PREFIX})
37-
3820
# Create the main library
3921
add_library(${PROJECT_NAME} SHARED "")
4022

@@ -45,6 +27,8 @@ target_link_libraries(${PROJECT_NAME} PUBLIC ${HOOMD_LIBRARIES} dlpack::dlpack)
4527
add_subdirectory(dlext)
4628

4729
# Install
30+
message(STATUS "Plugin will be installed at: ${CMAKE_INSTALL_PREFIX}")
31+
4832
install(TARGETS ${PROJECT_NAME}
4933
DESTINATION ${HOOMD_INSTALL_PREFIX}
5034
)

cmake/Modules/FindHOOMD.cmake

+96-107
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,107 @@
1-
# CMake script for finding HOOMD and setting up all needed compile options to create and link a plugin library
1+
# FindHOOMD
2+
# ---------
3+
#
4+
# CMake script for finding HOOMD and setting up all needed compile options
5+
# to create and link a plugin library
26
#
37
# Variables taken as input to this module:
4-
# HOOMD_ROOT : location to look for HOOMD, if it is not in the python path
8+
# HOOMD_ROOT -- Location to look for HOOMD, if it is not in the python path
59
#
610
# Variables defined by this module:
7-
# FOUND_HOOMD : set to true if HOOMD is found
8-
# HOOMD_LIBRARIES : a list of all libraries needed to link to to access hoomd (uncached)
9-
# HOOMD_INCLUDE_DIR : a list of all include directories that need to be set to include HOOMD
10-
# HOOMD_LIB : a cached var locating the hoomd library to link to
11+
# HOOMD_INCLUDE_DIR -- Include directories that need to be set to include HOOMD
12+
# HOOMD_LIB -- Cached var locating the hoomd library to link to
13+
# HOOMD_LIBRARIES -- Libraries needed to link to to access hoomd (uncached)
14+
# HOOMD_FOUND
1115
#
12-
# various ENABLE_ flags translated from hoomd_config.h so this plugin build can match the ABI of the installed hoomd
16+
# Various ENABLE_ flags are translated from hoomd_config.h
17+
# so this plugin build can match the ABI of the installed hoomd
1318
#
14-
# as a convenience (for the intended purpose of this find script), all include directories and definitions needed
15-
# to compile with all the various libs (boost, python, winsoc, etc...) are set within this script
16-
17-
set(HOOMD_ROOT "" CACHE FILEPATH "Directory containing a hoomd installation (i.e. _hoomd.so)")
19+
# As a convenience (for the intended purpose of this find script),
20+
# all include directories and definitions needed to compile with all the various libs
21+
# (boost, python, winsoc, etc...) are set within this script
1822

19-
# Let HOOMD_ROOT take precedence, but if unset, try letting Python find a hoomd package in its default paths.
20-
if(HOOMD_ROOT)
21-
set(hoomd_installation_guess ${HOOMD_ROOT})
22-
else(HOOMD_ROOT)
23-
find_package(PythonInterp)
24-
25-
set(find_hoomd_script "
23+
function(find_hoomd_with_python)
24+
find_package(Python QUIET COMPONENTS Interpreter)
25+
set(FIND_HOOMD_SCRIPT "
2626
from __future__ import print_function;
27-
import sys, os; sys.stdout = open(os.devnull, 'w')
28-
import hoomd
29-
print(os.path.dirname(hoomd.__file__), file=sys.stderr, end='')")
30-
31-
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "${find_hoomd_script}"
32-
ERROR_VARIABLE hoomd_installation_guess)
33-
message(STATUS "Python output: " ${hoomd_installation_guess})
34-
endif(HOOMD_ROOT)
27+
import os
28+
try:
29+
import hoomd
30+
print(os.path.dirname(hoomd.__file__), end='')
31+
except:
32+
print('', end='')"
33+
)
34+
execute_process(
35+
COMMAND ${Python_EXECUTABLE} -c "${FIND_HOOMD_SCRIPT}"
36+
OUTPUT_VARIABLE HOOMD_ROOT
37+
)
38+
set(HOOMD_DIR ${HOOMD_ROOT} PARENT_SCOPE)
39+
endfunction()
3540

36-
message(STATUS "Looking for a HOOMD installation at " ${hoomd_installation_guess})
37-
find_path(FOUND_HOOMD_ROOT
38-
NAMES _hoomd.so __init__.py
39-
HINTS ${hoomd_installation_guess}
40-
)
41-
42-
if(FOUND_HOOMD_ROOT)
43-
set(HOOMD_ROOT ${FOUND_HOOMD_ROOT} CACHE FILEPATH "Directory containing a hoomd installation (i.e. _hoomd.so)" FORCE)
44-
message(STATUS "Found hoomd installation at " ${HOOMD_ROOT})
45-
else(FOUND_HOOMD_ROOT)
46-
message(FATAL_ERROR "Could not find hoomd installation, either set HOOMD_ROOT or set PYTHON_EXECUTABLE to a python which can find hoomd")
47-
endif(FOUND_HOOMD_ROOT)
41+
if(HOOMD_ROOT)
42+
set(HOOMD_DIR ${HOOMD_ROOT})
43+
elseif(DEFINED ENV{HOOMD_ROOT})
44+
set(HOOMD_DIR $ENV{HOOMD_ROOT})
45+
else()
46+
find_hoomd_with_python()
47+
endif()
4848

49-
# search for the hoomd include directory
5049
find_path(HOOMD_INCLUDE_DIR
51-
NAMES HOOMDVersion.h
52-
HINTS ${HOOMD_ROOT}/include
53-
)
54-
55-
if (HOOMD_INCLUDE_DIR)
56-
message(STATUS "Found HOOMD include directory: ${HOOMD_INCLUDE_DIR}")
57-
mark_as_advanced(HOOMD_INCLUDE_DIR)
58-
endif (HOOMD_INCLUDE_DIR)
59-
60-
set(HOOMD_FOUND FALSE)
61-
if (HOOMD_INCLUDE_DIR AND HOOMD_ROOT)
62-
set(HOOMD_FOUND TRUE)
63-
mark_as_advanced(HOOMD_ROOT)
64-
endif (HOOMD_INCLUDE_DIR AND HOOMD_ROOT)
65-
66-
if (NOT HOOMD_FOUND)
67-
message(SEND_ERROR "HOOMD Not found. Please specify the location of your hoomd installation in HOOMD_ROOT")
68-
endif (NOT HOOMD_FOUND)
69-
70-
#############################################################
71-
## Now that we've found hoomd, lets do some setup
72-
if (HOOMD_FOUND)
73-
74-
include_directories(${HOOMD_INCLUDE_DIR})
75-
76-
# run all of HOOMD's generic lib setup scripts
77-
set(CMAKE_MODULE_PATH ${HOOMD_ROOT}
78-
${HOOMD_ROOT}/CMake/hoomd
79-
${HOOMD_ROOT}/CMake/thrust
80-
${CMAKE_MODULE_PATH}
81-
)
82-
83-
# grab previously-set hoomd configuration
84-
include (hoomd_cache)
85-
86-
# Handle user build options
87-
include (CMake_build_options)
88-
include (CMake_preprocessor_flags)
89-
# setup the install directories
90-
include (CMake_install_options)
91-
92-
# Find the python executable and libraries
93-
include (HOOMDPythonSetup)
94-
# Find CUDA and set it up
95-
include (HOOMDCUDASetup)
96-
# Set default CFlags
97-
include (HOOMDCFlagsSetup)
98-
# include some os specific options
99-
include (HOOMDOSSpecificSetup)
100-
# setup common libraries used by all targets in this project
101-
include (HOOMDCommonLibsSetup)
102-
# setup macros
103-
include (HOOMDMacros)
104-
# setup MPI support
105-
include (HOOMDMPISetup)
106-
107-
set(HOOMD_LIB ${HOOMD_ROOT}/_hoomd${PYTHON_MODULE_EXTENSION})
108-
set(HOOMD_MD_LIB ${HOOMD_ROOT}/md/_md${PYTHON_MODULE_EXTENSION})
109-
set(HOOMD_DEM_LIB ${HOOMD_ROOT}/dem/_dem${PYTHON_MODULE_EXTENSION})
110-
set(HOOMD_HPMC_LIB ${HOOMD_ROOT}/hpmc/_hpmc${PYTHON_MODULE_EXTENSION})
111-
set(HOOMD_CGCMM_LIB ${HOOMD_ROOT}/cgcmm/_cgcmm${PYTHON_MODULE_EXTENSION})
112-
set(HOOMD_METAL_LIB ${HOOMD_ROOT}/metal/_metal${PYTHON_MODULE_EXTENSION})
113-
set(HOOMD_DEPRECATED_LIB ${HOOMD_ROOT}/deprecated/_deprecated${PYTHON_MODULE_EXTENSION})
114-
115-
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
116-
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
117-
118-
endif (HOOMD_FOUND)
50+
NAMES HOOMDVersion.h
51+
HINTS ${HOOMD_DIR}/include
52+
)
53+
if(HOOMD_INCLUDE_DIR)
54+
file(READ "${HOOMD_INCLUDE_DIR}/HOOMDVersion.h" HOOMD_VERSION_HEADER)
55+
string(REGEX
56+
MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" HOOMD_VERSION ${HOOMD_VERSION_HEADER}
57+
)
58+
endif()
59+
mark_as_advanced(HOOMD_FOUND HOOMD_DIR HOOMD_INCLUDE_DIR HOOMD_VERSION)
60+
61+
include(FindPackageHandleStandardArgs)
62+
find_package_handle_standard_args(HOOMD
63+
REQUIRED_VARS HOOMD_DIR HOOMD_INCLUDE_DIR HOOMD_VERSION
64+
)
65+
66+
if(HOOMD_FOUND)
67+
include_directories(${HOOMD_INCLUDE_DIR})
68+
# Run all of HOOMD's generic lib setup scripts
69+
set(CMAKE_MODULE_PATH
70+
${HOOMD_DIR}
71+
${HOOMD_DIR}/CMake/hoomd
72+
${HOOMD_DIR}/CMake/thrust
73+
${CMAKE_MODULE_PATH}
74+
)
75+
# Grab previously-set hoomd configuration
76+
include(hoomd_cache)
77+
# Handle user build options
78+
include(CMake_build_options)
79+
include(CMake_preprocessor_flags)
80+
# setup the install directories
81+
include(CMake_install_options)
82+
# Find the python executable and libraries
83+
include(HOOMDPythonSetup)
84+
# Find CUDA and set it up
85+
include(HOOMDCUDASetup)
86+
# Set default CFlags
87+
include(HOOMDCFlagsSetup)
88+
# Include some os specific options
89+
include(HOOMDOSSpecificSetup)
90+
# Setup common libraries used by all targets in this project
91+
include(HOOMDCommonLibsSetup)
92+
# Setup macros
93+
include(HOOMDMacros)
94+
# Setup MPI support
95+
include(HOOMDMPISetup)
96+
97+
set(HOOMD_LIB ${HOOMD_DIR}/_hoomd${PYTHON_MODULE_EXTENSION})
98+
set(HOOMD_MD_LIB ${HOOMD_DIR}/md/_md${PYTHON_MODULE_EXTENSION})
99+
set(HOOMD_DEM_LIB ${HOOMD_DIR}/dem/_dem${PYTHON_MODULE_EXTENSION})
100+
set(HOOMD_HPMC_LIB ${HOOMD_DIR}/hpmc/_hpmc${PYTHON_MODULE_EXTENSION})
101+
set(HOOMD_CGCMM_LIB ${HOOMD_DIR}/cgcmm/_cgcmm${PYTHON_MODULE_EXTENSION})
102+
set(HOOMD_METAL_LIB ${HOOMD_DIR}/metal/_metal${PYTHON_MODULE_EXTENSION})
103+
set(HOOMD_DEPRECATED_LIB ${HOOMD_DIR}/deprecated/_deprecated${PYTHON_MODULE_EXTENSION})
104+
105+
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
106+
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
107+
endif(HOOMD_FOUND)

cmake/Modules/FindHOOMDTools.cmake

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Try finding HOOMD first from the current environment
2+
set(HOOMD_GPU_PLATFORM "CUDA" CACHE STRING "GPU backend: CUDA or HIP.")
3+
find_package(HOOMD QUIET)
4+
5+
if(HOOMD_FOUND)
6+
if(
7+
${HOOMD_VERSION} VERSION_GREATER_EQUAL "3.5.0" OR (
8+
${HOOMD_VERSION} VERSION_LESS "3" AND
9+
${HOOMD_VERSION} VERSION_GREATER_EQUAL "2.6.0"
10+
)
11+
)
12+
message(STATUS "Found HOOMD: ${HOOMD_DIR} (version ${HOOMD_VERSION})")
13+
else()
14+
message(FATAL_ERROR
15+
"Supported HOOMD versions are v2.6.0 to v2.9.7 or >= v3.5.0 "
16+
"(version ${HOOMD_VERSION})"
17+
)
18+
endif()
19+
endif()
20+
21+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_MODULE_PATH})
22+
23+
if(NOT HOOMD_FOUND)
24+
find_package(HOOMD 2.6.0 REQUIRED)
25+
endif()
26+
27+
# Plugins must be built as shared libraries
28+
if(ENABLE_STATIC)
29+
message(SEND_ERROR "Plugins cannot be built against a statically compiled hoomd")
30+
endif()
31+
32+
if(${HOOMD_VERSION} VERSION_LESS "3.5.0")
33+
add_compile_definitions(EXPORT_HALFSTEPHOOK)
34+
if(${HOOMD_VERSION} VERSION_LESS "3")
35+
add_compile_definitions(HOOMD2)
36+
endif()
37+
endif()
38+
39+
if(ENABLE_HIP AND (HIP_PLATFORM STREQUAL "nvcc"))
40+
add_compile_definitions(ENABLE_CUDA)
41+
endif()
42+
43+
if(NOT HOOMD_INSTALL_PREFIX)
44+
set(HOOMD_INSTALL_PREFIX ${HOOMD_DIR})
45+
else()
46+
set(HOOMD_INSTALL_PREFIX "${HOOMD_INSTALL_PREFIX}/${PYTHON_SITE_INSTALL_DIR}")
47+
endif()
48+
49+
if(NOT HOOMD_LIBRARIES)
50+
set(HOOMD_LIBRARIES HOOMD::_hoomd HOOMD::_md)
51+
endif()

dlext/include/DLExt.h

+7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111
#include <type_traits>
1212
#include <vector>
1313

14+
namespace hoomd
15+
{
16+
namespace md
17+
{
1418
namespace dlext
1519
{
1620

21+
using namespace cxx11utils;
1722
using namespace hoomd;
1823

1924
// { // Aliases
@@ -103,5 +108,7 @@ template <>
103108
constexpr int64_t stride1<unsigned int>() { return 1; }
104109

105110
} // namespace dlext
111+
} // namespace md
112+
} // namespace hoomd
106113

107114
#endif // HOOMD_DLPACK_EXTENSION_H_

0 commit comments

Comments
 (0)