cuda_resource_pool.cc 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2020 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.

15
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
16 17 18 19 20 21 22 23 24 25 26 27
#include "paddle/fluid/platform/cuda_resource_pool.h"
#include "paddle/fluid/platform/gpu_info.h"

namespace paddle {
namespace platform {

CudaStreamResourcePool::CudaStreamResourcePool() {
  int dev_cnt = platform::GetCUDADeviceCount();
  pool_.reserve(dev_cnt);
  for (int dev_idx = 0; dev_idx < dev_cnt; ++dev_idx) {
    auto creator = [dev_idx] {
      platform::SetDeviceId(dev_idx);
28 29 30 31 32
      gpuStream_t stream;
#ifdef PADDLE_WITH_HIP
      PADDLE_ENFORCE_CUDA_SUCCESS(
          hipStreamCreateWithFlags(&stream, hipStreamNonBlocking));
#else
33
      PADDLE_ENFORCE_CUDA_SUCCESS(
34
          cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
35
#endif
36 37 38
      return stream;
    };

39
    auto deleter = [dev_idx](gpuStream_t stream) {
40
      platform::SetDeviceId(dev_idx);
41 42 43
#ifdef PADDLE_WITH_HIP
      PADDLE_ENFORCE_CUDA_SUCCESS(hipStreamDestroy(stream));
#else
44
      PADDLE_ENFORCE_CUDA_SUCCESS(cudaStreamDestroy(stream));
45
#endif
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    };

    pool_.emplace_back(
        ResourcePool<CudaStreamObject>::Create(creator, deleter));
  }
}

CudaStreamResourcePool& CudaStreamResourcePool::Instance() {
  static CudaStreamResourcePool pool;
  return pool;
}

std::shared_ptr<CudaStreamObject> CudaStreamResourcePool::New(int dev_idx) {
  PADDLE_ENFORCE_GE(
      dev_idx, 0,
      platform::errors::InvalidArgument(
G
GaoWei8 已提交
62
          "The dev_idx should be not less than 0, but got %d.", dev_idx));
63 64 65
  PADDLE_ENFORCE_LT(
      dev_idx, pool_.size(),
      platform::errors::OutOfRange(
G
GaoWei8 已提交
66
          "The dev_idx should be less than device count %d, but got %d.",
67 68 69 70 71 72 73 74 75 76
          pool_.size(), dev_idx));
  return pool_[dev_idx]->New();
}

CudaEventResourcePool::CudaEventResourcePool() {
  int dev_cnt = platform::GetCUDADeviceCount();
  pool_.reserve(dev_cnt);
  for (int dev_idx = 0; dev_idx < dev_cnt; ++dev_idx) {
    auto creator = [dev_idx] {
      platform::SetDeviceId(dev_idx);
77 78 79 80 81
      gpuEvent_t event;
#ifdef PADDLE_WITH_HIP
      PADDLE_ENFORCE_CUDA_SUCCESS(
          hipEventCreateWithFlags(&event, hipEventDisableTiming));
#else
82
      PADDLE_ENFORCE_CUDA_SUCCESS(
83
          cudaEventCreateWithFlags(&event, cudaEventDisableTiming));
84
#endif
85 86 87
      return event;
    };

88
    auto deleter = [dev_idx](gpuEvent_t event) {
89
      platform::SetDeviceId(dev_idx);
90 91 92
#ifdef PADDLE_WITH_HIP
      PADDLE_ENFORCE_CUDA_SUCCESS(hipEventDestroy(event));
#else
93
      PADDLE_ENFORCE_CUDA_SUCCESS(cudaEventDestroy(event));
94
#endif
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    };

    pool_.emplace_back(ResourcePool<CudaEventObject>::Create(creator, deleter));
  }
}

CudaEventResourcePool& CudaEventResourcePool::Instance() {
  static CudaEventResourcePool pool;
  return pool;
}

std::shared_ptr<CudaEventObject> CudaEventResourcePool::New(int dev_idx) {
  PADDLE_ENFORCE_GE(
      dev_idx, 0,
      platform::errors::InvalidArgument(
G
GaoWei8 已提交
110
          "The dev_idx should be not less than 0, but got %d.", dev_idx));
111 112 113
  PADDLE_ENFORCE_LT(
      dev_idx, pool_.size(),
      platform::errors::OutOfRange(
G
GaoWei8 已提交
114
          "The dev_idx should be less than device count %d, but got %d.",
115 116 117 118 119 120 121 122
          pool_.size(), dev_idx));
  return pool_[dev_idx]->New();
}

}  // namespace platform
}  // namespace paddle

#endif