mlu_task_queue.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef EASYINFER_MLU_TASK_QUEUE_H_
  21. #define EASYINFER_MLU_TASK_QUEUE_H_
  22. #include <cnrt.h>
  23. #include <map>
  24. #include <memory>
  25. #include <string>
  26. #include <utility>
  27. #include <vector>
  28. #include "device/mlu_context.h"
  29. namespace edk {
  30. #define CALL_CNRT_FUNC(func, msg) \
  31. do { \
  32. cnrtRet_t ret = (func); \
  33. if (CNRT_RET_SUCCESS != ret) { \
  34. THROW_EXCEPTION(Exception::INTERNAL, std::string(msg) + ", cnrt error code : " + std::to_string(ret)); \
  35. } \
  36. } while (0)
  37. class TimeMark {
  38. public:
  39. TimeMark() { CALL_CNRT_FUNC(cnrtCreateNotifier(&base_), "Create notifier failed"); }
  40. TimeMark(TimeMark&& other) : base_(other.base_) { other.base_ = nullptr; }
  41. TimeMark& operator=(TimeMark&& other) {
  42. base_ = other.base_;
  43. other.base_ = nullptr;
  44. return *this;
  45. }
  46. ~TimeMark() {
  47. if (nullptr != base_) cnrtDestroyNotifier(&base_);
  48. }
  49. void Mark(cnrtQueue_t queue) { CALL_CNRT_FUNC(cnrtPlaceNotifier(base_, queue), "cnrtPlaceNotifier failed"); }
  50. void Mark(MluTaskQueue_t queue);
  51. cnrtNotifier_t GetNotifier() noexcept { return base_; }
  52. // get hardware time in ms
  53. static float Count(const TimeMark& start, const TimeMark& end) {
  54. float dura;
  55. CALL_CNRT_FUNC(cnrtNotifierDuration(start.base_, end.base_, &dura), "Calculate elapsed time failed.");
  56. dura /= 1000;
  57. return dura;
  58. }
  59. private:
  60. TimeMark(const TimeMark&) = delete;
  61. TimeMark& operator=(const TimeMark&) = delete;
  62. cnrtNotifier_t base_{nullptr};
  63. };
  64. struct MluTaskQueuePrivate {
  65. ~MluTaskQueuePrivate();
  66. cnrtQueue_t queue = nullptr;
  67. std::vector<TimeMark> marks;
  68. std::vector<bool> marks_valid;
  69. };
  70. inline void MluTaskQueue::_PrivDelete::operator()(MluTaskQueuePrivate* p) { delete p; }
  71. class MluTaskQueueProxy {
  72. public:
  73. static cnrtQueue_t GetCnrtQueue(MluTaskQueue_t q) noexcept { return q->priv_->queue; }
  74. static void SetCnrtQueue(MluTaskQueue_t q, cnrtQueue_t cnrt_q) {
  75. if (q->priv_->queue) {
  76. q->priv_.reset(new MluTaskQueuePrivate);
  77. }
  78. q->priv_->queue = cnrt_q;
  79. }
  80. static MluTaskQueue_t Wrap(cnrtQueue_t cnrt_q) {
  81. auto q = std::shared_ptr<MluTaskQueue>(new MluTaskQueue);
  82. q->priv_->queue = cnrt_q;
  83. return q;
  84. }
  85. };
  86. inline void TimeMark::Mark(MluTaskQueue_t queue) { Mark(MluTaskQueueProxy::GetCnrtQueue(std::move(queue))); }
  87. } // namespace edk
  88. #endif // EASYINFER_MLU_TASK_QUEUE_H_