You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpu_affinity.cpp 2.0 kB

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