From 4524138acae9ce55d98ff0cd0417fb61a8d5652c Mon Sep 17 00:00:00 2001 From: "baolei.an" Date: Fri, 17 Apr 2020 11:09:57 +0800 Subject: [PATCH] [LITE][BM] multi thread ok, test=develop --- lite/api/test_classify_lite_bm.cc | 39 +++++++++++++++++++++-------- lite/kernels/bm/bridges/graph.cc | 5 ++++ lite/kernels/bm/bridges/graph.h | 3 +++ lite/kernels/bm/subgraph_compute.cc | 2 ++ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/lite/api/test_classify_lite_bm.cc b/lite/api/test_classify_lite_bm.cc index b2507e28ad..e3d6f4b844 100644 --- a/lite/api/test_classify_lite_bm.cc +++ b/lite/api/test_classify_lite_bm.cc @@ -14,6 +14,7 @@ #include #include +#include #include #include #include "lite/api/cxx_api.h" @@ -30,14 +31,18 @@ DEFINE_string(input_img_txt_path, namespace paddle { namespace lite { -void TestModel(const std::vector& valid_places) { +const int g_batch_size = 4; +const int g_thread_num = 10; + +void instance_run() { lite::Predictor predictor; std::vector passes; + std::vector valid_places({Place{TARGET(kBM), PRECISION(kFloat)}, + Place{TARGET(kX86), PRECISION(kFloat)}}); predictor.Build(FLAGS_model_dir, "", "", valid_places, passes); - auto* input_tensor = predictor.GetInput(0); - input_tensor->Resize(DDim( - std::vector({1, 3, FLAGS_im_height, FLAGS_im_width}))); + input_tensor->Resize(DDim(std::vector( + {g_batch_size, 3, FLAGS_im_height, FLAGS_im_width}))); auto* data = input_tensor->mutable_data(); auto item_size = input_tensor->dims().production(); if (FLAGS_input_img_txt_path.empty()) { @@ -45,12 +50,15 @@ void TestModel(const std::vector& valid_places) { data[i] = 1; } } else { - std::fstream fs(FLAGS_input_img_txt_path, std::ios::in); - if (!fs.is_open()) { - LOG(FATAL) << "open input_img_txt error."; - } - for (int i = 0; i < item_size; i++) { - fs >> data[i]; + for (int j = 0; j < g_batch_size; j++) { + std::fstream fs(FLAGS_input_img_txt_path, std::ios::in); + if (!fs.is_open()) { + LOG(FATAL) << "open input_img_txt error."; + } + for (int i = 0; i < item_size / g_batch_size; i++) { + fs >> data[i]; + } + data += j * item_size / g_batch_size; } } for (int i = 0; i < FLAGS_warmup; ++i) { @@ -72,6 +80,7 @@ void TestModel(const std::vector& valid_places) { FILE* fp = fopen("result.txt", "wb"); for (int i = 0; i < out.size(); i++) { auto* out_data = out[i]->data(); + LOG(INFO) << out[i]->numel(); for (int j = 0; j < out[i]->numel(); j++) { fprintf(fp, "%f\n", out_data[j]); } @@ -79,6 +88,16 @@ void TestModel(const std::vector& valid_places) { fclose(fp); } +void TestModel(const std::vector& valid_places) { + std::vector> instances_vec; + for (int i = 0; i < g_thread_num; ++i) { + instances_vec.emplace_back(new std::thread(&instance_run)); + } + for (int i = 0; i < g_thread_num; ++i) { + instances_vec[i]->join(); + } +} + TEST(Classify, test_bm) { std::vector valid_places({Place{TARGET(kBM), PRECISION(kFloat)}, Place{TARGET(kX86), PRECISION(kFloat)}}); diff --git a/lite/kernels/bm/bridges/graph.cc b/lite/kernels/bm/bridges/graph.cc index 32b10f5020..aeb810f028 100644 --- a/lite/kernels/bm/bridges/graph.cc +++ b/lite/kernels/bm/bridges/graph.cc @@ -20,11 +20,14 @@ namespace lite { namespace subgraph { namespace bm { +pthread_mutex_t Graph::mutex_compiler_ = PTHREAD_MUTEX_INITIALIZER; + void Graph::AddNode(const std::string& name) { nodes_.insert(std::make_pair(name, name)); } void Graph::CreateCompilerHandle() { + pthread_mutex_lock(&mutex_compiler_); #ifdef BM1682 compiler_handle_ = create_bmcompiler("BM1682"); #else @@ -33,6 +36,8 @@ void Graph::CreateCompilerHandle() { CHECK(compiler_handle_ != nullptr); } +void Graph::UnlockCompilerMutex() { pthread_mutex_unlock(&mutex_compiler_); } + } // namespace bm } // namespace subgraph } // namespace lite diff --git a/lite/kernels/bm/bridges/graph.h b/lite/kernels/bm/bridges/graph.h index 40dadcc92d..c54f4d7ad0 100644 --- a/lite/kernels/bm/bridges/graph.h +++ b/lite/kernels/bm/bridges/graph.h @@ -14,6 +14,7 @@ #pragma once +#include #include #include #include @@ -36,10 +37,12 @@ class Graph { } void CreateCompilerHandle(); void* GetCompilerHandle() { return compiler_handle_; } + void UnlockCompilerMutex(); private: std::unordered_map nodes_; void* compiler_handle_; + static pthread_mutex_t mutex_compiler_; }; } // namespace bm diff --git a/lite/kernels/bm/subgraph_compute.cc b/lite/kernels/bm/subgraph_compute.cc index c6059461d1..d7640e1ac7 100644 --- a/lite/kernels/bm/subgraph_compute.cc +++ b/lite/kernels/bm/subgraph_compute.cc @@ -40,6 +40,7 @@ int SubgraphEngine::BuildDeviceProgram() { op->CheckShape(); op->InferShape(); std::string op_type = op->op_info()->Type(); + LOG(INFO) << op_type; if (!bridges.Exists(op_type, TARGET(kBM))) { return subgraph::FAILED; } @@ -59,6 +60,7 @@ int SubgraphEngine::BuildDeviceProgram() { unsigned int data_size = 0; bm_hd_ = static_cast(ctx.GetHandle()); finish_bmcompiler_data(graph.GetCompilerHandle(), &bmodel_data, &data_size); + graph.UnlockCompilerMutex(); bmrt_hd_ = bmrt_create(bm_hd_); if (false == bmrt_load_bmodel_data(bmrt_hd_, bmodel_data, data_size)) { return subgraph::FAILED; -- GitLab