simd.cmake 2.9 KB
Newer Older
1 2 3
# This file is use to check all support level of AVX on your machine
# so that PaddlePaddle can unleash the vectorization power of muticore.

4 5
include(CheckCXXSourceRuns)
include(CheckCXXSourceCompiles)
6

7
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
G
gangliao 已提交
8 9 10
    set(MMX_FLAG "-mmmx")
    set(SSE2_FLAG "-msse2")
    set(SSE3_FLAG "-msse3")
11 12
    set(AVX_FLAG "-mavx")
    set(AVX2_FLAG "-mavx2")
13
    set(AVX512F_FLAG "-mavx512f")
14
elseif(MSVC)
G
gangliao 已提交
15 16 17 18 19
    set(MMX_FLAG "/arch:MMX")
    set(SSE2_FLAG "/arch:SSE2")
    set(SSE3_FLAG "/arch:SSE3")
    SET(AVX_FLAG "/arch:AVX")
    SET(AVX2_FLAG "/arch:AVX2")
20
endif()
21

22 23
set(CMAKE_REQUIRED_FLAGS_RETAINED ${CMAKE_REQUIRED_FLAGS})

G
gangliao 已提交
24 25
# Check  MMX
set(CMAKE_REQUIRED_FLAGS ${MMX_FLAG})
26
set(MMX_FOUND_EXITCODE 1 CACHE STRING "Result from TRY_RUN" FORCE)
27
CHECK_CXX_SOURCE_RUNS("
G
gangliao 已提交
28
#include <mmintrin.h>
29 30
int main()
{
G
gangliao 已提交
31
    _mm_setzero_si64();
32
    return 0;
G
gangliao 已提交
33
}" MMX_FOUND)
34

G
gangliao 已提交
35 36
# Check SSE2
set(CMAKE_REQUIRED_FLAGS ${SSE2_FLAG})
37
set(SSE2_FOUND_EXITCODE 1 CACHE STRING "Result from TRY_RUN" FORCE)
G
gangliao 已提交
38 39 40 41 42 43 44
CHECK_CXX_SOURCE_RUNS("
#include <emmintrin.h>
int main()
{
    _mm_setzero_si128();
    return 0;
}" SSE2_FOUND)
45

G
gangliao 已提交
46 47
# Check SSE3
set(CMAKE_REQUIRED_FLAGS ${SSE3_FLAG})
48
set(SSE3_FOUND_EXITCODE 1 CACHE STRING "Result from TRY_RUN" FORCE)
G
gangliao 已提交
49 50 51 52 53 54 55 56 57 58 59
CHECK_CXX_SOURCE_RUNS("
#include <pmmintrin.h>
int main()
{
    __m128d a = _mm_set1_pd(6.28);
    __m128d b = _mm_set1_pd(3.14);
    __m128d result = _mm_addsub_pd(a, b);
    result = _mm_movedup_pd(result);
    return 0;
}" SSE3_FOUND)

W
wopeizl 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
# disable AVX by default on windows
if(NOT WIN32)
    # Check AVX
    set(CMAKE_REQUIRED_FLAGS ${AVX_FLAG})
    set(AVX_FOUND_EXITCODE 1 CACHE STRING "Result from TRY_RUN" FORCE)
    CHECK_CXX_SOURCE_RUNS("
    #include <immintrin.h>
    int main()
    {
        __m256 a = _mm256_set_ps (-1.0f, 2.0f, -3.0f, 4.0f, -1.0f, 2.0f, -3.0f, 4.0f);
        __m256 b = _mm256_set_ps (1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f);
        __m256 result = _mm256_add_ps (a, b);
        return 0;
    }" AVX_FOUND)
74

W
wopeizl 已提交
75 76 77 78 79 80 81 82 83 84 85
    # Check AVX 2
    set(CMAKE_REQUIRED_FLAGS ${AVX2_FLAG})
    set(AVX2_FOUND_EXITCODE 1 CACHE STRING "Result from TRY_RUN" FORCE)
    CHECK_CXX_SOURCE_RUNS("
    #include <immintrin.h>
    int main()
    {
        __m256i a = _mm256_set_epi32 (-1, 2, -3, 4, -1, 2, -3, 4);
        __m256i result = _mm256_abs_epi32 (a);
        return 0;
    }" AVX2_FOUND)
86

W
wopeizl 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
    # Check AVX512F
    set(CMAKE_REQUIRED_FLAGS ${AVX512F_FLAG})
    set(AVX512F_FOUND_EXITCODE 1 CACHE STRING "Result from TRY_RUN" FORCE)
    CHECK_CXX_SOURCE_RUNS("
    #include <immintrin.h>
    int main()
    {
        __m512i a = _mm512_set_epi32 (-1, 2, -3, 4, -1, 2, -3, 4,
                                      13, -5, 6, -7, 9, 2, -6, 3);
        __m512i result = _mm512_abs_epi32 (a);
        return 0;
    }" AVX512F_FOUND)
endif(NOT WIN32)
100

101
set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_RETAINED})
102
mark_as_advanced(MMX_FOUND SSE2_FOUND SSE3_FOUND AVX_FOUND AVX2_FOUND AVX512F_FOUND)