Skip to content

Commit 9023479

Browse files
gangliaoemailweixu
authored andcommitted
Add automatic check AVX in CMake (#145)
* Add automatic check AVX in CMake * Revise table format and some words in build docs
1 parent 2920b6b commit 9023479

File tree

3 files changed

+86
-23
lines changed

3 files changed

+86
-23
lines changed

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ find_package(PythonInterp 2.7 REQUIRED)
1717
find_package(ZLIB REQUIRED)
1818
find_package(NumPy REQUIRED)
1919
find_package(Threads REQUIRED)
20+
find_package(AVX QUIET)
2021
find_package(Glog)
2122
find_package(Gflags QUIET)
2223
find_package(GTest)
@@ -28,7 +29,7 @@ find_program(M4_EXECUTABLE m4)
2829
option(WITH_DSO "Compile PaddlePaddle with dynamic linked libraries" ON)
2930
option(WITH_GPU "Compile PaddlePaddle with gpu" ${CUDA_FOUND})
3031
option(WITH_DOUBLE "Compile PaddlePaddle with double precision, otherwise use single precision" OFF)
31-
option(WITH_AVX "Compile PaddlePaddle with avx intrinsics" ON) # TODO(yuyang18): Check AVX is supported or not as default value
32+
option(WITH_AVX "Compile PaddlePaddle with avx intrinsics" ${AVX_FOUND})
3233
option(WITH_PYTHON "Compile PaddlePaddle with python interpreter" ON)
3334
option(WITH_STYLE_CHECK "Style Check for PaddlePaddle" ${PYTHONINTERP_FOUND})
3435
option(WITH_RDMA "Compile PaddlePaddle with rdma support" OFF)
@@ -101,8 +102,8 @@ if(NOT WITH_TIMER)
101102
endif(NOT WITH_TIMER)
102103

103104
if(WITH_AVX)
104-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx")
105-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx")
105+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${AVX_FLAGS}")
106+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${AVX_FLAGS}")
106107
else(WITH_AVX)
107108
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse3")
108109
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse3")

cmake/FindAVX.cmake

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This file is use to check all support level of AVX on your machine
2+
# so that PaddlePaddle can unleash the vectorization power of muticore.
3+
4+
INCLUDE(CheckCXXSourceRuns)
5+
6+
SET(FIND_AVX_10)
7+
SET(FIND_AVX_20)
8+
SET(AVX_FLAGS)
9+
SET(AVX_FOUND)
10+
11+
# Check AVX 2
12+
SET(CMAKE_REQUIRED_FLAGS)
13+
IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
14+
SET(CMAKE_REQUIRED_FLAGS "-mavx2")
15+
ELSEIF(MSVC AND NOT CMAKE_CL_64) # reserve for WINDOWS
16+
SET(CMAKE_REQUIRED_FLAGS "/arch:AVX2")
17+
ENDIF()
18+
19+
CHECK_CXX_SOURCE_RUNS("
20+
#include <immintrin.h>
21+
int main()
22+
{
23+
__m256i a = _mm256_set_epi32 (-1, 2, -3, 4, -1, 2, -3, 4);
24+
__m256i result = _mm256_abs_epi32 (a);
25+
return 0;
26+
}" FIND_AVX_20)
27+
28+
# Check AVX
29+
SET(CMAKE_REQUIRED_FLAGS)
30+
IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
31+
SET(CMAKE_REQUIRED_FLAGS "-mavx")
32+
ELSEIF(MSVC AND NOT CMAKE_CL_64)
33+
SET(CMAKE_REQUIRED_FLAGS "/arch:AVX")
34+
endif()
35+
36+
CHECK_CXX_SOURCE_RUNS("
37+
#include <immintrin.h>
38+
int main()
39+
{
40+
__m256 a = _mm256_set_ps (-1.0f, 2.0f, -3.0f, 4.0f, -1.0f, 2.0f, -3.0f, 4.0f);
41+
__m256 b = _mm256_set_ps (1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f);
42+
__m256 result = _mm256_add_ps (a, b);
43+
return 0;
44+
}" FIND_AVX_10)
45+
46+
IF(${FIND_AVX_20})
47+
IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
48+
SET(AVX_FLAGS "${AVX_FLAGS} -mavx2")
49+
ELSEIF(MSVC)
50+
SET(AVX_FLAGS "${AVX_FLAGS} /arch:AVX2")
51+
ENDIF()
52+
ENDIF()
53+
54+
IF(${FIND_AVX_10})
55+
IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
56+
SET(AVX_FLAGS "${AVX_FLAGS} -mavx")
57+
ELSEIF(MSVC)
58+
SET(AVX_FLAGS "${AVX_FLAGS} /arch:AVX")
59+
ENDIF()
60+
ENDIF()
61+
62+
IF(${FIND_AVX_10} OR ${FIND_AVX_20})
63+
SET(AVX_FOUND TRUE)
64+
MESSAGE(STATUS "Find CPU supports ${AVX_FLAGS}.")
65+
ENDIF()

doc/build/build_from_source.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ You can download PaddlePaddle from the [github source](https://github.com/gangli
1111

1212
```bash
1313
git clone https://github.com/baidu/Paddle paddle
14+
cd paddle
1415
```
1516

1617
## <span id="requirements">Requirements</span>
1718

18-
To compile the source code, your computer must be equipped with GCC >=4.6 or Clang Compiler.
19+
To compile the source code, your computer must be equipped with GCC >=4.6 or Clang compiler.
1920
### Dependencies
2021

2122
- **CMake**: version >= 2.8
@@ -27,17 +28,17 @@ To compile the source code, your computer must be equipped with GCC >=4.6 or Cla
2728

2829
PaddlePaddle supports some build options. To enable it, first you need to install the related libraries.
2930

30-
Optional | Description
31-
------------ | :-----------
32-
**WITH_GPU** | Compile with GPU mode.
33-
**WITH_DOUBLE** | Compile with double precision floating-point, default: single precision. |
34-
**WITH_GLOG** | Compile with glog. If not found, default: an internal log implementation.
35-
**WITH_GFLAGS** | Compile with gflags. If not found, default: an internal flag implementation.
36-
**WITH_TESTING** | Compile with gtest for PaddlePaddle's unit testing.
37-
**WITH_DOC** | Compile to generate PaddlePaddle's docs, default: disabled (OFF).
38-
**WITH_SWIG_PY** | Compile with python predict API, default: disabled (OFF).
39-
**WITH_STYLE_CHECK**| Compile with code style check, default: enabled (ON).
40-
|
31+
32+
| Optional | Description |
33+
| -------------------- | :--------------------------------------------------------------------------- |
34+
| **WITH_GPU** | Compile with GPU mode. |
35+
| **WITH_DOUBLE** | Compile with double precision floating-point, default: single precision. |
36+
| **WITH_GLOG** | Compile with glog. If not found, default: an internal log implementation. |
37+
| **WITH_GFLAGS** | Compile with gflags. If not found, default: an internal flag implementation. |
38+
| **WITH_TESTING** | Compile with gtest for PaddlePaddle's unit testing. |
39+
| **WITH_DOC** | Compile to generate PaddlePaddle's docs, default: disabled (OFF). |
40+
| **WITH_SWIG_PY** | Compile with python predict API, default: disabled (OFF). |
41+
| **WITH_STYLE_CHECK** | Compile with code style check, default: enabled (ON). |
4142

4243
**Note:**
4344
- The GPU version works best with Cuda Toolkit 7.5 and cuDNN v5.
@@ -118,11 +119,10 @@ As a simple example, consider the following:
118119
sudo tar -xzf cudnn-7.5-linux-x64-v5.1.tgz -C /usr/local
119120
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
120121
```
121-
Then you need to set LD\_LIBRARY\_PATH, CUDA\_HOME and PATH environment variables in ~/.bashrc.
122+
Then you need to set LD\_LIBRARY\_PATH, PATH environment variables in ~/.bashrc.
122123
123124
```bash
124125
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
125-
export CUDA_HOME=/usr/local/cuda
126126
export PATH=/usr/local/cuda/bin:$PATH
127127
```
128128
@@ -158,13 +158,12 @@ As a simple example, consider the following:
158158
cmake .. -DWITH_GPU=ON -DWITH_DOC=ON -DWITH_SWIG_PY=ON
159159
```
160160

161-
Finally, you can download source code and build:
161+
Finally, you can build PaddlePaddle:
162162

163163
```bash
164164
# you can add build option here, such as:
165165
cmake .. -DWITH_GPU=ON -DWITH_DOC=OFF -DCMAKE_INSTALL_PREFIX=<path to install>
166-
# please use sudo make install, if you want
167-
# to install PaddlePaddle into the system
166+
# please use sudo make install, if you want to install PaddlePaddle into the system
168167
make -j `nproc` && make install
169168
# set PaddlePaddle installation path in ~/.bashrc
170169
export PATH=<path to install>/bin:$PATH
@@ -240,7 +239,7 @@ easy_install pip
240239
sudo tar -xzf cudnn-7.5-osx-x64-v5.0-ga.tgz -C /usr/local
241240
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
242241
```
243-
2. Then you need to set DYLD\_LIBRARY\_PATH, CUDA\_HOME and PATH environment variables in ~/.bashrc.
242+
2. Then you need to set DYLD\_LIBRARY\_PATH, PATH environment variables in ~/.bashrc.
244243

245244
```bash
246245
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH
@@ -289,8 +288,6 @@ make -j `nproc` && make install
289288
# set PaddlePaddle installation path in ~/.bashrc
290289
export PATH=<installation path>/bin:$PATH
291290
```
292-
293-
294291
**Note:**
295292
296293
If you set `WITH_SWIG_PY=ON`, related python dependencies also need to be installed.

0 commit comments

Comments
 (0)