device_context.cc 3.0 KB
Newer Older
F
fwenguang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* 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. */

#ifdef PADDLE_WITH_MLU
#include "paddle/fluid/platform/device/mlu/device_context.h"
#endif

namespace paddle {
namespace platform {

#ifdef PADDLE_WITH_MLU
thread_local std::unordered_map<const MLUDeviceContext*,
                                std::shared_ptr<MLUContext>>
    MLUDeviceContext::thread_ctx_;
thread_local std::mutex MLUDeviceContext::ctx_mtx_;

MLUContext::MLUContext(const MLUPlace& place, const int priority) {
  place_ = place;
  MLUDeviceGuard guard(place_.device);
  stream_.reset(new stream::MLUStream(place_, priority));
  InitCNNLContext();
C
cifar10 已提交
31
  InitMLUOPContext();
F
fwenguang 已提交
32 33 34 35 36
}

MLUContext::~MLUContext() {
  MLUDeviceGuard guard(place_.device);
  DestoryCNNLContext();
C
cifar10 已提交
37
  DestoryMLUOPContext();
F
fwenguang 已提交
38 39 40 41 42 43 44
}

MLUDeviceContext::MLUDeviceContext(MLUPlace place) : place_(place) {
  MLUDeviceGuard guard(place_.device);
  compute_capability_ = GetMLUComputeCapability(place_.device);
  driver_version_ = GetMLUDriverVersion(place_.device);
  runtime_version_ = GetMLURuntimeVersion(place_.device);
Q
qipengh 已提交
45
  cnnl_version_ = GetMLUCnnlVersion(place_.device);
C
cifar10 已提交
46
  mluOp_version_ = GetMLUOpVersion(place_.device);
F
fwenguang 已提交
47

48 49 50 51 52 53 54 55 56
  LOG_FIRST_N(WARNING, 1)
      << "Please NOTE: device: " << static_cast<int>(place_.device)
      << ", MLU Compute Capability: " << compute_capability_ / 10 << "."
      << compute_capability_ % 10
      << ", Driver API Version: " << driver_version_ / 10000 << "."
      << (driver_version_ / 100) % 100 << "." << driver_version_ % 100
      << ", Runtime API Version: " << runtime_version_ / 10000 << "."
      << (runtime_version_ / 100) % 100 << "." << runtime_version_ % 100
      << ", Cnnl API Version: " << cnnl_version_ / 10000 << "."
C
cifar10 已提交
57 58 59
      << (cnnl_version_ / 100) % 100 << "." << cnnl_version_ % 100
      << ", MluOp API Version: " << mluOp_version_ / 10000 << "."
      << (mluOp_version_ / 100) % 100 << "." << mluOp_version_ % 100;
F
fwenguang 已提交
60 61 62 63 64 65

  default_ctx_.reset(new MLUContext(place_));
}

MLUDeviceContext::~MLUDeviceContext() {}

Q
qipengh 已提交
66
const Place& MLUDeviceContext::GetPlace() const { return place_; }
F
fwenguang 已提交
67 68 69 70 71 72 73 74 75 76 77

void MLUDeviceContext::Wait() const { context()->Stream()->Wait(); }

int MLUDeviceContext::GetComputeCapability() const {
  return compute_capability_;
}

mluCnnlHandle MLUDeviceContext::cnnl_handle() const {
  return context()->CnnlHandle();
}

C
cifar10 已提交
78 79 80 81
mluOpHandle MLUDeviceContext::mluOp_handle() const {
  return context()->MluOpHandle();
}

F
fwenguang 已提交
82 83 84 85 86
mluStream MLUDeviceContext::stream() const { return context()->RawStream(); }

#endif
}  // namespace platform
}  // namespace paddle