buffer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 buffer.h
  22. *
  23. * This file contains a declaration of the Buffer class.
  24. */
  25. #ifndef INFER_SERVER_BUFFER_H_
  26. #define INFER_SERVER_BUFFER_H_
  27. #include <atomic>
  28. #include <condition_variable>
  29. #include <functional>
  30. #include <memory>
  31. #include <mutex>
  32. #include <queue>
  33. namespace infer_server {
  34. /**
  35. * @brief Enumerator of memory type
  36. */
  37. enum class MemoryType {
  38. CPU = 0, ///< memory on CPU
  39. MLU = 1, ///< memory on MLU
  40. };
  41. namespace detail {
  42. struct Memory;
  43. } // namespace detail
  44. class Buffer {
  45. public:
  46. /// callback function to deallocate memory
  47. using MemoryDeallocator = std::function<void(void *memory, int device_id)>;
  48. /**
  49. * @brief Construct a new Buffer object contained CPU memory
  50. *
  51. * @param memory_size Memory size in bytes
  52. */
  53. explicit Buffer(size_t memory_size);
  54. /**
  55. * @brief Construct a new Buffer object contained MLU memory
  56. *
  57. * @param memory_size Memory size in bytes
  58. * @param device_id memory on which device
  59. */
  60. explicit Buffer(size_t memory_size, int device_id);
  61. /**
  62. * @brief Construct a new Buffer object with raw MLU memory
  63. *
  64. * @param mlu_memory raw pointer
  65. * @param memory_size Memory size in bytes
  66. * @param d A function to handle memory when destruct
  67. * @param device_id memory on which device
  68. */
  69. Buffer(void *mlu_memory, size_t memory_size, MemoryDeallocator d, int device_id);
  70. /**
  71. * @brief Construct a new Buffer object with raw CPU memory
  72. *
  73. * @param cpu_memory raw pointer
  74. * @param memory_size Memory size in bytes
  75. * @param d A function to handle memory when destruct
  76. */
  77. Buffer(void *cpu_memory, size_t memory_size, MemoryDeallocator d);
  78. /**
  79. * @brief default constructor
  80. *
  81. * @warning generated Buffer cannot be used until assigned
  82. */
  83. Buffer() = default;
  84. /**
  85. * @brief default copy constructor (shallow)
  86. */
  87. Buffer(const Buffer &another) = default;
  88. /**
  89. * @brief default copy assign (shallow)
  90. */
  91. Buffer &operator=(const Buffer &another) = default;
  92. /**
  93. * @brief default move construct
  94. */
  95. Buffer(Buffer &&another) = default;
  96. /**
  97. * @brief default move assign
  98. */
  99. Buffer &operator=(Buffer &&another) = default;
  100. /**
  101. * @brief Get a shallow copy of buffer by offset
  102. *
  103. * @param offset offset
  104. * @return copied buffer
  105. */
  106. Buffer operator()(size_t offset) const;
  107. /**
  108. * @brief Get mutable raw pointer
  109. *
  110. * @return raw pointer
  111. */
  112. void *MutableData();
  113. /**
  114. * @brief Get const raw pointer
  115. *
  116. * @return raw pointer
  117. */
  118. const void *Data() const;
  119. /**
  120. * @brief Get size of MLU memory
  121. *
  122. * @return memory size in bytes
  123. */
  124. size_t MemorySize() const noexcept { return memory_size_ - offset_; }
  125. /**
  126. * @brief Get device id
  127. *
  128. * @return device id
  129. */
  130. int DeviceId() const noexcept;
  131. /**
  132. * @brief Get memory type
  133. *
  134. * @return memory type
  135. */
  136. MemoryType Type() const noexcept { return type_; }
  137. /**
  138. * @brief Query whether memory is on MLU
  139. *
  140. * @retval true memory on MLU
  141. * @retval false memory on CPU
  142. */
  143. bool OnMlu() const noexcept { return type_ == MemoryType::MLU; }
  144. /**
  145. * @brief query whether Buffer own memory
  146. *
  147. * @retval true own memory
  148. * @retval false not own memory
  149. */
  150. bool OwnMemory() const noexcept;
  151. /**
  152. * @brief Copy data from raw CPU memory
  153. *
  154. * @param cpu_src Copy source, data on CPU
  155. * @param copy_size Memory size in bytes
  156. */
  157. void CopyFrom(void *cpu_src, size_t copy_size);
  158. /**
  159. * @brief Copy data from another buffer
  160. *
  161. * @param src Copy source
  162. * @param copy_size Memory size in bytes
  163. */
  164. void CopyFrom(const Buffer &src, size_t copy_size);
  165. /**
  166. * @brief Copy data to raw CPU memory
  167. *
  168. * @param cpu_dst Copy destination, memory on CPU
  169. * @param copy_size Memory size in bytes
  170. */
  171. void CopyTo(void *cpu_dst, size_t copy_size) const;
  172. /**
  173. * @brief Copy data to another buffer
  174. *
  175. * @param dst Copy source
  176. * @param copy_size Memory size in bytes
  177. */
  178. void CopyTo(Buffer *dst, size_t copy_size) const;
  179. private:
  180. void LazyMalloc();
  181. std::shared_ptr<detail::Memory> data_{nullptr};
  182. size_t memory_size_{0};
  183. size_t offset_{0};
  184. MemoryType type_{MemoryType::CPU};
  185. };
  186. /**
  187. * @brief MluMemoryPool is a MLU memory helper class.
  188. *
  189. * @note It provides a easy way to manage memory on MLU.
  190. */
  191. class MluMemoryPool {
  192. public:
  193. /**
  194. * @brief Construct a new Mlu Memory Pool object
  195. *
  196. * @param memory_size Memory size in bytes
  197. * @param max_buffer_num max number of memory cached in pool
  198. * @param device_id memory on which device
  199. */
  200. MluMemoryPool(size_t memory_size, size_t max_buffer_num, int device_id = 0);
  201. /**
  202. * @brief A destructor
  203. * @note wait until all MluMemory requested is released
  204. */
  205. ~MluMemoryPool();
  206. /**
  207. * @brief Request Buffer from pool, wait for timeout_ms if pool is empty
  208. *
  209. * @param timeout_ms wait timeout in milliseconds
  210. * @return a Buffer
  211. */
  212. Buffer Request(int timeout_ms = -1);
  213. /**
  214. * @brief Get size of MLU memory
  215. *
  216. * @return memory size in bytes
  217. */
  218. size_t MemorySize() const noexcept { return memory_size_; }
  219. /**
  220. * @brief Get how many pieces of MLU memory cached
  221. *
  222. * @return number of memory cached
  223. */
  224. size_t BufferNum() const noexcept { return buffer_num_; }
  225. /**
  226. * @brief Get device id
  227. *
  228. * @return device id
  229. */
  230. int DeviceId() const noexcept { return device_id_; }
  231. private:
  232. std::queue<void *> cache_;
  233. std::mutex q_mutex_;
  234. std::condition_variable empty_cond_;
  235. size_t memory_size_;
  236. size_t max_buffer_num_;
  237. size_t buffer_num_;
  238. int device_id_;
  239. std::atomic<bool> running_{false};
  240. };
  241. } // namespace infer_server
  242. #endif // INFER_SERVER_BUFFER_H_