| 270 | |
| 271 | namespace { |
| 272 | void test_multi_thread(bool multi_thread_compnode) { |
| 273 | Config config; |
| 274 | auto lite_tensor = get_input_data("./input_data.npy"); |
| 275 | std::string model_path = "./shufflenet.mge"; |
| 276 | |
| 277 | size_t nr_threads = 2; |
| 278 | std::vector<size_t> thread_ids_user(nr_threads); |
| 279 | std::vector<size_t> thread_ids_worker(nr_threads); |
| 280 | auto runner = [&](size_t i) { |
| 281 | thread_ids_user[i] = std::hash<std::thread::id>{}(std::this_thread::get_id()); |
| 282 | std::shared_ptr<Network> network = std::make_shared<Network>(config); |
| 283 | Runtime::set_cpu_inplace_mode(network); |
| 284 | if (multi_thread_compnode) { |
| 285 | Runtime::set_cpu_threads_number(network, 2); |
| 286 | } |
| 287 | |
| 288 | network->load_model(model_path); |
| 289 | Runtime::set_runtime_thread_affinity( |
| 290 | network, [&multi_thread_compnode, &thread_ids_worker, i](int id) { |
| 291 | if (multi_thread_compnode) { |
| 292 | if (id == 1) { |
| 293 | thread_ids_worker[i] = std::hash<std::thread::id>{}( |
| 294 | std::this_thread::get_id()); |
| 295 | } |
| 296 | } else { |
| 297 | thread_ids_worker[i] = std::hash<std::thread::id>{}( |
| 298 | std::this_thread::get_id()); |
| 299 | } |
| 300 | }); |
| 301 | |
| 302 | std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0); |
| 303 | auto src_ptr = lite_tensor->get_memory_ptr(); |
| 304 | auto src_layout = lite_tensor->get_layout(); |
| 305 | input_tensor->reset(src_ptr, src_layout); |
| 306 | |
| 307 | network->forward(); |
| 308 | network->wait(); |
| 309 | std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0); |
| 310 | }; |
| 311 | std::vector<std::thread> threads; |
| 312 | for (size_t i = 0; i < nr_threads; i++) { |
| 313 | threads.emplace_back(runner, i); |
| 314 | threads[i].join(); |
| 315 | } |
| 316 | for (size_t i = 0; i < nr_threads; i++) { |
| 317 | ASSERT_EQ(thread_ids_user[i], thread_ids_worker[i]); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | } // namespace |
| 322 |
no test coverage detected