Skip to content

Commit

Permalink
vector: Extend support for custom execution policies
Browse files Browse the repository at this point in the history
  • Loading branch information
stotko committed Nov 19, 2024
1 parent 1fb525f commit 99fe4a0
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 29 deletions.
148 changes: 136 additions & 12 deletions src/stdgpu/impl/vector_detail.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,31 @@ vector<T, Allocator>::empty() const
return (size() == 0);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
inline bool
vector<T, Allocator>::empty(ExecutionPolicy&& policy) const
{
return (size(std::forward<ExecutionPolicy>(policy)) == 0);
}

template <typename T, typename Allocator>
inline STDGPU_HOST_DEVICE bool
vector<T, Allocator>::full() const
{
return (size() == max_size());
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
inline bool
vector<T, Allocator>::full(ExecutionPolicy&& policy) const
{
return (size(std::forward<ExecutionPolicy>(policy)) == max_size());
}

template <typename T, typename Allocator>
inline STDGPU_HOST_DEVICE index_t
vector<T, Allocator>::size() const
Expand Down Expand Up @@ -472,6 +490,37 @@ vector<T, Allocator>::size() const
return current_size;
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
inline index_t
vector<T, Allocator>::size(ExecutionPolicy&& policy) const
{
index_t current_size = static_cast<index_t>(_size.load(std::forward<ExecutionPolicy>(policy)));

// Check boundary cases where the push/pop caused the pointers to be overful/underful
if (current_size < 0)
{
printf("stdgpu::vector::size : Size out of bounds: %" STDGPU_PRIINDEX " not in [0, %" STDGPU_PRIINDEX

Check warning on line 504 in src/stdgpu/impl/vector_detail.cuh

View check run for this annotation

Codecov / codecov/patch

src/stdgpu/impl/vector_detail.cuh#L504

Added line #L504 was not covered by tests
"]. Clamping to 0\n",
current_size,
capacity());
return 0;

Check warning on line 508 in src/stdgpu/impl/vector_detail.cuh

View check run for this annotation

Codecov / codecov/patch

src/stdgpu/impl/vector_detail.cuh#L508

Added line #L508 was not covered by tests
}
if (current_size > capacity())
{
printf("stdgpu::vector::size : Size out of bounds: %" STDGPU_PRIINDEX " not in [0, %" STDGPU_PRIINDEX

Check warning on line 512 in src/stdgpu/impl/vector_detail.cuh

View check run for this annotation

Codecov / codecov/patch

src/stdgpu/impl/vector_detail.cuh#L512

Added line #L512 was not covered by tests
"]. Clamping to %" STDGPU_PRIINDEX "\n",
current_size,
capacity(),
capacity());
return capacity();

Check warning on line 517 in src/stdgpu/impl/vector_detail.cuh

View check run for this annotation

Codecov / codecov/patch

src/stdgpu/impl/vector_detail.cuh#L517

Added line #L517 was not covered by tests
}

STDGPU_ENSURES(current_size <= capacity());
return current_size;
}

template <typename T, typename Allocator>
inline STDGPU_HOST_DEVICE index_t
vector<T, Allocator>::max_size() const noexcept
Expand Down Expand Up @@ -520,14 +569,14 @@ template <typename ExecutionPolicy,
inline void
vector<T, Allocator>::clear(ExecutionPolicy&& policy)
{
if (empty())
if (empty(std::forward<ExecutionPolicy>(policy)))
{
return;
}

if (!detail::is_destroy_optimizable<value_type>())
{
const index_t current_size = size();
const index_t current_size = size(std::forward<ExecutionPolicy>(policy));

detail::unoptimized_destroy(std::forward<ExecutionPolicy>(policy),
stdgpu::device_begin(_data),
Expand All @@ -536,9 +585,9 @@ vector<T, Allocator>::clear(ExecutionPolicy&& policy)

_occupied.reset(std::forward<ExecutionPolicy>(policy));

_size.store(static_cast<int>(0));
_size.store(std::forward<ExecutionPolicy>(policy), static_cast<int>(0));

STDGPU_ENSURES(empty());
STDGPU_ENSURES(empty(std::forward<ExecutionPolicy>(policy)));
STDGPU_ENSURES(valid(std::forward<ExecutionPolicy>(policy)));
}

Expand All @@ -561,13 +610,23 @@ vector<T, Allocator>::valid(ExecutionPolicy&& policy) const
return true;
}

return (size_valid() && occupied_count_valid(std::forward<ExecutionPolicy>(policy)) &&
return (size_valid(std::forward<ExecutionPolicy>(policy)) &&
occupied_count_valid(std::forward<ExecutionPolicy>(policy)) &&
_locks.valid(std::forward<ExecutionPolicy>(policy)));
}

template <typename T, typename Allocator>
device_ptr<T>
vector<T, Allocator>::device_begin()
{
return device_begin(execution::device);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
device_ptr<T>
vector<T, Allocator>::device_begin([[maybe_unused]] ExecutionPolicy&& policy)
{
return stdgpu::device_begin(_data);
}
Expand All @@ -576,12 +635,30 @@ template <typename T, typename Allocator>
device_ptr<T>
vector<T, Allocator>::device_end()
{
return device_begin() + size();
return device_end(execution::device);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
device_ptr<T>
vector<T, Allocator>::device_end(ExecutionPolicy&& policy)
{
return stdgpu::device_begin(_data) + size(std::forward<ExecutionPolicy>(policy));
}

template <typename T, typename Allocator>
device_ptr<const T>
vector<T, Allocator>::device_begin() const
{
return device_begin(execution::device);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
device_ptr<const T>
vector<T, Allocator>::device_begin([[maybe_unused]] ExecutionPolicy&& policy) const
{
return stdgpu::device_begin(_data);
}
Expand All @@ -590,12 +667,30 @@ template <typename T, typename Allocator>
device_ptr<const T>
vector<T, Allocator>::device_end() const
{
return device_begin() + size();
return device_end(execution::device);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
device_ptr<const T>
vector<T, Allocator>::device_end(ExecutionPolicy&& policy) const
{
return stdgpu::device_begin(_data) + size(std::forward<ExecutionPolicy>(policy));
}

template <typename T, typename Allocator>
device_ptr<const T>
vector<T, Allocator>::device_cbegin() const
{
return device_cbegin(execution::device);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
device_ptr<const T>
vector<T, Allocator>::device_cbegin([[maybe_unused]] ExecutionPolicy&& policy) const
{
return stdgpu::device_cbegin(_data);
}
Expand All @@ -604,21 +699,48 @@ template <typename T, typename Allocator>
device_ptr<const T>
vector<T, Allocator>::device_cend() const
{
return device_cbegin() + size();
return device_cend(execution::device);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
device_ptr<const T>
vector<T, Allocator>::device_cend(ExecutionPolicy&& policy) const
{
return stdgpu::device_cbegin(_data) + size(std::forward<ExecutionPolicy>(policy));
}

template <typename T, typename Allocator>
stdgpu::device_range<T>
vector<T, Allocator>::device_range()
{
return stdgpu::device_range<T>(_data, size());
return device_range(execution::device);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
stdgpu::device_range<T>
vector<T, Allocator>::device_range(ExecutionPolicy&& policy)
{
return stdgpu::device_range<T>(_data, size(std::forward<ExecutionPolicy>(policy)));
}

template <typename T, typename Allocator>
stdgpu::device_range<const T>
vector<T, Allocator>::device_range() const
{
return stdgpu::device_range<const T>(_data, size());
return device_range(execution::device);
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
stdgpu::device_range<const T>
vector<T, Allocator>::device_range(ExecutionPolicy&& policy) const
{
return stdgpu::device_range<const T>(_data, size(std::forward<ExecutionPolicy>(policy)));
}

template <typename T, typename Allocator>
Expand All @@ -643,10 +765,12 @@ vector<T, Allocator>::occupied_count_valid(ExecutionPolicy&& policy) const
}

template <typename T, typename Allocator>
template <typename ExecutionPolicy,
STDGPU_DETAIL_OVERLOAD_DEFINITION_IF(is_execution_policy_v<remove_cvref_t<ExecutionPolicy>>)>
bool
vector<T, Allocator>::size_valid() const
vector<T, Allocator>::size_valid(ExecutionPolicy&& policy) const
{
index_t current_size = static_cast<index_t>(_size.load());
index_t current_size = static_cast<index_t>(_size.load(std::forward<ExecutionPolicy>(policy)));
return (0 <= current_size && current_size <= capacity());
}

Expand Down
Loading

0 comments on commit 99fe4a0

Please sign in to comment.