Skip to content

Commit

Permalink
Add support for HOOMD 3 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz authored Sep 15, 2022
2 parents 38fa49c + 7e18eb3 commit b41eb54
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 187 deletions.
3 changes: 2 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
IncludeBlocks: Regroup
IndentWidth: 4
NamespaceIndentation: None
PenaltyBreakAssignment: 100
SpacesBeforeTrailingComments: 2
Standard: Cpp11
TabWidth: 4
UseTab: Never
UseTab: Never
24 changes: 4 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,20 @@ cmake_minimum_required(VERSION 3.16..3.24)
# Set-up project
project(dlext LANGUAGES C CXX)

# Find HOOMD
set(PROJECT_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_MODULE_PATH})

if(NOT HOOMD_FOUND)
find_package(HOOMD 2.6.0 REQUIRED)
endif()

if(NOT HOOMD_INSTALL_PREFIX)
set(HOOMD_INSTALL_PREFIX ${HOOMD_ROOT})
endif()

if(NOT HOOMD_LIBRARIES)
set(HOOMD_LIBRARIES HOOMD::_hoomd)
endif()
include("${PROJECT_MODULE_PATH}/FindHOOMDTools.cmake")

include(GNUInstallDirs)
include("${PROJECT_MODULE_PATH}/FetchCPM.cmake")
include("${PROJECT_MODULE_PATH}/FetchDLPack.cmake")

# Plugins must be built as shared libraries
if(ENABLE_STATIC)
message(SEND_ERROR "Plugins cannot be built against a statically compiled hoomd")
endif()

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX ${HOOMD_INSTALL_PREFIX} CACHE PATH "" FORCE)
endif()

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

message(STATUS "Install plugin to: " ${CMAKE_INSTALL_PREFIX})

# Create the main library
add_library(${PROJECT_NAME} SHARED "")

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

# Install
message(STATUS "Plugin will be installed at: ${CMAKE_INSTALL_PREFIX}")

install(TARGETS ${PROJECT_NAME}
DESTINATION ${HOOMD_INSTALL_PREFIX}
)
Expand Down
203 changes: 96 additions & 107 deletions cmake/Modules/FindHOOMD.cmake
Original file line number Diff line number Diff line change
@@ -1,118 +1,107 @@
# CMake script for finding HOOMD and setting up all needed compile options to create and link a plugin library
# FindHOOMD
# ---------
#
# CMake script for finding HOOMD and setting up all needed compile options
# to create and link a plugin library
#
# Variables taken as input to this module:
# HOOMD_ROOT : location to look for HOOMD, if it is not in the python path
# HOOMD_ROOT -- Location to look for HOOMD, if it is not in the python path
#
# Variables defined by this module:
# FOUND_HOOMD : set to true if HOOMD is found
# HOOMD_LIBRARIES : a list of all libraries needed to link to to access hoomd (uncached)
# HOOMD_INCLUDE_DIR : a list of all include directories that need to be set to include HOOMD
# HOOMD_LIB : a cached var locating the hoomd library to link to
# HOOMD_INCLUDE_DIR -- Include directories that need to be set to include HOOMD
# HOOMD_LIB -- Cached var locating the hoomd library to link to
# HOOMD_LIBRARIES -- Libraries needed to link to to access hoomd (uncached)
# HOOMD_FOUND
#
# various ENABLE_ flags translated from hoomd_config.h so this plugin build can match the ABI of the installed hoomd
# Various ENABLE_ flags are translated from hoomd_config.h
# so this plugin build can match the ABI of the installed hoomd
#
# as a convenience (for the intended purpose of this find script), all include directories and definitions needed
# to compile with all the various libs (boost, python, winsoc, etc...) are set within this script

set(HOOMD_ROOT "" CACHE FILEPATH "Directory containing a hoomd installation (i.e. _hoomd.so)")
# As a convenience (for the intended purpose of this find script),
# all include directories and definitions needed to compile with all the various libs
# (boost, python, winsoc, etc...) are set within this script

# Let HOOMD_ROOT take precedence, but if unset, try letting Python find a hoomd package in its default paths.
if(HOOMD_ROOT)
set(hoomd_installation_guess ${HOOMD_ROOT})
else(HOOMD_ROOT)
find_package(PythonInterp)

set(find_hoomd_script "
function(find_hoomd_with_python)
find_package(Python QUIET COMPONENTS Interpreter)
set(FIND_HOOMD_SCRIPT "
from __future__ import print_function;
import sys, os; sys.stdout = open(os.devnull, 'w')
import hoomd
print(os.path.dirname(hoomd.__file__), file=sys.stderr, end='')")

execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "${find_hoomd_script}"
ERROR_VARIABLE hoomd_installation_guess)
message(STATUS "Python output: " ${hoomd_installation_guess})
endif(HOOMD_ROOT)
import os
try:
import hoomd
print(os.path.dirname(hoomd.__file__), end='')
except:
print('', end='')"
)
execute_process(
COMMAND ${Python_EXECUTABLE} -c "${FIND_HOOMD_SCRIPT}"
OUTPUT_VARIABLE HOOMD_ROOT
)
set(HOOMD_DIR ${HOOMD_ROOT} PARENT_SCOPE)
endfunction()

