Unverified Commit 4a5125f4 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

make behavior of CMAKE_TUNE_FLAGS more consistent and allow to turn it off

parent c1268bd1
Loading
Loading
Loading
Loading
+15 −13
Original line number Diff line number Diff line
@@ -51,33 +51,34 @@ check_for_autogen_files(${LAMMPS_SOURCE_DIR})
include(CheckCCompilerFlag)
include(CheckIncludeFileCXX)

# set required compiler flags and compiler/CPU arch specific optimizations
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -restrict")
  if (NOT CMAKE_TUNE_FLAGS)
  if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 17.3 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 17.4)
      set(CMAKE_TUNE_FLAGS "-xCOMMON-AVX512")
    set(CMAKE_TUNE_DEFAULT "-xCOMMON-AVX512")
  else()
      set(CMAKE_TUNE_FLAGS "-xHost")
    endif()
    set(CMAKE_TUNE_DEFAULT "-xHost")
  endif()
endif()

if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  if(NOT CMAKE_TUNE_FLAGS)
    set (CMAKE_TUNE_FLAGS "-ffast-math -march=native")
  set(CMAKE_TUNE_DEFAULT "-ffast-math -march=native")
endif()

if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
  set(CMAKE_TUNE_DEFAULT "-ffast-math -march=native")
endif()

# we require C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# GNU compiler features
# GNU compiler specific features for testing
if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  option(ENABLE_COVERAGE "Enable code coverage" OFF)
  option(ENABLE_COVERAGE "Enable collecting code coverage data" OFF)
  mark_as_advanced(ENABLE_COVERAGE)
  if(ENABLE_COVERAGE)
    set (CMAKE_TUNE_FLAGS "--coverage")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
  endif()
endif()

@@ -339,6 +340,7 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
  list(APPEND LAMMPS_LINK_LIBS -lwsock32 -lpsapi)
endif()

set(CMAKE_TUNE_FLAGS "${CMAKE_TUNE_DEFAULT}" CACHE STRING "Compiler specific optimization or instrumentation")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_TUNE_FLAGS}")
if(CMAKE_Fortran_FLAGS)
  set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${CMAKE_TUNE_FLAGS}")
+2 −1
Original line number Diff line number Diff line
@@ -218,7 +218,8 @@ flags to tune for optimal performance on given hosts. By default these are
initialized to some compiler specific flags, where known, to optimize the
LAMMPS executable with optimizations and instructions available on the host
where LAMMPS is compiled. For example, for Intel compilers this would be
``-xHost`` and for GNU compilers this would be ``-march=native``.
``-xHost`` and for GNU compilers this would be ``-march=native``. To turn
these flags off, set ``-D CMAKE_TUNE_FLAGS=``.

.. note::

+24 −11
Original line number Diff line number Diff line
@@ -25,6 +25,12 @@ Another way of doing this without reconfiguration is calling make with variable

   make VERBOSE=1

Or when using the :ref:`"cmbuild" wrapper script <cmake>`:

.. code-block:: bash

   cmbuild -v

----------

.. _sanitizer:
@@ -32,20 +38,27 @@ Another way of doing this without reconfiguration is calling make with variable
Address, Undefined Behavior, and Thread Sanitizer Support
-------------------------------------------------------------------------

Compilers such as GCC and Clang support generating binaries which use different
sanitizers to detect problems in code during run-time. They can detect `memory leaks <https://clang.llvm.org/docs/AddressSanitizer.html>`_,
code that runs into `undefined behavior <https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html>`_ of the
language and `data races <https://clang.llvm.org/docs/ThreadSanitizer.html>`_ in threaded code.
Compilers such as GCC and Clang support generating instrumented binaries
which use different sanitizer libraries to detect problems in code
during run-time. They can detect issues like:

 - `memory leaks <https://clang.llvm.org/docs/AddressSanitizer.html>`_
 - `undefined behavior <https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html>`_
 - `data races <https://clang.llvm.org/docs/ThreadSanitizer.html>`_

The following settings allow you enable these features if your compiler supports
it. Please note that they come with a performance hit. However, they are
usually faster than using tools like Valgrind.
Please note that this kind of instrumentation usually comes with a small
performance hit (much less than using tools like `Valgrind <valgrind_>`_).
The to enable these features additional compiler flags need to be added
to the compilation and linking stages.  This is most easily done through
setting the ``CMAKE_TUNE_FLAGS`` variable during configuration. Examples:

.. code-block:: bash

   -D ENABLE_SANITIZE_ADDRESS=value    # enable Address Sanitizer, value = no (default) or yes
   -D ENABLE_SANITIZE_UNDEFINED=value  # enable Undefined Behaviour Sanitizer, value = no (default) or yes
   -D ENABLE_SANITIZE_THREAD=value     # enable Thread Sanitizer, value = no (default) or yes
   -D CMAKE_TUNE_FLAGS=-fsanitize=address    # enable address sanitizer / memory leak checker
   -D CMAKE_TUNE_FLAGS=-fsanitize=undefined  # enable undefined behaviour sanitizer
   -D CMAKE_TUNE_FLAGS=-fsanitize=thread     # enable thread sanitizer

.. _valgrind: https://valgrind.org

----------