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.

handle.h 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #include "megcore.h"
  3. #include "megdnn/basic_types.h"
  4. #include "megdnn/config/config.h"
  5. #include <functional>
  6. #include <memory>
  7. #include "megdnn/internal/visibility_prologue.h"
  8. namespace megdnn {
  9. class OperatorBase;
  10. class Handle {
  11. public:
  12. enum class HandleType {
  13. NAIVE = 0,
  14. FALLBACK = 1,
  15. X86 = 2,
  16. ARM_COMMON = 3,
  17. ARMV7 = 4,
  18. AARCH64 = 5,
  19. CUDA = 6,
  20. ROCM = 11,
  21. ATLAS = 13,
  22. CAMBRICON = 12,
  23. };
  24. static std::string handle_type_name(HandleType handle) {
  25. #define INSTANCE_HANDLE(name) \
  26. case HandleType::name: \
  27. return #name
  28. switch (handle) {
  29. INSTANCE_HANDLE(NAIVE);
  30. INSTANCE_HANDLE(FALLBACK);
  31. INSTANCE_HANDLE(X86);
  32. INSTANCE_HANDLE(ARM_COMMON);
  33. INSTANCE_HANDLE(ARMV7);
  34. INSTANCE_HANDLE(AARCH64);
  35. INSTANCE_HANDLE(CUDA);
  36. INSTANCE_HANDLE(ROCM);
  37. INSTANCE_HANDLE(ATLAS);
  38. INSTANCE_HANDLE(CAMBRICON);
  39. default:
  40. return "Unknown";
  41. }
  42. #undef INSTANCE_HANDLE
  43. }
  44. //! Device vendor
  45. enum class HandleVendorType : uint32_t {
  46. NOT_SPEC = 0,
  47. MALI = 1,
  48. ADRENO = 2,
  49. CUDA = 3,
  50. INTEL = 4,
  51. POWERVR = 5,
  52. AMD = 6,
  53. };
  54. protected:
  55. Handle(megcoreComputingHandle_t computing_handle, HandleType type);
  56. public:
  57. /**
  58. * \brief Create a MegDNN handle from a MegCore Computing handle.
  59. *
  60. * \param[in] computing_handle MegCore computing handle. Please note
  61. * that computing_handle would not be released when this Handle is
  62. * destructed
  63. * \param[in] debug_level
  64. * Applicable for CPU computing handle.
  65. * 0 means taking the fastest possible code path; it may contains
  66. * platform-specific instructions such as SSE for x86_64 or NEON for
  67. * armv7v7.
  68. * 1 means taking the fastest possible code path without
  69. * platform-specific instructions in C++ code. Note that the compiled
  70. * binary file still contains platform-specific codes.
  71. * 2 means taking the naive code path. Performance is severely
  72. * hampered, but it is less error-prone since the internal
  73. * implementation is rather straightforward.
  74. *
  75. * **Debug level 1 and 2 should not be used in productions.**
  76. */
  77. MGE_WIN_DECLSPEC_FUC static std::unique_ptr<Handle> make(
  78. megcoreComputingHandle_t computing_handle, int debug_level = 0);
  79. #if MEGDNN_WITH_CUDA
  80. MGE_WIN_DECLSPEC_FUC static std::unique_ptr<Handle> make_cuda_handle(
  81. megcoreComputingHandle_t computing_handle);
  82. template <typename opr>
  83. MGE_WIN_DECLSPEC_FUC std::unique_ptr<opr> create_cuda_operator();
  84. #endif
  85. #if MEGDNN_WITH_ROCM
  86. MGE_WIN_DECLSPEC_FUC static std::unique_ptr<Handle> make_rocm_handle(
  87. megcoreComputingHandle_t computing_handle);
  88. template <typename opr>
  89. MGE_WIN_DECLSPEC_FUC std::unique_ptr<opr> create_rocm_operator();
  90. #endif
  91. virtual ~Handle();
  92. /*!
  93. * \brief Get the underlying megcore computing handle.
  94. */
  95. megcoreComputingHandle_t megcore_computing_handle() const {
  96. return m_computing_handle;
  97. }
  98. /*!
  99. * \brief set a callback function to be invoked when this handle is
  100. * destructed, so associated resources can be released (e.g.
  101. * computing handle)
  102. *
  103. * This function can be called at most once.
  104. */
  105. MGE_WIN_DECLSPEC_FUC void set_destructor(const thin_function<void()>& d);
  106. /*!
  107. * \brief set a callback to be invoked when an operator is destructed
  108. * \param[in,out] cb the callback function; it would be set to the
  109. * previous callback function
  110. */
  111. void set_opr_destruct_callback(thin_function<void(OperatorBase*)>& cb) {
  112. cb.swap(m_on_opr_destructed);
  113. }
  114. MGE_WIN_DECLSPEC_FUC void on_opr_destructed(OperatorBase* opr);
  115. /**
  116. * \brief Create operator of Opr type.
  117. */
  118. template <typename Opr>
  119. MGE_WIN_DECLSPEC_FUC std::unique_ptr<Opr> create_operator();
  120. /*
  121. * =============================================================
  122. * Users should call functions below to query memory requirement.
  123. * =============================================================
  124. */
  125. /**
  126. * \brief The internal data pointer of TensorND should be aligned to
  127. * alignment_requirement() in bytes.
  128. */
  129. MGE_WIN_DECLSPEC_FUC virtual size_t alignment_requirement() const;
  130. //! get alignment in bytes for rows of image 2D tensor format
  131. MGE_WIN_DECLSPEC_FUC virtual size_t image2d_pitch_alignment() const;
  132. //! get vendor type
  133. MGE_WIN_DECLSPEC_FUC virtual HandleVendorType vendor_type() const;
  134. HandleType type() const { return m_handle_type; }
  135. /**
  136. * \brief Check is the layout satisfy cross device copy constraint.
  137. * 1. The handle of the src and the dst is the same kind
  138. * 2. The dst is continguous.
  139. */
  140. MGE_WIN_DECLSPEC_FUC virtual bool check_cross_dev_copy_constraint(
  141. const TensorLayout& src);
  142. private:
  143. static constexpr uint32_t ALIVE_MAGIC = 0x8595e9d2u;
  144. volatile uint32_t m_alive_magic = ALIVE_MAGIC;
  145. megcoreComputingHandle_t m_computing_handle;
  146. const HandleType m_handle_type;
  147. thin_function<void()> m_destructor;
  148. thin_function<void(OperatorBase*)> m_on_opr_destructed;
  149. Handle() = delete;
  150. Handle(const Handle& rhs) = delete;
  151. Handle& operator=(const Handle& rhs) = delete;
  152. };
  153. } // namespace megdnn
  154. #include "megdnn/internal/visibility_epilogue.h"
  155. // vim: syntax=cpp.doxygen