Skip to content

Commit de8c576

Browse files
authored
CMake: AMReX::Flags_FASTMATH Interface Helper Target (#4591)
## Summary This PR _changes no defaults_. It just refactors #4545 a bit further prior to adding the option `AMReX_FASTMATH` in a first AMReX release, providing a better internal implementation and a user-friendly helper target (fully optional). Add an interface target for the fast-math flags. This makes it easier to re-use them downstream when AMReX is pre-compiled w/o fast-math (default in all of our current package managers), but the main code wants to still use fast-math on the side of the application code. ## Additional background Follow-up to #4545, see comment #4545 (comment)
1 parent 05d5fa0 commit de8c576

File tree

6 files changed

+52
-44
lines changed

6 files changed

+52
-44
lines changed

Docs/sphinx_documentation/source/BuildingAMReX.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ The list of available options is reported in the :ref:`table <tab:cmakevar>` bel
443443
+------------------------------+-------------------------------------------------+-------------------------+-----------------------+
444444
| AMReX_BUILD_SHARED_LIBS | Build as shared C++ library | NO (unless xSDK) | YES, NO |
445445
+------------------------------+-------------------------------------------------+-------------------------+-----------------------+
446-
| AMReX_FASTMATH | Enable fast-math optimizations | NO (CPU), YES (CUDA) | YES, NO |
446+
| AMReX_FASTMATH | Enable fast-math optimizations | NO (CUDA is ON) | YES, NO |
447447
+------------------------------+-------------------------------------------------+-------------------------+-----------------------+
448448
| AMReX_FORTRAN | Enable Fortran language | NO | YES, NO |
449449
+------------------------------+-------------------------------------------------+-------------------------+-----------------------+

Src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ foreach(D IN LISTS AMReX_SPACEDIM)
7979
)
8080
endif ()
8181

