Skip to content

Commit

Permalink
Multi-Dim Support
Browse files Browse the repository at this point in the history
Support compiling pyAMReX for multiple `AMReX_SPACEDIM` variants
at once.
  • Loading branch information
ax3l committed May 19, 2023
1 parent 96743d0 commit efcb46d
Show file tree
Hide file tree
Showing 41 changed files with 647 additions and 536 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Configure (C++)
if: ${{ matrix.language == 'cpp' }}
run: |
$CMAKE -S . -B build
$CMAKE -S . -B build -DAMReX_SPACEDIM="1;2;3"
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/hip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ jobs:
# https://github.com/open-mpi/ompi/issues/9317
export LDFLAGS="-lopen-pal"
cmake -S . -B build \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DAMReX_GPU_BACKEND=HIP \
-DAMReX_AMD_ARCH=gfx900
cmake -S . -B build \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DAMReX_GPU_BACKEND=HIP \
-DAMReX_AMD_ARCH=gfx900 \
-DAMReX_SPACEDIM="1;2;3"
cmake --build build --target pip_install -j 2
4 changes: 3 additions & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
python3 -m pip install -U pip setuptools wheel pytest
python3 -m pip install -U cmake
python3 -m pip install -v .
python3 -c "import amrex; print(amrex.__version__)"
python3 -c "import amr.space1d as amr; print(amr.__version__)"
python3 -c "import amr.space2d as amr; print(amr.__version__)"
python3 -c "import amr.space3d as amr; print(amr.__version__)"
- name: Unit tests
run: |
python3 -m pytest tests/
Expand Down
17 changes: 13 additions & 4 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ jobs:
run: |
python3 -m pip install -U pip setuptools wheel pytest
AMREX_MPI=ON python3 -m pip install -v .
python3 -c "import amrex; print(amrex.__version__)"
python3 -c "import amr.space1d as amr; print(amr.__version__)"
python3 -c "import amr.space2d as amr; print(amr.__version__)"
python3 -c "import amr.space3d as amr; print(amr.__version__)"
- name: Unit tests
run: |
mpiexec -np 1 python3 -m pytest tests/
Expand All @@ -54,7 +56,8 @@ jobs:
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DBUILD_SHARED_LIBS=ON \
-DAMReX_MPI=ON
-DAMReX_MPI=ON \
-DAMReX_SPACEDIM="1;2;3"
cmake --build build --target pip_install -j 2
- name: Unit tests
Expand All @@ -76,7 +79,9 @@ jobs:
export CXX=$(which clang++-6.0)
python3 -m pip install -U pip pytest
python3 -m pip install -v .
python3 -c "import amrex; print(amrex.__version__)"
python3 -c "import amr.space1d as amr; print(amr.__version__)"
python3 -c "import amr.space2d as amr; print(amr.__version__)"
python3 -c "import amr.space3d as amr; print(amr.__version__)"
- name: Unit tests
run: |
python3 -m pytest tests/
Expand All @@ -98,7 +103,9 @@ jobs:
run: |
python3 -m pip install -U pip pytest
python3 -m pip install -v .
python3 -c "import amrex; print(amrex.__version__)"
python3 -c "import amr.space1d as amr; print(amr.__version__)"
python3 -c "import amr.space2d as amr; print(amr.__version__)"
python3 -c "import amr.space3d as amr; print(amr.__version__)"
- name: Unit tests
run: |
python3 -m pytest tests/
Expand Down Expand Up @@ -128,6 +135,7 @@ jobs:
cmake -S . -B build \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DAMReX_GPU_BACKEND=CUDA \
-DAMReX_SPACEDIM="1;2;3" \
-DCMAKE_CUDA_STANDARD=17 \
-DCMAKE_CXX_STANDARD=17 \
-DAMReX_CUDA_ARCH=8.0 \
Expand Down Expand Up @@ -156,6 +164,7 @@ jobs:
# -DCMAKE_CXX_COMPILER_VERSION=12.0 \
# -DCMAKE_CXX_STANDARD_COMPUTED_DEFAULT="17" \
# -DAMReX_GPU_BACKEND=SYCL \
# -DAMReX_SPACEDIM="1;2;3" \
# -DCMAKE_C_COMPILER=$(which clang) \
# -DCMAKE_CXX_COMPILER=$(which dpcpp)
# cmake --build build -j 2
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
python3 -m pip install -v .
if(!$?) { Exit $LASTEXITCODE }
python3 -c "import amrex; print(amrex.__version__)"
python3 -c "import amr.space1d as amr; print(amr.__version__)"
python3 -c "import amr.space2d as amr; print(amr.__version__)"
python3 -c "import amr.space3d as amr; print(amr.__version__)"
- name: Unit tests
shell: cmd
run: python3 -m pytest tests
Expand All @@ -47,7 +49,8 @@ jobs:
-T "ClangCl" `
-DCMAKE_VERBOSE_MAKEFILE=ON `
-DBUILD_SHARED_LIBS=ON `
-DAMReX_MPI=OFF
-DAMReX_MPI=OFF `
-DAMReX_SPACEDIM="1;2;3"
if(!$?) { Exit $LASTEXITCODE }
cmake --build build --config Debug -j 2
Expand Down
183 changes: 96 additions & 87 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ set_testing_option(pyAMReX_BUILD_TESTING) # default: ON (BUILD_TESTING)
# AMReX
# builds AMReX from source (default) or finds an existing install
include(${pyAMReX_SOURCE_DIR}/cmake/dependencies/AMReX.cmake)
include(AMReXBuildInfo) # <AMReX_buildInfo.H>

