finish host_device_vector.cu

This commit is contained in:
amdsc21 2023-03-10 05:45:38 +01:00
parent 14cc438a64
commit d27f9dfdce
3 changed files with 51 additions and 2 deletions

View File

@ -1,7 +1,7 @@
/*! /*!
* Copyright 2017 XGBoost contributors * Copyright 2017 XGBoost contributors
*/ */
#ifndef XGBOOST_USE_CUDA #if !defined(XGBOOST_USE_CUDA) && !defined(XGBOOST_USE_HIP)
// dummy implementation of HostDeviceVector in case CUDA is not used // dummy implementation of HostDeviceVector in case CUDA is not used
@ -197,4 +197,4 @@ template class HostDeviceVector<std::size_t>;
} // namespace xgboost } // namespace xgboost
#endif // XGBOOST_USE_CUDA #endif // XGBOOST_USE_CUDA && !defined(XGBOOST_USE_HIP)

View File

@ -12,7 +12,12 @@
#include "xgboost/data.h" #include "xgboost/data.h"
#include "xgboost/host_device_vector.h" #include "xgboost/host_device_vector.h"
#include "xgboost/tree_model.h" #include "xgboost/tree_model.h"
#if defined(XGBOOST_USE_CUDA)
#include "device_helpers.cuh" #include "device_helpers.cuh"
#elif defined(XGBOOST_USE_HIP)
#include "device_helpers.hip.h"
#endif
namespace xgboost { namespace xgboost {
@ -140,10 +145,18 @@ class HostDeviceVectorImpl {
auto ptr = other->ConstDevicePointer(); auto ptr = other->ConstDevicePointer();
SetDevice(); SetDevice();
CHECK_EQ(this->DeviceIdx(), other->DeviceIdx()); CHECK_EQ(this->DeviceIdx(), other->DeviceIdx());
#if defined(XGBOOST_USE_CUDA)
dh::safe_cuda(cudaMemcpyAsync(this->DevicePointer() + ori_size, dh::safe_cuda(cudaMemcpyAsync(this->DevicePointer() + ori_size,
ptr, ptr,
other->Size() * sizeof(T), other->Size() * sizeof(T),
cudaMemcpyDeviceToDevice)); cudaMemcpyDeviceToDevice));
#elif defined(XGBOOST_USE_HIP)
dh::safe_cuda(hipMemcpyAsync(this->DevicePointer() + ori_size,
ptr,
other->Size() * sizeof(T),
hipMemcpyDeviceToDevice));
#endif
} }
} }
@ -196,10 +209,18 @@ class HostDeviceVectorImpl {
gpu_access_ = access; gpu_access_ = access;
if (data_h_.size() != data_d_->size()) { data_h_.resize(data_d_->size()); } if (data_h_.size() != data_d_->size()) { data_h_.resize(data_d_->size()); }
SetDevice(); SetDevice();
#if defined(XGBOOST_USE_CUDA)
dh::safe_cuda(cudaMemcpy(data_h_.data(), dh::safe_cuda(cudaMemcpy(data_h_.data(),
data_d_->data().get(), data_d_->data().get(),
data_d_->size() * sizeof(T), data_d_->size() * sizeof(T),
cudaMemcpyDeviceToHost)); cudaMemcpyDeviceToHost));
#elif defined(XGBOOST_USE_HIP)
dh::safe_cuda(hipMemcpy(data_h_.data(),
data_d_->data().get(),
data_d_->size() * sizeof(T),
hipMemcpyDeviceToHost));
#endif
} }
void LazySyncDevice(GPUAccess access) { void LazySyncDevice(GPUAccess access) {
@ -212,10 +233,18 @@ class HostDeviceVectorImpl {
// data is on the host // data is on the host
LazyResizeDevice(data_h_.size()); LazyResizeDevice(data_h_.size());
SetDevice(); SetDevice();
#if defined(XGBOOST_USE_CUDA)
dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(), dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(),
data_h_.data(), data_h_.data(),
data_d_->size() * sizeof(T), data_d_->size() * sizeof(T),
cudaMemcpyHostToDevice)); cudaMemcpyHostToDevice));
#elif defined(XGBOOST_USE_HIP)
dh::safe_cuda(hipMemcpyAsync(data_d_->data().get(),
data_h_.data(),
data_d_->size() * sizeof(T),
hipMemcpyHostToDevice));
#endif
gpu_access_ = access; gpu_access_ = access;
} }
@ -240,8 +269,14 @@ class HostDeviceVectorImpl {
LazyResizeDevice(Size()); LazyResizeDevice(Size());
gpu_access_ = GPUAccess::kWrite; gpu_access_ = GPUAccess::kWrite;
SetDevice(); SetDevice();
#if defined(XGBOOST_USE_CUDA)
dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(), other->data_d_->data().get(), dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(), other->data_d_->data().get(),
data_d_->size() * sizeof(T), cudaMemcpyDefault)); data_d_->size() * sizeof(T), cudaMemcpyDefault));
#elif defined(XGBOOST_USE_HIP)
dh::safe_cuda(hipMemcpyAsync(data_d_->data().get(), other->data_d_->data().get(),
data_d_->size() * sizeof(T), hipMemcpyDefault));
#endif
} }
} }
@ -249,8 +284,14 @@ class HostDeviceVectorImpl {
LazyResizeDevice(Size()); LazyResizeDevice(Size());
gpu_access_ = GPUAccess::kWrite; gpu_access_ = GPUAccess::kWrite;
SetDevice(); SetDevice();
#if defined(XGBOOST_USE_CUDA)
dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(), begin, dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(), begin,
data_d_->size() * sizeof(T), cudaMemcpyDefault)); data_d_->size() * sizeof(T), cudaMemcpyDefault));
#elif defined(XGBOOST_USE_HIP)
dh::safe_cuda(hipMemcpyAsync(data_d_->data().get(), begin,
data_d_->size() * sizeof(T), hipMemcpyDefault));
#endif
} }
void LazyResizeDevice(size_t new_size) { void LazyResizeDevice(size_t new_size) {
@ -262,7 +303,11 @@ class HostDeviceVectorImpl {
void SetDevice() { void SetDevice() {
CHECK_GE(device_, 0); CHECK_GE(device_, 0);
if (cudaSetDeviceHandler == nullptr) { if (cudaSetDeviceHandler == nullptr) {
#if defined(XGBOOST_USE_CUDA)
dh::safe_cuda(cudaSetDevice(device_)); dh::safe_cuda(cudaSetDevice(device_));
#elif defined(XGBOOST_USE_HIP)
dh::safe_cuda(hipSetDevice(device_));
#endif
} else { } else {
(*cudaSetDeviceHandler)(device_); (*cudaSetDeviceHandler)(device_);
} }

View File

@ -0,0 +1,4 @@
#if defined(XGBOOST_USE_HIP)
#include "host_device_vector.cu"
#endif