| 6 | |
| 7 | namespace { |
| 8 | bool cpu_affinity(const Args& args) { |
| 9 | std::string network_path = args.model_path; |
| 10 | std::string input_path = args.input_path; |
| 11 | |
| 12 | //! create and load the network |
| 13 | std::shared_ptr<Network> network = std::make_shared<Network>(); |
| 14 | |
| 15 | //! run with multi theads |
| 16 | Runtime::set_cpu_threads_number(network, 4); |
| 17 | |
| 18 | network->load_model(network_path); |
| 19 | |
| 20 | std::vector<int> core_ids = {0, 1, 2, 3}; |
| 21 | auto affinity = [core_ids](int id) { |
| 22 | //! add user define affinity function |
| 23 | set_cpu_affinity({core_ids[id]}); |
| 24 | printf("set thread id = %d with the affinity of core %d.\n", id, core_ids[id]); |
| 25 | }; |
| 26 | Runtime::set_runtime_thread_affinity(network, affinity); |
| 27 | |
| 28 | //! set input data to input tensor |
| 29 | std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0); |
| 30 | //! copy or forward data to network |
| 31 | size_t length = input_tensor->get_tensor_total_size_in_byte(); |
| 32 | void* dst_ptr = input_tensor->get_memory_ptr(); |
| 33 | auto src_tensor = parse_npy(input_path); |
| 34 | void* src = src_tensor->get_memory_ptr(); |
| 35 | memcpy(dst_ptr, src, length); |
| 36 | |
| 37 | //! forward |
| 38 | network->forward(); |
| 39 | network->wait(); |
| 40 | |
| 41 | //! get the output data or read tensor set in network_in |
| 42 | std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0); |
| 43 | void* out_data = output_tensor->get_memory_ptr(); |
| 44 | size_t out_length = output_tensor->get_tensor_total_size_in_byte() / |
| 45 | output_tensor->get_layout().get_elem_size(); |
| 46 | printf("length=%zu\n", length); |
| 47 | float max = -1.0f; |
| 48 | float sum = 0.0f; |
| 49 | for (size_t i = 0; i < out_length; i++) { |
| 50 | float data = static_cast<float*>(out_data)[i]; |
| 51 | sum += data; |
| 52 | if (max < data) |
| 53 | max = data; |
| 54 | } |
| 55 | printf("max=%e, sum=%e\n", max, sum); |
| 56 | return true; |
| 57 | } |
| 58 | } // namespace |
| 59 | |
| 60 | REGIST_EXAMPLE("cpu_affinity", cpu_affinity); |
nothing calls this directly
no test coverage detected