Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a CUDA smart pointer?

If not, what is the standard way to free up cudaMalloced memory when an exception is thrown? (Note that I am unable to use Thrust.)

like image 418
mchen Avatar asked May 12 '13 16:05

mchen


People also ask

Does pointer arithmetic work in CUDA?

Pointer arithmetic does work just fine in CUDA. You can add an offset to a CUDA pointer in host code and it will work correctly (remembering the offset isn't a byte offset, it is a plain word or element offset).

What is the difference between smart pointers and unique pointers?

And to use smart pointers memory header file must be included as it contains the necessary class and member methods used by the smart pointers. A unique pointer is the type of smart pointer that owns an object to which it points i.e. two unique pointers can not point to the same object.

What is a smart pointer in C++?

A smart pointer is an object that acts as a wrapper around the raw pointer in C++. Smart pointer like a raw pointer point to objects but is more enhanced in terms of functionality.

How to add an offset to a CUDA pointer?

You can add an offset to a CUDA pointer in host code and it will work correctly (remembering the offset isn't a byte offset, it is a plain word or element offset).


2 Answers

You can use RAII idiom and put your cudaMalloc() and cudaFree() calls to the constructor and destructor of your object respectively.

Once the exception is thrown your destructor will be called which will free the allocated memory.

If you wrap this object into a smart-pointer (or make it behave like a pointer) you will get your CUDA smart-pointer.

like image 59
Sergey K. Avatar answered Sep 17 '22 05:09

Sergey K.


You can use this custom cuda::shared_ptr implementation. As mentioned above, this implementation uses std::shared_ptr as a wrapper for CUDA device memory.

Usage Example:

std::shared_ptr<T[]> data_host =  std::shared_ptr<T[]>(new T[n]);
.
.
.

// In host code:
fun::cuda::shared_ptr<T> data_dev;
data_dev->upload(data_host.get(), n);
// In .cu file:
// data_dev.data() points to device memory which contains data_host;
 

This repository is indeed a single header file (cudasharedptr.h), so it will be easy to manipulate it if is necessary for your application.

like image 23
Mahdi Avatar answered Sep 21 '22 05:09

Mahdi