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.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * \file example/cpp_example/cpu_affinity.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "example.h"
  12. #if LITE_BUILD_WITH_MGE
  13. using namespace lite;
  14. using namespace example;
  15. namespace {
  16. bool cpu_affinity(const Args& args) {
  17. std::string network_path = args.model_path;
  18. std::string input_path = args.input_path;
  19. //! create and load the network
  20. std::shared_ptr<Network> network = std::make_shared<Network>();
  21. //! run with multi theads
  22. Runtime::set_cpu_threads_number(network, 4);
  23. network->load_model(network_path);
  24. std::vector<int> core_ids = {0, 1, 2, 3};
  25. auto affinity = [core_ids](int id) {
  26. //! add user define affinity function
  27. set_cpu_affinity({core_ids[id]});
  28. printf("set thread id = %d with the affinity of core %d.\n", id, core_ids[id]);
  29. };
  30. Runtime::set_runtime_thread_affinity(network, affinity);
  31. //! set input data to input tensor
  32. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  33. //! copy or forward data to network
  34. size_t length = input_tensor->get_tensor_total_size_in_byte();
  35. void* dst_ptr = input_tensor->get_memory_ptr();
  36. auto src_tensor = parse_npy(input_path);
  37. void* src = src_tensor->get_memory_ptr();
  38. memcpy(dst_ptr, src, length);
  39. //! forward
  40. network->forward();
  41. network->wait();
  42. //! get the output data or read tensor set in network_in
  43. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  44. void* out_data = output_tensor->get_memory_ptr();
  45. size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
  46. output_tensor->get_layout().get_elem_size();
  47. printf("length=%zu\n", length);
  48. float max = -1.0f;
  49. float sum = 0.0f;
  50. for (size_t i = 0; i < out_length; i++) {
  51. float data = static_cast<float*>(out_data)[i];
  52. sum += data;
  53. if (max < data)
  54. max = data;
  55. }
  56. printf("max=%e, sum=%e\n", max, sum);
  57. return true;
  58. }
  59. } // namespace
  60. REGIST_EXAMPLE("cpu_affinity", cpu_affinity);
  61. #endif
  62. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}