Skip to content

Commit

Permalink
Unbreak packaging on BSD and non-x86_64 (colmap#348)
Browse files Browse the repository at this point in the history
* VLfeat: use threads on more Unices (e.g., Solaris, BSDs)

* PBA: GCC/Clang on aarch64 only defines __ARM_NEON

* build: treat DragonFly as another BSD system

* build: don't force SSE2/AVX, enable at runtime instead

Make binary redistributable by default between machines of the same
architecture by not enabling SIMD outside of routines gated by CPUID.
No need to reinvent -march=native just for auto-vectorization.

* VLFeat: actually use AVX when it should

(cherry picked from commit vlfeat/vlfeat@df2a27cd5fb5)

* VLFeat: enable SSE2 on Windows
  • Loading branch information
jbeich authored and ahojnnes committed Apr 16, 2018
1 parent 827bbb8 commit 91ac87a
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 197 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ include(CheckCXXCompilerFlag)
# the CMake policies in this file
include(${CMAKE_SOURCE_DIR}/cmake/CMakeHelper.cmake NO_POLICY_SCOPE)

include(${CMAKE_SOURCE_DIR}/cmake/CheckSSEExtensions.cmake)

if(EXISTS ${CMAKE_SOURCE_DIR}/LocalConfig.cmake)
include(${CMAKE_SOURCE_DIR}/LocalConfig.cmake)
endif()
Expand All @@ -29,6 +27,7 @@ endif()
# Options
################################################################################

option(SIMD_ENABLED "Whether to enable SIMD optimizations" ON)
option(OPENMP_ENABLED "Whether to enable OpenMP" ON)
option(IPO_ENABLED "Whether to enable interprocedural optimization" ON)
option(CUDA_ENABLED "Whether to enable CUDA, if available" ON)
Expand Down
7 changes: 6 additions & 1 deletion cmake/CMakeHelper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(IS_CLANG TRUE)
endif()

# Determine project architecture.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "[ix].?86|amd64|AMD64")
set(IS_X86 TRUE)
endif()

# Determine project operating system.
string(REGEX MATCH "Linux" IS_LINUX ${CMAKE_SYSTEM_NAME})
string(REGEX MATCH "BSD" IS_BSD ${CMAKE_SYSTEM_NAME})
string(REGEX MATCH "DragonFly|BSD" IS_BSD ${CMAKE_SYSTEM_NAME})
string(REGEX MATCH "SunOS" IS_SOLARIS ${CMAKE_SYSTEM_NAME})
if(WIN32)
SET(IS_WINDOWS TRUE BOOL INTERNAL)
Expand Down
166 changes: 0 additions & 166 deletions cmake/CheckSSEExtensions.cmake

This file was deleted.

8 changes: 7 additions & 1 deletion src/ext/PBA/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
if(NOT IS_MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -O3 -pthread -march=core2 -mfpmath=sse -Wno-c++11-narrowing")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wno-narrowing")
endif()

if(NOT SIMD_ENABLED)
add_definitions(-DDISABLE_CPU_NEON)
add_definitions(-DDISABLE_CPU_AVX)
add_definitions(-DDISABLE_CPU_SSE)
endif()

if(CUDA_ENABLED)
Expand Down
7 changes: 4 additions & 3 deletions src/ext/PBA/SparseBundleCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ using std::max;
#if defined(_M_ARM) && _M_ARM >= 7 && !defined(DISABLE_CPU_NEON)
#include <arm_neon.h>
#define CPUPBA_USE_NEON
#elif defined(__ARM_NEON__) && !defined(DISABLE_CPU_NEON)
#elif defined(__ARM_NEON) && !defined(DISABLE_CPU_NEON)
#include <arm_neon.h>
#define CPUPBA_USE_NEON
#endif
#elif defined(CPUPBA_USE_AVX) // Using AVX
#elif defined(__AVX__) && !defined(DISABLE_CPU_AVX)
#include <immintrin.h>
#define CPUPBA_USE_AVX
#undef CPUPBA_USE_SSE
#undef POINT_DATA_ALIGN4
#elif !defined(DISABLE_CPU_SSE) // Using SSE
#elif (defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86) && _M_IX86_FP >= 2)) && !defined(DISABLE_CPU_SSE)
#define CPUPBA_USE_SSE
#include <xmmintrin.h>
#include <emmintrin.h>
Expand Down
40 changes: 24 additions & 16 deletions src/ext/VLFeat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,35 @@ set(VLFEAT_SOURCE_FILES
vlad.c
vlad.h)

if(IS_GNU OR IS_CLANG)
if(HAS_AVX_EXTENSION)
list(APPEND VLFEAT_SOURCE_FILES
mathop_avx.c
mathop_avx.h)
else()
if(SIMD_ENABLED AND IS_X86)
if (MSVC)
# https://github.com/vlfeat/vlfeat/commit/4f0098fd47e9
add_definitions(-DVL_DISABLE_AVX)
else()
set(AVX_SOURCES
mathop_avx.c
mathop_avx.h)
endif()

if(HAS_SSE2_EXTENSION)
list(APPEND VLFEAT_SOURCE_FILES
mathop_sse2.c
mathop_sse2.h
imopv_sse2.c
imopv_sse2.h)
set(SSE2_SOURCES
imopv_sse2.c
imopv_sse2.h
mathop_sse2.c
mathop_sse2.h)

list(APPEND VLFEAT_SOURCE_FILES ${AVX_SOURCES} ${SSE2_SOURCES})

if (MSVC)
set_source_files_properties(${AVX_SOURCES}
PROPERTIES COMPILE_FLAGS "/arch:AVX")
set_source_files_properties(${SSE2_SOURCES}
PROPERTIES COMPILE_FLAGS "/arch:SSE2 /D__SSE2__")
else()
add_definitions(-DVL_DISABLE_SSE2)
set_source_files_properties(${AVX_SOURCES}
PROPERTIES COMPILE_FLAGS "-mavx")
set_source_files_properties(${SSE2_SOURCES}
PROPERTIES COMPILE_FLAGS "-msse2")
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SSE_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SSE_FLAGS}")
else()
add_definitions(-DVL_DISABLE_AVX)
add_definitions(-DVL_DISABLE_SSE2)
Expand Down
8 changes: 3 additions & 5 deletions src/ext/VLFeat/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -777,16 +777,14 @@ calling @c omp_set_num_threads() in the application. Note that:

