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

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