Skip to content

Commit

Permalink
Add host-to-device async copy for host::noncached::unique_ptr (#529)
Browse files Browse the repository at this point in the history
Adds the type-safe wrappers for performing host-to-device asynchronous copies of data from a cms::cuda::host::noncached::unique_ptr<T> to a cms::cuda::device::unique_ptr<T>.
  • Loading branch information
fwyzard committed Aug 4, 2020
1 parent 537f09e commit 2222f36
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions HeterogeneousCore/CUDAUtilities/interface/copyAsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

#include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
#include "HeterogeneousCore/CUDAUtilities/interface/device_unique_ptr.h"
#include "HeterogeneousCore/CUDAUtilities/interface/host_noncached_unique_ptr.h"
#include "HeterogeneousCore/CUDAUtilities/interface/host_unique_ptr.h"

#include <type_traits>

namespace cms {
namespace cuda {

// Single element

template <typename T>
inline void copyAsync(device::unique_ptr<T>& dst, const host::unique_ptr<T>& src, cudaStream_t stream) {
// Shouldn't compile for array types because of sizeof(T), but
Expand All @@ -19,6 +22,15 @@ namespace cms {
cudaCheck(cudaMemcpyAsync(dst.get(), src.get(), sizeof(T), cudaMemcpyHostToDevice, stream));
}

template <typename T>
inline void copyAsync(device::unique_ptr<T>& dst, const host::noncached::unique_ptr<T>& src, cudaStream_t stream) {
// Shouldn't compile for array types because of sizeof(T), but
// let's add an assert with a more helpful message
static_assert(std::is_array<T>::value == false,
"For array types, use the other overload with the size parameter");
cudaCheck(cudaMemcpyAsync(dst.get(), src.get(), sizeof(T), cudaMemcpyHostToDevice, stream));
}

template <typename T>
inline void copyAsync(host::unique_ptr<T>& dst, const device::unique_ptr<T>& src, cudaStream_t stream) {
static_assert(std::is_array<T>::value == false,
Expand All @@ -27,6 +39,7 @@ namespace cms {
}

// Multiple elements

template <typename T>
inline void copyAsync(device::unique_ptr<T[]>& dst,
const host::unique_ptr<T[]>& src,
Expand All @@ -35,6 +48,14 @@ namespace cms {
cudaCheck(cudaMemcpyAsync(dst.get(), src.get(), nelements * sizeof(T), cudaMemcpyHostToDevice, stream));
}

template <typename T>
inline void copyAsync(device::unique_ptr<T[]>& dst,
const host::noncached::unique_ptr<T[]>& src,
size_t nelements,
cudaStream_t stream) {
cudaCheck(cudaMemcpyAsync(dst.get(), src.get(), nelements * sizeof(T), cudaMemcpyHostToDevice, stream));
}

template <typename T>
inline void copyAsync(host::unique_ptr<T[]>& dst,
const device::unique_ptr<T[]>& src,
Expand Down

0 comments on commit 2222f36

Please sign in to comment.