diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b491694ff0d380114e46e1fb8fcbe43d9ee26ff..eb0ffd459e28644deec4ad71e107d40e502e05f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,12 @@ file(GLOB_RECURSE PADDLE_MOBILE_H src/*.h) include_directories(src/) +option(USE_OPENMP "openmp support" OFF) +if(USE_OPENMP) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp") + add_definitions(-DPADDLE_MOBILE_USE_OPENMP) +endif() + if (googlenet) add_definitions(-DCONCAT_OP) add_definitions(-DCONV_OP) diff --git a/src/common/openmp-fix.cpp b/src/common/openmp-fix.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c31ef45c68227c612155e826e664367a7917501 --- /dev/null +++ b/src/common/openmp-fix.cpp @@ -0,0 +1,27 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#ifdef PADDLE_MOBILE_USE_OPENMP +/** + * android-ndk-r17 has a problem when linking with openmp. + * if paddle-mobile enables -fopenmp, but didn't use those omp_* functions, + * after linking another binary with libpaddle-mobile.so, the omp_get_thread_num + * will not work. see test/common/test_openmp.cc the detailed reason is still + * unclear, but this trick will work. a better solution is hacking the linker, + * try some flags to make it link omp_* functions, but I didn't find out how to + * make it work. + */ +#include +static int _ = omp_get_num_procs(); +#endif diff --git a/src/operators/kernel/arm/sigmoid_kernel.cpp b/src/operators/kernel/arm/sigmoid_kernel.cpp index c03a8644cc086e14a24abd32bf2bdb347187ce0e..20f275ff482d7073195d075c374e4a0969993714 100644 --- a/src/operators/kernel/arm/sigmoid_kernel.cpp +++ b/src/operators/kernel/arm/sigmoid_kernel.cpp @@ -41,7 +41,9 @@ void sigmoid(const Tensor *X, Tensor *Y) { int out_size = paddle_mobile::framework::product(outer_ddim); int inner_size = paddle_mobile::framework::product(inner_ddim); -#pragma omp parallel for + DLOG << "outsize=" << out_size; + DLOG << "innersize=" << inner_size; + #pragma omp parallel for for (int i = 0; i < out_size; ++i) { const float *input_outer_ptr = input + i * inner_size; float *output_outer_ptr = output + i * inner_size; diff --git a/src/operators/math/pooling.cpp b/src/operators/math/pooling.cpp index 0a823f2cc066e487bf1e3131105b28c0a44e44a4..09bc2a6ae4a7b5f2876350375a333c457c72b68a 100644 --- a/src/operators/math/pooling.cpp +++ b/src/operators/math/pooling.cpp @@ -59,7 +59,7 @@ class PoolFunctor { T *output_data = output->mutable_data(); for (int i = 0; i < batch_size; i++) { -#pragma omp parallel for + #pragma omp parallel for for (int c = 0; c < output_channels; ++c) { for (int ph = 0; ph < output_height; ++ph) { int hstart = ph * stride_height - padding_height; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a799c648b660625d0a61c89cbaf8b2b3d0eda2d1..7bde05f70c85b9ea363e58ff483d45436fd7abd2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -121,6 +121,14 @@ else () ADD_EXECUTABLE(test-enforce common/test_enforce.cpp) target_link_libraries(test-enforce paddle-mobile) + # gen test - test if openmp works + ADD_EXECUTABLE(test-openmp common/test_openmp.cc test_helper.h test_include.h executor_for_test.h) + target_link_libraries(test-openmp paddle-mobile) + + # gen test + ADD_EXECUTABLE(test-yolo net/test_yolo.cpp test_helper.h test_include.h executor_for_test.h) + target_link_libraries(test-yolo paddle-mobile) + # gen test ADD_EXECUTABLE(test-mobilenetssd net/test_mobilenet+ssd.cpp test_helper.h test_include.h executor_for_test.h) target_link_libraries(test-mobilenetssd paddle-mobile) diff --git a/test/common/test_openmp.cpp b/test/common/test_openmp.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7393810a3cd5cafcd75e3964cc21d31036d83817 --- /dev/null +++ b/test/common/test_openmp.cpp @@ -0,0 +1,26 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include +#include + +int main(void) { + #pragma omp parallel num_threads(2) + { + int thread_id = omp_get_thread_num(); + int nthreads = omp_get_num_threads(); + std::cout << "Hello, OMP " << thread_id << "/" << nthreads << "\n"; + } + return 0; +} diff --git a/tools/pre-commit.hooks/clang-format.hook b/tools/pre-commit.hooks/clang-format.hook index 406850d1a2450b49463563c0034c6c969895bfe4..4fa4253bad78fe287fb92863a684a5d7def71061 100644 --- a/tools/pre-commit.hooks/clang-format.hook +++ b/tools/pre-commit.hooks/clang-format.hook @@ -12,4 +12,8 @@ if ! [[ $version == *"$VERSION"* ]]; then exit -1 fi -clang-format $@ +# https://medicineyeh.wordpress.com/2017/07/13/clang-format-with-pragma/ +shift +perl -i -pe 's|#pragma\s+omp|// #pragma omp|' "$@" +clang-format -i $@ +perl -i -pe 's|// ||' "$@"