82+
if (AMReX_FASTMATH)
83+
target_link_libraries(amrex_${D}d PUBLIC AMReX::Flags_FASTMATH)
84+
endif ()
85+
8286
if (AMReX_FPE)
8387
target_link_libraries(amrex_${D}d
8488
PUBLIC

Tools/CMake/AMReXFlagsTargets.cmake

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55
# Flags_CXX --> Optional flags for C++ code
66
# Flags_Fortran --> Optional flags for Fortran code
7+
# Flags_FASTMATH --> Optional flags for fast-math (floating-point)
78
# Flags_FPE --> Floating-Point Exception flags for both C++ and Fortran
89
# Flags_INLINE --> Optional flags for inlining
910
#
@@ -21,15 +22,15 @@ include_guard(GLOBAL)
2122
#
2223
# for every combination of
2324
#
24-
# <lang> = cxx,fortran
25-
# <id> = gnu,intel,pgi,cray,clang,appleclang,intelllvm,msvc
25+
# <lang> = cxx,fortran,cuda
26+
# <id> = gnu,intel,pgi,cray,clang,appleclang,crayclang,ibmclang,intelllvm,msvc,nvidia,nvhpc,xlclang
2627
#
2728
if (CMAKE_VERSION VERSION_LESS 3.20)
28-
foreach (_language CXX Fortran )
29+
foreach (_language CXX Fortran CUDA )
2930
set(_comp_lang "$<COMPILE_LANGUAGE:${_language}>")
3031
string(TOLOWER "${_language}" _lang)
3132

32-
foreach (_comp GNU Intel PGI Cray Clang AppleClang IntelLLVM MSVC )
33+
foreach (_comp GNU Intel PGI Cray Clang AppleClang CrayClang IBMClang IntelLLVM MSVC NVIDIA NVHPC XLClang )
3334
string(TOLOWER "${_comp}" _id)
3435
# Define variables
3536
set(_comp_id "$<${_language}_COMPILER_ID:${_comp}>")
@@ -44,10 +45,10 @@ if (CMAKE_VERSION VERSION_LESS 3.20)
4445
unset(_lang)
4546
endforeach ()
4647
else ()
47-
foreach (_language CXX Fortran )
48+
foreach (_language CXX Fortran CUDA )
4849
string(TOLOWER "${_language}" _lang)
4950

50-
foreach (_comp GNU Intel PGI Cray Clang AppleClang IntelLLVM MSVC )
51+
foreach (_comp GNU Intel PGI Cray Clang AppleClang CrayClang IBMClang IntelLLVM MSVC NVIDIA NVHPC XLClang )
5152
string(TOLOWER "${_comp}" _id)
5253
# Define variables
5354
set(_${_lang}_${_id} "$<COMPILE_LANG_AND_ID:${_language},${_comp}>")
@@ -127,6 +128,35 @@ target_compile_options( Flags_Fortran
127128
)
128129

129130

131+
#
132+
# Fast-Math (for floating point)
133+
#
134+
add_library(Flags_FASTMATH INTERFACE)
135+
add_library(AMReX::Flags_FASTMATH ALIAS Flags_FASTMATH)
136+
137+
target_compile_options( Flags_FASTMATH
138+
INTERFACE
139+
$<${_cuda_nvidia}:--use_fast_math>
140+
$<${_cuda_nvhpc}:-fast>
141+
$<${_fortran_gnu}:-ffast-math>
142+
$<${_cxx_gnu}:-ffast-math>
143+
$<${_fortran_intel}:-ffast-math>
144+
$<${_cxx_intel}:-ffast-math>
145+
$<${_fortran_pgi}:-ffast-math>
146+
$<${_cxx_pgi}:-ffast-math>
147+
$<${_fortran_cray}:-ffast-math>
148+
$<${_cxx_cray}:-ffast-math>
149+
$<${_fortran_clang}:-ffast-math>
150+
$<${_cxx_clang}:-ffast-math>
151+
$<${_cxx_appleclang}:-ffast-math>
152+
$<${_cxx_crayclang}:-ffast-math>
153+
$<${_cxx_ibmclang}:-ffast-math>
154+
$<${_cxx_intelllvm}:-ffast-math>
155+
$<${_cxx_xlclang}:-ffast-math>
156+
$<${_cxx_msvc}:/fp:fast>
157+
)
158+
159+
130160
#
131161
# Floating point exceptions
132162
#

Tools/CMake/AMReXOptions.cmake

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,8 @@ print_option(AMReX_GPU_RDC)
256256
#
257257
# Fast Math ================================================================
258258
#
259-
set(_FASTMATH_default OFF)
260-
if(AMReX_GPU_BACKEND STREQUAL CUDA) # note: historic settings
261-
# if(NOT AMReX_GPU_BACKEND STREQUAL NONE) # note: this would be more consistent for GPUs
262-
set(_FASTMATH_default ON)
263-
endif()
264-
option(AMReX_FASTMATH "Enable fast-math optimizations" ${_FASTMATH_default})
259+
option(AMReX_FASTMATH "Enable fast-math optimizations" OFF)
265260
print_option(AMReX_FASTMATH)
266-
unset(_FASTMATH_default)
267261

268262
#
269263
# Parallel backends ========================================================

Tools/CMake/AMReXParallelBackends.cmake

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,6 @@ if (AMReX_SYCL)
217217
include(AMReXSYCL)
218218
foreach(D IN LISTS AMReX_SPACEDIM)
219219
target_link_libraries(amrex_${D}d PUBLIC SYCL)
220-
221-
# fast math
222-
if(AMReX_FASTMATH)
223-
target_compile_options(amrex_${D}d PUBLIC -ffast-math)
224-
endif()
225220
endforeach()
226221
endif ()
227222

@@ -367,11 +362,6 @@ if (AMReX_HIP)
367362
foreach(D IN LISTS AMReX_SPACEDIM)
368363
target_compile_options(amrex_${D}d PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-m64>)
369364

370-
# fast math
371-
if(AMReX_FASTMATH)
372-
target_compile_options(amrex_${D}d PUBLIC -ffast-math)
373-
endif()
374-
375365
# ROCm 4.5: use unsafe floating point atomics, otherwise atomicAdd is much slower
376366
#
377367
target_compile_options(amrex_${D}d PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-munsafe-fp-atomics>)

Tools/CMake/AMReX_Config.cmake

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
#
1313
function (configure_amrex AMREX_TARGET)
1414

15+
#
16+
# Include the required modules
17+
#
18+
include( AMReX_ThirdPartyProfilers )
19+
include( AMReXGenexHelpers )
20+
include( AMReXFlagsTargets )
21+
1522
#
1623
# Check if target "amrex" has been defined before
1724
# calling this macro
@@ -24,16 +31,11 @@ function (configure_amrex AMREX_TARGET)
2431
# Check that needed options have already been defined
2532
#
2633
if ( ( NOT ( DEFINED AMReX_MPI ) ) OR ( NOT (DEFINED AMReX_OMP) )
27-
OR ( NOT (DEFINED AMReX_PIC) ) OR (NOT (DEFINED AMReX_FPE)))
34+
OR ( NOT (DEFINED AMReX_PIC) ) OR (NOT (DEFINED AMReX_FASTMATH))
35+
OR (NOT (DEFINED AMReX_FPE)))
2836
message ( AUTHOR_WARNING "Required options are not defined" )
2937
endif ()
3038

31-
#
32-
# Include the required modules
33-
#
34-
include( AMReX_ThirdPartyProfilers )
35-
include( AMReXGenexHelpers )
36-
3739
#
3840
# Setup compilers
3941
#
@@ -147,19 +149,7 @@ function (configure_amrex AMREX_TARGET)
147149

148150
# fast math
149151
if (AMReX_FASTMATH)
150-
# GPU specific backends set in AMReXParallelBackends.cmake
151-
if (AMReX_GPU_BACKEND STREQUAL NONE)
152-
# See https://cmake.org/cmake/help/v4.1/variable/CMAKE_LANG_COMPILER_ID.html#variable:CMAKE_%3CLANG%3E_COMPILER_ID
153-
target_compile_options(${AMREX_TARGET} PUBLIC
154-
$<$<CXX_COMPILER_ID:AppleClang,Clang,CrayClang,GNU,IBMClang,IntelLLVM,XLClang>:-ffast-math>
155-
$<${_cxx_msvc}:"/fp:fast"> # MSVC
156-
)
157-
if (CMAKE_Fortran_COMPILER_LOADED)
158-
target_compile_options(${AMREX_TARGET} PUBLIC
159-
$<$<Fortran_COMPILER_ID:AppleClang,Clang,CrayClang,GNU,IBMClang,IntelLLVM,XLClang>:-ffast-math>
160-
)
161-
endif ()
162-
endif()
152+
target_link_libraries(${AMREX_TARGET} PUBLIC AMReX::Flags_FASTMATH)
163153
endif()
164154

165155
#

0 commit comments

Comments
 (0)