CUDA初学者-Thrust - Memory Management(13)

本文深入解析了Thrust库中的内存管理模块,包括device_ptr_memory_resource、各种内存分配器如device_allocator及其子类,以及memory_resources和池资源管理。讲解了device_ptr的使用、类型转换和内存操作。是CUDA开发者理解内存管理的重要参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人 CUDA小白一枚,要是有什么不对,还望各位大佬指点。
本文及后面的几篇将分别从几个方面来大概阐述一下Thrust的一些接口。原来的网址在这里

6.Memory manage

6.1 Allocators

template <typename Upstream>
class thrust::device_ptr_memory_resource;

/* An allocator which creates new elements in memory accessible by devices. */
template <typename T>
class thrust::device_allocator;

template <typename T>
class thrust::device_malloc_allocator;

template <typename T>
class thrust::device_new_allocator;

template <typename T,  class MR>
class thrust::mr::allocator;

template <typename T, typename Upstream>
class thrust::mr::stateless_resource_allocator;

template <typename T, typename Pointer>
using thrust::mr::polymorphic_allocator = see below;

template <typename T, typename MR>
__host__ __device__ bool
thrust::mr::operator==(const allocator< T, MR > & lhs,
  const allocator< T, MR > & rhs);

template <typename T, typename MR>
__host__ __device__ bool
thrust::mr::operator!=(const allocator< T, MR > & lhs,
  const allocator< T, MR > & rhs);

6.2 Memory Resources

template <typename Upstream, typename Bookkeeper>
class thrust::mr::disjoint_unsynchronized_pool_resource;

template <typename Upstream, typename Bookkeeper>
struct thrust::mr::disjoint_synchronized_pool_resource;

template <typename Pointer = void *>
class thrust::mr::memory_resource;

class thrust::mr::memory_resource< void * >;

class thrust::mr::new_delete_resource;

template <typename Upstream>
class thrust::mr::unsynchronized_pool_resource;

struct thrust::mr::pool_options;

template <typename Upstream>
struct thrust::mr::synchronized_pool_resource;

6.3 device_ptr

template <typename T>
class thrust::device_reference;

/* device_ptr is a pointer-like object which points to an object that resides in memory associated with the device system. */
template <typename T>
class thrust::device_ptr;

// 删除 device_reference
template <typename T>
void thrust::device_delete(thrust::device_ptr< T > ptr,
  const size_t n = 1);

// 释放 device_reference
void thrust::device_free(thrust::device_ptr< void > ptr);

// malloc device_reference   与   device_free
thrust::device_ptr< void >
thrust::device_malloc(const std::size_t n);

// malloc device_reference    与   device_free
template <typename T>
thrust::device_ptr< T >
thrust::device_malloc(const std::size_t n);

// new device_reference    与  device_delete
template <typename T>
device_ptr< T > thrust::device_new(
	device_ptr< void > p,
  	const size_t n = 1);

// new device_reference    与  device_delete
template <typename T>
device_ptr< T > thrust::device_new(
	device_ptr< void > p,
  	const T & exemplar,
  	const size_t n = 1);

template <typename T>
device_ptr< T > thrust::device_new(const size_t n = 1);

template <typename T, typename CharT, typename Traits>
__host__ std::basic_ostream< CharT, Traits > &
thrust::operator<<(std::basic_ostream< CharT, Traits > & os,
  device_ptr< T > const & dp);

// 类型转换
template <typename T>
__host__ __device__ device_ptr< T >
thrust::device_pointer_cast(T * ptr);

// 类型转换
template <typename T>
__host__ __device__ device_ptr< T >
thrust::device_pointer_cast(device_ptr< T > const & dptr);

// 元素交换
template <typename T>
__host__ __device__ void
thrust::swap(
	device_reference< T > & x,
  	device_reference< T > & y);

template <typename T, typename charT, typename traits>
std::basic_ostream< charT, traits > &
thrust::operator<<(std::basic_ostream< charT, traits > & os,
  const device_reference< T > & y);

// malloc
template <typename DerivedPolicy>
__host__ __device__ pointer< void, DerivedPolicy >
thrust::malloc(
	const thrust::detail::execution_policy_base< DerivedPolicy > & system,
  	std::size_t n);

// malloc
template <typename T, typename DerivedPolicy>
__host__ __device__ pointer< T, DerivedPolicy >
thrust::malloc(
	const thrust::detail::execution_policy_base< DerivedPolicy > & system,
  	std::size_t n);

template <typename T, typename DerivedPolicy>
__host__ __device__ thrust::pair< thrust::pointer< T, DerivedPolicy >, typename thrust::pointer< T, DerivedPolicy >::difference_type >
thrust::get_temporary_buffer(
	const thrust::detail::execution_policy_base< DerivedPolicy > & system,
  	typename thrust::pointer< T, DerivedPolicy >::difference_type n);

// free 与 malloc 对应
template <typename DerivedPolicy, typename Pointer>
__host__ __device__ void
thrust::free(
	const thrust::detail::execution_policy_base< DerivedPolicy > & system,
  	Pointer ptr);

template <typename DerivedPolicy, typename Pointer>
__host__ __device__ void
thrust::return_temporary_buffer(
	const thrust::detail::execution_policy_base< DerivedPolicy > & system,
  	Pointer p,
  	std::ptrdiff_t n);

template <typename Pointer>
__host__ __device__ thrust::detail::pointer_traits< Pointer >::raw_pointer
thrust::raw_pointer_cast(Pointer ptr);

template <typename T>
__host__ __device__ detail::raw_reference< T >::type
thrust::raw_reference_cast(T & ref);

template <typename T>
__host__ __device__ detail::raw_reference< constT >::type
thrust::raw_reference_cast(const T & ref);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值