Merge branch release-4-6 into master
[alexxy/gromacs.git] / cmake / gmxDetectAcceleration.cmake
1 # - Check the username performing the build, as well as date and time
2 #
3 # gmx_detect_acceleration(GMX_SUGGESTED_CPU_ACCELERATION)
4 #
5 # Try to detect CPU information and suggest an acceleration option
6 # (such as SSE/AVX) that fits the current CPU. These functions assume
7 # that gmx_detect_target_architecture() has already been run, so that
8 # things like GMX_IS_X86 are already available.
9 #
10 # Sets ${GMX_SUGGESTED_CPU_ACCELERATION} in the parent scope if
11 # GMX_CPU_ACCELERATION is not set (e.g. by the user, or a previous run
12 # of CMake).
13 #
14
15 # we rely on inline asm support for GNU!
16 include(gmxTestInlineASM)
17
18 function(gmx_suggest_x86_acceleration _suggested_acceleration)
19
20     gmx_test_inline_asm_gcc_x86(GMX_X86_GCC_INLINE_ASM)
21
22     if(GMX_X86_GCC_INLINE_ASM)
23         set(GCC_INLINE_ASM_DEFINE "-DGMX_X86_GCC_INLINE_ASM")
24     else(GMX_X86_GCC_INLINE_ASM)
25         set(GCC_INLINE_ASM_DEFINE "")
26     endif(GMX_X86_GCC_INLINE_ASM)
27
28     message(STATUS "Detecting best acceleration for this CPU")
29
30     # Get CPU acceleration information
31     try_run(GMX_CPUID_RUN_ACC GMX_CPUID_COMPILED
32             ${CMAKE_BINARY_DIR}
33             ${CMAKE_SOURCE_DIR}/src/gromacs/gmxlib/gmx_cpuid.c
34             COMPILE_DEFINITIONS "@GCC_INLINE_ASM_DEFINE@ -I${CMAKE_SOURCE_DIR}/src/gromacs/legacyheaders -DGMX_CPUID_STANDALONE -DGMX_IS_X86"
35             RUN_OUTPUT_VARIABLE OUTPUT_TMP
36             COMPILE_OUTPUT_VARIABLE GMX_CPUID_COMPILE_OUTPUT
37             ARGS "-acceleration")
38
39     if(NOT GMX_CPUID_COMPILED)
40         message(WARNING "Cannot compile CPUID code, which means no CPU-specific acceleration.")
41         message(STATUS "Compile output: ${GMX_CPUID_COMPILE_OUTPUT}")
42         set(OUTPUT_TMP "None")
43     elseif(NOT GMX_CPUID_RUN_ACC EQUAL 0)
44         message(WARNING "Cannot run CPUID code, which means no CPU-specific optimization.")
45         message(STATUS "Run output: ${OUTPUT_TMP}")
46         set(OUTPUT_TMP "None")
47     endif(NOT GMX_CPUID_COMPILED)
48
49     string(STRIP "@OUTPUT_TMP@" OUTPUT_ACC)
50
51     set(${_suggested_acceleration} "@OUTPUT_ACC@" PARENT_SCOPE)
52     message(STATUS "Detected best acceleration for this CPU - @OUTPUT_ACC@")
53 endfunction()
54
55 function(gmx_detect_acceleration _suggested_acceleration)
56     if(NOT DEFINED GMX_CPU_ACCELERATION)
57         if(GMX_IS_BGQ)
58             set(${_suggested_acceleration} "IBM_QPX")
59         elseif(GMX_IS_X86)
60             gmx_suggest_x86_acceleration(${_suggested_acceleration})
61         else()
62             set(${_suggested_acceleration} "None")
63         endif()
64
65         set(${_suggested_acceleration} ${${_suggested_acceleration}} PARENT_SCOPE)
66     endif()
67 endfunction()