1

我是 CUDA 新手,我想使用cudaHostAlloc. 我能够将我的问题与以下代码隔离开来。malloc用于主机分配工作,使用导致cudaHostAlloc段错误,可能是因为分配的区域无效?当我在这两种情况下转储指针时它都不为空,所以cudaHostAlloc返回一些东西......

作品

    in_h = (int*) malloc(length*sizeof(int)); //works
    for (int i = 0;i<length;i++)
            in_h[i]=2; 

不工作

    cudaHostAlloc((void**)&in_h,length*sizeof(int),cudaHostAllocDefault); 
    for (int i = 0;i<length;i++)
            in_h[i]=2; //segfaults

独立代码

#include <stdio.h>
void checkDevice()
{
        cudaDeviceProp info;
        int deviceName;
        cudaGetDevice(&deviceName);
        cudaGetDeviceProperties(&info,deviceName);
        if (!info.deviceOverlap)
        {
                printf("Compute device can't use streams and should be discarded.");
                exit(EXIT_FAILURE);
        }
}
int main()
{
        checkDevice();
        int *in_h;
        const int length = 10000;
        cudaHostAlloc((void**)&in_h,length*sizeof(int),cudaHostAllocDefault);
        printf("segfault comming %d\n",in_h);
        for (int i = 0;i<length;i++)
        {
                in_h[i]=2; // Segfaults here
        }
        return EXIT_SUCCESS;
}

~
调用

[id129]$ nvcc fun.cu 
[id129]$ ./a.out 
segfault comming 327641824
Segmentation fault (core dumped)

细节

程序在集群上以交互模式运行。有人告诉我,从计算节点调用程序会将其推送到集群。其他自制玩具 cuda 代码没有任何问题。

编辑

cudaError_t err = cudaHostAlloc((void**)&in_h,length*sizeof(int),cudaHostAllocDefault);
printf("Error status is %s\n",cudaGetErrorString(err));

给驱动错误...

Error status is CUDA driver version is insufficient for CUDA runtime version
4

2 回答 2

3

始终检查错误。很可能cudaHostAlloc无法分配任何内存。如果它失败了,你不是在逃避,而是在写入未分配的地址空间。使用时malloc,它会按要求分配内存并且不会失败。但是有些情况下 malloc 也可能导致失败,因此最好在写入指针之前对其进行检查。

对于未来,最好做这样的事情

int *ptr = NULL;
// Allocate using cudaHostAlloc or malloc
// If using cudaHostAlloc check for success 
if (!ptr) ERROR_OUT();
// Write to this memory

编辑(对问题中的编辑的回应)

错误消息表明与工具包相比,您的驱动程序较旧。如果您不想被卡住一段时间,请尝试下载与您的驱动程序兼容的旧版本的 cuda 工具包。您可以将它安装在您的用户帐户中并临时使用它的 nvcc + 库。

于 2012-11-27T23:21:13.497 回答
0

您的段错误不是由对 cudaHostAlloc 分配的内存块的写入引起的,而是由尝试“释放”从 cudaHostAlloc 返回的地址引起的。我能够使用您提供的代码重现您的问题,但是用 cudaFreeHost 替换 free 为我修复了段错误。

cudaFreeHost

于 2012-11-27T23:02:45.510 回答