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

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