Skip to content

Commit

Permalink
Fixes #606: Can now use contiguous containers as sources and destinat…
Browse files Browse the repository at this point in the history
…ions of memory copying functions
  • Loading branch information
eyalroz committed Mar 11, 2024
1 parent d7259aa commit 659e877
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
6 changes: 3 additions & 3 deletions examples/modified_cuda_samples/vectorAdd/vectorAdd.cu
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ int main()
auto d_B = cuda::memory::make_unique_span<float>(device, numElements);
auto d_C = cuda::memory::make_unique_span<float>(device, numElements);

cuda::memory::copy(d_A, h_A.data());
cuda::memory::copy(d_B, h_B.data());
cuda::memory::copy(d_A, h_A);
cuda::memory::copy(d_B, h_B);

auto launch_config = cuda::launch_config_builder()
.overall_size(numElements)
Expand All @@ -63,7 +63,7 @@ int main()
d_A.data(), d_B.data(), d_C.data(), numElements
);

cuda::memory::copy(h_C.data(), d_C);
cuda::memory::copy(h_C, d_C);

// Verify that the result vector is correct
for (int i = 0; i < numElements; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ int main()
auto d_B_sp = d_B.as_requested().as_span<float>();
auto d_C_sp = d_C.as_requested().as_span<float>();

cuda::memory::copy(d_A_sp, h_A.data());
cuda::memory::copy(d_B_sp, h_B.data());
cuda::memory::copy(d_A_sp, h_A);
cuda::memory::copy(d_B_sp, h_B);

// Launch the Vector Add CUDA Kernel
auto launch_config = cuda::launch_config_builder()
Expand All @@ -295,7 +295,7 @@ int main()
d_A_sp.data(), d_B_sp.data(), d_C_sp.data(), num_elements
);

cuda::memory::copy(h_C.data(), d_C_sp);
cuda::memory::copy(h_C, d_C_sp);

// std::cout << "Checking results...\n\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ int main(void)
auto d_B = cuda::memory::make_unique_span<float>(device, numElements);
auto d_C = cuda::memory::make_unique_span<float>(device, numElements);

cuda::memory::copy(d_A, h_A.data());
cuda::memory::copy(d_B, h_B.data());
cuda::memory::copy(d_A, h_A);
cuda::memory::copy(d_B, h_B);

auto launch_config = cuda::launch_config_builder()
.overall_size(numElements)
Expand All @@ -87,7 +87,7 @@ int main(void)
d_A.get(), d_B.get(), d_C.get(), numElements
);

cuda::memory::copy(h_C.data(), d_C);
cuda::memory::copy(h_C, d_C);

// Verify that the result vector is correct
for (int i = 0; i < numElements; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ int main(void)
auto d_B = cuda::memory::make_unique_span<float>(device, numElements);
auto d_C = cuda::memory::make_unique_span<float>(device, numElements);

cuda::memory::copy(d_A, h_A.data());
cuda::memory::copy(d_B, h_B.data());
cuda::memory::copy(d_A, h_A);
cuda::memory::copy(d_B, h_B);

auto launch_config = cuda::launch_config_builder()
.overall_size(numElements)
Expand All @@ -127,7 +127,7 @@ int main(void)
d_A.get(), d_B.get(), d_C.get(), numElements
);

cuda::memory::copy(h_C.data(), d_C);
cuda::memory::copy(h_C, d_C);

// Verify that the result vector is correct
for (int i = 0; i < numElements; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ int main()
auto sp_B = d_B.as_span<float>();
auto sp_C = d_C.as_span<float>();

cuda::memory::copy(sp_A, h_A.data());
cuda::memory::copy(sp_B, h_B.data());
cuda::memory::copy(sp_A, h_A);
cuda::memory::copy(sp_B, h_B);

auto launch_config = cuda::launch_config_builder()
.overall_size(numElements)
Expand All @@ -68,7 +68,7 @@ int main()
sp_A.data(), sp_B.data(), sp_C.data(), numElements
);

cuda::memory::copy(h_C.data(), sp_C);
cuda::memory::copy(h_C, sp_C);

// Verify that the result vector is correct
for (int i = 0; i < numElements; ++i) {
Expand Down
28 changes: 27 additions & 1 deletion src/cuda/api/detail/region.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef CUDA_API_WRAPPERS_REGION_HPP_
#define CUDA_API_WRAPPERS_REGION_HPP_

#include <type_traits>
#include "type_traits.hpp"
#include <stdexcept>

#ifndef CPP14_CONSTEXPR
Expand Down Expand Up @@ -60,12 +60,38 @@ class base_region_t {
constexpr base_region_t(pointer start, size_type size_in_bytes) noexcept
: start_(start), size_in_bytes_(size_in_bytes) {}

/*
template <typename U>
constexpr base_region_t(span<U> span) noexcept : start_(span.data()), size_in_bytes_(span.size() * sizeof(U))
{
static_assert(::std::is_const<T>::value or not ::std::is_const<U>::value,
"Attempt to construct a non-const memory region from a const span");
}
*/
/**
* A constructor from types such as `::std::span`'s or `::std::vector`'s, whose data is in
* a contiguous region of memory
*/
template <typename ContiguousContainer, typename = cuda::detail_::enable_if_t<
cuda::detail_::is_kinda_like_contiguous_container<ContiguousContainer>::value, void>>
constexpr base_region_t(ContiguousContainer&& contiguous_container) noexcept
: start_(contiguous_container.data()), size_in_bytes_(contiguous_container.size() * sizeof(*(contiguous_container.data())))
{
static_assert(::std::is_const<T>::value or not ::std::is_const<decltype(*(contiguous_container.data()))>::value,
"Attempt to construct a non-const memory region from a container of const data");
}

/*
template <typename U, template <typename> class ContiguousContainer,
typename = cuda::detail_::enable_if_t<
cuda::detail_::is_kinda_like_contiguous_container<ContiguousContainer<U>>::value, void>>
constexpr base_region_t(ContiguousContainer<U>&& contiguous) noexcept
: start_(contiguous.data()), size_in_bytes_(contiguous.size() * sizeof(U))
{
static_assert(::std::is_const<T>::value or not ::std::is_const<U>::value,
"Attempt to construct a non-const memory region from a const contiguous container");
}
*/

template <typename U>
CPP14_CONSTEXPR span<U> as_span() const NOEXCEPT_IF_NDEBUG
Expand Down

0 comments on commit 659e877

Please sign in to comment.