Cuda 2D内存分配和复制问题

发布于 2024-10-13 08:07:40 字数 514 浏览 2 评论 0原文

   int **F;
   int **dev_pF;
   size_t *pitchF;
   void init_cuda_mem(int mF,int mT,int nF,int nT){

    cudaMallocPitch((void **)dev_pF,pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2);
    cudaMemcpy2D((void *)dev_pF,*pitchF,(void *)pF,*pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2,cudaMemcpyHostToDevice);
   }

大家好,

在上面的代码片段中,我正在尝试使用 cudaMallocPitch 分配一个 2D 数组

,然后使用 cudaMemcpy2D 将该数组从主机复制到设备,

不幸的是它崩溃了,我认为错误是(我认为)在 cudaMemcpy2D 上,

有人可以帮助我吗?请找到它

   int **F;
   int **dev_pF;
   size_t *pitchF;
   void init_cuda_mem(int mF,int mT,int nF,int nT){

    cudaMallocPitch((void **)dev_pF,pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2);
    cudaMemcpy2D((void *)dev_pF,*pitchF,(void *)pF,*pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2,cudaMemcpyHostToDevice);
   }

Well hello everyone

in the above snippet i am trying to allocate a 2D array using cudaMallocPitch

and then copying that array using cudaMemcpy2D from the host to the device

unfortunately it crashes and i think the error is (i think) at the cudaMemcpy2D

can someone help me locate it please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2024-10-20 08:07:40

我认为问题在于您对指针和指向指针的指针有误解。

您可能应该执行以下操作:

int *dev_pF;
size_t pitchF;

void init_cuda_mem(int mF,int mT,int nF,int nT) {
    cudaMallocPitch((void **)&dev_pF, &pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2);
    cudaMemcpy2D((void *)dev_pF,pitchF,(void *)pF, pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2,cudaMemcpyHostToDevice);
}

请注意,您现在在 cudaMallocPitch 调用中获取变量的地址,然后在第二次调用中直接使用它们,这是不同的。

在原始代码中,您首先要求 cudaMalloc 将指针存储在 dec_pF 恰好指向的任何内存中,并将大小存储在itchF 指向的任何内存中。这两者都是统一的,所以那里可能会发生灾难。在第二次调用中,您将 dev_pF 从指针转换为指向常规指针的指针,因此您告诉 memcpy 从存储指针的位置开始复制内存,而不是从存储分配的内存的位置开始。由于指向指针的指针和大小最初都是统一的,所以几乎任何事情都可能发生。

另外,您正在使用我在原始代码中看不到的 pF 指针,请确保它已正确初始化。

I think the issue is that you are mistaken with regards to pointers and pointers to pointers.

You should probably do something on the lines of:

int *dev_pF;
size_t pitchF;

void init_cuda_mem(int mF,int mT,int nF,int nT) {
    cudaMallocPitch((void **)&dev_pF, &pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2);
    cudaMemcpy2D((void *)dev_pF,pitchF,(void *)pF, pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2,cudaMemcpyHostToDevice);
}

Note the difference that you are now taking the address of the variables in the cudaMallocPitch call and then just using them directly in the second call.

In your original code, you were first asking cudaMalloc to store a pointer in whatever memory dec_pF happened to point to and store the size in whatever memory pitchF was pointing to. Both of these where unitialized, so disaster could occur there. In the second call you were converting dev_pF from a pointer to pointer to a regular pointer, so you are telling the memcpy to copy memory starting where the pointer was stored rather than where the allocated memory was stored. And since both the pointer to pointer and the size where unitialized at first, pretty much anything could happen.

Also, you are making use of a pF pointer that I cannot see in the original code, make sure it is initialized properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文