有没有CUDA智能指针? [英] Is there a CUDA smart pointer?

查看:430
本文介绍了有没有CUDA智能指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有,当抛出异常时,释放 cudaMalloc ed内存的标准方法是什么? (注意,我无法使用Thrust。)

解决方案

您可以使用 RAII 成语,并将您的 cudaMalloc() cudaFree / code>分别调用你的对象的构造函数和析构函数。



一旦异常被抛出,你的析构函数将被释放分配的内存。 / p>

如果你将这个对象包装到一个智能指针中(或者使它像一个指针一样),你会得到你的CUDA智能指针。


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.)

解决方案

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.

这篇关于有没有CUDA智能指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