使用免费的分割故障主机结果 [英] using free for the host results in segmentation fault

查看:135
本文介绍了使用免费的分割故障主机结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一样的内核立方米文件:

I have a cu file with the kernel like:

__global__ void kernel(float* A,float *B, curandState* globalState,int Asize,int Bsize)
{
   ....

}

void kernel_wrapper(float** A_host,float** B_host, int Asize ,int Bsize)
{
   ...

    //allocate host memory 
    *A_host=(float*)malloc(Asize*sizeof(float));
    *B_host=(float*)malloc(Bsize*sizeof(float));

    //allocate device memory
    float* A_dev,*B_dev;
    gpuErrchk(cudaMalloc((void**) &A_dev,Asize* sizeof(float)));
    gpuErrchk(cudaMalloc((void**) &B_dev,Bsize* sizeof(float)));

    // copy arrays from host to device
    gpuErrchk(cudaMemcpy(A_dev, *A_host,Asize* sizeof(float), cudaMemcpyHostToDevice));
    gpuErrchk(cudaMemcpy(B_dev, *B_host,Bsize* sizeof(float), cudaMemcpyHostToDevice));

    ....
    kernel<<<dimGrid,dimBlock>>>(A_dev,B_dev, devStates,Asize,Bsize);

    // copy result from device to host
    gpuErrchk(cudaMemcpy(*A_host, A_dev,Asize* sizeof(float), cudaMemcpyDeviceToHost));
    gpuErrchk(cudaMemcpy(*B_host, B_dev,Bsize* sizeof(float), cudaMemcpyDeviceToHost));

    //clean up device memory
    gpuErrchk(cudaFree(A_dev));
    gpuErrchk(cudaFree(B_dev));
    gpuErrchk(cudaFree(devStates));
    //clean up host memory
    free(*A_host);
    free(*B_host);
}

和一个CPP文件,从中我打电话内核:

and a cpp file from which I am calling the kernel:

...
extern void kernel_wrapper(float** A,float** B, int Asize ,int Bsize);
...
int main()
{
    ...
    float* A;
    float* B;
    ...
    kernel_wrapper(&A,&B,Asize ,Bsize);
    ...
    free(A);
    free(B);

现在,使用

free(*A_host);
free(*B_host);

在铜文件结果

分段故障

如果我用cudaFree或cudaFreeHost(这是正确的,因为我与ALLOC未分配)显示无效的设备指针或无效参数。

If I use cudaFree or cudaFreeHost (which are not right because I am allocating with alloc) it shows "invalid device pointer" or "invalid argument".

如果我不会免费试用所有,程序运行正常。

If I will not use free at all ,the program runs fine.

这是为什么,什么是对这些内存分配相应的进程?

Why is that and what is the appropriate process regarding these memory allocations?

推荐答案

您在呼唤免费()在同一个三分球两次,这是无效的。在这code中的内存管理是奇怪和困惑。

You are calling free() twice on the same pointers, that's not valid. The memory management in this code is strange and confusing.

我想这是很有道理的,除去免费() kernel_wrapper()函数中调用;因为它设置为指针返回给调用者,它没有任何意义免费()在函数的内存。

I guess it makes the most sense to remove the free() calls inside the kernel_wrapper() function; since it's set up to return the pointers to the caller, it doesn't make sense to free() the memory in the function.

这篇关于使用免费的分割故障主机结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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