Skip to content

Commit

Permalink
Try simultaneous compilation of serial and MPI executables
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanBryan51 committed Jan 29, 2024
1 parent f4901b2 commit ecc53ed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 57 deletions.
83 changes: 37 additions & 46 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,31 @@ project(
LANGUAGES Fortran
)

option(CABLE_MPI "Build the MPI executable" OFF)

# third party libs
find_package(MPI REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(NETCDF REQUIRED IMPORTED_TARGET "netcdf-fortran")
if(CABLE_MPI)
find_package(MPI REQUIRED)
endif()
set(
CABLE_LINK_LIBRARIES
PkgConfig::NETCDF
MPI::MPI_Fortran
)

set(CABLE_INTEL_Fortran_FLAGS -fp-model precise)
set(CABLE_INTEL_Fortran_FLAGS_DEBUG -O0 -g -traceback -fpe0)
set(CABLE_INTEL_Fortran_FLAGS_RELEASE -O2)
if(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
set(
CABLE_Fortran_FLAGS
${CABLE_INTEL_Fortran_FLAGS}
"$<$<CONFIG:Release>:${CABLE_INTEL_Fortran_FLAGS_RELEASE}>"
"$<$<CONFIG:Debug>:${CABLE_INTEL_Fortran_FLAGS_DEBUG}>"
)
endif()

# CMake $<...> syntax is explained here:
# https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#introduction
set(CABLE_EXE_NAME "$<IF:$<BOOL:${CABLE_MPI}>,cable-mpi,cable>")

set(
CABLE_SRCS_SERIAL
src/offline/cable_driver.F90
)
set(
CABLE_SRCS_MPI
src/offline/cable_mpidrv.F90
src/offline/cable_mpicommon.F90
src/offline/cable_mpimaster.F90
src/offline/cable_mpiworker.F90
src/science/pop/pop_mpi.F90
)
set(
CABLE_SRCS_COMMON
add_library(
cable_common_objlib
OBJECT
src/science/casa-cnp/bgcdriver.F90
src/science/casa-cnp/biogeochem_casa.F90
src/offline/cable_abort.F90
Expand Down Expand Up @@ -146,30 +140,27 @@ set(
src/science/roughness/roughnessHGT_effLAI_cbl.F90
src/offline/spincasacnp.F90
)
set(
CABLE_SRCS
"$<IF:$<BOOL:${CABLE_MPI}>,${CABLE_SRCS_MPI},${CABLE_SRCS_SERIAL}>"
${CABLE_SRCS_COMMON}
)
target_compile_options(cable_common_objlib PRIVATE ${CABLE_Fortran_FLAGS})
target_link_libraries(cable_common_objlib ${CABLE_LINK_LIBRARIES})

add_executable(cable ${CABLE_SRCS})

set_target_properties(cable PROPERTIES OUTPUT_NAME ${CABLE_EXE_NAME})

if(CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
target_compile_options(
cable
PRIVATE
${CABLE_INTEL_Fortran_FLAGS}
"$<$<CONFIG:Release>:${CABLE_INTEL_Fortran_FLAGS_RELEASE}>"
"$<$<CONFIG:Debug>:${CABLE_INTEL_Fortran_FLAGS_DEBUG}>"
)
endif()

target_link_libraries(
add_executable(
cable
PkgConfig::NETCDF
"$<$<BOOL:${CABLE_MPI}>:MPI::MPI_Fortran>"
src/offline/cable_driver.F90
"$<TARGET_OBJECTS:cable_common_objlib>"
)
target_compile_options(cable PRIVATE ${CABLE_Fortran_FLAGS})
target_link_libraries(cable ${CABLE_LINK_LIBRARIES})

add_executable(
cable-mpi
src/offline/cable_mpidrv.F90
src/offline/cable_mpicommon.F90
src/offline/cable_mpimaster.F90
src/offline/cable_mpiworker.F90
src/science/pop/pop_mpi.F90
"$<TARGET_OBJECTS:cable_common_objlib>"
)
target_compile_options(cable-mpi PRIVATE ${CABLE_Fortran_FLAGS})
target_link_libraries(cable-mpi ${CABLE_LINK_LIBRARIES})

install(TARGETS cable RUNTIME)
install(TARGETS cable cable-mpi RUNTIME)
14 changes: 3 additions & 11 deletions build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Build script wrapper around CMake.
Options:
--clean Delete build directory before invoking CMake.
--mpi Compile MPI executable.
-d, --debug Compile in debug mode.
-v, --verbose Enable verbose output when building the project.
-h, --help Show this screen.
Expand All @@ -22,9 +21,6 @@ while [ $# -gt 0 ]; do
--clean)
clean=1
;;
--mpi)
mpi=1
;;
-d|--debug)
debug=1
;;
Expand All @@ -45,15 +41,13 @@ done
if hostname -f | grep gadi.nci.org.au > /dev/null; then
module purge
module add intel-compiler/2019.5.281
module add intel-mpi/2019.5.281
module add netcdf/4.6.3
# This is required so that the netcdf-fortran library is discoverable by
# pkg-config (Note, we manually prepend to PKG_CONFIG_PATH instead of
# using `module prepend-path` so that we can invoke this script successfully
# in benchcab):
export PKG_CONFIG_PATH="${NETCDF_BASE}/lib/Intel/pkgconfig/:${PKG_CONFIG_PATH}"
if [[ -n $mpi ]]; then
module add intel-mpi/2019.5.281
fi
fi

if [[ -n $clean ]]; then
Expand All @@ -66,10 +60,8 @@ if [[ -n $debug ]]; then
else
cmake_args+=(-DCMAKE_BUILD_TYPE=Release)
fi
if [[ -n $mpi ]]; then
cmake_args+=(-DCABLE_MPI="ON")
cmake_args+=(-DMPI_Fortran_COMPILER="mpif90")
fi

cmake_args+=(-DMPI_Fortran_COMPILER="mpif90")

cmake_build_args=()
if [[ -n $verbose ]]; then
Expand Down

0 comments on commit ecc53ed

Please sign in to comment.