Skip to content

Commit

Permalink
Add framework for pair potentials
Browse files Browse the repository at this point in the history
  • Loading branch information
mphoward committed Jun 3, 2024
1 parent 9e91833 commit 53abc07
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 288 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ listed in the HOOMD-blue documentation to build an external component.

### Testing

To be added.
After building and installing azplugins, you can run the tests with pytest:

```
python -m pytest --pyargs hoomd.azplugins
```

## Contributing

Expand Down
26 changes: 26 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(COMPONENT_NAME azplugins)
# TODO: List all host C++ source code files in _${COMPONENT_NAME}_sources.
set(_${COMPONENT_NAME}_sources
GroupVelocityCompute.cc
module.cc
)

# TODO: List all GPU C++ source code files in _${COMPONENT_NAME}_cu_sources.
Expand All @@ -13,8 +14,29 @@ set(_${COMPONENT_NAME}_cu_sources
# TODO: List all Python modules in python_files.
set(python_files
__init__.py
pair.py
)

set(_pair_evaluators )

foreach(_evaluator ${_pair_evaluators})
configure_file(export_PotentialPair.cc.inc
export_PotentialPair${_evaluator}.cc
@ONLY)
set(_${COMPONENT_NAME}_sources ${_${COMPONENT_NAME}_sources} export_PotentialPair${_evaluator}.cc)

if (ENABLE_HIP)
configure_file(export_PotentialPairGPU.cc.inc
export_PotentialPair${_evaluator}GPU.cc
@ONLY)
configure_file(PotentialPairGPUKernel.cu.inc
PotentialPair${_evaluator}GPUKernel.cu
@ONLY)
set(_${COMPONENT_NAME}_sources ${_${COMPONENT_NAME}_sources} export_PotentialPair${_evaluator}GPU.cc)
set(_${COMPONENT_NAME}_cu_sources ${_${COMPONENT_NAME}_cu_sources} PotentialPair${_evaluator}GPUKernel.cu)
endif()
endforeach()

if (ENABLE_HIP)
set(_cuda_sources ${_${COMPONENT_NAME}_cu_sources})
endif (ENABLE_HIP)
Expand All @@ -36,6 +58,10 @@ target_link_libraries(_${COMPONENT_NAME}
PUBLIC HOOMD::_md
)

# Explicitly include this directory during builds. This seems to be needed for
# the autogenerated source files that are in the build directory.
target_include_directories(_${COMPONENT_NAME} PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

# Install the library.
install(TARGETS _${COMPONENT_NAME}
LIBRARY DESTINATION ${PYTHON_SITE_INSTALL_DIR}/${COMPONENT_NAME}
Expand Down
88 changes: 65 additions & 23 deletions src/PairEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,61 @@
// Copyright (c) 2021-2024, Auburn University
// Part of azplugins, released under the BSD 3-Clause License.

/*!
* \file PairEvaluator.h
* \brief Base class for pair evaluators.
*/

#ifndef AZPLUGINS_PAIR_EVALUATOR_H_
#define AZPLUGINS_PAIR_EVALUATOR_H_

#ifndef __HIPCC__
#include <string>
#endif // __HIPCC__

#include "hoomd/HOOMDMath.h"

#ifdef NVCC
#ifdef __HIPCC__
#define DEVICE __device__
#define HOSTDEVICE __host__ __device__
#else
#define DEVICE
#include <string>
#endif
#define HOSTDEVICE
#endif // __HIPCC__

namespace hoomd
{
namespace azplugins
{
namespace detail
{

//! Base class for isotropic pair potential parameters
/*!
* This class covers the default case of a simple potential that doesn't do
* anything special loading its parameters. This class can then be used as
* a \a param_type for the evaluator.
*
* Deriving classes \b must implement the constructors below. They should also
* set the aligned attribute based on the size of the object.
*/
struct PairParameters
{
#ifndef __HIPCC__
PairParameters() { }

PairParameters(pybind11::dict v, bool managed = false) { }

pybind11::dict asDict()
{
return pybind11::dict();
}
#endif //__HIPCC__

DEVICE void load_shared(char*& ptr, unsigned int& available_bytes) { }

HOSTDEVICE void allocate_shared(char*& ptr, unsigned int& available_bytes) const { }

#ifdef ENABLE_HIP
void set_memory_hint() const { }
#endif
};

//! Base class for isotropic pair potential evaluator
/*!
* This class covers the default case of a simple potential that doesn't require
Expand All @@ -36,20 +69,9 @@ namespace detail
class PairEvaluator
{
public:
DEVICE PairEvaluator(const Scalar _rsq, const Scalar _rcutsq) : rsq(_rsq), rcutsq(_rcutsq) { }

//! Base potential does not need diameter
DEVICE static bool needsDiameter()
{
return false;
}
typedef PairParameters param_type;

//! Accept the optional diameter values
/*!
* \param di Diameter of particle i
* \param dj Diameter of particle j
*/
DEVICE void setDiameter(Scalar di, Scalar dj) { }
DEVICE PairEvaluator(const Scalar _rsq, const Scalar _rcutsq) : rsq(_rsq), rcutsq(_rcutsq) { }

//! Base potential does not need charge
DEVICE static bool needsCharge()
Expand Down Expand Up @@ -80,7 +102,25 @@ class PairEvaluator
return false;
}

#ifndef NVCC
//! Evaluate the long-ranged correction to the pressure.
/*!
* \returns Default value of 0 (no correction).
*/
DEVICE Scalar evalPressureLRCIntegral()
{
return Scalar(0.0);
}

//! Evaluate the long-ranged correction to the energy.
/*!
* \returns Default value of 0 (no correction).
*/
DEVICE Scalar evalEnergyLRCIntegral()
{
return Scalar(0.0);
}

#ifndef __HIPCC__
//! Get the name of this potential
/*!
* This method must be overridden by deriving classes.
Expand All @@ -94,7 +134,7 @@ class PairEvaluator
{
throw std::runtime_error("Shape definition not supported for this pair potential.");
}
#endif // NVCC
#endif // __HIPCC__

protected:
Scalar rsq; //!< Squared distance between particles
Expand All @@ -103,7 +143,9 @@ class PairEvaluator

} // end namespace detail
} // end namespace azplugins
} // end namespace hoomd

#undef DEVICE
#undef HOSTDEVICE

#endif // AZPLUGINS_PAIR_EVALUATOR_H_
70 changes: 0 additions & 70 deletions src/PairPotentials.h

This file was deleted.

31 changes: 31 additions & 0 deletions src/PotentialPairGPUKernel.cu.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2018-2020, Michael P. Howard
// Copyright (c) 2021-2024, Auburn University
// Part of azplugins, released under the BSD 3-Clause License.

// Adapted from hoomd/md/export_PotentialPairGPUKernel.cu.inc of HOOMD-blue.
// Copyright (c) 2009-2024 The Regents of the University of Michigan.
// Part of HOOMD-blue, released under the BSD 3-Clause License.

// See CMakeLists.txt for the source of these variables to be processed by CMake's
// configure_file().

// clang-format off
#include "hoomd/md/PotentialPairGPU.cuh"
#include "PairEvaluator@[email protected]"

#define EVALUATOR_CLASS PairEvaluator@_evaluator@
// clang-format on

namespace hoomd
{
namespace md
{
namespace kernel
{
template __attribute__((visibility("default"))) hipError_t
gpu_compute_pair_forces<hoomd::azplugins::detail::EVALUATOR_CLASS>(
const pair_args_t& pair_args,
const hoomd::azplugins::detail::EVALUATOR_CLASS::param_type* d_params);
} // end namespace kernel
} // end namespace md
} // end namespace hoomd
2 changes: 2 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

"""azplugins."""

from hoomd.azplugins import pair

__version__ = '1.0.0'
32 changes: 32 additions & 0 deletions src/export_PotentialPair.cc.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2018-2020, Michael P. Howard
// Copyright (c) 2021-2024, Auburn University
// Part of azplugins, released under the BSD 3-Clause License.

// Adapted from hoomd/md/export_PotentialPair.cc.inc of HOOMD-blue.
// Copyright (c) 2009-2024 The Regents of the University of Michigan.
// Part of HOOMD-blue, released under the BSD 3-Clause License.

// See CMakeLists.txt for the source of these variables to be processed by CMake's
// configure_file().

// clang-format off
#include "hoomd/md/PotentialPair.h"
#include "PairEvaluator@[email protected]"

#define EVALUATOR_CLASS PairEvaluator@_evaluator@
#define EXPORT_FUNCTION export_PotentialPair@_evaluator@
// clang-format on

namespace hoomd
{
namespace azplugins
{
namespace detail
{
void EXPORT_FUNCTION(pybind11::module& m)
{
hoomd::md::detail::export_PotentialPair<EVALUATOR_CLASS>(m, "PotentialPair@_evaluator@");
}
} // end namespace detail
} // end namespace azplugins
} // end namespace hoomd
35 changes: 35 additions & 0 deletions src/export_PotentialPairGPU.cc.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2018-2020, Michael P. Howard
// Copyright (c) 2021-2024, Auburn University
// Part of azplugins, released under the BSD 3-Clause License.

// Adapted from hoomd/md/export_PotentialPairGPU.cc.inc of HOOMD-blue.
// Copyright (c) 2009-2024 The Regents of the University of Michigan.
// Part of HOOMD-blue, released under the BSD 3-Clause License.

// See CMakeLists.txt for the source of these variables to be processed by CMake's
// configure_file().

// clang-format off
#include "hoomd/md/PotentialPairGPU.h"
#include "PairEvaluator@[email protected]"

#define EVALUATOR_CLASS PairEvaluator@_evaluator@
#define EXPORT_FUNCTION export_PotentialPair@_evaluator@GPU
// clang-format on

// Use CPU class from another compilation unit to reduce compile time and compiler memory usage.
extern template class hoomd::md::PotentialPair<hoomd::azplugins::detail::EVALUATOR_CLASS>;

namespace hoomd
{
namespace azplugins
{
namespace detail
{
void EXPORT_FUNCTION(pybind11::module& m)
{
hoomd::md::detail::export_PotentialPairGPU<EVALUATOR_CLASS>(m, "PotentialPair@_evaluator@GPU");
}
} // end namespace detail
} // end namespace azplugins
} // end namespace hoomd
Loading

0 comments on commit 53abc07

Please sign in to comment.