heter_resource.cc 3.4 KB
Newer Older
T
Thunderbrook 已提交
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. */

T
Thunderbrook 已提交
15
#ifdef PADDLE_WITH_HETERPS
T
Thunderbrook 已提交
16 17 18 19 20 21
#include "heter_resource.h"
#include "paddle/fluid/platform/cuda_device_guard.h"

namespace paddle {
namespace framework {

22
GPUResource::GPUResource(std::vector<int>& dev_ids, int index) {
T
Thunderbrook 已提交
23
  index_ = index;
24 25
  dev_ids_ = dev_ids;
  dev_id_ = dev_ids_[index];
T
Thunderbrook 已提交
26 27

  platform::CUDADeviceGuard guard(dev_id_);
28 29 30 31 32 33 34 35 36
  local_streams_.resize(dev_ids_.size());
  comm_streams_.resize(dev_ids_.size());

  for (size_t i = 0; i < dev_ids_.size(); ++i) {
    PADDLE_ENFORCE_CUDA_SUCCESS(
        cudaStreamCreateWithFlags(&local_streams_[i], cudaStreamNonBlocking));
    PADDLE_ENFORCE_CUDA_SUCCESS(
        cudaStreamCreateWithFlags(&comm_streams_[i], cudaStreamNonBlocking));
  }
T
Thunderbrook 已提交
37 38

  PADDLE_ENFORCE_CUDA_SUCCESS(
39
      cudaStreamCreateWithFlags(&remote_stream_, cudaStreamNonBlocking));
T
Thunderbrook 已提交
40 41 42 43
}

GPUResource::~GPUResource() {
  platform::CUDADeviceGuard guard(dev_id_);
44 45 46 47 48 49 50
  for (size_t i = 0; i < local_streams_.size(); ++i) {
    PADDLE_ENFORCE_CUDA_SUCCESS(cudaStreamDestroy(local_streams_[i]));
  }
  for (size_t i = 0; i < comm_streams_.size(); ++i) {
    PADDLE_ENFORCE_CUDA_SUCCESS(cudaStreamDestroy(comm_streams_[i]));
  }
  PADDLE_ENFORCE_CUDA_SUCCESS(cudaStreamDestroy(remote_stream_));
T
Thunderbrook 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
}

void HeterPsResource::enable_p2p() {
  for (size_t i = 0; i < dev_ids_.size(); ++i) {
    platform::CUDADeviceGuard guard(dev_ids_[i]);
    for (size_t j = 0; j < dev_ids_.size(); ++j) {
      if (i != j) {
        int p2p_flag;
        PADDLE_ENFORCE_CUDA_SUCCESS(
            cudaDeviceCanAccessPeer(&p2p_flag, dev_ids_[i], dev_ids_[j]));
        if (p2p_flag == 1) {
          cudaError_t ret = cudaDeviceEnablePeerAccess(dev_ids_[j], 0);
          if (ret != cudaSuccess && ret != cudaErrorPeerAccessAlreadyEnabled) {
            VLOG(0) << " Cuda error(" << ret << "), " << cudaGetErrorString(ret)
                    << ".";
          } else {
            cudaGetLastError();
          }
        }
      }
    }
  }
}

HeterPsResource::HeterPsResource(const std::vector<int>& dev_ids) {
  dev_ids_ = dev_ids;
  for (size_t i = 0; i < dev_ids_.size(); ++i) {
    std::shared_ptr<GPUResource> resource =
79
        std::make_shared<GPUResource>(dev_ids_, i);
T
Thunderbrook 已提交
80 81 82 83 84
    resources_.push_back(resource);
    devid_2_index_[dev_ids_[i]] = i;
  }
}

85 86 87 88 89 90
cudaStream_t HeterPsResource::comm_stream(int gpu_num, int stream_num) {
  return resources_[gpu_num]->comm_stream(stream_num);
}

cudaStream_t HeterPsResource::local_stream(int gpu_num, int stream_num) {
  return resources_[gpu_num]->local_stream(stream_num);
T
Thunderbrook 已提交
91 92
}

93 94
cudaStream_t HeterPsResource::remote_stream(int gpu_num) {
  return resources_[gpu_num]->remote_stream();
T
Thunderbrook 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107
}

int HeterPsResource::dev_id(int num) { return dev_ids_[num]; }

int HeterPsResource::get_index_by_devid(int devid) {
  return devid_2_index_[devid];
}

int HeterPsResource::total_gpu() { return dev_ids_.size(); }

}  // end namespace framework
}  // end namespace paddle
#endif