pool_allocator轻量模板内存池

只能一次申请一个对象,没有线程锁。

template <typename T, size_t PAGE>
class pool_allocator
{
public:
	typedef pool_allocator    this_type;
	typedef T                 value_type;
	typedef value_type*       pointer;
	typedef const value_type* const_pointer;
	typedef value_type&       reference;
	typedef const value_type& const_reference;
	typedef std::size_t       size_type;
	typedef std::ptrdiff_t    difference_type;

	const static size_t PAGE_SIZE = PAGE;
private:
	struct node{
		node* next;
		//byte_t value[sizeof(value_type)];
		value_type value;
	};
	node* entry;
	static std::vector<void*> poolbuf;
public:
	pool_allocator() : entry()
	{
	}

	pool_allocator(const this_type&) : entry()
	{
	}

	~pool_allocator()
	{
		this->dispose();
	}

	this_type& operator=(const this_type&)
	{
		return *this;
	}

	bool operator==(const this_type&)
	{
		return true;
	}

	bool operator!=(const this_type&)
	{
		return false;
	}

	pointer allocate(size_type size = 1, const_pointer = 0)
	{
		assert(size == 1);
		if(!entry){
			node* buf = allocate_buffer();
			entry = buf;
		}
		node* n = entry;
		entry = entry->next;
		return this->address(n);
	}

	pointer allocate(size_type n, size_type alignment, const_pointer = 0)
	{
		assert(false);
		return null;
	}

	void deallocate(pointer p, size_type = 0)
	{
		//node * n = reinterpret_cast<node*>(p);
		node * n = reinterpret_cast<node*>(reinterpret_cast<byte_t*>(p) - sizeof(node*));
        n->next = entry;
		entry = n;
	}

	pointer reallocate(pointer ptr, size_type n)
	{
		assert(false);
		return null;
	}

	void dispose()
    {
        for(size_type i = 0; i < poolbuf.size(); ++i)
        {
			deallocate_buffer(poolbuf[i]);
		}
		poolbuf.clear();
		entry = null;
	}

	size_type max_size()const
	{
		return static_cast<size_type>(-1)/sizeof(T);
	}

	size_type size()const
	{
		return poolbuf.size() * PAGE_SIZE;
	}

	size_type free_size()const
    {
		size_type n = 0;
		node* p = entry;
        while(p){
            ++n;
            p = p->next;
        }
        return n;
	}

	void construct(pointer p, const value_type& x)
	{
		//new(p) value_type(x);
	}

	void construct(pointer p)
	{
		//new (p) value_type();
	}

	void destroy(pointer p)
	{
		//p->~T();
	}
private:
	node* allocate_buffer()
	{
		node* buf = new node[PAGE_SIZE];
		//node* buf = static_cast<node*>(malloc(PAGE_SIZE * sizeof(node)));

		for(size_type i=0; i<PAGE_SIZE; ++i){
			buf[i].next = buf + i + 1;
		}
		buf[PAGE_SIZE - 1].next = null;
		poolbuf.push_back(buf);
		return buf;
	}

	void deallocate_buffer(void* &buf)
	{
		node* p = static_cast<node*>(buf);
		delete []p;
		//free(buf);
		buf = null;
	}

	pointer address(node* n)
	{
		//return reinterpret_cast<pointer>(n->value);
		return &n->value;
	}
};

template<typename T, size_t P>
std::vector<void*> pool_allocator<T, P>::poolbuf = std::vector<void*>();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: b'tf_gpu_allocator=cuda_malloc_async'是什么意思? 这是一个TensorFlow配置参数,用于设置TensorFlow在GPU上的内存分配方式。具体来说,设置为cuda_malloc_async表示TensorFlow将使用异步的CUDA内存分配器,在GPU上为张量动态分配内存。这种方式可以提高GPU的利用率和性能,但也可能会导致一些额外的延迟。 ### 回答2: tf_gpu_allocator=cuda_malloc_async是TensorFlow的一个配置选项,其作用是将CUDA异步内存分配器用于TensorFlow的GPU分配器中。 在TensorFlow中,GPU分配器负责为张量分配显存。而在传统的同步内存分配器中,内存的分配和释放都是同步的,即TensorFlow执行的操作必须严格按照内存的分配和释放顺序进行。而采用了异步内存分配器后,TensorFlow可以并行地分配和释放内存,从而进一步提高GPU显存的利用率和TensorFlow运行效率。 通过使用tf_gpu_allocator=cuda_malloc_async,TensorFlow会将异步内存分配器(称为cudaMallocAsync)用于GPU分配器中,从而实现异步的显存分配和释放。此外,为了避免OOM(Out of Memory)错误,TensorFlow还提供了一种称为TF_GPU_GROWTH的标志,用于一旦分配的显存不足时自动增加GPU显存。 需要注意的是,采用异步内存分配器虽然可以提高TensorFlow的运行效率,但也可能导致一些问题。例如,当进行GPU显存调优时,需要格外小心。因此,在使用tf_gpu_allocator=cuda_malloc_async时需要谨慎处理,尤其是在对GPU显存要求较高的场景下。 ### 回答3: tf_gpu_allocator=cuda_malloc_async是TensorFlow中的一种GPU内存分配器。它使用了CUDA异步分配内存的功能,可以显著提高GPU内存分配的性能。 在GPU内存分配方面,传统的方式是使用CUDA的cudaMalloc()函数进行同步分配。这种方式的效率较低,因为当CPU请求分配内存时,GPU需要停止当前的计算任务,并等待CPU将内存分配好后再继续计算。这样的过程显然会降低GPU的计算能力,影响整个系统的性能。 与之相反,tf_gpu_allocator=cuda_malloc_async采用了CUDA的异步内存分配机制。当CPU请求分配内存时,GPU并不直接停止当前计算任务,而是在后台启动一个异步的内存分配任务,并继续进行当前的计算任务。当分配任务完成后,GPU会立即通知CPU,告诉它分配的内存的地址。CPU再将这些信息传给TensorFlow,从而使得TensorFlow能够根据需要使用这些内存。这样,GPU的计算能力就可以得到最大的保护,系统的性能也能够得到有效提高。 总的来说,tf_gpu_allocator=cuda_malloc_async是一种非常高效的GPU内存分配器,它可以有效提高GPU的效率,并提升系统的整体性能。在深度学习等需要大量计算的场景下,这种技术的应用至关重要,可以大大加快计算速度和提高训练效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值