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 Apr 29, 2024
1 parent 4d9870a commit 1b10c66
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 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
16 changes: 11 additions & 5 deletions 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,11 +60,17 @@ 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))
/**
* 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<U>::value,
"Attempt to construct a non-const memory region from a const span");
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>
Expand Down

0 comments on commit 1b10c66

Please sign in to comment.