message(STATUS "Looking for a HOOMD installation at " ${hoomd_installation_guess})
find_path(FOUND_HOOMD_ROOT
NAMES _hoomd.so __init__.py
HINTS ${hoomd_installation_guess}
)

if(FOUND_HOOMD_ROOT)
set(HOOMD_ROOT ${FOUND_HOOMD_ROOT} CACHE FILEPATH "Directory containing a hoomd installation (i.e. _hoomd.so)" FORCE)
message(STATUS "Found hoomd installation at " ${HOOMD_ROOT})
else(FOUND_HOOMD_ROOT)
message(FATAL_ERROR "Could not find hoomd installation, either set HOOMD_ROOT or set PYTHON_EXECUTABLE to a python which can find hoomd")
endif(FOUND_HOOMD_ROOT)
if(HOOMD_ROOT)
set(HOOMD_DIR ${HOOMD_ROOT})
elseif(DEFINED ENV{HOOMD_ROOT})
set(HOOMD_DIR $ENV{HOOMD_ROOT})
else()
find_hoomd_with_python()
endif()

# search for the hoomd include directory
find_path(HOOMD_INCLUDE_DIR
NAMES HOOMDVersion.h
HINTS ${HOOMD_ROOT}/include
)

if (HOOMD_INCLUDE_DIR)
message(STATUS "Found HOOMD include directory: ${HOOMD_INCLUDE_DIR}")
mark_as_advanced(HOOMD_INCLUDE_DIR)
endif (HOOMD_INCLUDE_DIR)

set(HOOMD_FOUND FALSE)
if (HOOMD_INCLUDE_DIR AND HOOMD_ROOT)
set(HOOMD_FOUND TRUE)
mark_as_advanced(HOOMD_ROOT)
endif (HOOMD_INCLUDE_DIR AND HOOMD_ROOT)

if (NOT HOOMD_FOUND)
message(SEND_ERROR "HOOMD Not found. Please specify the location of your hoomd installation in HOOMD_ROOT")
endif (NOT HOOMD_FOUND)

#############################################################
## Now that we've found hoomd, lets do some setup
if (HOOMD_FOUND)

include_directories(${HOOMD_INCLUDE_DIR})

# run all of HOOMD's generic lib setup scripts
set(CMAKE_MODULE_PATH ${HOOMD_ROOT}
${HOOMD_ROOT}/CMake/hoomd
${HOOMD_ROOT}/CMake/thrust
${CMAKE_MODULE_PATH}
)

# grab previously-set hoomd configuration
include (hoomd_cache)

# Handle user build options
include (CMake_build_options)
include (CMake_preprocessor_flags)
# setup the install directories
include (CMake_install_options)

# Find the python executable and libraries
include (HOOMDPythonSetup)
# Find CUDA and set it up
include (HOOMDCUDASetup)
# Set default CFlags
include (HOOMDCFlagsSetup)
# include some os specific options
include (HOOMDOSSpecificSetup)
# setup common libraries used by all targets in this project
include (HOOMDCommonLibsSetup)
# setup macros
include (HOOMDMacros)
# setup MPI support
include (HOOMDMPISetup)

set(HOOMD_LIB ${HOOMD_ROOT}/_hoomd${PYTHON_MODULE_EXTENSION})
set(HOOMD_MD_LIB ${HOOMD_ROOT}/md/_md${PYTHON_MODULE_EXTENSION})
set(HOOMD_DEM_LIB ${HOOMD_ROOT}/dem/_dem${PYTHON_MODULE_EXTENSION})
set(HOOMD_HPMC_LIB ${HOOMD_ROOT}/hpmc/_hpmc${PYTHON_MODULE_EXTENSION})
set(HOOMD_CGCMM_LIB ${HOOMD_ROOT}/cgcmm/_cgcmm${PYTHON_MODULE_EXTENSION})
set(HOOMD_METAL_LIB ${HOOMD_ROOT}/metal/_metal${PYTHON_MODULE_EXTENSION})
set(HOOMD_DEPRECATED_LIB ${HOOMD_ROOT}/deprecated/_deprecated${PYTHON_MODULE_EXTENSION})

set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})

endif (HOOMD_FOUND)
NAMES HOOMDVersion.h
HINTS ${HOOMD_DIR}/include
)
if(HOOMD_INCLUDE_DIR)
file(READ "${HOOMD_INCLUDE_DIR}/HOOMDVersion.h" HOOMD_VERSION_HEADER)
string(REGEX
MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" HOOMD_VERSION ${HOOMD_VERSION_HEADER}
)
endif()
mark_as_advanced(HOOMD_FOUND HOOMD_DIR HOOMD_INCLUDE_DIR HOOMD_VERSION)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(HOOMD
REQUIRED_VARS HOOMD_DIR HOOMD_INCLUDE_DIR HOOMD_VERSION
)

