gpt4 book ai didi

c - cudaMemcpy 中的参数无效

转载 作者:行者123 更新时间:2023-11-30 15:24:18 24 4
gpt4 key购买 nike

我无法追踪 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

我不知道为什么 write 函数中的 d_conc 在 cudaGetSymbolAddress 调用后指向不同的内存位置,或者为什么我在 memcpy 上收到无效参数。我确信我在做一些愚蠢的事情,但我一生都看不到它。如果您能帮助追查其来源,我们将不胜感激,谢谢!

最佳答案

您的代码片段中没有任何内容表明您具有 d_concextern 范围,因此,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 的上下文中分配了内存,因为它的同名变量,但不是在这里,错误发生的地方。

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

关于c - cudaMemcpy 中的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28483215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com