mlu_context.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. /**
  21. * @file mlu_context.h
  22. *
  23. * This file contains a declaration of the MluContext class.
  24. */
  25. #ifndef EDK_MLU_CONTEXT_H_
  26. #define EDK_MLU_CONTEXT_H_
  27. #include <functional>
  28. #include <memory>
  29. #include <utility>
  30. #include "cxxutil/edk_attribute.h"
  31. #include "cxxutil/exception.h"
  32. namespace edk {
  33. /**
  34. * @brief Enumeration to describe MLU core version
  35. */
  36. enum class CoreVersion {
  37. MLU220 = 1, ///< MLU220 platform
  38. MLU270 = 2, ///< MLU270 platform
  39. };
  40. struct MluTaskQueuePrivate;
  41. class MluTaskQueueProxy;
  42. /**
  43. * @brief encapsulation of cnrtQueue
  44. */
  45. struct MluTaskQueue {
  46. public:
  47. /**
  48. * @brief Create a MluTaskQueue
  49. *
  50. * @return a MluTaskQueue_t
  51. */
  52. static std::shared_ptr<MluTaskQueue> Create();
  53. /**
  54. * @brief Sync MluTaskQueue
  55. */
  56. void Sync();
  57. /**
  58. * @brief TimeMark holder class
  59. *
  60. * @note Mark maps to device notifier resources, store Mark without release will cause increase of resources,
  61. * an exception will be thrown while number of Marks reaches limit
  62. */
  63. class Mark {
  64. public:
  65. /**
  66. * @brief Construct a new Mark object
  67. *
  68. * @param release release function
  69. * @param idx index of TimeMark
  70. */
  71. Mark(std::function<void(int)> release, int idx) : release_(release), idx_(idx) {}
  72. /**
  73. * @brief Destroy the Mark object
  74. */
  75. ~Mark() {
  76. if (release_) release_(idx_);
  77. }
  78. /**
  79. * @brief Move construct a new Mark object
  80. *
  81. * @param other another Mark in right value reference
  82. */
  83. Mark(Mark&& other) : release_(std::move(other.release_)), idx_(other.idx_) { other.release_ = nullptr; }
  84. /**
  85. * @brief Move assign the Mark object
  86. *
  87. * @param other another Mark in right value reference
  88. * @return Mark& reference to this object
  89. */
  90. Mark& operator=(Mark&& other) {
  91. release_ = std::move(other.release_);
  92. idx_ = other.idx_;
  93. other.release_ = nullptr;
  94. return *this;
  95. }
  96. /**
  97. * @brief Get TimeMark index
  98. *
  99. * @return int Mapped TimeMark index
  100. */
  101. int Index() const noexcept { return idx_; }
  102. private:
  103. Mark() = delete;
  104. Mark(const Mark&) = delete;
  105. Mark& operator=(const Mark&) = delete;
  106. std::function<void(int)> release_{nullptr};
  107. int idx_{0};
  108. };
  109. /**
  110. * @brief Place a mark to measure hardware time of task between two mark
  111. *
  112. * @return Mark a Mark
  113. */
  114. Mark PlaceMark();
  115. /**
  116. * @brief Calculate hardware time between two mark
  117. *
  118. * @param start Mark before task
  119. * @param end Mark after task
  120. * @return float hardware time in milliseconds
  121. */
  122. float Count(const Mark& start, const Mark& end) const;
  123. private:
  124. struct _PrivDelete {
  125. void operator()(MluTaskQueuePrivate* p);
  126. };
  127. friend class MluTaskQueueProxy;
  128. MluTaskQueue();
  129. std::unique_ptr<MluTaskQueuePrivate, _PrivDelete> priv_{nullptr};
  130. };
  131. /**
  132. * @brief convience alias of shared pointer to MluTaskQueue
  133. */
  134. using MluTaskQueue_t = std::shared_ptr<MluTaskQueue>;
  135. /**
  136. * @brief MLU environment helper class
  137. */
  138. class MluContext {
  139. public:
  140. /**
  141. * @brief Construct a new Mlu Context object
  142. */
  143. MluContext() = default;
  144. /**
  145. * @brief Construct a new Mlu Context object
  146. *
  147. * @param dev_id Device id
  148. */
  149. explicit MluContext(int dev_id) : dev_id_(dev_id) {}
  150. /**
  151. * @brief Get the device id
  152. *
  153. * @return Device id
  154. */
  155. inline int DeviceId() const { return dev_id_; }
  156. /**
  157. * @brief Set the device id
  158. *
  159. * @param id Device id
  160. */
  161. inline void SetDeviceId(int id) { dev_id_ = id; }
  162. /**
  163. * @brief Get available device number
  164. *
  165. * @return Available device number
  166. */
  167. static uint32_t GetDeviceNum();
  168. /**
  169. * @brief Check whether device exists
  170. *
  171. * @param id Device id
  172. * @return true if device exists, otherwise false returned
  173. */
  174. static bool CheckDeviceId(int id);
  175. /**
  176. * @brief Get the MLU channel id
  177. * @deprecated
  178. *
  179. * @return MLU Channel id
  180. */
  181. attribute_deprecated inline int ChannelId() const { return channel_id_; }
  182. /**
  183. * @brief Set the MLU channel id in range [0, 3]
  184. * @deprecated
  185. *
  186. * @param id MLU channel id
  187. */
  188. attribute_deprecated inline void SetChannelId(int id) { channel_id_ = id; }
  189. /**
  190. * @brief Bind MLU device
  191. * @note Any process on MLU need to bind MLU device
  192. */
  193. void BindDevice();
  194. /**
  195. * @brief Get MLU core version
  196. *
  197. * @return MLU core version
  198. */
  199. CoreVersion GetCoreVersion();
  200. private:
  201. int dev_id_ = 0;
  202. int channel_id_ = -1;
  203. }; // class MluContext
  204. } // namespace edk
  205. #endif // EDK_MLU_CONTEXT_H_