有没有更好/更干净/更优雅的方式来分配和释放cuda? [英] Is there a better/cleaner/more elegant way to malloc and free in cuda?

查看:84
本文介绍了有没有更好/更干净/更优雅的方式来分配和释放cuda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试cudaMalloc一堆设备指针,如果任何malloc都不起作用,请正常退出.我有正常运行的代码-但很ated肿,因为如果一个失败了,我必须cudaFree以前我之前分配的所有内容.因此,现在我想知道是否有更简洁的方法来实现这一目标.显然,我无法释放尚未分配的内容-肯定会导致问题.

I am trying to cudaMalloc a bunch of device pointers, and gracefully exit if any of the mallocs didn't work. I have functioning code - but bloated because I have to cudaFree everything I'd previously malloc'd if one fails. So now I am wondering if there is a more succinct method of accomplishing this. Obviously I can't free something that hasn't been malloc'd - that will definitely cause problems.

下面是我要使代码更优雅的代码段.

Below is the snippet of code I am trying to make more elegant.

    //define device pointers
    float d_norm, *d_dut, *d_stdt, *d_gamma, *d_zeta;

    //allocate space on the device for the vectors and answer
    if (cudaMalloc(&d_norm, sizeof(float)*vSize) != cudaSuccess) {
            std::cout << "failed malloc";
            return;
    };

    if (cudaMalloc(&d_data, sizeof(float)*vSize) != cudaSuccess) {
            std::cout << "failed malloc";
            cudaFree(d_norm);
            return;
    };

    if (cudaMalloc(&d_stdt, sizeof(float)*wSize) != cudaSuccess) {
            std::cout << "failed malloc";
            cudaFree(d_norm);
            cudaFree(d_data);
            return;
    };

    if (cudaMalloc(&d_gamma, sizeof(float)*vSize) != cudaSuccess) {
            std::cout << "failed malloc";
            cudaFree(d_norm);
            cudaFree(d_dut);
            cudaFree(d_stdt);
            return;
    };

    if (cudaMalloc(&d_zeta, sizeof(float)*w) != cudaSuccess) {
            std::cout << "failed malloc";
            cudaFree(d_norm);
            cudaFree(d_dut);
            cudaFree(d_stdt);
            cudaFree(d_gamma);
            return;
    };

这是一个简化的版本,但是您可以看到它是如何继续构建的.实际上,我正在尝试分配大约15个数组.它开始变得丑陋-但可以正常工作.

This is a shortened version, but you can see how it just keeps building. In reality I am trying to malloc about 15 arrays. It starts getting ugly - but it works correctly.

有什么想法吗?

推荐答案

一些可能性:

  1. cudaDeviceReset() 将免费发布所有设备分配,而不必遍历指针列表.

  1. cudaDeviceReset() will free all device allocations, without you having to run through a list of pointers.

如果您打算退出(应用程序),则无论如何在应用程序终止时将自动释放所有设备分配. cuda运行时检测与应用程序设备上下文关联的进程的终止,并在该时刻擦除该上下文.因此,如果您只是要退出,则不要执行任何cudaFree()操作应该是安全的.

if you intend to exit (the application), all device allocations are freed automatically upon application termination anyway. The cuda runtime detects the termination of the process associated with an application's device context, and wipes that context at that point. So if you're just going to exit, it should be safe to not perform any cudaFree() operations.

这篇关于有没有更好/更干净/更优雅的方式来分配和释放cuda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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