#if defined(VL_OS_WIN)
#include <Windows.h>
#else
#include <unistd.h>
#endif

#if ! defined(VL_DISABLE_THREADS) && defined(VL_THREADS_POSIX)
#include <pthread.h>
#endif

#if defined(VL_OS_MACOSX) || defined(VL_OS_LINUX)
#include <unistd.h>
#endif

#if defined(_OPENMP)
#include <omp.h>
#endif
Expand Down Expand Up @@ -1564,7 +1562,7 @@ vl_constructor (void)
GetSystemInfo (&info) ;
state->numCPUs = info.dwNumberOfProcessors ;
}
#elif defined(VL_OS_MACOSX) || defined(VL_OS_LINUX)
#elif defined(_SC_NPROCESSORS_ONLN)
state->numCPUs = sysconf(_SC_NPROCESSORS_ONLN) ;
#else
state->numCPUs = 1 ;
Expand Down
4 changes: 2 additions & 2 deletions src/ext/VLFeat/gmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,13 +1102,13 @@ VL_XCAT(_vl_gmm_maximization_, SFX)

#ifndef VL_DISABLE_AVX
if (vl_get_simd_enabled() && vl_cpu_has_avx()) {
VL_XCAT(_vl_weighted_mean_sse2_, SFX)
VL_XCAT(_vl_weighted_mean_avx_, SFX)
(self->dimension,
means_+ i_cl * self->dimension,
data + i_d * self->dimension,
p) ;

VL_XCAT(_vl_weighted_sigma_sse2_, SFX)
VL_XCAT(_vl_weighted_sigma_avx_, SFX)
(self->dimension,
covariances_ + i_cl * self->dimension,
data + i_d * self->dimension,
Expand Down
2 changes: 1 addition & 1 deletion src/ext/VLFeat/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ the terms of the BSD license (see the COPYING file).

/** @name Identifying the host threading library
** @{ */
#if defined(VL_OS_MACOSX) || defined(VL_OS_LINUX) || \
#if !defined(VL_OS_WIN) && !defined(VL_OS_WIN64) || \
defined(__DOXYGEN__)
#define VL_THREADS_POSIX 1
#endif
Expand Down

0 comments on commit 91ac87a

Please sign in to comment.