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_dispatch_checker.cpp 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * \file src/plugin/impl/cpu_dispatch_checker.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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 "megbrain/plugin/cpu_dispatch_checker.h"
  12. #include "megbrain/graph.h"
  13. #include "megbrain/graph/event.h"
  14. #include "megbrain/comp_node_env.h"
  15. using namespace mgb;
  16. CPUDispatchChecker::CPUDispatchChecker(cg::ComputingGraph *graph):
  17. PluginBase(graph)
  18. {
  19. auto on_exec_start = [this](const cg::event::OprExecKernelStart &event) {
  20. for (auto cn: cg::get_opr_comp_node_set(event.opr)) {
  21. if (cn.device_type() == CompNode::DeviceType::CPU) {
  22. auto callback = [this, cn]() {
  23. record(cn);
  24. };
  25. event.env->dispatch_on_comp_node(cn, callback);
  26. }
  27. }
  28. };
  29. auto on_exec_finish = [this](const cg::event::OprExecKernelEnd &event) {
  30. for (auto cn: cg::get_opr_comp_node_set(event.opr)) {
  31. if (cn.device_type() == CompNode::DeviceType::CPU) {
  32. auto callback = [this, cn, opr=event.opr]() {
  33. check(cn, opr);
  34. };
  35. event.env->dispatch_on_comp_node(cn, callback);
  36. }
  37. }
  38. };
  39. auto on_subgraph_associated = [this](
  40. const cg::event::SubgraphAssociated &event) {
  41. mgb_assert(event.par_graph == m_owner_graph);
  42. auto sub = std::make_unique<CPUDispatchChecker>(event.sub_graph);
  43. sub->m_failed_oprs = m_failed_oprs;
  44. sub->m_failed_oprs_mtx = m_failed_oprs_mtx;
  45. m_sub_graph_checkers.emplace_back(std::move(sub));
  46. };
  47. add_event_handler(graph->event().register_receiver<
  48. cg::event::OprExecKernelStart>(on_exec_start));
  49. add_event_handler(graph->event().register_receiver<
  50. cg::event::OprExecKernelEnd>(on_exec_finish));
  51. add_event_handler(graph->event().register_receiver<
  52. cg::event::SubgraphAssociated>(on_subgraph_associated));
  53. }
  54. void CPUDispatchChecker::record(CompNode cn) {
  55. auto num = CompNodeEnv::from_comp_node(
  56. cn).cpu_env().dispatcher->get_nr_dispatched_tasks();
  57. MGB_LOCK_GUARD(m_cn2nr_task_mtx);
  58. m_cn2nr_task[cn] = num;
  59. }
  60. void CPUDispatchChecker::check(CompNode cn, cg::OperatorNodeBase *opr) {
  61. size_t prev, now;
  62. {
  63. MGB_LOCK_GUARD(m_cn2nr_task_mtx);
  64. prev = m_cn2nr_task.at(cn);
  65. }
  66. now = CompNodeEnv::from_comp_node(
  67. cn).cpu_env().dispatcher->get_nr_dispatched_tasks();
  68. if (prev == now) {
  69. fprintf(stderr, "operator %s{%s} does not dispatch kernel on %s\n",
  70. opr->cname(), opr->dyn_typeinfo()->name,
  71. cn.to_string().c_str());
  72. {
  73. MGB_LOCK_GUARD(*m_failed_oprs_mtx);
  74. m_failed_oprs->insert(opr);
  75. }
  76. }
  77. }
  78. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台

Contributors (1)