From: @zoloft Reviewed-by: @wangchengyuan,@hangangqiang Signed-off-by: @wangchengyuantags/v1.2.0-rc1
| @@ -43,7 +43,6 @@ set(CODER_OPCODERS_SRC | |||||
| ${MICRO_DIR}/coder/opcoders/op_coder.cc | ${MICRO_DIR}/coder/opcoders/op_coder.cc | ||||
| ${MICRO_DIR}/coder/opcoders/op_coder_builder.cc | ${MICRO_DIR}/coder/opcoders/op_coder_builder.cc | ||||
| ${MICRO_DIR}/coder/opcoders/op_coder_register.cc | ${MICRO_DIR}/coder/opcoders/op_coder_register.cc | ||||
| ${MICRO_DIR}/coder/opcoders/parallel.cc | |||||
| #### serializer | #### serializer | ||||
| ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.cc | ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.cc | ||||
| ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.cc | ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.cc | ||||
| @@ -16,6 +16,7 @@ | |||||
| #include "coder/generator/component/parallel_component.h" | #include "coder/generator/component/parallel_component.h" | ||||
| #include <string> | #include <string> | ||||
| #include "coder/opcoders/parallel.h" | |||||
| namespace mindspore::lite::micro { | namespace mindspore::lite::micro { | ||||
| @@ -51,13 +52,14 @@ void CodeSetGlobalThreadPoolState(std::ofstream &ofs) { | |||||
| } | } | ||||
| void CodeSetGlobalThreadPoolImplement(std::ofstream &ofs) { | void CodeSetGlobalThreadPoolImplement(std::ofstream &ofs) { | ||||
| ofs << "struct ThreadPool *g_thread_pool = NULL;\n" | |||||
| ofs << "struct ThreadPool *" << gThreadPool << " = NULL;\n" | |||||
| << "int " | << "int " | ||||
| << "SetThreadPool(struct ThreadPool *thread_pool) {\n" | << "SetThreadPool(struct ThreadPool *thread_pool) {\n" | ||||
| << " if (thread_pool == NULL) {\n" | << " if (thread_pool == NULL) {\n" | ||||
| " return RET_ERROR;\n" | " return RET_ERROR;\n" | ||||
| " }\n" | " }\n" | ||||
| " g_thread_pool = thread_pool;\n" | |||||
| << gThreadPool | |||||
| << " = thread_pool;\n" | |||||
| " return RET_OK;\n" | " return RET_OK;\n" | ||||
| "}\n"; | "}\n"; | ||||
| } | } | ||||
| @@ -35,6 +35,8 @@ void CodeWeightFileHeader(std::ofstream &ofs, const std::unique_ptr<CoderContext | |||||
| " RET_OK = 0,\n" | " RET_OK = 0,\n" | ||||
| " RET_ERROR = 1,\n" | " RET_ERROR = 1,\n" | ||||
| "};\n\n"; | "};\n\n"; | ||||
| // set a global var for thread_pool | |||||
| ofs << "static int" << gThreadNum << " = 1;\n"; | |||||
| } | } | ||||
| void CodeModelParamsState(std::ofstream &ofs, const std::map<std::string, Tensor *> &weights) { | void CodeModelParamsState(std::ofstream &ofs, const std::map<std::string, Tensor *> &weights) { | ||||
| @@ -100,7 +102,6 @@ void CodeWeightInitFunc(std::ofstream &ofs, const std::unique_ptr<CoderContext> | |||||
| << " if (weight_buffer == NULL) {\n" | << " if (weight_buffer == NULL) {\n" | ||||
| << " return RET_ERROR;\n" | << " return RET_ERROR;\n" | ||||
| << " }\n"; | << " }\n"; | ||||
| ofs << " int " << gThreadNum << " = 1;\n\n"; | |||||
| ofs << " struct ModelParameter {\n" | ofs << " struct ModelParameter {\n" | ||||
| << " void *addr;\n" | << " void *addr;\n" | ||||
| << " size_t size;\n" | << " size_t size;\n" | ||||
| @@ -30,6 +30,7 @@ | |||||
| #include "coder/generator/component/const_blocks/benchmark.h" | #include "coder/generator/component/const_blocks/benchmark.h" | ||||
| #include "coder/generator/component/const_blocks/license.h" | #include "coder/generator/component/const_blocks/license.h" | ||||
| #include "coder/log.h" | #include "coder/log.h" | ||||
| #include "coder/opcoders/parallel.h" | |||||
| namespace mindspore::lite::micro { | namespace mindspore::lite::micro { | ||||
| int WriteContentToFile(const std::string &file, const std::string &content) { | int WriteContentToFile(const std::string &file, const std::string &content) { | ||||
| @@ -62,9 +63,7 @@ void Generator::CodeNetRunFunc(std::ofstream &ofs) { | |||||
| // generate net inference code | // generate net inference code | ||||
| ofs << "void Inference() {\n"; | ofs << "void Inference() {\n"; | ||||
| if (config_->support_parallel()) { | 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()) { | for (const auto &block : ctx_->code_blocks()) { | ||||
| ofs << " {\n" << block << " }\n"; | ofs << " {\n" << block << " }\n"; | ||||
| @@ -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 | |||||
| @@ -19,22 +19,22 @@ | |||||
| namespace mindspore::lite::micro { | 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 | // ParallelLaunch is defined in thread_pool | ||||
| extern const char *kParallelLaunch; | |||||
| constexpr auto kParallelLaunch = "ParallelLaunch"; | |||||
| // g_thread_pool and g_thread_num are global variable, | // g_thread_pool and g_thread_num are global variable, | ||||
| // assign g_thread_pool by CreateThreadPool, | // assign g_thread_pool by CreateThreadPool, | ||||
| // and g_thread_num is equal to GetCurrentThreadNum | // 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 | // 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 | } // namespace mindspore::lite::micro | ||||