Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile on raspberry pi #1835

Merged
merged 5 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions doc/howto/raspberry/build_for_raspberry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 如何构建raspberry pi下运行的paddle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raspberry pi => Raspberry Pi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddle => PaddlePaddle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


这里考虑的是交叉编译方式,即在Linux-X86环境下构建raspberry pi下可运行的paddle。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X86 => x86

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


## 下载交叉编译环境
```
git clone https://github.com/raspberrypi/tools
```
如果host是x86-64环境,选用`arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64`下的作为编译工具。注意,需要系统glibc支持2.14以上。


## 编译第三方库
Paddle在cmake会在编译paddle的时候自动编译依赖的第三方库,不过openblas和protobuf最好还是在编译paddle之前先编译好,这样可以保证编译paddle的时候更加顺畅。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paddle在cmake会在编译paddle的时候自动编译依赖的第三方库

==>

cmake会自动下载PaddlePaddle依赖的第三方库。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过openblas和protobuf最好还是在编译paddle之前先编译好,这样可以保证编译paddle的时候更加顺畅。

为什么最好?和依赖cmake去下载和编译OpenBLAS和protobuf有什么区别?我理解这个工作应该是让在Raspberry
Pi上编译和在其他平台编译的用户体验尽量一致。如果可以依赖cmake去下载所有依赖,貌似更合理?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在交叉编译方式下,直接依赖cmake去下载和编译中间会断掉,不能一次cmake & make成功。比如,编译protobuf时编译出来的protoc是arm版本的,在后续用到protoc的地方编译过程就崩溃了,需要用一个x86版本的protoc去替换掉,然后再继续make。
如果,直接在Pi编译,应该是直接cmake & make就可以了,不过编译耗时会比较长。


### 编译openblas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openblas => OpenBLAS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

```
git clone https://github.com/xianyi/OpenBLAS.git
make TARGET=ARMV7 HOSTCC=gcc CC=arm-linux-gnueabihf-gcc NOFORTRAN=1 USE_THREAD=0
```

### 编译protobuf
```
git clone https://github.com/google/protobuf.git
git checkout 9f75c5aa851cd877fb0d93ccc31b8567a6706546
cmake ../protobuf/cmake \
-Dprotobuf_BUILD_TESTS=OFF \
-DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ \
-DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_LIBDIR=lib
```
注意:这样编译出来的`libprotobuf.a`和`protoc`都是ARM版本的,而我们需要的是一个x86-64版本的`protoc`,所以需要用host gcc再编译一遍protobuf然后使用其中的`protoc`。


## 编译Paddle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paddle ==> PaddlePaddle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

```
cmake .. -DWITH_GPU=OFF -DWITH_PYTHON=OFF -DWITH_SWIG_PY=OFF \
-DCMAKE_CXX_COMPILER:FILEPATH=arm-linux-gnueabihf-g++ \
-DCMAKE_C_COMPILER:FILEPATH=arm-linux-gnueabihf-gcc \
-DCMAKE_C_FLAGS="-mfpu=neon" \
-DCMAKE_CXX_FLAGS="-mfpu=neon" \
-DOPENBLAS_ROOT=openblas \
-DCMAKE_PREFIX_PATH=protobuf
```
2 changes: 1 addition & 1 deletion paddle/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FILE(GLOB PY_PADDLE_PYTHON_FILES ${PROJ_ROOT}/paddle/py_paddle/*.py)
SET_SOURCE_FILES_PROPERTIES(Paddle.i PROPERTIES CPLUSPLUS ON)

SET(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR})
SET(CMAKE_CXX_FLAGS "-std=c++11 -fPIC -Wall")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -Wall")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

IF(WITH_COVERAGE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
ENDIF(WITH_COVERAGE)
Expand Down
11 changes: 6 additions & 5 deletions paddle/utils/CpuId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ limitations under the License. */
/// for MSVC
#define CPUID(info, x) __cpuidex(info, x, 0)

#elif !defined(__ANDROID__)
#else

#if !defined(__arm__)
#include <cpuid.h>

/// for GCC/Clang
#define CPUID(info, x) __cpuid_count(x, 0, info[0], info[1], info[2], info[3])
#endif

#endif

namespace paddle {

SIMDFlags::SIMDFlags() {
#if !defined(__ANDROID__)
#if defined(__arm__)
simd_flags_ = SIMD_NEON;
#else
unsigned int cpuInfo[4];
// CPUID: https://en.wikipedia.org/wiki/CPUID
// clang-format off
Expand All @@ -52,8 +55,6 @@ SIMDFlags::SIMDFlags() {
CPUID(cpuInfo, 0x80000001);
simd_flags_ |= cpuInfo[2] & (1 << 16) ? SIMD_FMA4 : SIMD_NONE;
// clang-fotmat on
#else
simd_flags_ = SIMD_NEON;
#endif
}

Expand Down