Skip to content

Commit

Permalink
Allow architecture-specific optimizations to be skipped
Browse files Browse the repository at this point in the history
march=native builds optimized but non-portable binaries. When building
binaries for distribution or sharing between machines, a more careful
approach to platform optimization must be taken.

A better approach might be to provide optimization profiles that target
broadly available instruction extensions, e.g., AVX2. But there seems to
be a number of tuning parameters (e.g., cache sizes) and
compiler-specific flags involved, so I am deferring this for now.

Along the way, refactor find_packages(ArchFlags) to
include(CheckArchFlags) since determining compiler flags does not have
much similarity to finding packages. find_package_handle_standard_args
in FindArchFlags.cmake also was raising warnings due its non-standard
form:

  The package name passed to `find_package_handle_standard_args` (ARCH_FLAGS)
  does not match the name of the calling package (ArchFlags).  This can lead
  to problems in calling code that expects `find_package` result variables
  (e.g., `_FOUND`) to follow a certain pattern.
  • Loading branch information
ddn0 committed Apr 7, 2020
1 parent a7dd2b3 commit 68df52e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 49 deletions.
15 changes: 9 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ set(SKIP_COMPILE_APPS OFF CACHE BOOL "Skip compilation of applications using Gal
set(GRAPH_LOCATION "" CACHE PATH "Location of inputs for tests if downloaded/stored separately.")
set(CXX_CLANG_TIDY "" CACHE STRING "Semi-colon list specifying clang-tidy command and arguments")
set(CMAKE_CXX_COMPILER_LAUNCHER "" CACHE STRING "Semi-colon list specifying command to wrap compiler invocations (e.g., ccache)")
set(USE_ARCH native CACHE STRING "Optimize for a specific processor architecture ('none' to disable)")

if(WIN32 AND NOT CYGWIN)
set(DEFAULT_INSTALL_CMAKE_DIR "${CMAKE_INSTALL_PREFIX}/CMake")
Expand Down Expand Up @@ -82,6 +83,10 @@ set(USE_EMBEDDED_REVISION ON CACHE BOOL "Embed revision numbers")
set(USE_ARCH native CACHE STRING "Use specific architecture for cross compilation (Default : native)")
set(NUM_TEST_THREADS "" CACHE STRING "Number of threads to use for running the tests.")

if(ENABLE_HETERO_GALOIS)
set(USE_ARCH none)
endif()

###### Configure (users don't need to go beyond here) ######

enable_testing()
Expand Down Expand Up @@ -136,12 +141,10 @@ if(CYGWIN)
endif()

# Enable architecture-specific optimizations
if(NOT ENABLE_HETERO_GALOIS)
find_package(ArchFlags)
if(ARCH_FLAGS_FOUND)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${ARCH_CXX_FLAGS}> $<$<COMPILE_LANGUAGE:C>:${ARCH_C_FLAGS}>)
add_link_options(${ARCH_EXE_FLAGS})
endif()
include(CheckArchFlags)
if(ARCH_FLAGS_FOUND)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${ARCH_CXX_FLAGS}> $<$<COMPILE_LANGUAGE:C>:${ARCH_C_FLAGS}>)
add_link_options(${ARCH_LINK_FLAGS})
endif()

# GNU profiling
Expand Down
36 changes: 36 additions & 0 deletions cmake/Modules/CheckArchFlags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Find architecture-specific flags
#
# Once done this will define
# ARCH_FLAGS_FOUND
# ARCH_CXX_FLAGS - Compiler flags to enable architecture-specific optimizations
# ARCH_C_FLAGS - Compiler flags to enable architecture-specific optimizations
# ARCH_LINK_FLAGS - Compiler flags to enable architecture-specific optimizations
include(CheckCXXCompilerFlag)

if(NOT USE_ARCH OR USE_ARCH STREQUAL "none" OR ARCH_FLAGS_FOUND)
set(ARCH_CXX_FLAGS_CANDIDATES)
else()
set(ARCH_CXX_FLAGS_CANDIDATES "-march=${USE_ARCH}")
endif()

if(USE_ARCH STREQUAL "mic")
if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
list(APPEND ARCH_CXX_FLAGS_CANDIDATES -mmic)
endif()

if(CMAKE_COMPILER_IS_GNUCC)
list(APPEND ARCH_CXX_FLAGS_CANDIDATES -march=knc)
endif()
endif()

foreach(FLAG ${ARCH_CXX_FLAGS_CANDIDATES})
message(STATUS "Try architecture flag = [${FLAG}]")
unset(ARCH_CXX_FLAGS_DETECTED)
check_cxx_compiler_flag("${FLAG}" ARCH_CXX_FLAGS_DETECTED)
if(ARCH_CXX_FLAGS_DETECTED)
set(ARCH_FLAGS_FOUND "YES")
set(ARCH_CXX_FLAGS "${FLAG}")
set(ARCH_C_FLAGS "${FLAG}")
set(ARCH_LINK_FLAGS "${FLAG}")
endif()
endforeach()
43 changes: 0 additions & 43 deletions cmake/Modules/FindArchFlags.cmake

This file was deleted.

0 comments on commit 68df52e

Please sign in to comment.