Skip to content

Commit

Permalink
Merge pull request #74 from ecmwf-ifs/naan-dir-reorder
Browse files Browse the repository at this point in the history
Directory reorganisation
  • Loading branch information
mlange05 authored Jan 27, 2025
2 parents 596bf04 + f9a4878 commit f868832
Show file tree
Hide file tree
Showing 52 changed files with 631 additions and 125 deletions.
143 changes: 29 additions & 114 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,14 @@ endif()
project(field_api VERSION 0.3.4 LANGUAGES Fortran)
set(LIBNAME field_api)

## load cmake macros
include( field_api_macros )

## find required packages
ecbuild_find_package(OpenMP COMPONENTS Fortran REQUIRED)

## find fypp
ecbuild_find_package( fckit )
find_program( FYPP fypp )

if( fckit_FOUND AND fckit_HAVE_FCKIT_VENV )
set( FYPP ${FCKIT_VENV_EXE} -m fypp )
elseif( fckit_FOUND )
# This is only needed for building in environments with python3 older than 3.8
list( APPEND _fckit_fypp_path "${FYPP}" )
list( LENGTH _fckit_fypp_path _list_length )
MATH( EXPR _last_entry "${_list_length} - 1" )
list( GET _fckit_fypp_path ${_last_entry} FYPP )
elseif( FYPP MATCHES FYPP-NOTFOUND )
include(cmake/field_api_fetchcontent_fypp.cmake)
set(FYPP ${fypp_SOURCE_DIR}/bin/fypp)
ecbuild_info("fypp downloaded to: ${FYPP}")
endif()
field_api_find_fypp()

## find OpenACC
if( ${CMAKE_VERSION} VERSION_LESS "3.25" )
Expand All @@ -65,7 +53,7 @@ ecbuild_add_option( FEATURE ACC
CONDITION CMAKE_Fortran_COMPILER_ID MATCHES "PGI|NVHPC")

## set general compiler flags
include(cmake/field_api_compile_options.cmake)
field_api_compile_options()

