Skip to content

Temporary memory

Raul edited this page Nov 17, 2020 · 3 revisions

System offers an cached allocator (a pool allocator) as defined in [1]. Allocations using this allocator will be cached so that if the same number of bytes is requested/freed several times only the first request will result in an actual cudaMalloc and only the last free will result in an actual cudaFree.

Usage

System provides two aliases for convenience:

template<class T>
using System::allocator<T> = polymorphic_allocator<T, pool_memory_resource_adaptor<device_memory_resource>>;
template<class T>
using System::allocator_thrust<T> = polymorphic_allocator<T, pool_memory_resource_adaptor<device_memory_resource>, thrust::cuda::pointer<T>>;

If UAMMD_DEBUG is defined then System will use managed_memory_resource, which makes memory allocated with System::allocator be seamlessly accessible from the CPU and GPU.
Both do the same thing as described, but the second one provides thrust::pointers instead of regular pointers, so that it can be used with thrust::device_vector like this:

template<class T>
using cached_vector<T> = thrust::device_vector<T, System::allocator_thrust<T>>;
int main(){
cached_vector<int> vec(100); //Results in a cudaMalloc of 100*sizeof(int) bytes
vec.clear(); //Does not result in a cudaFree
cached_vector<int> vec1(100); //Requests 100*sizeof(int) bytes and gets handed the pointer vec just freed
return 0;
} //The memory is freed at some unspecified point

This construction is useful when a certain algorithm needs some GPU memory buffer

References

[1] https://github.com/RaulPPelaez/UAMMD/wiki/Allocator









Clone this wiki locally