From 104ffddb4c3596edf841a69f80f386d15aa8628e Mon Sep 17 00:00:00 2001 From: Davis King Date: Mon, 21 May 2018 21:21:40 -0400 Subject: [PATCH] Make python extension automatically enable AVX instructions if the host machine supports them. --- ..._avx_instructions_executable_on_host.cmake | 19 +++++++++++++++ dlib/cmake_utils/test_for_avx/CMakeLists.txt | 23 +++++++++++++++++++ dlib/cmake_utils/test_for_avx/avx_test.cpp | 13 +++++++++++ .../test_for_avx/this_file_doesnt_compile.cpp | 3 +++ tools/python/CMakeLists.txt | 7 +++++- 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake create mode 100644 dlib/cmake_utils/test_for_avx/CMakeLists.txt create mode 100644 dlib/cmake_utils/test_for_avx/avx_test.cpp create mode 100644 dlib/cmake_utils/test_for_avx/this_file_doesnt_compile.cpp diff --git a/dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake b/dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake new file mode 100644 index 000000000..a5d7320f4 --- /dev/null +++ b/dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake @@ -0,0 +1,19 @@ +# This script checks if your compiler and host processor can generate and then run programs with AVX instructions. + +cmake_minimum_required(VERSION 2.8.12) + +# Don't rerun this script if its already been executed. +if (DEFINED AVX_IS_AVAILABLE_ON_HOST) + return() +endif() + +# Set to false unless we find out otherwise in the code below. +set(AVX_IS_AVAILABLE_ON_HOST 0) + +try_compile(test_for_avx_worked ${PROJECT_BINARY_DIR}/avx_test_build ${CMAKE_CURRENT_LIST_DIR}/test_for_avx + avx_test) + +if(test_for_avx_worked) + message (STATUS "AVX instructions can be executed by the host processor.") + set(AVX_IS_AVAILABLE_ON_HOST 1) +endif() diff --git a/dlib/cmake_utils/test_for_avx/CMakeLists.txt b/dlib/cmake_utils/test_for_avx/CMakeLists.txt new file mode 100644 index 000000000..614b9c052 --- /dev/null +++ b/dlib/cmake_utils/test_for_avx/CMakeLists.txt @@ -0,0 +1,23 @@ + +cmake_minimum_required(VERSION 2.8.12) +project(avx_test) + +set(USE_AVX_INSTRUCTIONS ON CACHE BOOL "Use SSE4 instructions") + +# Pull this in since it sets the AVX compile options by putting that kind of stuff into the active_compile_opts list. +include(../set_compiler_specific_options.cmake) + + +try_run(run_result compile_result ${PROJECT_BINARY_DIR}/avx_test_try_run_build ${CMAKE_CURRENT_LIST_DIR}/avx_test.cpp + COMPILE_DEFINITIONS ${active_compile_opts}) + +message(STATUS "run_result = ${run_result}") +message(STATUS "compile_result = ${compile_result}") + +if (${run_result} EQUAL 0 AND compile_result) + message(STATUS "Ran AVX test program successfully, you have AVX available.") +else() + message(STATUS "Unable to run AVX test program, you don't seem to have AVX instructions available.") + # make this build fail so that calling try_compile statements will error in this case. + add_library(make_this_build_fail ${CMAKE_CURRENT_LIST_DIR}/this_file_doesnt_compile.cpp) +endif() diff --git a/dlib/cmake_utils/test_for_avx/avx_test.cpp b/dlib/cmake_utils/test_for_avx/avx_test.cpp new file mode 100644 index 000000000..98e9b0b99 --- /dev/null +++ b/dlib/cmake_utils/test_for_avx/avx_test.cpp @@ -0,0 +1,13 @@ + +#include + +int main() +{ + __m256 x; + x = _mm256_set1_ps(1.23); + x = _mm256_add_ps(x,x); + return 0; +} + +// ------------------------------------------------------------------------------------ + diff --git a/dlib/cmake_utils/test_for_avx/this_file_doesnt_compile.cpp b/dlib/cmake_utils/test_for_avx/this_file_doesnt_compile.cpp new file mode 100644 index 000000000..83f89c0c7 --- /dev/null +++ b/dlib/cmake_utils/test_for_avx/this_file_doesnt_compile.cpp @@ -0,0 +1,3 @@ + +#error "This file doesn't compile!" + diff --git a/tools/python/CMakeLists.txt b/tools/python/CMakeLists.txt index f3915b41c..bf2418aa4 100644 --- a/tools/python/CMakeLists.txt +++ b/tools/python/CMakeLists.txt @@ -3,10 +3,15 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12) set(USE_SSE4_INSTRUCTIONS ON CACHE BOOL "Use SSE4 instructions") +include(../../dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake) +if (AVX_IS_AVAILABLE_ON_HOST) + set(USE_AVX_INSTRUCTIONS ON CACHE BOOL "Use AVX instructions") +endif() + # Set this to disable link time optimization. The only reason for # doing this to make the compile faster which is nice when developing # new modules. - set(PYBIND11_LTO_CXX_FLAGS "") +# set(PYBIND11_LTO_CXX_FLAGS "") # Avoid cmake warnings about changes in behavior of some Mac OS X path