# for some targets need to be triggered once, so any dim dependency will do
list(LENGTH AMReX_SPACEDIM list_len)
math(EXPR list_last "${list_len} - 1")
list(GET AMReX_SPACEDIM ${list_last} AMReX_SPACEDIM_LAST)

# Python
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
Expand All @@ -84,96 +90,95 @@ include(${pyAMReX_SOURCE_DIR}/cmake/dependencies/pybind11.cmake)

# Targets #####################################################################
#
# collect all objects for compilation
add_library(pyAMReX MODULE src/pyAMReX.cpp)
add_library(pyAMReX::pyAMReX ALIAS pyAMReX)

# own headers
target_include_directories(pyAMReX PUBLIC
$<BUILD_INTERFACE:${pyAMReX_SOURCE_DIR}/src>
)

# if we include <AMReX_buildInfo.H> we will need to call:
include(AMReXBuildInfo)
generate_buildinfo(pyAMReX "${pyAMReX_SOURCE_DIR}")
target_link_libraries(pyAMReX PRIVATE buildInfo::pyAMReX)
foreach(D IN LISTS AMReX_SPACEDIM)
# collect all objects for compilation
add_library(pyAMReX_${D}d MODULE src/pyAMReX.cpp)
add_library(pyAMReX::pyAMReX_${D}d ALIAS pyAMReX_${D}d)

# own headers
target_include_directories(pyAMReX_${D}d PUBLIC
$<BUILD_INTERFACE:${pyAMReX_SOURCE_DIR}/src>
)

# add sources
add_subdirectory(src)
# if we include <AMReX_buildInfo.H> we will need to call:
generate_buildinfo(pyAMReX_${D}d "${pyAMReX_SOURCE_DIR}")
target_link_libraries(pyAMReX_${D}d PRIVATE buildInfo::pyAMReX_${D}d)

