cudaMemcpy中的参数无效

时间:2015-02-12 16:53:31

标签: c cuda

我无法追踪cudaMemcpy调用的无效参数来源,以下是相关代码:

在gpu_memory.cu中,我为设备指针声明并分配内存:

#define cudaErrorCheck(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
    if (code != cudaSuccess)
    {
        fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
        if (abort) exit(code);
    }
}
...
__device__ double* conc;
...
__global__ void pointer_set_kernel(..., double* conc_in...) {
...
   conc = conc_in;
...
}

double* d_conc;
...
//memory initialization
void initialize_gpu_memory(int NUM, int block_size, int grid_size) {
    ...
    cudaErrorCheck(cudaMalloc((void**)&d_conc, NUM * 53 * sizeof(double)));
    ...
    pointer_set_kernel<<<1, 1>>>(...d_conc...);
    cudaErrorCheck( cudaPeekAtLastError() ); // Checks for launch error
    cudaErrorCheck( cudaThreadSynchronize() ); // Checks for execution error
}

接下来在另一个文件(mechanism.cu)中,我将设备指针声明为extern以将数据复制到它:

extern __device__ double* conc;
void write_jacobian_and_rates_output(int NUM, int block_size, int grid_size) {
    ...
    initialize_gpu_memory(NUM, block_size, grid_size);
    ...
    //get address of conc
    double* d_conc;
    cudaErrorCheck(cudaGetSymbolAddress((void **)&d_conc, conc));
    //populate the concentrations on the host
    double conc_host[NSP];
    double* conc_host_full = (double*)malloc(NUM * NSP * sizeof(double));
    //populate the concentrations
    get_concentrations(1.01325e6, y_host, conc_host);
    for (int i = 0; i < NUM; ++i) {
        for (int j = 0; j < NSP; ++j) {
           conc_host_full[i + j * NUM] = conc_host[j];
        }
    }
    //check for errors, and copy over
    cudaErrorCheck( cudaPeekAtLastError() ); // Checks for launch error
    cudaErrorCheck( cudaThreadSynchronize() ); // Checks for execution error
    cudaErrorCheck(cudaMemcpy(d_conc, conc_host_full, NUM * 53 * sizeof(double), cudaMemcpyHostToDevice));
    ...
}

我在最后一行收到错误(Memcpy)。似乎initialize_gpu_memory函数正常工作,这是malloc和pointer_set_kernel之后的cuda-gdb检查:

p d_conc 
$1 = (double *) 0x1b03236000
p conc
$2 = (@generic double * @global) 0x1b03236000

并在write_jacobian_and_rates函数中:

p d_conc
$3 = (double *) 0x1b02e20600
p conc
$4 = (@generic double * @global) 0x1b03236000

我不知道为什么写入函数中的d_conc在cudaGetSymbolAddress调用之后指向不同的内存位置,或者为什么我在memcpy上得到一个无效的参数。我确定我做的事情很愚蠢,但对于我的生活,我看不到它。非常感谢帮助追踪这个来源,谢谢!

1 个答案:

答案 0 :(得分:1)

您的代码段中没有任何内容表明externd_conc范围,因此,d_conc的两个实例在两个不同的文件中是完全不同的对象。所以,
在此上下文中 :( mechanism.cu

double* d_conc;  //you create a new variable in this context
cudaErrorCheck(cudaGetSymbolAddress((void **)&d_conc, conc));
//populate the concentrations on the host
double conc_host[NSP];
double* conc_host_full = (double*)malloc(NUM * NSP * sizeof(double));  

没有分配内存 d_conc

我看到你已经在gpu_memory.cu的上下文中为它的变量分配了内存,但它的名称相同,但不是在这里发生错误。

这也似乎解决了您的问题: 我不知道为什么写入函数中的d_conc指向cudaGetSymbolAddress调用后的不同内存位置 < / p>