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

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