c++ - 如果调用 cudaMalloc,简单的控制台程序将不会退出

标签 c++ windows cuda

如果执行 cudaMalloc 调用,下面的简单程序永远不会退出。仅注释掉 cudaMalloc 会导致它正常退出。

#include <iostream>
using std::cout;
using std::cin;

#include "cuda.h"
#include "cutil_inline.h"

void PrintCudaVersion(int version, const char *name)
{
    int versionMaj = version / 1000;
    int versionMin = (version - (versionMaj * 1000)) / 10;
    cout << "CUDA " << name << " version: " << versionMaj << "." << versionMin << "\n";
}

void ReportCudaVersions()
{
    int version = 0;
    cudaDriverGetVersion(&version);
    PrintCudaVersion(version, "Driver");

    cudaRuntimeGetVersion(&version);
    PrintCudaVersion(version, "Runtime");
}

int main(int argc, char **argv)
{
    //CUresult r = cuInit(0);                 << These two lines were in original post
    //cout << "Init result: " << r << "\n";   << but have no effect on the problem

    ReportCudaVersions();

    void *ptr = NULL;
    cudaError_t err = cudaSuccess;
    err = cudaMalloc(&ptr, 1024*1024);
    cout << "cudaMalloc returned: " << err << "  ptr: " << ptr << "\n";
    err = cudaFree(ptr);
    cout << "cudaFree returned: " << err << "\n";

    return(0);
 }

这是在 Windows 7、CUDA 4.1 驱动程序、CUDA 3.2 运行时上运行。我跟踪了从 main 到 CRT 到 ExitProcess() 的返回,它永远不会返回(如预期的那样),但该过程也永远不会结束。从 VS2008 我可以停止调试 OK。从命令行,我必须终止控制台窗口。

程序输出:

Init result: 0
CUDA Driver version: 4.1
CUDA Runtime version: 3.2
cudaMalloc returned: 0  ptr: 00210000
cudaFree returned: 0

我尝试使分配量太大,以至于 cudaMalloc 会失败。确实是报错了,但是程序还是不退出。所以它显然只与调用 cudaMalloc 有关,而不是分配内存的存在。

关于这里发生的事情有什么想法吗?

编辑:我在第二句话中错了 - 我必须消除 cudaMalloc 和 cudaFree 才能让程序退出。留下任何一个都会导致挂断。

编辑:虽然有很多关于 CUDA 驱动程序版本向后兼容这一事实的引用资料,但当我将驱动程序恢复到 V3.2 时,这个问题就消失了。

最佳答案

您似乎将驱动程序 API (cuInit) 与运行时 API (cudaMalloc) 混合在一起。

我不知道幕后是否发生(或应该发生)任何有趣的事情,但您可以尝试的一件事是删除 cuInit 并看看会发生什么。

关于c++ - 如果调用 cudaMalloc,简单的控制台程序将不会退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8526742/

相关文章:

c++ - 对 std::vector 使用 std::copy_if 时出现断言错误

c++ - Emacs 语义无法在 Windows 上正确解析文件

regex - 在字符串中查找日期和时间并在 Windows 或 cmd 中使用 sed 重新格式化删除空格

c++ - 从结构启动 Cuda Call

c++ - Vim:将连续的行与空格对齐

android - 如何将 OpenCV 集成到 Qt Creator Android 项目中

c++ - C++中的引用调用

c# - 在 native 函数回调线程上运行异步任务继续

编译多个 cuda 文件(具有动态并行性)和 MPI 代码

cuda - CUDA 内核中的不同线程组