if(HOOMD_FOUND)
include_directories(${HOOMD_INCLUDE_DIR})
# Run all of HOOMD's generic lib setup scripts
set(CMAKE_MODULE_PATH
${HOOMD_DIR}
${HOOMD_DIR}/CMake/hoomd
${HOOMD_DIR}/CMake/thrust
${CMAKE_MODULE_PATH}
)
# Grab previously-set hoomd configuration
include(hoomd_cache)
# Handle user build options
include(CMake_build_options)
include(CMake_preprocessor_flags)
# setup the install directories
include(CMake_install_options)
# Find the python executable and libraries
include(HOOMDPythonSetup)
# Find CUDA and set it up
include(HOOMDCUDASetup)
# Set default CFlags
include(HOOMDCFlagsSetup)
# Include some os specific options
include(HOOMDOSSpecificSetup)
# Setup common libraries used by all targets in this project
include(HOOMDCommonLibsSetup)
# Setup macros
include(HOOMDMacros)
# Setup MPI support
include(HOOMDMPISetup)

set(HOOMD_LIB ${HOOMD_DIR}/_hoomd${PYTHON_MODULE_EXTENSION})
set(HOOMD_MD_LIB ${HOOMD_DIR}/md/_md${PYTHON_MODULE_EXTENSION})
set(HOOMD_DEM_LIB ${HOOMD_DIR}/dem/_dem${PYTHON_MODULE_EXTENSION})
set(HOOMD_HPMC_LIB ${HOOMD_DIR}/hpmc/_hpmc${PYTHON_MODULE_EXTENSION})
set(HOOMD_CGCMM_LIB ${HOOMD_DIR}/cgcmm/_cgcmm${PYTHON_MODULE_EXTENSION})
set(HOOMD_METAL_LIB ${HOOMD_DIR}/metal/_metal${PYTHON_MODULE_EXTENSION})
set(HOOMD_DEPRECATED_LIB ${HOOMD_DIR}/deprecated/_deprecated${PYTHON_MODULE_EXTENSION})

set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
set(HOOMD_LIBRARIES ${HOOMD_LIB} ${HOOMD_COMMON_LIBS})
endif(HOOMD_FOUND)
51 changes: 51 additions & 0 deletions cmake/Modules/FindHOOMDTools.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Try finding HOOMD first from the current environment
set(HOOMD_GPU_PLATFORM "CUDA" CACHE STRING "GPU backend: CUDA or HIP.")
find_package(HOOMD QUIET)

if(HOOMD_FOUND)
if(
${HOOMD_VERSION} VERSION_GREATER_EQUAL "3.5.0" OR (
${HOOMD_VERSION} VERSION_LESS "3" AND
${HOOMD_VERSION} VERSION_GREATER_EQUAL "2.6.0"
)
)
message(STATUS "Found HOOMD: ${HOOMD_DIR} (version ${HOOMD_VERSION})")
else()
message(FATAL_ERROR
"Supported HOOMD versions are v2.6.0 to v2.9.7 or >= v3.5.0 "
"(version ${HOOMD_VERSION})"
)
endif()
endif()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_MODULE_PATH})

if(NOT HOOMD_FOUND)
find_package(HOOMD 2.6.0 REQUIRED)
endif()

# Plugins must be built as shared libraries
if(ENABLE_STATIC)
message(SEND_ERROR "Plugins cannot be built against a statically compiled hoomd")
endif()

if(${HOOMD_VERSION} VERSION_LESS "3.5.0")
add_compile_definitions(EXPORT_HALFSTEPHOOK)
if(${HOOMD_VERSION} VERSION_LESS "3")
add_compile_definitions(HOOMD2)
endif()
endif()

if(ENABLE_HIP AND (HIP_PLATFORM STREQUAL "nvcc"))
add_compile_definitions(ENABLE_CUDA)
endif()

if(NOT HOOMD_INSTALL_PREFIX)
set(HOOMD_INSTALL_PREFIX ${HOOMD_DIR})
else()
set(HOOMD_INSTALL_PREFIX "${HOOMD_INSTALL_PREFIX}/${PYTHON_SITE_INSTALL_DIR}")
endif()

if(NOT HOOMD_LIBRARIES)
set(HOOMD_LIBRARIES HOOMD::_hoomd HOOMD::_md)
endif()
7 changes: 7 additions & 0 deletions dlext/include/DLExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
#include <type_traits>
#include <vector>

namespace hoomd
{
namespace md
{
namespace dlext
{

using namespace cxx11utils;
using namespace hoomd;

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

} // namespace dlext
} // namespace md
} // namespace hoomd

#endif // HOOMD_DLPACK_EXTENSION_H_
Loading

0 comments on commit b41eb54

Please sign in to comment.