Arrays 将用malloc制作的数组传递到cuda 我对C++非常陌生,CUDA更是如此。因此,如果这个问题显然在其他地方得到了回答,我表示歉意。我尽我所能搜索答案库,但我能找到的最接近我问题的答案是

Arrays 将用malloc制作的数组传递到cuda 我对C++非常陌生,CUDA更是如此。因此,如果这个问题显然在其他地方得到了回答,我表示歉意。我尽我所能搜索答案库,但我能找到的最接近我问题的答案是,arrays,cuda,Arrays,Cuda,然而,这个答案涉及将2d数组传递到cuda内存。这比我想的要复杂得多 我知道,为了将标准阵列传递到cuda内核,您可以执行以下操作: int array[size]; int *pointer; cudaMalloc((void**) &pointer, size*sizeof(int)); cudaMemcpy(pointer, array, size*sizeof(int), cudaMemcpyHostToDevice); 然后在我的内核中,我接收到如下内容: __globa

然而,这个答案涉及将2d数组传递到cuda内存。这比我想的要复杂得多

我知道,为了将标准阵列传递到cuda内核,您可以执行以下操作:

int array[size];
int *pointer;

cudaMalloc((void**) &pointer, size*sizeof(int)); 
cudaMemcpy(pointer, array, size*sizeof(int), cudaMemcpyHostToDevice);
然后在我的内核中,我接收到如下内容:

__global__ void kernel(int *array){
  int bid = blockIdx.x;
  array[i] = whatever; // Fill the array
}
int *differenceArray = (int*)malloc(sizeof(int)*1280*720);
int *differenceArray;
CUDA_CALL(cudaMalloc((void**) &differenceArray, 1280*720*sizeof(int)));
CUDA_CALL(cudaMemcpy(differenceArray, array, 1280 * 720*sizeof(int), cudaMemcpyHostToDevice));
         //          (dev ptr)  <--- (host ptr)
然而,仅使用上面的代码我就遇到了一个问题。我需要整数数组是1920*1080*4字节长的图像处理的东西。但是,当我使用上面的代码使数组达到这个大小时,程序崩溃了

我从中发现是因为我超出了我的堆栈大小。所以我学会了像这样为数组分配空间:

__global__ void kernel(int *array){
  int bid = blockIdx.x;
  array[i] = whatever; // Fill the array
}
int *differenceArray = (int*)malloc(sizeof(int)*1280*720);
int *differenceArray;
CUDA_CALL(cudaMalloc((void**) &differenceArray, 1280*720*sizeof(int)));
CUDA_CALL(cudaMemcpy(differenceArray, array, 1280 * 720*sizeof(int), cudaMemcpyHostToDevice));
         //          (dev ptr)  <--- (host ptr)
但现在我很困惑如何将其传递到cuda内核中。如果我尝试:

CUDA_CALL(cudaMalloc((void**) &differenceArray, 1280*720*sizeof(int)));
CUDA_CALL(cudaMemcpy(differenceArray, 1280 * 720*sizeof(int), cudaMemcpyHostToDevice));
我得到这个错误:

error : argument of type "unsigned int" is incompatible with parameter of type "const void *"
任何帮助都将不胜感激!谢谢大家!

首先研究memcpy是如何工作的。您以概念上类似的方式使用cudaMemcpy。前3个参数基本相同

您在这里遇到了堆栈问题:

int array[size];
所以正确的做法不是这样:

int *differenceArray = (int*)malloc(sizeof(int)*1280*720);
但这是:

int *array = (int*)malloc(sizeof(int)*1280*720);
当然,还要删除前面的数组定义

通过该更改,cudaMemcpy操作如下所示:

__global__ void kernel(int *array){
  int bid = blockIdx.x;
  array[i] = whatever; // Fill the array
}
int *differenceArray = (int*)malloc(sizeof(int)*1280*720);
int *differenceArray;
CUDA_CALL(cudaMalloc((void**) &differenceArray, 1280*720*sizeof(int)));
CUDA_CALL(cudaMemcpy(differenceArray, array, 1280 * 720*sizeof(int), cudaMemcpyHostToDevice));
         //          (dev ptr)  <--- (host ptr)

您忘记了cudaMemcpy的源参数。@MilesBudnek源参数是什么?它是数组的指针吗?我是否可以通过使用前面带a&的数组名,以与第一个代码相同的方式传递它?旁白:IDK关于Cuda,但对于16位int/unsigned,sizeofint*1280*720可能与1280*720*sizeofint不同。建议从sizeofint*…cudaMemcpydifferenceArray开始,1280*720*sizeofint,cudamemcpyhostodevice有3个参数,当然它缺少src指针。哇,你确实是对的。我在youtube上看了一段5分钟的视频,讲述memcpy是如何工作的,一切都变得更有意义。谢谢你的帮助,谢谢!