device_context.h 5.1 KB
Newer Older
F
fwenguang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once

#ifdef PADDLE_WITH_MLU
#include <mutex>
15

F
fwenguang 已提交
16 17 18
#include "paddle/fluid/platform/device/mlu/enforce.h"
#include "paddle/fluid/platform/device/mlu/mlu_stream.h"
#include "paddle/fluid/platform/device_context.h"
19 20 21
#ifdef PADDLE_WITH_CNCL
#include <cncl.h>
#endif
F
fwenguang 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

namespace Eigen {
struct DefaultDevice;
struct GpuDevice;
}  // namespace Eigen

namespace paddle {
namespace platform {

class MLUContext {
 public:
  MLUContext() = default;
  explicit MLUContext(const MLUPlace& place, const int priority = 0);

  ~MLUContext();

  const MLUPlace& Place() const { return place_; }

  const std::unique_ptr<Eigen::DefaultDevice>& EigenDevice() const {
    return eigen_device_;
  }

  const std::unique_ptr<stream::MLUStream>& Stream() const { return stream_; }

  stream::MLUStream* SetStream(stream::MLUStream* new_stream_ptr) {
    auto* old_stream_ptr = stream_.release();
    stream_.reset(new_stream_ptr);
    return old_stream_ptr;
  }

  const mluStream& RawStream() { return stream_->raw_stream(); }

  const mluCnnlHandle& CnnlHandle() const { return cnnl_handle_; }

C
cifar10 已提交
56 57
  const mluOpHandle& MluOpHandle() const { return mluOp_handle_; }

F
fwenguang 已提交
58 59 60 61 62 63
 private:
  void InitCNNLContext() {
    PADDLE_ENFORCE_MLU_SUCCESS(cnnlCreate(&cnnl_handle_));
    PADDLE_ENFORCE_MLU_SUCCESS(cnnlSetQueue(cnnl_handle_, RawStream()));
  }

C
cifar10 已提交
64 65 66 67 68
  void InitMLUOPContext() {
    PADDLE_ENFORCE_MLU_SUCCESS(mluOpCreate(&mluOp_handle_));
    PADDLE_ENFORCE_MLU_SUCCESS(mluOpSetQueue(mluOp_handle_, RawStream()));
  }

F
fwenguang 已提交
69 70 71 72 73 74 75
  void DestoryCNNLContext() {
    if (cnnl_handle_) {
      PADDLE_ENFORCE_MLU_SUCCESS(cnnlDestroy(cnnl_handle_));
    }
    cnnl_handle_ = nullptr;
  }

C
cifar10 已提交
76 77 78 79 80 81 82
  void DestoryMLUOPContext() {
    if (mluOp_handle_) {
      PADDLE_ENFORCE_MLU_SUCCESS(mluOpDestroy(mluOp_handle_));
    }
    mluOp_handle_ = nullptr;
  }

F
fwenguang 已提交
83 84 85 86
  MLUPlace place_;
  std::unique_ptr<Eigen::DefaultDevice> eigen_device_;
  std::unique_ptr<stream::MLUStream> stream_;
  mluCnnlHandle cnnl_handle_;
C
cifar10 已提交
87
  mluOpHandle mluOp_handle_;
F
fwenguang 已提交
88 89 90 91

  DISABLE_COPY_AND_ASSIGN(MLUContext);
};

92 93 94
class MLUDeviceContext
    : public DeviceContext,
      public phi::TypeInfoTraits<DeviceContext, MLUDeviceContext> {
F
fwenguang 已提交
95 96 97 98
 public:
  explicit MLUDeviceContext(MLUPlace place);
  virtual ~MLUDeviceContext();
  Eigen::DefaultDevice* eigen_device() const { return nullptr; }
Q
qipengh 已提交
99
  const Place& GetPlace() const override;
F
fwenguang 已提交
100 101 102 103 104 105 106 107 108

  int GetComputeCapability() const;

  /*! \brief  Wait for all operations completion in the stream. */
  void Wait() const override;

  /*! \brief  Return cnnl handle in the device context. */
  mluCnnlHandle cnnl_handle() const;

C
cifar10 已提交
109 110 111
  /*! \brief  Return mluOp handle in the device context. */
  mluOpHandle mluOp_handle() const;

F
fwenguang 已提交
112 113 114
  /*! \brief  Return mlu stream in the device context. */
  mluStream stream() const;

115 116 117 118 119 120 121 122
#ifdef PADDLE_WITH_CNCL
  /*! \brief  Return cncl communicators. */
  cnclComm_t cncl_comm() const { return cncl_comm_; }

  /*! \brief  Set cncl communicators. */
  void set_cncl_comm(cnclComm_t comm) { cncl_comm_ = comm; }
#endif

F
fwenguang 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  template <typename Callback>
  void RecordEvent(mluEventHandle ev, Callback callback) const {
    return context()->Stream()->RecordEvent(ev, callback);
  }

  template <typename Callback>
  void AddStreamCallback(Callback&& callback) const {
    return context()->Stream()->AddCallback(callback);
  }

  void WaitStreamCallback() const {
    return context()->Stream()->WaitCallback();
  }

  void ResetDefaultContext(const int priority) {
    default_ctx_.reset(new MLUContext(place_, priority));
  }

  void ResetThreadContext(const int priority) {
    std::lock_guard<std::mutex> guard(ctx_mtx_);
    thread_ctx_[this].reset(new MLUContext(place_, priority));
  }

  std::shared_ptr<MLUContext> context() const {
    if (!thread_ctx_.count(this)) {
      return default_ctx_;
    }
    return thread_ctx_.at(this);
  }

153 154
  static const char* name() { return "MLUDeviceContext"; }

F
fwenguang 已提交
155 156 157 158
 private:
  int compute_capability_;
  int driver_version_;
  int runtime_version_;
Q
qipengh 已提交
159
  int cnnl_version_;
C
cifar10 已提交
160
  int mluOp_version_;
F
fwenguang 已提交
161 162 163 164 165 166 167 168 169 170
  MLUPlace place_;
  std::shared_ptr<MLUContext> default_ctx_;

  // The thread_local static variable will be released before the
  // global static variable, so avoid using it in dtor.
  static thread_local std::unordered_map<const MLUDeviceContext*,
                                         std::shared_ptr<MLUContext>>
      thread_ctx_;
  static thread_local std::mutex ctx_mtx_;

171 172 173 174
#ifdef PADDLE_WITH_CNCL
  cnclComm_t cncl_comm_{nullptr};
#endif

F
fwenguang 已提交
175 176 177 178 179 180 181 182 183 184 185 186
  DISABLE_COPY_AND_ASSIGN(MLUDeviceContext);
};

template <>
struct DefaultDeviceContextType<platform::MLUPlace> {
  using TYPE = MLUDeviceContext;
};

#endif

}  // namespace platform
}  // namespace paddle