From 2c1d4c280d523733bfcc6207c9dbe1f21c760491 Mon Sep 17 00:00:00 2001 From: liaogang Date: Thu, 29 Sep 2016 22:49:44 +0800 Subject: [PATCH 1/5] Add automatic check AVX in CMake --- CMakeLists.txt | 7 ++--- cmake/FindAVX.cmake | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 cmake/FindAVX.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 92c866da8fc7c..af6a13efbde9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ find_package(PythonInterp 2.7 REQUIRED) find_package(ZLIB REQUIRED) find_package(NumPy REQUIRED) find_package(Threads REQUIRED) +find_package(AVX QUIET) find_package(Glog) find_package(Gflags QUIET) find_package(GTest) @@ -28,7 +29,7 @@ find_program(M4_EXECUTABLE m4) option(WITH_DSO "Compile PaddlePaddle with dynamic linked libraries" ON) option(WITH_GPU "Compile PaddlePaddle with gpu" ${CUDA_FOUND}) option(WITH_DOUBLE "Compile PaddlePaddle with double precision, otherwise use single precision" OFF) -option(WITH_AVX "Compile PaddlePaddle with avx intrinsics" ON) # TODO(yuyang18): Check AVX is supported or not as default value +option(WITH_AVX "Compile PaddlePaddle with avx intrinsics" ${AVX_FOUND}) option(WITH_PYTHON "Compile PaddlePaddle with python interpreter" ON) option(WITH_STYLE_CHECK "Style Check for PaddlePaddle" ${PYTHONINTERP_FOUND}) option(WITH_RDMA "Compile PaddlePaddle with rdma support" OFF) @@ -101,8 +102,8 @@ if(NOT WITH_TIMER) endif(NOT WITH_TIMER) if(WITH_AVX) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${AVX_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${AVX_FLAGS}") else(WITH_AVX) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse3") diff --git a/cmake/FindAVX.cmake b/cmake/FindAVX.cmake new file mode 100644 index 0000000000000..ef57beb596ba7 --- /dev/null +++ b/cmake/FindAVX.cmake @@ -0,0 +1,65 @@ +# This file is use to check all support level of AVX on your machine +# so that PaddlePaddle can unleash the vectorization power of muticore. + +INCLUDE(CheckCXXSourceRuns) + +SET(FIND_AVX_10) +SET(FIND_AVX_20) +SET(AVX_FLAGS) +SET(AVX_FOUND) + +# Check AVX 2 +SET(CMAKE_REQUIRED_FLAGS) +IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + SET(CMAKE_REQUIRED_FLAGS "-mavx2") +ELSEIF(MSVC AND NOT CMAKE_CL_64) # reserve for WINDOWS + SET(CMAKE_REQUIRED_FLAGS "/arch:AVX2") +ENDIF() + +CHECK_CXX_SOURCE_RUNS(" +#include +int main() +{ + __m256i a = _mm256_set_epi32 (-1, 2, -3, 4, -1, 2, -3, 4); + __m256i result = _mm256_abs_epi32 (a); + return 0; +}" FIND_AVX_20) + +# Check AVX +SET(CMAKE_REQUIRED_FLAGS) +IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + SET(CMAKE_REQUIRED_FLAGS "-mavx") +ELSEIF(MSVC AND NOT CMAKE_CL_64) + SET(CMAKE_REQUIRED_FLAGS "/arch:AVX") +endif() + +CHECK_CXX_SOURCE_RUNS(" +#include +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; +}" FIND_AVX_10) + +IF(${FIND_AVX_20}) + IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + SET(AVX_FLAGS "${AVX_FLAGS} -mavx2") + ELSEIF(MSVC) + SET(AVX_FLAGS "${AVX_FLAGS} /arch:AVX2") + ENDIF() +ENDIF() + +IF(${FIND_AVX_10}) + IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + SET(AVX_FLAGS "${AVX_FLAGS} -mavx") + ELSEIF(MSVC) + SET(AVX_FLAGS "${AVX_FLAGS} /arch:AVX") + ENDIF() +ENDIF() + +IF(${FIND_AVX_10} OR ${FIND_AVX_20}) + SET(AVX_FOUND TRUE) + MESSAGE(STATUS "Find CPU supports ${AVX_FLAGS}.") +ENDIF() \ No newline at end of file From 5463cfd2be81ed8600bc7b608109360e4079a577 Mon Sep 17 00:00:00 2001 From: liaogang Date: Thu, 29 Sep 2016 22:59:04 +0800 Subject: [PATCH 2/5] Add indent in FindAVX.cmake --- cmake/FindAVX.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/FindAVX.cmake b/cmake/FindAVX.cmake index ef57beb596ba7..e0f1b7bff5162 100644 --- a/cmake/FindAVX.cmake +++ b/cmake/FindAVX.cmake @@ -40,7 +40,7 @@ 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; + return 0; }" FIND_AVX_10) IF(${FIND_AVX_20}) From 5b1a5963e15047fad7b46259c20bbf7f8226e15c Mon Sep 17 00:00:00 2001 From: liaogang Date: Thu, 29 Sep 2016 23:45:48 +0800 Subject: [PATCH 3/5] Revise table format and some words in build docs --- doc/build/build_from_source.md | 37 ++++++++++++++++------------------ 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/doc/build/build_from_source.md b/doc/build/build_from_source.md index a6090d6819162..948f84abae86f 100644 --- a/doc/build/build_from_source.md +++ b/doc/build/build_from_source.md @@ -11,11 +11,12 @@ You can download PaddlePaddle from the [github source](https://github.com/gangli ```bash git clone https://github.com/baidu/Paddle paddle +cd paddle ``` ## Requirements -To compile the source code, your computer must be equipped with GCC >=4.6 or Clang Compiler. +To compile the source code, your computer must be equipped with GCC >=4.6 or Clang compiler. ### Dependencies - **CMake**: version >= 2.8 @@ -27,17 +28,17 @@ To compile the source code, your computer must be equipped with GCC >=4.6 or Cla PaddlePaddle supports some build options. To enable it, first you need to install the related libraries. - Optional | Description - ------------ | :----------- - **WITH_GPU** | Compile with GPU mode. - **WITH_DOUBLE** | Compile with double precision floating-point, default: single precision. | - **WITH_GLOG** | Compile with glog. If not found, default: an internal log implementation. - **WITH_GFLAGS** | Compile with gflags. If not found, default: an internal flag implementation. - **WITH_TESTING** | Compile with gtest for PaddlePaddle's unit testing. - **WITH_DOC** | Compile to generate PaddlePaddle's docs, default: disabled (OFF). - **WITH_SWIG_PY** | Compile with python predict API, default: disabled (OFF). - **WITH_STYLE_CHECK**| Compile with code style check, default: enabled (ON). -| + +| Optional | Description | +| -------------------- | :--------------------------------------------------------------------------- | +| **WITH_GPU** | Compile with GPU mode. | +| **WITH_DOUBLE** | Compile with double precision floating-point, default: single precision. | +| **WITH_GLOG** | Compile with glog. If not found, default: an internal log implementation. | +| **WITH_GFLAGS** | Compile with gflags. If not found, default: an internal flag implementation. | +| **WITH_TESTING** | Compile with gtest for PaddlePaddle's unit testing. | +| **WITH_DOC** | Compile to generate PaddlePaddle's docs, default: disabled (OFF). | +| **WITH_SWIG_PY** | Compile with python predict API, default: disabled (OFF). | +| **WITH_STYLE_CHECK** | Compile with code style check, default: enabled (ON). | **Note:** - The GPU version works best with Cuda Toolkit 7.5 and cuDNN v5. @@ -118,11 +119,10 @@ As a simple example, consider the following: sudo tar -xzf cudnn-7.5-linux-x64-v5.1.tgz -C /usr/local sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn* ``` - Then you need to set LD\_LIBRARY\_PATH, CUDA\_HOME and PATH environment variables in ~/.bashrc. + Then you need to set LD\_LIBRARY\_PATH, PATH environment variables in ~/.bashrc. ```bash export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH - export CUDA_HOME=/usr/local/cuda export PATH=/usr/local/cuda/bin:$PATH ``` @@ -158,13 +158,12 @@ As a simple example, consider the following: cmake .. -DWITH_GPU=ON -DWITH_DOC=ON -DWITH_SWIG_PY=ON ``` -Finally, you can download source code and build: +Finally, you can build PaddlePaddle: ```bash # you can add build option here, such as: cmake .. -DWITH_GPU=ON -DWITH_DOC=OFF -DCMAKE_INSTALL_PREFIX= -# please use sudo make install, if you want -# to install PaddlePaddle into the system +# please use sudo make install, if you want to install PaddlePaddle into the system make -j `nproc` && make install # set PaddlePaddle installation path in ~/.bashrc export PATH=/bin:$PATH @@ -240,7 +239,7 @@ easy_install pip sudo tar -xzf cudnn-7.5-osx-x64-v5.0-ga.tgz -C /usr/local sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn* ``` - 2. Then you need to set DYLD\_LIBRARY\_PATH, CUDA\_HOME and PATH environment variables in ~/.bashrc. + 2. Then you need to set DYLD\_LIBRARY\_PATH, PATH environment variables in ~/.bashrc. ```bash export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH @@ -289,8 +288,6 @@ make -j `nproc` && make install # set PaddlePaddle installation path in ~/.bashrc export PATH=/bin:$PATH ``` - - **Note:** If you set `WITH_SWIG_PY=ON`, related python dependencies also need to be installed. From 54b2f7b40f49291b6b1807bffd1760749b07f101 Mon Sep 17 00:00:00 2001 From: liaogang Date: Fri, 30 Sep 2016 10:17:33 +0800 Subject: [PATCH 4/5] Update build docs --- doc/build/build_from_source.md | 56 +++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/doc/build/build_from_source.md b/doc/build/build_from_source.md index 948f84abae86f..c671f483863c7 100644 --- a/doc/build/build_from_source.md +++ b/doc/build/build_from_source.md @@ -28,17 +28,51 @@ To compile the source code, your computer must be equipped with GCC >=4.6 or Cla PaddlePaddle supports some build options. To enable it, first you need to install the related libraries. - -| Optional | Description | -| -------------------- | :--------------------------------------------------------------------------- | -| **WITH_GPU** | Compile with GPU mode. | -| **WITH_DOUBLE** | Compile with double precision floating-point, default: single precision. | -| **WITH_GLOG** | Compile with glog. If not found, default: an internal log implementation. | -| **WITH_GFLAGS** | Compile with gflags. If not found, default: an internal flag implementation. | -| **WITH_TESTING** | Compile with gtest for PaddlePaddle's unit testing. | -| **WITH_DOC** | Compile to generate PaddlePaddle's docs, default: disabled (OFF). | -| **WITH_SWIG_PY** | Compile with python predict API, default: disabled (OFF). | -| **WITH_STYLE_CHECK** | Compile with code style check, default: enabled (ON). | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OptionalDescription
WITH_GPUCompile with GPU mode.
WITH_DOUBLECompile with double precision floating-point, default: single precision.
WITH_GLOGCompile with glog. If not found, default: an internal log implementation.
WITH_GFLAGSCompile with gflags. If not found, default: an internal flag implementation.
WITH_TESTINGCompile with gtest for PaddlePaddle's unit testing.
WITH_DOCCompile to generate PaddlePaddle's docs, default: disabled (OFF)
WITH_SWIG_PYCompile with python predict API, default: disabled (OFF).
WITH_STYLE_CHECKCompile with code style check, default: enabled (ON).
**Note:** - The GPU version works best with Cuda Toolkit 7.5 and cuDNN v5. From 53fe383c60436df19668646a3a8c5543f28186bf Mon Sep 17 00:00:00 2001 From: gangliao Date: Fri, 30 Sep 2016 12:54:21 +0800 Subject: [PATCH 5/5] Update build docs --- doc/build/build_from_source.md | 65 +++++++++++----------------------- 1 file changed, 20 insertions(+), 45 deletions(-) diff --git a/doc/build/build_from_source.md b/doc/build/build_from_source.md index c671f483863c7..a952902222837 100644 --- a/doc/build/build_from_source.md +++ b/doc/build/build_from_source.md @@ -28,51 +28,26 @@ To compile the source code, your computer must be equipped with GCC >=4.6 or Cla PaddlePaddle supports some build options. To enable it, first you need to install the related libraries. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +
OptionalDescription
WITH_GPUCompile with GPU mode.
WITH_DOUBLECompile with double precision floating-point, default: single precision.
WITH_GLOGCompile with glog. If not found, default: an internal log implementation.
WITH_GFLAGSCompile with gflags. If not found, default: an internal flag implementation.
WITH_TESTINGCompile with gtest for PaddlePaddle's unit testing.
WITH_DOCCompile to generate PaddlePaddle's docs, default: disabled (OFF)
WITH_SWIG_PYCompile with python predict API, default: disabled (OFF).
WITH_STYLE_CHECKCompile with code style check, default: enabled (ON).
+ + + + + + + + + + + + + + + +
OptionalDescription
WITH_GPUCompile with GPU mode.
WITH_DOUBLECompile with double precision floating-point, default: single precision.
WITH_GLOGCompile with glog. If not found, default: an internal log implementation.
WITH_GFLAGSCompile with gflags. If not found, default: an internal flag implementation.
WITH_TESTINGCompile with gtest for PaddlePaddle's unit testing.
WITH_DOC Compile to generate PaddlePaddle's docs, default: disabled (OFF).
WITH_SWIG_PYCompile with python predict API, default: disabled (OFF).
WITH_STYLE_CHECKCompile with code style check, default: enabled (ON).
+ **Note:** - The GPU version works best with Cuda Toolkit 7.5 and cuDNN v5. @@ -334,4 +309,4 @@ It may require sudo privileges: sudo pip install /opt/paddle/share/wheels/*.whl # or just run sudo paddle version -``` \ No newline at end of file +```