malloc works, cudaHostAlloc segfaults?

Posted by Mikhail on Stack Overflow See other posts from Stack Overflow or by Mikhail
Published on 2012-11-27T22:19:59Z Indexed on 2012/11/27 23:04 UTC
Read the original article Hit count: 360

Filed under:

I am new to CUDA and I want to use cudaHostAlloc. I was able to isolate my problem to this following code. Using malloc for host allocation works, using cudaHostAlloc results in a segfault, possibly because the area allocated is invalid? When I dump the pointer in both cases it is not null, so cudaHostAlloc returns something...

works

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

doesn't work

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

Standalone Code

#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 discared.");
                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;
        }
        free(in_h);
        return EXIT_SUCCESS;
}

~
Invocation

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

Details

Program is run in interactive mode on a cluster. I was told that an invocation of the program from the compute node pushes it to the cluster. Have not had any trouble with other home made toy cuda codes.

© Stack Overflow or respective owner

Related posts about cuda