Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory: Add and use destroy* functions #60

Merged
merged 2 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/stdgpu/impl/deque_detail.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#ifndef STDGPU_DEQUE_DETAIL_H
#define STDGPU_DEQUE_DETAIL_H

#include <thrust/fill.h>
#include <thrust/for_each.h>
#include <thrust/iterator/counting_iterator.h>

Expand Down Expand Up @@ -469,17 +468,13 @@ deque<T>::clear()
// One large block
if (begin <= end)
{
thrust::for_each(stdgpu::make_device(_data + begin), stdgpu::make_device(_data + end),
stdgpu::detail::destroy_value<T>());
stdgpu::destroy(stdgpu::make_device(_data + begin), stdgpu::make_device(_data + end));
}
// Two disconnected blocks
else
{
thrust::for_each(stdgpu::device_begin(_data), stdgpu::make_device(_data + end),
stdgpu::detail::destroy_value<T>());

thrust::for_each(stdgpu::make_device(_data + begin), stdgpu::device_end(_data),
stdgpu::detail::destroy_value<T>());
stdgpu::destroy(stdgpu::device_begin(_data), stdgpu::make_device(_data + end));
stdgpu::destroy(stdgpu::make_device(_data + begin), stdgpu::device_end(_data));
}


Expand Down
46 changes: 37 additions & 9 deletions src/stdgpu/impl/memory_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* limitations under the License.
*/

#ifndef STDGPU_MEMORYDETAIL_H
#define STDGPU_MEMORYDETAIL_H
#ifndef STDGPU_MEMORY_DETAIL_H
#define STDGPU_MEMORY_DETAIL_H

#include <cstdio>
#include <type_traits>
Expand Down Expand Up @@ -226,8 +226,7 @@ destroyDeviceArray(T*& device_array)
{
#if !STDGPU_USE_FAST_DESTROY
#if STDGPU_BACKEND != STDGPU_BACKEND_CUDA || STDGPU_DEVICE_COMPILER == STDGPU_DEVICE_COMPILER_NVCC
thrust::for_each(stdgpu::device_begin(device_array), stdgpu::device_end(device_array),
stdgpu::detail::destroy_value<T>());
stdgpu::destroy(stdgpu::device_begin(device_array), stdgpu::device_end(device_array));

stdgpu::detail::workaround_synchronize_device_thrust();
#else
Expand All @@ -254,8 +253,7 @@ void
destroyHostArray(T*& host_array)
{
#if !STDGPU_USE_FAST_DESTROY
thrust::for_each(stdgpu::host_begin(host_array), stdgpu::host_end(host_array),
stdgpu::detail::destroy_value<T>());
stdgpu::destroy(stdgpu::host_begin(host_array), stdgpu::host_end(host_array));
#endif

stdgpu::safe_host_allocator<T> host_allocator;
Expand All @@ -271,8 +269,7 @@ destroyManagedArray(T*& managed_array)
{
#if !STDGPU_USE_FAST_DESTROY
// Call on host since the initialization place is not known
thrust::for_each(stdgpu::host_begin(managed_array), stdgpu::host_end(managed_array),
stdgpu::detail::destroy_value<T>());
stdgpu::destroy(stdgpu::host_begin(managed_array), stdgpu::host_end(managed_array));
#endif

stdgpu::safe_managed_allocator<T> managed_allocator;
Expand Down Expand Up @@ -506,11 +503,42 @@ default_allocator_traits::construct(T* p,
template <typename T>
STDGPU_HOST_DEVICE void
default_allocator_traits::destroy(T* p)
{
destroy_at(p);
}


template <typename T>
STDGPU_HOST_DEVICE void
destroy_at(T* p)
{
p->~T();
}


template <typename Iterator>
void
destroy(Iterator first,
Iterator last)
{
thrust::for_each(first, last,
detail::destroy_value<typename std::iterator_traits<Iterator>::value_type>());
}


template <typename Iterator, typename Size>
Iterator
destroy_n(Iterator first,
Size n)
{
Iterator last = first + n;

destroy(first, last);

return last;
}


template <>
dynamic_memory_type
get_dynamic_memory_type(void* array);
Expand Down Expand Up @@ -563,4 +591,4 @@ struct [[deprecated("Replaced by stdgpu::safe_host_allocator<T>")]] safe_pinned_



#endif // STDGPU_MEMORYDETAIL_H
#endif // STDGPU_MEMORY_DETAIL_H
5 changes: 1 addition & 4 deletions src/stdgpu/impl/vector_detail.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#ifndef STDGPU_VECTOR_DETAIL_H
#define STDGPU_VECTOR_DETAIL_H

#include <thrust/fill.h>

#include <stdgpu/contract.h>
#include <stdgpu/iterator.h>
#include <stdgpu/memory.h>
Expand Down Expand Up @@ -331,8 +329,7 @@ vector<T>::clear()

const index_t current_size = size();

thrust::for_each(stdgpu::device_begin(_data), stdgpu::device_begin(_data) + current_size,
stdgpu::detail::destroy_value<T>());
stdgpu::destroy(stdgpu::device_begin(_data), stdgpu::device_begin(_data) + current_size);

_occupied.reset();

Expand Down
36 changes: 36 additions & 0 deletions src/stdgpu/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,42 @@ struct default_allocator_traits
};


/**
* \brief Destroys the value at the given pointer
* \tparam T The value type
* \param[in] p A pointer to the value to destroy
*/
template <typename T>
STDGPU_HOST_DEVICE void
destroy_at(T* p);


/**
* \brief Destroys the range of values
* \tparam Iterator The iterator type of the values
* \param[in] first An iterator to the begin of the value range
* \param[in] last An iterator to the end of the value range
*/
template <typename Iterator>
void
destroy(Iterator first,
Iterator last);


/**
* \brief Destroys the range of values
* \tparam Iterator The iterator type of the values
* \tparam Size The size type
* \param[in] first An iterator to the begin of the value range
* \param[in] n The number of elements in the value range
* \return An iterator to the end of the value range
*/
template <typename Iterator, typename Size>
Iterator
destroy_n(Iterator first,
Size n);


/**
* \brief Returns the total number of allocations of a specific memory type
* \param[in] memory_type A dynamic memory type
Expand Down