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.

graph_rt.h 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * \file imperative/python/src/graph_rt.h
  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. #pragma once
  12. #include "./helper.h"
  13. #include <memory>
  14. #include <mutex>
  15. #include <future>
  16. #include "megbrain/plugin/opr_footprint.h"
  17. #include "megbrain/graph.h"
  18. template<typename T>
  19. class GraphNodePtr {
  20. std::shared_ptr<mgb::cg::ComputingGraph> m_graph;
  21. T* m_node;
  22. public:
  23. GraphNodePtr(T* node) :
  24. m_graph(node ? node->owner_graph()->shared_from_this() : nullptr),
  25. m_node(node) {}
  26. T* operator->() {return m_node;}
  27. T& operator*() {return *m_node;}
  28. operator bool() {return m_node;}
  29. T* get() {return m_node;}
  30. };
  31. PYBIND11_DECLARE_HOLDER_TYPE(T, GraphNodePtr<T>, true);
  32. template<typename R>
  33. class Rendezvous {
  34. std::mutex m_lock;
  35. int m_read_ahead = 0;
  36. bool m_drop_next = false;
  37. std::promise<R> m_promise;
  38. public:
  39. Rendezvous() = default;
  40. Rendezvous(const Rendezvous& rhs) = delete;
  41. Rendezvous(Rendezvous&& rhs) = delete;
  42. Rendezvous& operator=(const Rendezvous& rhs) = delete;
  43. R get() {
  44. std::future<R> f;
  45. {
  46. MGB_LOCK_GUARD(m_lock);
  47. mgb_assert(m_read_ahead <= 0);
  48. mgb_assert(m_read_ahead >= -1);
  49. f = m_promise.get_future();
  50. if (m_read_ahead == -1) {
  51. m_promise = {};
  52. }
  53. ++m_read_ahead;
  54. }
  55. return f.get();
  56. }
  57. void drop() {
  58. MGB_LOCK_GUARD(m_lock);
  59. mgb_assert(m_read_ahead <= 0);
  60. mgb_assert(m_read_ahead >= -1);
  61. if (m_read_ahead == -1) {
  62. m_promise = {};
  63. } else {
  64. m_drop_next = true;
  65. }
  66. ++m_read_ahead;
  67. }
  68. template<typename T>
  69. void set(T&& value) {
  70. MGB_LOCK_GUARD(m_lock);
  71. mgb_assert(m_read_ahead >= 0);
  72. mgb_assert(m_read_ahead <= 1);
  73. if (m_drop_next) {
  74. m_drop_next = false;
  75. } else {
  76. m_promise.set_value(std::forward<T>(value));
  77. }
  78. if (m_read_ahead == 1) {
  79. m_promise = {};
  80. }
  81. --m_read_ahead;
  82. }
  83. void reset() {
  84. MGB_LOCK_GUARD(m_lock);
  85. m_promise = {};
  86. m_read_ahead = 0;
  87. m_drop_next = false;
  88. }
  89. };
  90. void init_graph_rt(pybind11::module m);

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