Skip to content

Commit

Permalink
feat: export samurai CMake options (#224)
Browse files Browse the repository at this point in the history
## Description
samurai options are exported so that calling projects can enable/disable
them

-  through the `ccmake` command,
- in the `cmake` command line:
```bash
cmake -DSAMURAI_[OPTIONNAME]=ON ..
```
- or in the project's `CMakeLists.txt`:
```cmake
set(SAMURAI_[OPTIONNAME] On)
```
Examples of options: `SAMURAI_WITH_MPI`, `SAMURAI_WITH_OPENMP`,
`SAMURAI_CHECK_NAN`.

## Code of Conduct
By submitting this PR, you agree to follow our [Code of
Conduct](https://github.com/hpc-maths/samurai/blob/master/docs/CODE_OF_CONDUCT.md)
- [x] I agree to follow this project's Code of Conduct
  • Loading branch information
pmatalon authored Sep 10, 2024
1 parent 91a7082 commit df0ded2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ if(${WITH_OPENMP})
endif()

if(${WITH_MPI})
if (NOT HDF5_IS_PARALLEL)
message(FATAL_ERROR "HDF5 is not parallel. Please install a parallel version.")
endif()
find_package(Boost REQUIRED COMPONENTS serialization mpi)
target_link_system_libraries(
samurai
Expand Down Expand Up @@ -193,3 +196,7 @@ package_project(
INTERFACE_DEPENDENCIES_CONFIGURED ${DEPENDENCIES_CONFIGURED}
INTERFACE_INCLUDES ${INCLUDE_DIR}
)

# Append the content of the file cmake/samuraiConfig.cmake.in to the samurai.Config.cmake generated by the function `package_project`
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/cmake/samuraiConfig.cmake.in CONFIG_TO_APPEND)
file(APPEND "${CMAKE_BINARY_DIR}/CMakeFiles/samuraiConfig.cmake.install" ${CONFIG_TO_APPEND})
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,24 @@ The [tutorial](./demos/tutorial/) directory is a good first step followed by the
mamba install samurai
```

If you want to compile your scripts, you also have to install a C++ compiler and cmake.
For compiling purposes, you have to install a C++ compiler, `cmake`, and (optionaly) `make`:

```bash
mamba install cxx-compiler cmake
mamba install cxx-compiler cmake [make]
```

And finally, if you have to use PETSc to assemble the matrix of your problem, you need to install it
If you have to use PETSc to assemble the matrix of your problem, you need to install it:

```bash
mamba install petsc pkg-config
```

For parallel computation,

```bash
mamba install libboost-mpi libboost-devel libboost-headers 'hdf5=*=mpi*'
```

### From Conan Center

If you want to install samurai from Conan, you can use the following command:
Expand All @@ -248,13 +254,24 @@ Run the cmake configuration

- With mamba or conda

First, you need to create the environment with all the dependencies installed
First, you need to create the environment with all the dependencies
installed, run

```bash
mamba env create --file conda/environment.yml
mamba activate samurai-env
```

for sequential computation, or

```bash
mamba env create --file conda/mpi-environment.yml
```

for parallel computation. Then

```bash
mamba activate samurai-env
```

```bash
cmake . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_DEMOS=ON
Expand All @@ -278,6 +295,22 @@ Build the demos
cmake --build ./build --config Release
```

## CMake configuration

Here is a minimal example of `CMakeLists.txt`:

```cmake
cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 17)
project(my_samurai_project CXX)
find_package(samurai CONFIG REQUIRED)
add_executable(my_samurai_project main.cpp)
target_link_libraries(my_samurai_project PRIVATE samurai::samurai samurai::libdeps)
```

## Get help

For a better understanding of all the components of samurai, you can consult the documentation https://hpc-math-samurai.readthedocs.io.
Expand Down
38 changes: 38 additions & 0 deletions cmake/samuraiConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

option(SAMURAI_WITH_OPENMP "Enable OpenMP" OFF)
if(${SAMURAI_WITH_OPENMP})
find_dependency(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(samurai::samurai INTERFACE OpenMP::OpenMP_CXX)
target_compile_definitions(samurai::samurai INTERFACE SAMURAI_WITH_OPENMP)
else()
message(FATAL_ERROR "OpenMP not found")
endif()
endif()


option(SAMURAI_WITH_MPI "Enable MPI" OFF)
if(SAMURAI_WITH_MPI)
if (NOT HDF5_IS_PARALLEL)
message(FATAL_ERROR "HDF5 is not parallel. Please install a parallel version.")
endif()
find_dependency(Boost COMPONENTS serialization mpi)
target_link_libraries(samurai::samurai INTERFACE Boost::serialization Boost::mpi)
target_compile_definitions(samurai::samurai INTERFACE SAMURAI_WITH_MPI)
endif()


option(SAMURAI_CHECK_NAN "Check NaN in computations" OFF)
if(SAMURAI_CHECK_NAN)
target_compile_definitions(samurai::samurai INTERFACE SAMURAI_CHECK_NAN)
endif()


set(FLUX_CONTAINER_LIST array xtensor)
set(SAMURAI_FLUX_CONTAINER "xtensor" CACHE STRING "Container to store fluxes: ${FLUX_CONTAINER_LIST}")
set_property(CACHE SAMURAI_FLUX_CONTAINER PROPERTY STRINGS ${FLUX_CONTAINER_LIST})
if(NOT SAMURAI_FLUX_CONTAINER IN_LIST FLUX_CONTAINER_LIST)
message(FATAL_ERROR "SAMURAI_FLUX_CONTAINER must be one of: ${FLUX_CONTAINER_LIST}")
else()
target_compile_definitions(samurai::samurai INTERFACE FLUX_CONTAINER_${FLUX_CONTAINER})
endif()

0 comments on commit df0ded2

Please sign in to comment.