## choose to build dp or sp or both
ecbuild_add_option( FEATURE SINGLE_PRECISION
Expand All @@ -85,32 +73,7 @@ if(HAVE_DOUBLE_PRECISION)
endif()

## find fiat
ecbuild_find_package(NAME fiat COMPONENTS ${fiat_components})
if( NOT fiat_FOUND )
if(NOT UTIL_MODULE_PATH)
ecbuild_critical("If not building with fiat, then the path for utility modules must be specified")
endif()

ecbuild_info( "Checking for FIAT components in ${UTIL_MODULE_PATH}" )

find_file( ABOR1_PATH abor1.F90 REQUIRED
HINTS ${UTIL_MODULE_PATH} ${UTIL_MODULE_PATH}/src/fiat/util
)
ecbuild_info( "Found ABOR1: ${ABOR1_PATH}" )

find_file( OML_PATH oml_mod.F90 REQUIRED
HINTS ${UTIL_MODULE_PATH} ${UTIL_MODULE_PATH}/src/fiat/oml
)
ecbuild_info( "Found OML: ${OML_PATH}" )

find_file( PARKIND1_PATH parkind1.F90 REQUIRED
HINTS ${UTIL_MODULE_PATH} ${UTIL_MODULE_PATH}/src/parkind
)
ecbuild_info( "Found PARKIND1: ${PARKIND1_PATH}" )
list(APPEND srcs ${ABOR1_PATH} ${OML_PATH} ${PARKIND1_PATH})
endif()

list(APPEND srcs field_async_module.F90 field_basic_module.F90 field_defaults_module.F90 dev_alloc.c c_malloc.c field_constants_module.F90 field_abort_module.F90 field_statistics_module.F90 field_backtrace.c)
field_api_find_fiat_modules()

## check for CUDA
include(CheckLanguage)
Expand All @@ -132,8 +95,6 @@ ecbuild_add_option( FEATURE FIELD_GANG
CONDITION (NOT CMAKE_Fortran_COMPILER_ID MATCHES "Cray")
)

check_symbol_exists(backtrace execinfo.h HAVE_BACKTRACE)

## fypp preprocessor flags
if(HAVE_BUDDY_MALLOC)
list( APPEND fypp_defines "-DUSE_BUDDY_MALLOC")
Expand All @@ -148,90 +109,40 @@ if(HAVE_FIELD_GANG)
list( APPEND fypp_defines "-DHAVE_GANG")
endif()

unset( ranksuff_srcs )
## preprocess fypp files
list( APPEND ranksuff_srcs _shuffle _access _util _array _array_util _factory _gather _data)
if(HAVE_FIELD_GANG)
list( APPEND ranksuff_srcs _gang )
endif()

foreach (SUFF IN ITEMS IM RM RD LM)
string (TOLOWER ${SUFF} suff)
foreach (RANK RANGE 1 5)
foreach (FUNC ${ranksuff_srcs} "")
add_custom_command (OUTPUT field_${RANK}${suff}${FUNC}_module.F90
COMMAND ${FYPP} -DRANK=${RANK} -DSUFF='${SUFF}' ${fypp_defines} -m os -M ${CMAKE_CURRENT_SOURCE_DIR} -m fieldType
${CMAKE_CURRENT_SOURCE_DIR}/field_RANKSUFF${FUNC}_module.fypp > field_${RANK}${suff}${FUNC}_module.F90
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/field_RANKSUFF${FUNC}_module.fypp
VERBATIM)
list(APPEND srcs "field_${RANK}${suff}${FUNC}_module.F90")
set_source_files_properties( "field_${RANK}${suff}${FUNC}_module.F90" PROPERTIES COMPILE_OPTIONS $<${HAVE_CUDA}:-cuda>)
endforeach ()
endforeach ()
endforeach ()

unset( non_ranksuff_srcs )
list ( APPEND non_ranksuff_srcs dev_alloc_module field_factory_module field_access_module field_array_module field_module
field_shuffle_module field_util_module field_array_util_module field_shuffle_type_module host_alloc_module
field_gathscat_module field_gathscat_type_module)
if(HAVE_FIELD_GANG)
list( APPEND non_ranksuff_srcs field_gang_module )
endif()

foreach (SRC ${non_ranksuff_srcs} )
add_custom_command (OUTPUT ${SRC}.F90
COMMAND ${FYPP} -m os ${fypp_defines} -M ${CMAKE_CURRENT_SOURCE_DIR} -m fieldType ${CMAKE_CURRENT_SOURCE_DIR}/${SRC}.fypp > ${SRC}.F90
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SRC}.fypp
VERBATIM)
list(APPEND srcs "${SRC}.F90")
endforeach ()
set_source_files_properties("dev_alloc_module.F90" PROPERTIES COMPILE_OPTIONS $<${HAVE_CUDA}:-cuda>)
set_source_files_properties("host_alloc_module.F90" PROPERTIES COMPILE_OPTIONS $<${HAVE_CUDA}:-cuda>)
## build precision independent srcs
add_subdirectory(src)

## determine lib type
cmake_dependent_option(BUILD_SHARED_LIBS "Dynamically link field_api" ON "NOT HAVE_ACC" OFF)
if( HAVE_ACC )
ecbuild_warn("OpenACC builds force static linking.")
endif()

list(APPEND prec_srcs field_module.F90 field_array_module.F90 field_array_util_module.F90 field_util_module.F90)
list(REMOVE_ITEM srcs ${prec_srcs})

