From d27f9dfdce444b8b8b08be25c457c43b46aeee04 Mon Sep 17 00:00:00 2001 From: amdsc21 <96135754+amdsc21@users.noreply.github.com> Date: Fri, 10 Mar 2023 05:45:38 +0100 Subject: [PATCH] finish host_device_vector.cu --- src/common/host_device_vector.cc | 4 +-- src/common/host_device_vector.cu | 45 +++++++++++++++++++++++++++++++ src/common/host_device_vector.hip | 4 +++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/common/host_device_vector.cc b/src/common/host_device_vector.cc index 030070d9a..34677632d 100644 --- a/src/common/host_device_vector.cc +++ b/src/common/host_device_vector.cc @@ -1,7 +1,7 @@ /*! * 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 @@ -197,4 +197,4 @@ template class HostDeviceVector; } // namespace xgboost -#endif // XGBOOST_USE_CUDA +#endif // XGBOOST_USE_CUDA && !defined(XGBOOST_USE_HIP) diff --git a/src/common/host_device_vector.cu b/src/common/host_device_vector.cu index a5c5dbf8f..9d29582e1 100644 --- a/src/common/host_device_vector.cu +++ b/src/common/host_device_vector.cu @@ -12,7 +12,12 @@ #include "xgboost/data.h" #include "xgboost/host_device_vector.h" #include "xgboost/tree_model.h" + +#if defined(XGBOOST_USE_CUDA) #include "device_helpers.cuh" +#elif defined(XGBOOST_USE_HIP) +#include "device_helpers.hip.h" +#endif namespace xgboost { @@ -140,10 +145,18 @@ class HostDeviceVectorImpl { auto ptr = other->ConstDevicePointer(); SetDevice(); CHECK_EQ(this->DeviceIdx(), other->DeviceIdx()); + +#if defined(XGBOOST_USE_CUDA) dh::safe_cuda(cudaMemcpyAsync(this->DevicePointer() + ori_size, ptr, other->Size() * sizeof(T), 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; if (data_h_.size() != data_d_->size()) { data_h_.resize(data_d_->size()); } SetDevice(); + +#if defined(XGBOOST_USE_CUDA) dh::safe_cuda(cudaMemcpy(data_h_.data(), data_d_->data().get(), data_d_->size() * sizeof(T), 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) { @@ -212,10 +233,18 @@ class HostDeviceVectorImpl { // data is on the host LazyResizeDevice(data_h_.size()); SetDevice(); + +#if defined(XGBOOST_USE_CUDA) dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(), data_h_.data(), data_d_->size() * sizeof(T), 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; } @@ -240,8 +269,14 @@ class HostDeviceVectorImpl { LazyResizeDevice(Size()); gpu_access_ = GPUAccess::kWrite; SetDevice(); + +#if defined(XGBOOST_USE_CUDA) dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(), other->data_d_->data().get(), 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()); gpu_access_ = GPUAccess::kWrite; SetDevice(); + +#if defined(XGBOOST_USE_CUDA) dh::safe_cuda(cudaMemcpyAsync(data_d_->data().get(), begin, 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) { @@ -262,7 +303,11 @@ class HostDeviceVectorImpl { void SetDevice() { CHECK_GE(device_, 0); if (cudaSetDeviceHandler == nullptr) { +#if defined(XGBOOST_USE_CUDA) dh::safe_cuda(cudaSetDevice(device_)); +#elif defined(XGBOOST_USE_HIP) + dh::safe_cuda(hipSetDevice(device_)); +#endif } else { (*cudaSetDeviceHandler)(device_); } diff --git a/src/common/host_device_vector.hip b/src/common/host_device_vector.hip index e69de29bb..beae69382 100644 --- a/src/common/host_device_vector.hip +++ b/src/common/host_device_vector.hip @@ -0,0 +1,4 @@ + +#if defined(XGBOOST_USE_HIP) +#include "host_device_vector.cu" +#endif