Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Fixed CMake flags so that the fast or strict math model is usedon spe…
Browse files Browse the repository at this point in the history
…cific source files where necessary. Added a PROFILE build type that turns on the optimization report in ifort.
  • Loading branch information
daminton committed Nov 17, 2022
1 parent e6319df commit 33b77d1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 42 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# You should have received a copy of the GNU General Public License along with Swiftest.
# If not, see: https://www.gnu.org/licenses.

# CMake project file for FOO
# CMake project file for SWIFTEST

##################################################
# Define the project and the depencies that it has
Expand Down Expand Up @@ -63,7 +63,7 @@ SET(BIN ${CMAKE_SOURCE_DIR}/bin)
# Have the .mod files placed in the lib folder
SET(CMAKE_Fortran_MODULE_DIRECTORY ${LIB})

# The source for the FOO binary and have it placed in the bin folder
# The source for the SWIFTEST binary and have it placed in the bin folder
ADD_SUBDIRECTORY(${SRC} ${BIN})

# Add a distclean target to the Makefile
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ To build Swiftest with the release flags (default), type the following:
```
$ cmake ..
```
To buid with the debug flags, type:
To build with the debug flags, type:
```
$ cmake .. -DCMAKE_BUILD_TYPE=DEBUG
```
To build with profiling flags, type:
```
$ cmake .. -DCMAKE_BUILD_TYPE=PROFILE
```
Finally, to build with the testing flags, type:
```
$ cmake .. -DCMAKE_BUILD_TYPE=TESTING
Expand Down
4 changes: 2 additions & 2 deletions cmake/Modules/SetCompileFlag.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
# "-Wall" # GNU
# "-warn all" # Intel
# )
# The optin "-Wall" will be checked first, and if it works, will be
# The option "-Wall" will be checked first, and if it works, will be
# appended to the CMAKE_C_FLAGS variable. If it doesn't work, then
# "-warn all" will be tried. If this doesn't work then checking will
# terminate because REQUIRED was given.
#
# The reasong that the variable must be given twice (first as the name then
# The reasoning that the variable must be given twice (first as the name then
# as the value in quotes) is because of the way CMAKE handles the passing
# of variables in functions; it is difficult to extract a variable's
# contents and assign new values to it from within a function.
Expand Down
62 changes: 36 additions & 26 deletions cmake/Modules/SetFortranFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,36 @@ STRING(TOUPPER "${CMAKE_BUILD_TYPE}" BT)

IF(BT STREQUAL "RELEASE")
SET(CMAKE_BUILD_TYPE RELEASE CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, or TESTING."
"Choose the type of build, options are DEBUG, RELEASE, PROFILE, or TESTING."
FORCE)
ELSEIF(BT STREQUAL "DEBUG")
SET (CMAKE_BUILD_TYPE DEBUG CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, or TESTING."
"Choose the type of build, options are DEBUG, RELEASE, PROFILE, or TESTING."
FORCE)
ELSEIF(BT STREQUAL "TESTING")
SET (CMAKE_BUILD_TYPE TESTING CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, or TESTING."
"Choose the type of build, options are DEBUG, RELEASE, PROFILE, or TESTING."
FORCE)
ELSEIF(BT STREQUAL "PROFILE")
SET (CMAKE_BUILD_TYPE PROFILE CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, PROFILE, or TESTING."
FORCE)
ELSEIF(NOT BT)
SET(CMAKE_BUILD_TYPE RELEASE CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, or TESTING."
"Choose the type of build, options are DEBUG, RELEASE, PROFILE, or TESTING."
FORCE)
MESSAGE(STATUS "CMAKE_BUILD_TYPE not given, defaulting to RELEASE")
ELSE()
MESSAGE(FATAL_ERROR "CMAKE_BUILD_TYPE not valid, choices are DEBUG, RELEASE, or TESTING")
MESSAGE(FATAL_ERROR "CMAKE_BUILD_TYPE not valid, choices are DEBUG, RELEASE, PROFILE, or TESTING")
ENDIF(BT STREQUAL "RELEASE")

#########################################################
# If the compiler flags have already been set, return now
#########################################################

IF(CMAKE_Fortran_FLAGS_RELEASE AND CMAKE_Fortran_FLAGS_TESTING AND CMAKE_Fortran_FLAGS_DEBUG)
IF(CMAKE_Fortran_FLAGS_RELEASE AND CMAKE_Fortran_FLAGS_TESTING AND CMAKE_Fortran_FLAGS_DEBUG AND CMAKE_Fortran_FLAGS_PROFILE)
RETURN ()
ENDIF(CMAKE_Fortran_FLAGS_RELEASE AND CMAKE_Fortran_FLAGS_TESTING AND CMAKE_Fortran_FLAGS_DEBUG)
ENDIF(CMAKE_Fortran_FLAGS_RELEASE AND CMAKE_Fortran_FLAGS_TESTING AND CMAKE_Fortran_FLAGS_DEBUG AND CMAKE_Fortran_FLAGS_PROFILE)

########################################################################
# Determine the appropriate flags for this compiler for each build type.
Expand Down Expand Up @@ -81,7 +85,6 @@ SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS}"
###################
### DEBUG FLAGS ###
###################

# NOTE: debugging symbols (-g or /debug:full) are already on by default

# Disable optimizations
Expand Down Expand Up @@ -163,7 +166,7 @@ SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG}"

# Aligns a variable to a specified boundary and offset
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG}"
Fortran "-align all" # Intel
Fortran "-align all -align array64byte" # Intel
)

# Enables changing the variable and array memory layout
Expand Down Expand Up @@ -215,7 +218,6 @@ SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_TESTING "${CMAKE_Fortran_FLAGS_TESTING}"
#####################
### RELEASE FLAGS ###
#####################

# NOTE: agressive optimizations (-O3) are already turned on by default

# Unroll loops
Expand All @@ -234,19 +236,6 @@ SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
"-Minline" # Portland Group
)

# Interprocedural (link-time) optimizations
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-ipo" # Intel
"/Qipo" # Intel Windows
"-flto" # GNU
"-Mipa" # Portland Group
)

# Single-file optimizations
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-ip" # Intel
"/Qip" # Intel Windows
)

# Allows for lines longer than 80 characters without truncation
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Expand Down Expand Up @@ -299,7 +288,28 @@ SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-fma" # Intel
)

# Enables agressive optimixation on floating-points
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-fp-model=fast" # Intel

#####################
### MATH FLAGS ###
#####################
# Some subroutines require more strict floating point operation optimizations for repeatability
SET_COMPILE_FLAG(STRICTMATH_FLAGS "${STRICTMATH_FLAGS}"
Fortran "-fp-model=precise -prec-div -prec-sqrt -assume protect-parens" # Intel
"/fp:precise /Qprec-div /Qprec-sqrt /assume:protect-parens" # Intel Windows
)

# Most subroutines can use aggressive optimization of floating point operations without problems.
SET_COMPILE_FLAG(FASTMATH_FLAGS "${FASTMATH_FLAGS}"
Fortran "-fp-model=fast"
"/fp:fast"
)

#####################
### PROFILE FLAGS ###
#####################
# Enables the optimization reports to be generated
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_PROFILE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-pg -qopt-report=5 -traceback -p -g3" # Intel
"/Qopt-report:5 /traceback -g3" # Windows Intel
"-pg -fbacktrace"
)
33 changes: 22 additions & 11 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
########################################

# Add the source files
SET(FOO_src
SET(FAST_MATH_FILES
${SRC}/modules/encounter_classes.f90
${SRC}/modules/fraggle_classes.f90
${SRC}/modules/helio_classes.f90
Expand Down Expand Up @@ -41,12 +41,10 @@ SET(FOO_src
${SRC}/gr/gr.f90
${SRC}/helio/helio_drift.f90
${SRC}/helio/helio_gr.f90
${SRC}/helio/helio_kick.f90
${SRC}/helio/helio_setup.f90
${SRC}/helio/helio_step.f90
${SRC}/helio/helio_util.f90
${SRC}/io/io.f90
${SRC}/kick/kick.f90
${SRC}/netcdf/netcdf.f90
${SRC}/obl/obl.f90
${SRC}/operators/operator_cross.f90
Expand All @@ -55,7 +53,6 @@ SET(FOO_src
${SRC}/rmvs/rmvs_discard.f90
${SRC}/rmvs/rmvs_encounter_check.f90
${SRC}/rmvs/rmvs_io.f90
${SRC}/rmvs/rmvs_kick.f90
${SRC}/rmvs/rmvs_setup.f90
${SRC}/rmvs/rmvs_step.f90
${SRC}/rmvs/rmvs_util.f90
Expand All @@ -66,7 +63,6 @@ SET(FOO_src
${SRC}/symba/symba_encounter_check.f90
${SRC}/symba/symba_gr.f90
${SRC}/symba/symba_io.f90
${SRC}/symba/symba_kick.f90
${SRC}/symba/symba_setup.f90
${SRC}/symba/symba_step.f90
${SRC}/symba/symba_util.f90
Expand Down Expand Up @@ -96,24 +92,31 @@ SET(FOO_src
${SRC}/whm/whm_coord.f90
${SRC}/whm/whm_drift.f90
${SRC}/whm/whm_gr.f90
${SRC}/whm/whm_kick.f90
${SRC}/whm/whm_setup.f90
${SRC}/whm/whm_step.f90
${SRC}/whm/whm_util.f90
${SRC}/main/swiftest_driver.f90
)
SET(STRICT_MATH_FILES
${SRC}/kick/kick.f90
${SRC}/helio/helio_kick.f90
${SRC}/rmvs/rmvs_kick.f90
${SRC}/symba/symba_kick.f90
${SRC}/whm/whm_kick.f90
)

set(SWIFTEST_src ${FAST_MATH_FILES} ${STRICT_MATH_FILES})

# Define the executable in terms of the source files
ADD_EXECUTABLE(${SWIFTEST_DRIVER} ${FOO_src})
ADD_EXECUTABLE(${SWIFTEST_DRIVER} ${SWIFTEST_src})

#####################################################
# Add the needed libraries and special compiler flags
#####################################################

# Uncomment if you need to link to BLAS and LAPACK
# # Uncomment if you need to link to BLAS and LAPACK
TARGET_LINK_LIBRARIES(${SWIFTEST_DRIVER} ${NETCDF_LIBRARIES} ${NETCDF_FORTRAN_LIBRARIES})

# Uncomment if you have parallization
IF(USE_OPENMP)
SET_TARGET_PROPERTIES(${SWIFTEST_DRIVER} PROPERTIES
COMPILE_FLAGS "${OpenMP_Fortran_FLAGS}"
Expand All @@ -127,7 +130,6 @@ ELSEIF(USE_MPI)
ENDIF(USE_OPENMP)



#####################################
# Tell how to install this executable
#####################################
Expand All @@ -137,4 +139,13 @@ IF(WIN32)
ELSE()
SET(CMAKE_INSTALL_PREFIX /usr/local)
ENDIF(WIN32)
INSTALL(TARGETS ${SWIFTEST_DRIVER} RUNTIME DESTINATION bin)
INSTALL(TARGETS ${SWIFTEST_DRIVER} RUNTIME DESTINATION bin)


#Set strict vs fast math flags
STRING(TOUPPER "${CMAKE_BUILD_TYPE}" BT)
IF(BT STREQUAL "RELEASE" OR BT STREQUAL "PROFILE")
SET_PROPERTY(SOURCE ${STRICT_MATH_FILES} APPEND_STRING PROPERTY COMPILE_FLAGS "${STRICTMATH_FLAGS}")
SET_PROPERTY(SOURCE ${FAST_MATH_FILES} APPEND_STRING PROPERTY COMPILE_FLAGS "${FASTMATH_FLAGS}")
ENDIF()

0 comments on commit 33b77d1

Please sign in to comment.