## build precision independent source files
ecbuild_add_library(
TARGET ${LIBNAME}
TYPE OBJECT
SOURCES ${srcs}
DEFINITIONS $<$<NOT:${fiat_FOUND}>:${FIELD_API_DEFINITIONS}> $<${fiat_FOUND}:WITH_FIAT>
PRIVATE_LIBS
$<${HAVE_ACC}:OpenACC::OpenACC_Fortran>
$<${fiat_FOUND}:fiat>
$<${fiat_FOUND}:parkind_${DEFAULT_PRECISION}>
OpenMP::OpenMP_Fortran
)
target_include_directories( ${LIBNAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TARGET ${LIBNAME} PROPERTY C_STANDARD 99)
set_target_properties( ${LIBNAME} PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include/${LIBNAME} )
target_link_options( ${LIBNAME} PRIVATE $<${HAVE_CUDA}:-cuda> )
if( HAVE_BACKTRACE )
target_compile_definitions( ${LIBNAME} PRIVATE HAVE_BACKTRACE )
endif()

# set install location for .mod files
install(DIRECTORY ${CMAKE_BINARY_DIR}/include/${LIBNAME} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
## specify precision dependent sources that must be compiled twice
list(APPEND prec_srcs
${CMAKE_CURRENT_BINARY_DIR}/src/core/field_module.F90
${CMAKE_CURRENT_BINARY_DIR}/src/util/field_array_module.F90
${CMAKE_CURRENT_BINARY_DIR}/src/util/field_array_util_module.F90
${CMAKE_CURRENT_BINARY_DIR}/src/util/field_util_module.F90)

## add field_api targets
foreach(prec ${precisions})
## add field_api target
ecbuild_add_library(
TARGET ${LIBNAME}_${prec}
SOURCES ${prec_srcs} $<TARGET_OBJECTS:${LIBNAME}>
SOURCES ${prec_srcs}
DEFINITIONS $<$<NOT:${fiat_FOUND}>:${FIELD_API_DEFINITIONS}>
OBJECTS
field_api_core
field_api_debug
field_api_factory
field_api_util
field_api_shuffle
PRIVATE_LIBS
$<${fiat_FOUND}:fiat>
$<${fiat_FOUND}:parkind_${prec}>
OpenMP::OpenMP_Fortran
)
target_include_directories( ${LIBNAME}_${prec} PRIVATE ${CMAKE_BINARY_DIR}/include/${LIBNAME})

set_property(TARGET ${LIBNAME}_${prec} PROPERTY C_STANDARD 99)
set_target_properties( ${LIBNAME}_${prec} PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include/${LIBNAME}_${prec} )
target_link_options( ${LIBNAME}_${prec} PUBLIC $<${HAVE_CUDA}:-cuda> )
Expand All @@ -244,10 +155,14 @@ foreach(prec ${precisions})
target_include_directories( ${LIBNAME}_${prec}
INTERFACE $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include/${LIBNAME}_${prec}>
INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${LIBNAME}_${prec}>
INTERFACE $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include/${LIBNAME}>
INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${LIBNAME}>
)

# add sub-component module directories to target
field_api_target_add_module_dirs(
TARGET ${LIBNAME}_${prec}
LIBS field_api_debug field_api_core field_api_factory field_api_util field_api_shuffle
)

if( prec MATCHES sp )
target_compile_definitions( ${LIBNAME}_${prec} PRIVATE PARKIND1_SINGLE )
endif()
Expand Down
75 changes: 75 additions & 0 deletions cmake/field_api_add_object_library.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# (C) Copyright 2022- ECMWF.
# (C) Copyright 2022- Meteo-France.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

##############################################################################
#.rst:
#
# field_api_add_object_library
# ============================
#
# Create an object library from a given set of objects and/or sources. ::
#
# field_api_add_object_library( LIBNAME <name>
# OBJECTS <object-libraries>
# SRCS <sourcefiles>
# DEFINITIONS <compile-definitions>
# LIBARIES <libraries> )
#
# Note that an object library is not a full cmake target, for example
# object libraries never lead to a .so/.a file.
#
# Input variables
# ---------------
#
# :LIBNAME: Library name.
# :OBJECTS: Object libraries to include. Only target
# objects will be included, other link-time
# information is not propagated.
# :SRCS: Sources to compile.
# :DEFINITIONS: Preprocessor definitions.
# :LIBRARIES: Libraries to link against.
#
##############################################################################

macro(field_api_add_object_library)

set( options )
set( oneValueArgs LIBNAME )
set( multiValueArgs OBJECTS SRCS DEFINITIONS LIBRARIES )

cmake_parse_arguments( _PAR "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

ecbuild_add_library(
TARGET ${_PAR_LIBNAME}
TYPE OBJECT
OBJECTS ${_PAR_OBJECTS}
SOURCES ${_PAR_SRCS}
DEFINITIONS
${_PAR_DEFINITIONS}
$<$<NOT:${fiat_FOUND}>:${FIELD_API_DEFINITIONS}>
$<${fiat_FOUND}:WITH_FIAT>
PRIVATE_LIBS
${_PAR_LIBRARIES}
$<${HAVE_ACC}:OpenACC::OpenACC_Fortran>
$<${fiat_FOUND}:fiat>
$<${fiat_FOUND}:parkind_${DEFAULT_PRECISION}>
OpenMP::OpenMP_Fortran
)

target_include_directories( ${_PAR_LIBNAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
INTERFACE $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include/${_PAR_LIBNAME}> )

set_property(TARGET ${_PAR_LIBNAME} PROPERTY C_STANDARD 99)
set_target_properties( ${_PAR_LIBNAME} PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include/${_PAR_LIBNAME} )
target_link_options( ${_PAR_LIBNAME} PRIVATE $<${HAVE_CUDA}:-cuda> )

# set install location for .mod files
install(DIRECTORY ${CMAKE_BINARY_DIR}/include/${_PAR_LIBNAME} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

endmacro()
38 changes: 27 additions & 11 deletions cmake/field_api_compile_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

if(CMAKE_Fortran_COMPILER_ID MATCHES PGI|NVIDIA|NVHPC)
ecbuild_add_fortran_flags("-Mlarge_arrays")
ecbuild_add_fortran_flags("-gopt")
##############################################################################
#.rst:
#
# field_api_compile_options
# =========================
#
# Define the compiler flags used to build FIELD_API. ::
#
# field_api_compile_options()
#
##############################################################################

ecbuild_add_fortran_flags("-Minfo=accel,all,ccff" BUILD DEBUG)
macro( field_api_compile_options )

# These are architecture/compiler/offload-library specific options
# that should really be coming from external input
# set(CMAKE_Fortran_FLAGS "-gpu=cc70")
endif ()
if(CMAKE_Fortran_COMPILER_ID MATCHES PGI|NVIDIA|NVHPC)
ecbuild_add_fortran_flags("-Mlarge_arrays")
ecbuild_add_fortran_flags("-gopt")

ecbuild_add_fortran_flags("-Minfo=accel,all,ccff" BUILD DEBUG)

# These are architecture/compiler/offload-library specific options
# that should really be coming from external input
# set(CMAKE_Fortran_FLAGS "-gpu=cc70")
endif ()

if(CMAKE_Fortran_COMPILER_ID MATCHES Intel)
ecbuild_add_fortran_flags("-check nocontiguous" BUILD DEBUG)
endif()

if(CMAKE_Fortran_COMPILER_ID MATCHES Intel)
ecbuild_add_fortran_flags("-check nocontiguous" BUILD DEBUG)
endif()
endmacro()
52 changes: 52 additions & 0 deletions cmake/field_api_expand_fypp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# (C) Copyright 2022- ECMWF.
# (C) Copyright 2022- Meteo-France.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

##############################################################################
#.rst:
#
# field_api_expand_fypp_ranksuff
# ==============================
#
# Preprocess a given set of .fypp files. ::
#
# field_api_expand_fypp( PYTHON_MODULE_DIR <python-util-module-dir>
# SOURCE_DIR <source-directory>
# INPUT_SRCS <fypp files>
# OUTPUT_SRCS <processed F90 files> )
#
# Input variables
# ---------------
#
# :PYTHON_MODULE_DIR: Directory containing fieldType.py python module.
# :SOURCE_DIR: Directory containing .fypp files to be processed.
# :INPUT_SRCS: List of .fypp files to be processed.
# :OUTPUT_SRCS: List of processed .F90 files.
#
##############################################################################

macro( field_api_expand_fypp )

set( options )
set( oneValueArgs PYTHON_MODULE_DIR SOURCE_DIR )
set( multiValueArgs INPUT_SRCS OUTPUT_SRCS )

cmake_parse_arguments( _PAR "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

foreach (SRC ${_PAR_INPUT_SRCS} )

add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${SRC}.F90
COMMAND ${FYPP} -m os ${fypp_defines} -M ${_PAR_PYTHON_MODULE_DIR} -m fieldType ${_PAR_SOURCE_DIR}/${SRC}.fypp > ${CMAKE_CURRENT_BINARY_DIR}/${SRC}.F90
DEPENDS ${_PAR_SOURCE_DIR}/${SRC}.fypp
VERBATIM)

list(APPEND ${_PAR_OUTPUT_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/${SRC}.F90)

endforeach ()

endmacro()
Loading

0 comments on commit f868832

Please sign in to comment.