-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGNU_Fortran.cmake
More file actions
212 lines (180 loc) · 8.37 KB
/
GNU_Fortran.cmake
File metadata and controls
212 lines (180 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
if (CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.3)
message(FATAL_ERROR "${CMAKE_Fortran_COMPILER_ID} version must be at least 10.3!")
endif()
set (FOPT0 "-O0")
set (FOPT1 "-O1")
set (FOPT2 "-O2")
set (FOPT3 "-O3")
set (FOPT4 "-O3")
set (FOPTFAST "-Ofast")
set (DEBINFO "-g")
set (FPE0 "")
set (FPE3 "")
set (FP_MODEL_SOURCE "")
set (FP_MODEL_STRICT "")
set (FP_MODEL_CONSISTENT "")
set (OPTREPORT0 "")
set (OPTREPORT5 "")
set (FREAL8 "-fdefault-real-8 -fdefault-double-8")
set (FINT8 "-fdefault-integer-8")
set (UNUSED_DUMMY "-Wno-unused-dummy-argument")
set (PP "-cpp")
# GCC 10 changed behavior in how it handles Fortran. To wit:
# Mismatches between actual and dummy argument lists in a single file are
# now rejected with an error. Use the new option -fallow-argument-mismatch
# to turn these errors into warnings; this option is implied with
# -std=legacy. -Wargument-mismatch has been removed.
# The handling of a BOZ literal constant has been reworked to provide
# better conformance to the Fortran 2008 and 2018 standards. In these
# Fortran standards, a BOZ literal constant is a typeless and kindless
# entity. As a part of the rework, documented and undocumented extensions
# to the Fortran standard now emit errors during compilation. Some of
# these extensions are permitted with the -fallow-invalid-boz, where the
# error is degraded to a warning and the code is compiled as with older
# gfortran.
# GEOS has many places where, if not converted to warning, the model
# will not build. Until the code can be fixed, for now we set the flags to
# convert to warnings. But by using an option() we can allow this to be
# turned off for testing.
# GCC 9 and lower do not have the flag...
set (MISMATCH "")
set (ALLOW_BOZ "")
# With GCC 10...
if (CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER_EQUAL 10)
# First for the argument mismatch
option(MISMATCH_IS_ERROR "Argument mismatches are errors, not warnings" OFF)
if (NOT MISMATCH_IS_ERROR)
set (MISMATCH "-fallow-argument-mismatch")
message(STATUS "[GCC10+] Setting -fallow-argument-mismatch to allow argument mismatches")
endif ()
# Then for BOZ constants
option(INVALID_BOZ_IS_ERROR "Use of invalid BOZ constants are errors, not warnings" OFF)
if (NOT INVALID_BOZ_IS_ERROR)
set (ALLOW_BOZ "-fallow-invalid-boz")
message(STATUS "[GCC10+] Setting -fallow-invalid-boz to allow invalid BOZ constants")
endif ()
endif ()
#set (BIG_ENDIAN "-fconvert=swap") # This doesn't seem to work at the moment
#set (LITTLE_ENDIAN "") # Not sure
set (EXTENDED_SOURCE "-ffixed-line-length-132")
set (FIXED_SOURCE "-ffixed-form")
set (DISABLE_FIELD_WIDTH_WARNING "")
set (CRAY_POINTER "-fcray-pointer")
set (MCMODEL "")
set (HEAPARRAYS "")
set (BYTERECLEN "-frecord-marker=4")
set (ALIGNCOM "-falign-commons")
set (TRACEBACK "-fbacktrace")
set (NOOLD_MAXMINLOC "")
set (REALLOC_LHS "")
set (ARCH_CONSISTENCY "")
set (FTZ "")
set (ALIGN_ALL "")
set (NO_ALIAS "")
set (NO_RANGE_CHECK "-fno-range-check")
cmake_host_system_information(RESULT proc_description QUERY PROCESSOR_DESCRIPTION)
if ( ${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL aarch64 )
set (GNU_TARGET_ARCH "armv8.2-a+crypto+crc+fp16+rcpc+dotprod")
set (GNU_NATIVE_ARCH ${GNU_TARGET_ARCH})
elseif (${proc_description} MATCHES "Apple M")
if (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "arm64")
# Testing show GEOS fails with -march=native on M1 in native mode
set (GNU_TARGET_ARCH "armv8-a")
set (GNU_NATIVE_ARCH ${GNU_TARGET_ARCH})
elseif (${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
# Rosetta2 flags per tests by @climbfuji
set (GNU_TARGET_ARCH "westmere")
set (GNU_NATIVE_ARCH "native")
set (PREFER_AVX128 "-mprefer-avx128")
set (NO_FMA "-mno-fma")
endif ()
elseif (${proc_description} MATCHES "EPYC")
set (GNU_TARGET_ARCH "znver2")
set (GNU_NATIVE_ARCH "native")
set (NO_FMA "-mno-fma")
elseif (${proc_description} MATCHES "Hygon")
# Tests on a Hygon showed it returned 'nocona' for the native arch
# see https://github.com/geoschem/GCHP/issues/391
# Intel was happy with AVX2, but this test was done with GCC 10
# Perhaps later versions of GCC would return a different value?
# Until then, nocona is definitely safe. Pentium 4!
set (GNU_TARGET_ARCH "nocona")
set (GNU_NATIVE_ARCH "native")
set (NO_FMA "-mno-fma")
elseif (${proc_description} MATCHES "Intel")
set (GNU_TARGET_ARCH "haswell")
set (GNU_NATIVE_ARCH "native")
set (PREFER_AVX128 "-mprefer-avx128")
set (NO_FMA "-mno-fma")
elseif ( ${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64" )
message(WARNING "Unknown processor type. Defaulting to a generic x86_64 processor. Performance may be suboptimal.")
set (GNU_TARGET_ARCH "x86-64")
set (GNU_NATIVE_ARCH "native")
else ()
message(FATAL_ERROR "Unknown processor. Please file an issue at https://github.com/GEOS-ESM/ESMA_cmake")
endif ()
if (APPLE)
# Determine whether we need to add link options for version 15+ of the Apple command line utilities
execute_process(COMMAND "pkgutil"
"--pkg-info=com.apple.pkg.CLTools_Executables"
OUTPUT_VARIABLE TEST)
string(REGEX REPLACE ".*version: ([0-9]+).*" "\\1" CMDLINE_UTILS_VERSION ${TEST})
message(STATUS "Apple command line utils major version is '${CMDLINE_UTILS_VERSION}'")
if (${CMDLINE_UTILS_VERSION} VERSION_GREATER 14)
message(STATUS "Adding link options '-Wl,-ld_classic'")
add_link_options(-Wl,-ld_classic)
endif ()
#
# Also, if our C compiler is Apple Clang, we need to pass -Wno-implicit-int to our C flags
if (CMAKE_C_COMPILER_ID MATCHES "Clang")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-implicit-int")
endif ()
endif ()
####################################################
add_definitions(-D__GFORTRAN__)
# Common Fortran Flags
# --------------------
set (common_Fortran_flags "-ffree-line-length-none ${NO_RANGE_CHECK} -Wno-missing-include-dirs ${TRACEBACK} ${UNUSED_DUMMY}" )
set (common_Fortran_fpe_flags "-ffpe-trap=zero,overflow ${TRACEBACK} ${MISMATCH} ${ALLOW_BOZ}")
# GEOS Debug
# ----------
set (GEOS_Fortran_Debug_Flags "${FOPT0} ${DEBINFO} -fcheck=all,no-array-temps -finit-real=snan -save-temps")
set (GEOS_Fortran_Debug_FPE_Flags "${common_Fortran_fpe_flags}")
# GEOS Release
# ------------
set (GEOS_Fortran_Release_Flags "${FOPT3} -march=${GNU_TARGET_ARCH} -mtune=generic -funroll-loops ${DEBINFO}")
set (GEOS_Fortran_Release_FPE_Flags "${common_Fortran_fpe_flags}")
# Create a NoVectorize version for consistency. No difference from Release for GNU
# GEOS NoVectorize
# ----------------
set (GEOS_Fortran_NoVect_Flags "${GEOS_Fortran_Release_Flags}")
set (GEOS_Fortran_NoVect_FPE_Flags "${GEOS_Fortran_Release_FPE_Flags}")
# GEOS Vectorize
# --------------
# NOTE: gfortran does get a benefit from vectorization, but the resulting code
# does not layout regress. See Aggressive for the vectorizing flags
# Until good options can be found, make vectorize equal common flags
set (GEOS_Fortran_Vect_Flags ${GEOS_Fortran_Release_Flags})
set (GEOS_Fortran_Vect_FPE_Flags ${GEOS_Fortran_Release_FPE_Flags})
# GEOS Aggressive
# ---------------
# NOTE: gfortran does get a benefit from vectorization, but the resulting code
# does not layout regress.
# NOTE2: This uses -march=native so compile on your target architecture!!!
# Options per Jerry DeLisle on GCC Fortran List
if (${proc_description} MATCHES "Apple M1" AND ${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "arm64")
# For now the only arm64 we have tested is Apple M1. This
# might need to be revisited for M1 Max/Ultra and M2+.
# Testing has not yet found any aggressive flags better than
# the release so for now, use those
set (GEOS_Fortran_Aggressive_Flags "${GEOS_Fortran_Release_Flags}")
set (GEOS_Fortran_Aggressive_FPE_Flags "${GEOS_Fortran_Release_FPE_Flags}")
else ()
set (GEOS_Fortran_Aggressive_Flags "${FOPT2} -march=${GNU_NATIVE_ARCH} -ffast-math -ftree-vectorize -funroll-loops --param max-unroll-times=4 ${PREFER_AVX128} ${NO_FMA}")
set (GEOS_Fortran_Aggressive_FPE_Flags "${DEBINFO} ${TRACEBACK} ${MISMATCH} ${ALLOW_BOZ}")
endif ()
# Options per Jerry DeLisle on GCC Fortran List with SVML (does not seem to help)
#set (GEOS_Fortran_Aggressive_Flags "-O2 -march=native -ffast-math -ftree-vectorize -funroll-loops --param max-unroll-times=4 ${PREFER_AVX128} -mno-fma -mveclibabi=svml")
#set (GEOS_Fortran_Aggressive_FPE_Flags "${DEBINFO} ${TRACEBACK} ${MISMATCH} ${ALLOW_BOZ}")
# Common variables for every compiler
include(Generic_Fortran)