heter_resource.cc 5.1 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
16 17 18
#include "paddle/fluid/framework/fleet/heter_ps/heter_resource.h"

#ifdef PADDLE_WITH_CUDA
T
Thunderbrook 已提交
19
#include "paddle/fluid/platform/cuda_device_guard.h"
20 21 22 23 24 25
#endif

#ifdef PADDLE_WITH_XPU_KP
#include "paddle/fluid/platform/device/xpu/enforce_xpu.h"
#include "paddle/fluid/platform/device/xpu/xpu_info.h"
#endif
T
Thunderbrook 已提交
26 27 28 29

namespace paddle {
namespace framework {

30
#if defined(PADDLE_WITH_CUDA)
31
GPUResource::GPUResource(std::vector<int>& dev_ids, int index) {
T
Thunderbrook 已提交
32
  index_ = index;
33 34
  dev_ids_ = dev_ids;
  dev_id_ = dev_ids_[index];
T
Thunderbrook 已提交
35 36

  platform::CUDADeviceGuard guard(dev_id_);
37 38
  local_streams_.resize(dev_ids_.size());
  comm_streams_.resize(dev_ids_.size());
39
  remote_streams_.resize(dev_ids_.size());
40 41

  for (size_t i = 0; i < dev_ids_.size(); ++i) {
42
    PADDLE_ENFORCE_GPU_SUCCESS(
43
        cudaStreamCreateWithFlags(&local_streams_[i], cudaStreamNonBlocking));
44
    PADDLE_ENFORCE_GPU_SUCCESS(
45
        cudaStreamCreateWithFlags(&comm_streams_[i], cudaStreamNonBlocking));
46
    PADDLE_ENFORCE_GPU_SUCCESS(
47
        cudaStreamCreateWithFlags(&remote_streams_[i], cudaStreamNonBlocking));
48
  }
T
Thunderbrook 已提交
49 50 51 52
}

GPUResource::~GPUResource() {
  platform::CUDADeviceGuard guard(dev_id_);
53
  for (size_t i = 0; i < local_streams_.size(); ++i) {
54
    PADDLE_ENFORCE_GPU_SUCCESS(cudaStreamDestroy(local_streams_[i]));
55 56
  }
  for (size_t i = 0; i < comm_streams_.size(); ++i) {
57
    PADDLE_ENFORCE_GPU_SUCCESS(cudaStreamDestroy(comm_streams_[i]));
58
  }
59
  for (size_t i = 0; i < remote_streams_.size(); ++i) {
60
    PADDLE_ENFORCE_GPU_SUCCESS(cudaStreamDestroy(remote_streams_[i]));
61
  }
T
Thunderbrook 已提交
62 63
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#elif defined(PADDLE_WITH_XPU_KP)
XPUResource::XPUResource(std::vector<int>& dev_ids, int index) {
  index_ = index;
  dev_ids_ = dev_ids;
  dev_id_ = dev_ids_[index];

  platform::XPUDeviceGuard guard(dev_id_);
  local_streams_.resize(dev_ids_.size());
  comm_streams_.resize(dev_ids_.size(), NULL);
  remote_streams_.resize(dev_ids_.size());

  for (size_t i = 0; i < dev_ids_.size(); ++i) {
    PADDLE_ENFORCE_XPU_SUCCESS(xpu_stream_create(&local_streams_[i]));
    // PADDLE_ENFORCE_XPU_SUCCESS(xpu_stream_create(&comm_streams_[i]));
    PADDLE_ENFORCE_XPU_SUCCESS(xpu_stream_create(&remote_streams_[i]));
  }
}

XPUResource::~XPUResource() {
  platform::XPUDeviceGuard guard(dev_id_);
  for (size_t i = 0; i < local_streams_.size(); ++i) {
    PADDLE_ENFORCE_XPU_SUCCESS(xpu_stream_destroy(local_streams_[i]));
  }
  // for (size_t i = 0; i < comm_streams_.size(); ++i) {
  //  PADDLE_ENFORCE_XPU_SUCCESS(xpu_stream_destroy(comm_streams_[i]));
  // }
  for (size_t i = 0; i < remote_streams_.size(); ++i) {
    PADDLE_ENFORCE_XPU_SUCCESS(xpu_stream_destroy(remote_streams_[i]));
  }
}

#endif

T
Thunderbrook 已提交
97
void HeterPsResource::enable_p2p() {
98
#if defined(PADDLE_WITH_CUDA)
T
Thunderbrook 已提交
99 100 101 102 103
  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;
104
        PADDLE_ENFORCE_GPU_SUCCESS(
T
Thunderbrook 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
            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();
          }
        }
      }
    }
  }
118
#endif
T
Thunderbrook 已提交
119 120 121 122 123
}

HeterPsResource::HeterPsResource(const std::vector<int>& dev_ids) {
  dev_ids_ = dev_ids;
  for (size_t i = 0; i < dev_ids_.size(); ++i) {
124 125
    std::shared_ptr<DevResource> resource =
        std::make_shared<DevResource>(dev_ids_, i);
T
Thunderbrook 已提交
126 127 128 129 130
    resources_.push_back(resource);
    devid_2_index_[dev_ids_[i]] = i;
  }
}

131 132
ppStream HeterPsResource::comm_stream(int dev_num, int stream_num) {
  return resources_[dev_num]->comm_stream(stream_num);
133
}
134 135
ppStream HeterPsResource::local_stream(int dev_num, int stream_num) {
  return resources_[dev_num]->local_stream(stream_num);
T
Thunderbrook 已提交
136 137
}

138 139
ppStream HeterPsResource::remote_stream(int dev_num, int stream_num) {
  return resources_[dev_num]->remote_stream(stream_num);
T
Thunderbrook 已提交
140 141 142 143 144 145 146 147
}

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

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

148
int HeterPsResource::total_device() { return dev_ids_.size(); }
T
Thunderbrook 已提交
149

Y
yaoxuefeng 已提交
150 151 152 153 154 155 156
void HeterPsResource::set_multi_mf(int multi_mf_dim, int max_mf_dim) {
  multi_mf_dim_ = multi_mf_dim;
  max_mf_dim_ = max_mf_dim;
  VLOG(3) << "heter resource set mf dim: " << multi_mf_dim_
          << " max_mf_dim_: " << max_mf_dim_;
}

T
Thunderbrook 已提交
157 158 159
}  // end namespace framework
}  // end namespace paddle
#endif