From 705dd36a3133e0ca3cbbbba1ccb3020064056e76 Mon Sep 17 00:00:00 2001 From: SunTY Date: Fri, 15 May 2020 10:42:44 +0800 Subject: [PATCH] simplestl is an alternative std vector string implementation (#1762) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 去掉对stl的依赖 * 头文件名,push_back改正 * 去掉构造托管 * 好像是折腾 * data 的返回改为指针,非指针引用 * resize一处写错 * stdint * 加入c_str * 改文件名为小写 * NCNN_SIMPLESTL option * simplestl default to OFF * Update linux-x64-cpu-gcc.yml * Update linux-x64-cpu-gcc.yml * Update linux-x64-cpu-clang.yml * drop functional header * arm32 arm64 simplestl ci * 修改一处内存泄漏, 去掉编译器警告 * resize时默认量的bug Co-authored-by: nihuini Co-authored-by: nihui --- .github/workflows/linux-x64-cpu-clang.yml | 50 ++++++ .github/workflows/linux-x64-cpu-gcc.yml | 38 +++++ .travis.yml | 27 ++++ CMakeLists.txt | 1 + src/allocator.h | 1 - src/blob.h | 2 - src/command.h | 1 - src/cpu.cpp | 1 - src/gpu.cpp | 2 - src/gpu.h | 1 - src/layer.h | 2 - src/layer/arm/binaryop_arm.cpp | 33 ++-- src/layer/arm/unaryop_arm.cpp | 1 - src/layer/binaryop.cpp | 37 +++-- src/layer/proposal.cpp | 1 - src/layer/reduction.cpp | 29 ++-- src/layer/unaryop.cpp | 1 - src/layer/vulkan/binaryop_vulkan.cpp | 1 - src/modelbin.cpp | 1 - src/net.h | 1 - src/opencv.h | 1 - src/pipeline.cpp | 1 - src/platform.h.in | 8 + src/simplestl.h | 182 ++++++++++++++++++++++ 24 files changed, 377 insertions(+), 46 deletions(-) create mode 100644 src/simplestl.h diff --git a/.github/workflows/linux-x64-cpu-clang.yml b/.github/workflows/linux-x64-cpu-clang.yml index 7f51adf61..9c5844000 100644 --- a/.github/workflows/linux-x64-cpu-clang.yml +++ b/.github/workflows/linux-x64-cpu-clang.yml @@ -72,3 +72,53 @@ jobs: run: mkdir build && cd build && cmake -DNCNN_STDIO=OFF -DNCNN_STRING=OFF -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. - name: build run: cmake --build build -j 2 + + linux-clang-simplestl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: configure + env: + CC: clang + CXX: clang++ + run: mkdir build && cd build && cmake -DNCNN_STDIO=ON -DNCNN_STRING=ON -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TESTS=ON -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + - name: build + run: cmake --build build -j 2 + - name: test + run: cd build && ctest --output-on-failure -j 2 + + linux-clang-nostdio-simplestl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: configure + env: + CC: clang + CXX: clang++ + run: mkdir build && cd build && cmake -DNCNN_STDIO=OFF -DNCNN_STRING=ON -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + - name: build + run: cmake --build build -j 2 + + linux-clang-nostring-simplestl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: configure + env: + CC: clang + CXX: clang++ + run: mkdir build && cd build && cmake -DNCNN_STDIO=ON -DNCNN_STRING=OFF -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + - name: build + run: cmake --build build -j 2 + + linux-clang-nostdio-nostring-simplestl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: configure + env: + CC: clang + CXX: clang++ + run: mkdir build && cd build && cmake -DNCNN_STDIO=OFF -DNCNN_STRING=OFF -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + - name: build + run: cmake --build build -j 2 diff --git a/.github/workflows/linux-x64-cpu-gcc.yml b/.github/workflows/linux-x64-cpu-gcc.yml index 03bee79b6..4e675e847 100644 --- a/.github/workflows/linux-x64-cpu-gcc.yml +++ b/.github/workflows/linux-x64-cpu-gcc.yml @@ -57,3 +57,41 @@ jobs: run: mkdir build && cd build && cmake -DNCNN_STDIO=OFF -DNCNN_STRING=OFF -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. - name: build run: cmake --build build -j 2 + + linux-gcc-simplestl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: configure + run: mkdir build && cd build && cmake -DNCNN_STDIO=ON -DNCNN_STRING=ON -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TESTS=ON -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + - name: build + run: cmake --build build -j 2 + - name: test + run: cd build && ctest --output-on-failure -j 2 + + linux-gcc-nostdio-simplestl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: configure + run: mkdir build && cd build && cmake -DNCNN_STDIO=OFF -DNCNN_STRING=ON -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + - name: build + run: cmake --build build -j 2 + + linux-gcc-nostring-simplestl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: configure + run: mkdir build && cd build && cmake -DNCNN_STDIO=ON -DNCNN_STRING=OFF -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + - name: build + run: cmake --build build -j 2 + + linux-gcc-nostdio-nostring-simplestl: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: configure + run: mkdir build && cd build && cmake -DNCNN_STDIO=OFF -DNCNN_STRING=OFF -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TESTS=OFF -DNCNN_BUILD_BENCHMARK=OFF -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. + - name: build + run: cmake --build build -j 2 diff --git a/.travis.yml b/.travis.yml index 6600d542f..1a5455cb9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,6 +31,24 @@ matrix: - NAME=linux-clang - BUILD="mkdir build && cd build && cmake .. && make -j2 && ctest --output-on-failure -j 2" + - name: "linux-gcc-arm64-simplestl" + os: linux + arch: arm64 + dist: bionic + compiler: gcc + env: + - NAME=linux-gcc-simplestl + - BUILD="mkdir build && cd build && cmake -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. && make -j2 && ctest --output-on-failure -j 2" + + - name: "linux-clang-arm64-simplestl" + os: linux + arch: arm64 + dist: bionic + compiler: clang + env: + - NAME=linux-clang-simplestl + - BUILD="mkdir build && cd build && cmake -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF .. && make -j2 && ctest --output-on-failure -j 2" + - name: "windows-vs2017" os: windows dist: 1803-containers @@ -56,6 +74,15 @@ matrix: - BEFORE_BUILD="sudo dpkg --add-architecture armhf && sudo apt-get update && sudo apt-get -y install crossbuild-essential-armhf libc6:armhf libstdc++-5-dev:armhf linux-libc-dev:armhf" - BUILD="mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf.toolchain.cmake -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF - .. && make -j2 VERBOSE=1 && ctest --output-on-failure -j 2" + - name: "linux-gcc-arm32-simplestl" + os: linux + arch: arm64 + dist: bionic + env: + - NAME=linux-gcc-arm32-simplestl + - BEFORE_BUILD="sudo dpkg --add-architecture armhf && sudo apt-get update && sudo apt-get -y install crossbuild-essential-armhf libc6:armhf libstdc++-5-dev:armhf linux-libc-dev:armhf" + - BUILD="mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf.toolchain.cmake -DNCNN_SIMPLESTL=ON -DNCNN_BUILD_TOOLS=OFF -DNCNN_BUILD_EXAMPLES=OFF - .. && make -j2 VERBOSE=1 && ctest --output-on-failure -j 2" + - name: "test-coverage-arm32" os: linux arch: arm64 diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a2155efe..79935a714 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ option(NCNN_STDIO "load model from external file" ON) option(NCNN_STRING "plain and verbose string" ON) option(NCNN_INSTALL_SDK "install ncnn library and headers" ON) option(NCNN_OPENCV "minimal opencv structure emulation" OFF) +option(NCNN_SIMPLESTL "minimal cpp stl structure emulation" OFF) option(NCNN_BENCHMARK "print benchmark information for every layer" OFF) option(NCNN_PIXEL "convert and resize from/to image pixel" ON) option(NCNN_PIXEL_ROTATE "rotate image pixel orientation" ON) diff --git a/src/allocator.h b/src/allocator.h index 511f3c296..9a1807002 100644 --- a/src/allocator.h +++ b/src/allocator.h @@ -24,7 +24,6 @@ #include #include -#include #include "platform.h" #if NCNN_VULKAN diff --git a/src/blob.h b/src/blob.h index 3dc562143..a4ace208b 100644 --- a/src/blob.h +++ b/src/blob.h @@ -15,8 +15,6 @@ #ifndef NCNN_BLOB_H #define NCNN_BLOB_H -#include -#include #include "platform.h" #include "mat.h" diff --git a/src/command.h b/src/command.h index 5be346598..c988cfa5a 100644 --- a/src/command.h +++ b/src/command.h @@ -19,7 +19,6 @@ #if NCNN_VULKAN -#include #include #include "mat.h" diff --git a/src/cpu.cpp b/src/cpu.cpp index 6e96137b7..c5a147a27 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -17,7 +17,6 @@ #include #include -#include #ifdef _OPENMP #include diff --git a/src/gpu.cpp b/src/gpu.cpp index 86f7aacf9..7851a64f5 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -22,8 +22,6 @@ #include #include -#include -#include #include "mat.h" #include "command.h" diff --git a/src/gpu.h b/src/gpu.h index 4ee6afb2b..1950d18d5 100644 --- a/src/gpu.h +++ b/src/gpu.h @@ -20,7 +20,6 @@ #if NCNN_VULKAN #include -#include #include "mat.h" namespace ncnn { diff --git a/src/layer.h b/src/layer.h index a00907082..a17aba417 100644 --- a/src/layer.h +++ b/src/layer.h @@ -15,8 +15,6 @@ #ifndef NCNN_LAYER_H #define NCNN_LAYER_H -#include -#include #include #include "platform.h" #include "mat.h" diff --git a/src/layer/arm/binaryop_arm.cpp b/src/layer/arm/binaryop_arm.cpp index 424e2aad4..16dc0dac8 100644 --- a/src/layer/arm/binaryop_arm.cpp +++ b/src/layer/arm/binaryop_arm.cpp @@ -15,7 +15,6 @@ #include "binaryop_arm.h" #include #include -#include #if __ARM_NEON #include @@ -1734,6 +1733,22 @@ static int binary_op_scalar_inplace_bf16s(Mat& a, float b, const Option& opt) return 0; } +struct binary_op_add { + float operator() (const float& x, const float& y) const { return x + y; } +}; + +struct binary_op_sub { + float operator() (const float& x, const float& y) const { return x - y; } +}; + +struct binary_op_mul { + float operator() (const float& x, const float& y) const { return x * y; } +}; + +struct binary_op_div { + float operator() (const float& x, const float& y) const { return x / y; } +}; + struct binary_op_max { float operator() (const float& x, const float& y) const { return std::max(x, y); } }; @@ -1799,16 +1814,16 @@ int BinaryOp_arm::forward_bf16s(const std::vector& bottom_blobs, std::vecto if (elempack == 1 && elempack1 == 1) { if (op_type == Operation_ADD) - return binary_op_bf16s< std::plus >(bottom_blob, bottom_blob1, top_blob, opt); + return binary_op_bf16s(bottom_blob, bottom_blob1, top_blob, opt); if (op_type == Operation_SUB) - return binary_op_bf16s< std::minus >(bottom_blob, bottom_blob1, top_blob, opt); + return binary_op_bf16s(bottom_blob, bottom_blob1, top_blob, opt); if (op_type == Operation_MUL) - return binary_op_bf16s< std::multiplies >(bottom_blob, bottom_blob1, top_blob, opt); + return binary_op_bf16s(bottom_blob, bottom_blob1, top_blob, opt); if (op_type == Operation_DIV) - return binary_op_bf16s< std::divides >(bottom_blob, bottom_blob1, top_blob, opt); + return binary_op_bf16s(bottom_blob, bottom_blob1, top_blob, opt); if (op_type == Operation_MAX) return binary_op_bf16s(bottom_blob, bottom_blob1, top_blob, opt); @@ -1868,16 +1883,16 @@ int BinaryOp_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) if (elempack == 1) { if (op_type == Operation_ADD) - return binary_op_scalar_inplace_bf16s< std::plus >(bottom_top_blob, b, opt); + return binary_op_scalar_inplace_bf16s(bottom_top_blob, b, opt); if (op_type == Operation_SUB) - return binary_op_scalar_inplace_bf16s< std::minus >(bottom_top_blob, b, opt); + return binary_op_scalar_inplace_bf16s(bottom_top_blob, b, opt); if (op_type == Operation_MUL) - return binary_op_scalar_inplace_bf16s< std::multiplies >(bottom_top_blob, b, opt); + return binary_op_scalar_inplace_bf16s(bottom_top_blob, b, opt); if (op_type == Operation_DIV) - return binary_op_scalar_inplace_bf16s< std::divides >(bottom_top_blob, b, opt); + return binary_op_scalar_inplace_bf16s(bottom_top_blob, b, opt); if (op_type == Operation_MAX) return binary_op_scalar_inplace_bf16s(bottom_top_blob, b, opt); diff --git a/src/layer/arm/unaryop_arm.cpp b/src/layer/arm/unaryop_arm.cpp index 9de650253..6383f773f 100644 --- a/src/layer/arm/unaryop_arm.cpp +++ b/src/layer/arm/unaryop_arm.cpp @@ -14,7 +14,6 @@ #include "unaryop_arm.h" #include -#include #if __ARM_NEON #include diff --git a/src/layer/binaryop.cpp b/src/layer/binaryop.cpp index b1b05f508..e4ccf8878 100644 --- a/src/layer/binaryop.cpp +++ b/src/layer/binaryop.cpp @@ -15,7 +15,6 @@ #include "binaryop.h" #include #include -#include namespace ncnn { @@ -487,6 +486,26 @@ static int binary_op_scalar_inplace(Mat& a, float b, const Option& opt) return 0; } +template +struct binary_op_add { + T operator() (const T& x, const T& y) const { return x + y; } +}; + +template +struct binary_op_sub { + T operator() (const T& x, const T& y) const { return x - y; } +}; + +template +struct binary_op_mul { + T operator() (const T& x, const T& y) const { return x * y; } +}; + +template +struct binary_op_div { + T operator() (const T& x, const T& y) const { return x / y; } +}; + template struct binary_op_max { T operator() (const T& x, const T& y) const { return std::max(x, y); } @@ -520,16 +539,16 @@ int BinaryOp::forward(const std::vector& bottom_blobs, std::vector& to Mat& top_blob = top_blobs[0]; if (op_type == Operation_ADD) - return binary_op< std::plus >(bottom_blob, bottom_blob1, top_blob, opt); + return binary_op< binary_op_add >(bottom_blob, bottom_blob1, top_blob, opt); if (op_type == Operation_SUB) - return binary_op< std::minus >(bottom_blob, bottom_blob1, top_blob, opt); + return binary_op< binary_op_sub >(bottom_blob, bottom_blob1, top_blob, opt); if (op_type == Operation_MUL) - return binary_op< std::multiplies >(bottom_blob, bottom_blob1, top_blob, opt); + return binary_op< binary_op_mul >(bottom_blob, bottom_blob1, top_blob, opt); if (op_type == Operation_DIV) - return binary_op< std::divides >(bottom_blob, bottom_blob1, top_blob, opt); + return binary_op< binary_op_div >(bottom_blob, bottom_blob1, top_blob, opt); if (op_type == Operation_MAX) return binary_op< binary_op_max >(bottom_blob, bottom_blob1, top_blob, opt); @@ -552,16 +571,16 @@ int BinaryOp::forward(const std::vector& bottom_blobs, std::vector& to int BinaryOp::forward_inplace(Mat& bottom_top_blob, const Option& opt) const { if (op_type == Operation_ADD) - return binary_op_scalar_inplace< std::plus >(bottom_top_blob, b, opt); + return binary_op_scalar_inplace< binary_op_add >(bottom_top_blob, b, opt); if (op_type == Operation_SUB) - return binary_op_scalar_inplace< std::minus >(bottom_top_blob, b, opt); + return binary_op_scalar_inplace< binary_op_sub >(bottom_top_blob, b, opt); if (op_type == Operation_MUL) - return binary_op_scalar_inplace< std::multiplies >(bottom_top_blob, b, opt); + return binary_op_scalar_inplace< binary_op_mul >(bottom_top_blob, b, opt); if (op_type == Operation_DIV) - return binary_op_scalar_inplace< std::divides >(bottom_top_blob, b, opt); + return binary_op_scalar_inplace< binary_op_div >(bottom_top_blob, b, opt); if (op_type == Operation_MAX) return binary_op_scalar_inplace< binary_op_max >(bottom_top_blob, b, opt); diff --git a/src/layer/proposal.cpp b/src/layer/proposal.cpp index 53fe3291a..4cdaf3525 100644 --- a/src/layer/proposal.cpp +++ b/src/layer/proposal.cpp @@ -15,7 +15,6 @@ #include "proposal.h" #include #include -#include namespace ncnn { diff --git a/src/layer/reduction.cpp b/src/layer/reduction.cpp index 4bfe144bb..a09eaeb45 100644 --- a/src/layer/reduction.cpp +++ b/src/layer/reduction.cpp @@ -17,7 +17,6 @@ #include #include #include -#include namespace ncnn { @@ -761,6 +760,16 @@ struct post_process_log { T operator() (const T& x) const { return static_cast(log(x)); } }; +template +struct reduction_op_add { + T operator() (const T& x, const T& y) const { return x + y; } +}; + +template +struct reduction_op_mul { + T operator() (const T& x, const T& y) const { return x * y; } +}; + template struct reduction_op_asum { T operator() (const T& x, const T& y) const { return static_cast(x + fabs(y)); } @@ -833,13 +842,13 @@ int Reduction::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) } if (operation == ReductionOp_SUM) - return reduction< std::plus, std::plus, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); + return reduction< reduction_op_add, reduction_op_add, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); if (operation == ReductionOp_ASUM) - return reduction< reduction_op_asum, std::plus, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); + return reduction< reduction_op_asum, reduction_op_add, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); if (operation == ReductionOp_SUMSQ) - return reduction< reduction_op_sumsq, std::plus, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); + return reduction< reduction_op_sumsq, reduction_op_add, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); if (operation == ReductionOp_MEAN) { @@ -862,7 +871,7 @@ int Reduction::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) } float coeff_mean = coeff / scale; - return reduction< std::plus, std::plus, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, true, coeff_mean, keepdims, opt); + return reduction< reduction_op_add, reduction_op_add, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, true, coeff_mean, keepdims, opt); } if (operation == ReductionOp_MAX) @@ -872,19 +881,19 @@ int Reduction::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) return reduction< reduction_op_min, reduction_op_min, post_process_identity >(bottom_blob, top_blob, FLT_MAX, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); if (operation == ReductionOp_PROD) - return reduction< std::multiplies, std::multiplies, post_process_identity >(bottom_blob, top_blob, 1.f, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); + return reduction< reduction_op_mul, reduction_op_mul, post_process_identity >(bottom_blob, top_blob, 1.f, reduce_w, reduce_h, reduce_c, false, coeff, keepdims, opt); if (operation == ReductionOp_L1) - return reduction< reduction_op_asum, std::plus, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, false, 1.f, keepdims, opt); + return reduction< reduction_op_asum, reduction_op_add, post_process_identity >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, false, 1.f, keepdims, opt); if (operation == ReductionOp_L2) - return reduction< reduction_op_sumsq, std::plus, post_process_sqrt >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, true, 1.f, keepdims, opt); + return reduction< reduction_op_sumsq, reduction_op_add, post_process_sqrt >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, true, 1.f, keepdims, opt); if (operation == ReductionOp_LogSum) - return reduction< std::plus, std::plus, post_process_log >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, true, 1.f, keepdims, opt); + return reduction< reduction_op_add, reduction_op_add, post_process_log >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, true, 1.f, keepdims, opt); if (operation == ReductionOp_LogSumExp) - return reduction< reduction_op_sumsexp, std::plus, post_process_log >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, true, 1.f, keepdims, opt); + return reduction< reduction_op_sumsexp, reduction_op_add, post_process_log >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_c, true, 1.f, keepdims, opt); return 0; } diff --git a/src/layer/unaryop.cpp b/src/layer/unaryop.cpp index 325717c90..b6ae66e82 100644 --- a/src/layer/unaryop.cpp +++ b/src/layer/unaryop.cpp @@ -14,7 +14,6 @@ #include "unaryop.h" #include -#include namespace ncnn { diff --git a/src/layer/vulkan/binaryop_vulkan.cpp b/src/layer/vulkan/binaryop_vulkan.cpp index 8f179013f..7e3e619c8 100644 --- a/src/layer/vulkan/binaryop_vulkan.cpp +++ b/src/layer/vulkan/binaryop_vulkan.cpp @@ -15,7 +15,6 @@ #include "binaryop_vulkan.h" #include #include -#include #include "layer_shader_type.h" namespace ncnn { diff --git a/src/modelbin.cpp b/src/modelbin.cpp index 44cad70c4..c27909377 100644 --- a/src/modelbin.cpp +++ b/src/modelbin.cpp @@ -15,7 +15,6 @@ #include "modelbin.h" #include -#include #include "datareader.h" namespace ncnn { diff --git a/src/net.h b/src/net.h index 9c68f23a1..73b34cf30 100644 --- a/src/net.h +++ b/src/net.h @@ -15,7 +15,6 @@ #ifndef NCNN_NET_H #define NCNN_NET_H -#include #include "platform.h" #include "blob.h" #include "layer.h" diff --git a/src/opencv.h b/src/opencv.h index f0c4aff37..acb7ed603 100644 --- a/src/opencv.h +++ b/src/opencv.h @@ -20,7 +20,6 @@ #if NCNN_OPENCV #include -#include #include "mat.h" #if defined(_MSC_VER) || defined(__GNUC__) diff --git a/src/pipeline.cpp b/src/pipeline.cpp index 975baec5f..085f1e82c 100644 --- a/src/pipeline.cpp +++ b/src/pipeline.cpp @@ -15,7 +15,6 @@ #include "pipeline.h" #include #include -#include #include "mat.h" #include "option.h" #include "layer_shader_type.h" diff --git a/src/platform.h.in b/src/platform.h.in index df1776071..4d960b84a 100644 --- a/src/platform.h.in +++ b/src/platform.h.in @@ -18,6 +18,7 @@ #cmakedefine01 NCNN_STDIO #cmakedefine01 NCNN_STRING #cmakedefine01 NCNN_OPENCV +#cmakedefine01 NCNN_SIMPLESTL #cmakedefine01 NCNN_BENCHMARK #cmakedefine01 NCNN_PIXEL #cmakedefine01 NCNN_PIXEL_ROTATE @@ -135,6 +136,13 @@ private: } // namespace ncnn +#if NCNN_SIMPLESTL +#include "simplestl.h" +#else +#include +#include +#endif + #if NCNN_STDIO #include #define NCNN_LOGE(...) do { fprintf(stderr, ##__VA_ARGS__); fprintf(stderr, "\n"); } while(0) diff --git a/src/simplestl.h b/src/simplestl.h new file mode 100644 index 000000000..6f5877602 --- /dev/null +++ b/src/simplestl.h @@ -0,0 +1,182 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// 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. + +#ifndef NCNN_SIMPLESTL_H +#define NCNN_SIMPLESTL_H + +#include +#include +#include +#include + +// minimal stl data structure implementation +namespace std { + +template +struct vector +{ + vector() {} + vector(const size_t new_size, const T& value = T()) { resize(new_size, value); } + ~vector() { clear(); } + vector(const vector& v) + { + resize(v.size()); + for (size_t i = 0; i < size_; i++) { data_[i] = v.data_[i]; } + } + + vector& operator=(const vector& v) + { + if (this == &v) + { + return *this; + } + resize(0); + resize(v.size()); + for (size_t i = 0; i < size_; i++) + { + data_[i] = v.data_[i]; + } + return *this; + } + + void resize(const size_t new_size, const T& value = T()) + { + try_alloc(new_size); + if (new_size > size_) + { + for (size_t i = size_; i < new_size; i++) + { + new (&data_[i]) T(value); + } + } + else if (new_size < size_) + { + for (size_t i = new_size; i < size_; i++) + { + data_[i].~T(); + } + } + size_ = new_size; + } + + void clear() + { + for (size_t i = 0; i < size_; i++) + { + data_[i].~T(); + } + delete[](char*) data_; + data_ = nullptr; + size_ = 0; + capacity_ = 0; + } + + T* data() const { return data_; } + size_t size() const { return size_; } + T& operator[](size_t i) const { return data_[i]; } + T* begin() const { return &data_[0]; } + T* end() const { return &data_[size_]; } + bool empty() const { return size_ == 0; } + + void push_back(const T& t) + { + try_alloc(size_ + 1); + new (&data_[size_]) T(t); + size_++; + } + + void insert(T* pos, T* b, T* e) + { + vector* v = nullptr; + if (b >= begin() && b < end()) + { + //the same vector + v = new vector(*this); + b = v->begin() + (b - begin()); + e = v->begin() + (e - begin()); + } + size_t diff = pos - begin(); + try_alloc(size_ + (e - b)); + pos = begin() + diff; + memmove(pos + (e - b), pos, (end() - pos) * sizeof(T)); + size_t len = e - b; + size_ += len; + for (size_t i = 0; i < len; i++) + { + *pos = *b; + pos++; + b++; + } + delete v; + } + + T* erase(T* pos) + { + pos->~T(); + memmove(pos, pos + 1, (end() - pos - 1) * sizeof(T)); + size_--; + return pos; + } + +protected: + T* data_ = nullptr; + size_t size_ = 0; + size_t capacity_ = 0; + void try_alloc(size_t new_size) + { + if (new_size * 3 / 2 > capacity_ / 2) + { + capacity_ = new_size * 2; + T* new_data = (T*)new char[capacity_ * sizeof(T)]; + memset(new_data, 0, capacity_ * sizeof(T)); + if (data_) + { + memmove(new_data, data_, sizeof(T) * size_); + delete[](char*) data_; + } + data_ = new_data; + } + } +}; + +struct string : public vector +{ + string() {} + string(const char* str) + { + size_t len = strlen(str); + resize(len); + memcpy(data_, str, len); + } + const char* c_str() const { return (const char*)data_; } + bool operator==(const string& str2) const { return strcmp(data_, str2.data_) == 0; } + bool operator==(const char* str2) const { return strcmp(data_, str2) == 0; } + bool operator!=(const char* str2) const { return strcmp(data_, str2) != 0; } + string& operator+=(const string& str1) + { + insert(end(), str1.begin(), str1.end()); + return *this; + } +}; + +inline string operator+(const string& str1, const string& str2) +{ + string str(str1); + str.insert(str.end(), str2.begin(), str2.end()); + return str; +} + +} // namespace std + +#endif // NCNN_SIMPLESTL_H