From ada56f3c426c25834633af93d1275ae06f42569c Mon Sep 17 00:00:00 2001 From: z00512249 Date: Tue, 23 Mar 2021 10:40:06 +0800 Subject: [PATCH] fix micro thread_pool --- mindspore/lite/micro/cmake/file_list.cmake | 1 - .../generator/component/parallel_component.cc | 6 ++-- .../generator/component/weight_component.cc | 3 +- .../lite/micro/coder/generator/generator.cc | 5 ++- .../lite/micro/coder/opcoders/parallel.cc | 33 ------------------- .../lite/micro/coder/opcoders/parallel.h | 14 ++++---- 6 files changed, 15 insertions(+), 47 deletions(-) delete mode 100644 mindspore/lite/micro/coder/opcoders/parallel.cc diff --git a/mindspore/lite/micro/cmake/file_list.cmake b/mindspore/lite/micro/cmake/file_list.cmake index e8433927e81..fdb2f965a0b 100644 --- a/mindspore/lite/micro/cmake/file_list.cmake +++ b/mindspore/lite/micro/cmake/file_list.cmake @@ -42,7 +42,6 @@ set(CODER_OPCODERS_SRC ${MICRO_DIR}/coder/opcoders/op_coder.cc ${MICRO_DIR}/coder/opcoders/op_coder_builder.cc ${MICRO_DIR}/coder/opcoders/op_coder_register.cc - ${MICRO_DIR}/coder/opcoders/parallel.cc #### serializer ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.cc ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.cc diff --git a/mindspore/lite/micro/coder/generator/component/parallel_component.cc b/mindspore/lite/micro/coder/generator/component/parallel_component.cc index 8a73443f0f9..dbfad84b4ea 100644 --- a/mindspore/lite/micro/coder/generator/component/parallel_component.cc +++ b/mindspore/lite/micro/coder/generator/component/parallel_component.cc @@ -16,6 +16,7 @@ #include "coder/generator/component/parallel_component.h" #include +#include "coder/opcoders/parallel.h" namespace mindspore::lite::micro { @@ -51,13 +52,14 @@ void CodeSetGlobalThreadPoolState(std::ofstream &ofs) { } void CodeSetGlobalThreadPoolImplement(std::ofstream &ofs) { - ofs << "struct ThreadPool *g_thread_pool = NULL;\n" + ofs << "struct ThreadPool *" << gThreadPool << " = NULL;\n" << "int " << "SetThreadPool(struct ThreadPool *thread_pool) {\n" << " if (thread_pool == NULL) {\n" " return RET_ERROR;\n" " }\n" - " g_thread_pool = thread_pool;\n" + << gThreadPool + << " = thread_pool;\n" " return RET_OK;\n" "}\n"; } diff --git a/mindspore/lite/micro/coder/generator/component/weight_component.cc b/mindspore/lite/micro/coder/generator/component/weight_component.cc index e606d807777..8462f404526 100644 --- a/mindspore/lite/micro/coder/generator/component/weight_component.cc +++ b/mindspore/lite/micro/coder/generator/component/weight_component.cc @@ -35,6 +35,8 @@ void CodeWeightFileHeader(std::ofstream &ofs, const std::unique_ptr &weights) { @@ -100,7 +102,6 @@ void CodeWeightInitFunc(std::ofstream &ofs, const std::unique_ptr << " if (weight_buffer == NULL) {\n" << " return RET_ERROR;\n" << " }\n"; - ofs << " int " << gThreadNum << " = 1;\n\n"; ofs << " struct ModelParameter {\n" << " void *addr;\n" << " size_t size;\n" diff --git a/mindspore/lite/micro/coder/generator/generator.cc b/mindspore/lite/micro/coder/generator/generator.cc index 1ad60bc5750..1f124a22cd7 100644 --- a/mindspore/lite/micro/coder/generator/generator.cc +++ b/mindspore/lite/micro/coder/generator/generator.cc @@ -29,6 +29,7 @@ #include "coder/generator/component/const_blocks/benchmark.h" #include "coder/generator/component/const_blocks/license.h" #include "coder/log.h" +#include "coder/opcoders/parallel.h" namespace mindspore::lite::micro { int WriteContentToFile(const std::string &file, const std::string &content) { @@ -61,9 +62,7 @@ void Generator::CodeNetRunFunc(std::ofstream &ofs) { // generate net inference code ofs << "void Inference() {\n"; if (config_->support_parallel()) { - ofs << " const int g_thread_num = GetCurrentThreadNum(g_thread_pool);\n"; - } else { - ofs << " const int g_thread_num = 1;\n"; + ofs << gThreadNum << " = GetCurrentThreadNum(" << gThreadPool << ");\n "; } for (const auto &block : ctx_->code_blocks()) { ofs << " {\n" << block << " }\n"; diff --git a/mindspore/lite/micro/coder/opcoders/parallel.cc b/mindspore/lite/micro/coder/opcoders/parallel.cc deleted file mode 100644 index fbd6bb8619d..00000000000 --- a/mindspore/lite/micro/coder/opcoders/parallel.cc +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "coder/opcoders/parallel.h" - -namespace mindspore::lite::micro { - -// ParallelLaunch is defined in thread_pool -const char *kParallelLaunch = "ParallelLaunch"; - -// g_thread_pool and g_thread_num are global variable, -// assign g_thread_pool by CreateThreadPool, -// and g_thread_num is equal to GetCurrentThreadNum -const char *gThreadNum = "g_thread_num"; -const char *gThreadPool = "g_thread_pool"; - -// args represents the parameters required for operator to run -const char *kRunArgs = "args"; -const char *kRunArgsAddr = "&args"; - -} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/parallel.h b/mindspore/lite/micro/coder/opcoders/parallel.h index 08136078487..9663c727e9d 100644 --- a/mindspore/lite/micro/coder/opcoders/parallel.h +++ b/mindspore/lite/micro/coder/opcoders/parallel.h @@ -19,22 +19,22 @@ namespace mindspore::lite::micro { -constexpr int kDefaultTaskId = 0; +constexpr auto kDefaultTaskId = 0; -constexpr int kMaxThreadNumSupported = 4; +constexpr auto kMaxThreadNumSupported = 4; // ParallelLaunch is defined in thread_pool -extern const char *kParallelLaunch; +constexpr auto kParallelLaunch = "ParallelLaunch"; // g_thread_pool and g_thread_num are global variable, // assign g_thread_pool by CreateThreadPool, // and g_thread_num is equal to GetCurrentThreadNum -extern const char *gThreadNum; -extern const char *gThreadPool; +constexpr auto gThreadNum = "g_thread_num"; +constexpr auto gThreadPool = "g_thread_pool"; // args represents the parameters required for operator to run -extern const char *kRunArgs; -extern const char *kRunArgsAddr; +constexpr auto kRunArgs = "args"; +constexpr auto kRunArgsAddr = "&args"; } // namespace mindspore::lite::micro