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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. std::promise<R> m_promise;
  36. public:
  37. Rendezvous() = default;
  38. Rendezvous(const Rendezvous& rhs) = delete;
  39. Rendezvous(Rendezvous&& rhs) = default;
  40. Rendezvous& operator=(const Rendezvous& rhs) = delete;
  41. Rendezvous& operator=(Rendezvous&& rhs) {
  42. MGB_LOCK_GUARD(m_lock);
  43. m_read_ahead = rhs.m_read_ahead;
  44. m_promise = std::move(rhs.m_promise);
  45. return *this;
  46. }
  47. R get() {
  48. std::future<R> f;
  49. {
  50. MGB_LOCK_GUARD(m_lock);
  51. mgb_assert(m_read_ahead <= 0);
  52. mgb_assert(m_read_ahead >= -1);
  53. f = m_promise.get_future();
  54. if (m_read_ahead == -1) {
  55. m_promise = {};
  56. }
  57. ++m_read_ahead;
  58. }
  59. return f.get();
  60. }
  61. template<typename T>
  62. void set(T&& value) {
  63. MGB_LOCK_GUARD(m_lock);
  64. mgb_assert(m_read_ahead >= 0);
  65. mgb_assert(m_read_ahead <= 1);
  66. m_promise.set_value(std::forward<T>(value));
  67. if (m_read_ahead == 1) {
  68. m_promise = {};
  69. }
  70. --m_read_ahead;
  71. }
  72. void reset() {
  73. MGB_LOCK_GUARD(m_lock);
  74. m_promise = {};
  75. m_read_ahead = 0;
  76. }
  77. };
  78. void init_graph_rt(pybind11::module m);

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