From 6f22951a62e79efc4071e16e14e529217cb3bb40 Mon Sep 17 00:00:00 2001 From: liaogang Date: Tue, 21 Mar 2017 15:25:58 +0800 Subject: [PATCH] Add simd check and set SSE3 as default compilation --- CMakeLists.txt | 2 +- paddle/utils/Common.h | 1 + paddle/utils/CpuId.h | 33 +++++++++++++++++++++++++++++++++ paddle/utils/Util.cpp | 2 ++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b0682c4fe..3e85a90166 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ include(simd) ################################ Configurations ####################################### option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND}) -option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND}) +option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" OFF) option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON) option(WITH_TESTING "Compile PaddlePaddle with unit testing" ON) option(WITH_SWIG_PY "Compile PaddlePaddle with inference api" ON) diff --git a/paddle/utils/Common.h b/paddle/utils/Common.h index 1f1d0255a5..91b4759120 100644 --- a/paddle/utils/Common.h +++ b/paddle/utils/Common.h @@ -14,6 +14,7 @@ limitations under the License. */ #pragma once +#include "Error.h" #include "Excepts.h" /** diff --git a/paddle/utils/CpuId.h b/paddle/utils/CpuId.h index 0f3985cc7b..8b21347cae 100644 --- a/paddle/utils/CpuId.h +++ b/paddle/utils/CpuId.h @@ -97,4 +97,37 @@ private: #define HAS_AVX512 HAS_SIMD(SIMD_AVX512) // clang-format on +/** + * Invoke checkCPUFeature() before Paddle initialization to + * check target machine whether support compiled instructions. + * If not, simply throw out an error. + */ +inline Error __must_check checkCPUFeature() { + Error err; +#ifndef __AVX__ + if (HAS_AVX) { + LOG(WARNING) << "PaddlePaddle wasn't compiled to use avx instructions, " + << "but these are available on your machine and could " + << "speed up CPU computations via CMAKE .. -DWITH_AVX=ON"; + } +#else + if (!HAS_AVX) { + err = Errors( + "PaddlePaddle was compiled to use avx instructions, " + "but these aren't available on your machine, please " + "disable it via CMAKE .. -DWITH_AVX=OFF"); + } +#endif // __AVX__ +#ifdef __SSE3__ + if (!HAS_SSE3) { + err = Error( + "PaddlePaddle was compiled to use sse3 instructions, " + "which is the minimum requirement of PaddlePaddle. " + "But these aren't available on your current machine."); + } +#endif // __SSE3__ + + return err; +} + } // namespace paddle diff --git a/paddle/utils/Util.cpp b/paddle/utils/Util.cpp index dbab4ec43c..1f56b6b8a9 100644 --- a/paddle/utils/Util.cpp +++ b/paddle/utils/Util.cpp @@ -26,6 +26,7 @@ limitations under the License. */ #include +#include "CpuId.h" #include "CustomStackTrace.h" #include "Logging.h" #include "StringUtil.h" @@ -185,6 +186,7 @@ void initMain(int argc, char** argv) { } version::printVersion(); + checkCPUFeature().check(); runInitFunctions(); } -- GitLab