# C++ properties: at least a C++17 capable compiler is needed
target_compile_features(pyAMReX PUBLIC cxx_std_17)
set_target_properties(pyAMReX PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
# C++ properties: at least a C++17 capable compiler is needed
target_compile_features(pyAMReX_${D}d PUBLIC cxx_std_17)
set_target_properties(pyAMReX_${D}d PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)

# link dependencies
target_link_libraries(pyAMReX PUBLIC AMReX::amrex)
target_link_libraries(pyAMReX PRIVATE pybind11::module pybind11::lto pybind11::windows_extras)

# binary name: we will encoded 1D, 2D and 3D in here so we can provide a
# wrapper library that pre-compiled all three geometry variants of AMReX
#set_pyAMReX_binary_name()

# set Python module properties
set_target_properties(pyAMReX PROPERTIES
# hide symbols for combining multiple pybind11 modules downstream & for
# reduced binary size
CXX_VISIBILITY_PRESET "hidden"
CUDA_VISIBILITY_PRESET "hidden"
# name of the pybind-generated python module, which is wrapped in another
# fluffy front-end modules, so we can extend it with pure Python
ARCHIVE_OUTPUT_NAME amrex_pybind
LIBRARY_OUTPUT_NAME amrex_pybind
# build output directories - mainly set to run tests from CMake & IDEs
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
PDB_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
)
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(isMultiConfig)
foreach(CFG IN LISTS CMAKE_CONFIGURATION_TYPES)
string(TOUPPER "${CFG}" CFG_UPPER)
set_target_properties(pyAMReX PROPERTIES
# build output directories - mainly set to run tests from CMake & IDEs
# note: same as above, but for Multi-Config generators
ARCHIVE_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
LIBRARY_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
RUNTIME_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
COMPILE_PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex
# link dependencies
target_link_libraries(pyAMReX_${D}d PUBLIC AMReX::amrex_${D}d)
target_link_libraries(pyAMReX_${D}d PRIVATE pybind11::module pybind11::lto pybind11::windows_extras)

# set Python module properties
set_target_properties(pyAMReX_${D}d PROPERTIES
# hide symbols for combining multiple pybind11 modules downstream & for
# reduced binary size
CXX_VISIBILITY_PRESET "hidden"
CUDA_VISIBILITY_PRESET "hidden"
# name of the pybind-generated python module, which is wrapped in another
# fluffy front-end modules, so we can extend it with pure Python
ARCHIVE_OUTPUT_NAME amrex_${D}d_pybind
LIBRARY_OUTPUT_NAME amrex_${D}d_pybind
# build output directories - mainly set to run tests from CMake & IDEs
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
PDB_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
)
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(isMultiConfig)
foreach(CFG IN LISTS CMAKE_CONFIGURATION_TYPES)
string(TOUPPER "${CFG}" CFG_UPPER)
set_target_properties(pyAMReX_${D}d PROPERTIES
# build output directories - mainly set to run tests from CMake & IDEs
# note: same as above, but for Multi-Config generators
ARCHIVE_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
LIBRARY_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
RUNTIME_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
COMPILE_PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${CMAKE_PYTHON_OUTPUT_DIRECTORY}/amrex/space${D}d
)
endforeach()
endif()
if(EMSCRIPTEN)
set_target_properties(pyAMReX_${D}d PROPERTIES
PREFIX "")
else()
pybind11_extension(pyAMReX_${D}d)
endif()
if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
pybind11_strip(pyAMReX_${D}d)
endif()

# AMReX helper function: propagate CUDA specific target & source properties
if(AMReX_GPU_BACKEND STREQUAL CUDA)
setup_target_for_cuda_compilation(pyAMReX_${D}d)
target_compile_features(pyAMReX_${D}d PUBLIC cuda_std_17)
set_target_properties(pyAMReX_${D}d PROPERTIES
CUDA_EXTENSIONS OFF
CUDA_STANDARD_REQUIRED ON
)
endforeach()
endif()
if(EMSCRIPTEN)
set_target_properties(pyAMReX PROPERTIES
PREFIX "")
else()
pybind11_extension(pyAMReX)
endif()
if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
pybind11_strip(pyAMReX)
endif()
endif()
endforeach()

# AMReX helper function: propagate CUDA specific target & source properties
if(AMReX_GPU_BACKEND STREQUAL CUDA)
setup_target_for_cuda_compilation(pyAMReX)
target_compile_features(pyAMReX PUBLIC cuda_std_17)
set_target_properties(pyAMReX PROPERTIES
CUDA_EXTENSIONS OFF
CUDA_STANDARD_REQUIRED ON
)
endif()
# add sources
add_subdirectory(src)


# Defines #####################################################################
#
# for module __version__
target_compile_definitions(pyAMReX PRIVATE
PYAMReX_VERSION_INFO=${pyAMReX_VERSION_INFO})
foreach(D IN LISTS AMReX_SPACEDIM)
target_compile_definitions(pyAMReX_${D}d PRIVATE
PYAMReX_VERSION_INFO=${pyAMReX_VERSION_INFO})
endforeach()


# Warnings ####################################################################
Expand All @@ -197,7 +202,9 @@ set_cxx_warnings()
#

# headers, libraries and executables
set(pyAMReX_INSTALL_TARGET_NAMES pyAMReX)
foreach(D IN LISTS AMReX_SPACEDIM)
set(pyAMReX_INSTALL_TARGET_NAMES ${pyAMReX_INSTALL_TARGET_NAMES} pyAMReX_${D}d)
endforeach()

install(TARGETS ${pyAMReX_INSTALL_TARGET_NAMES}
EXPORT pyAMReXTargets
Expand All @@ -207,7 +214,7 @@ install(TARGETS ${pyAMReX_INSTALL_TARGET_NAMES}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# CMake package file for find_package(pyAMReX::pyAMReX) in depending projects
# CMake package file for find_package(pyAMReX) in depending projects
#install(EXPORT pyAMReXTargets
# FILE pyAMReXTargets.cmake
# NAMESPACE pyAMReX::
Expand Down Expand Up @@ -239,12 +246,12 @@ set(pyAMReX_CUSTOM_TARGET_PREFIX "${_pyAMReX_CUSTOM_TARGET_PREFIX_DEFAULT}"
add_custom_target(${pyAMReX_CUSTOM_TARGET_PREFIX}pip_wheel
${CMAKE_COMMAND} -E rm -f -r amrex-whl
COMMAND
${CMAKE_COMMAND} -E env PYAMREX_LIBDIR=$<TARGET_FILE_DIR:pyAMReX>
${CMAKE_COMMAND} -E env PYAMREX_LIBDIR=$<TARGET_FILE_DIR:pyAMReX_${AMReX_SPACEDIM_LAST}d>
${Python_EXECUTABLE} -m pip wheel -v --no-build-isolation --no-deps --wheel-dir=amrex-whl ${pyAMReX_SOURCE_DIR}
WORKING_DIRECTORY
${pyAMReX_BINARY_DIR}
DEPENDS
pyAMReX
${pyAMReX_INSTALL_TARGET_NAMES}
)

# this will also upgrade/downgrade dependencies, e.g., when the version of numpy changes
Expand All @@ -263,18 +270,20 @@ add_custom_target(${pyAMReX_CUSTOM_TARGET_PREFIX}pip_install
WORKING_DIRECTORY
${pyAMReX_BINARY_DIR}
DEPENDS
pyAMReX ${pyAMReX_CUSTOM_TARGET_PREFIX}pip_wheel ${pyAMReX_CUSTOM_TARGET_PREFIX}pip_install_requirements
${pyAMReX_INSTALL_TARGET_NAMES}
${pyAMReX_CUSTOM_TARGET_PREFIX}pip_wheel
${pyAMReX_CUSTOM_TARGET_PREFIX}pip_install_requirements
)


# Tests #######################################################################
#
if(pyAMReX_BUILD_TESTING)
# copy Python wrapper library to build directory
add_custom_command(TARGET pyAMReX POST_BUILD
add_custom_command(TARGET pyAMReX_${AMReX_SPACEDIM_LAST}d POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${pyAMReX_SOURCE_DIR}/src/amrex
$<TARGET_FILE_DIR:pyAMReX>
$<TARGET_FILE_DIR:pyAMReX_${AMReX_SPACEDIM_LAST}d>/..
)

add_test(NAME pytest.AMReX
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include README.md LICENSE
include pyproject.toml
include requirements.txt
include requirements.txt requirements_mpi.txt
global-include CMakeLists.txt *.cmake *.in
recursive-include cmake *
recursive-include src *
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ We will add further information here once first development versions are ready f
*to do*

```python
import amrex
import amrex.space3d as amr

small_end = amrex.Int_Vect()
big_end = amrex.Int_Vect(2, 3, 4)
small_end = amr.Int_Vect()
big_end = amr.Int_Vect(2, 3, 4)

b = amrex.Box(small_end, big_end)
b = amr.Box(small_end, big_end)
print(b)

# ...
Expand Down
2 changes: 1 addition & 1 deletion cmake/dependencies/AMReX.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ option(pyAMReX_amrex_internal "Download & build AMReX" ON)
set(pyAMReX_amrex_repo "https://github.com/AMReX-Codes/amrex.git"
CACHE STRING
"Repository URI to pull and build AMReX from if(pyAMReX_amrex_internal)")
set(pyAMReX_amrex_branch "a393d7ff7e320cefeeb55e31f1b0e0b5ac2d90ca"
set(pyAMReX_amrex_branch "ac8574131dd152ce40853e17af948360def806f4"
CACHE STRING
"Repository branch for pyAMReX_amrex_repo if(pyAMReX_amrex_internal)")

Expand Down
Loading

0 comments on commit efcb46d

Please sign in to comment.