将交错数组传递给 OpenCL 中的内核

passing a jagged array to a kernel in opencl

本文关键字:OpenCL 内核 数组      更新时间:2023-10-16

所以我知道如何在 CUDA 中做到这一点,但基本上,我想将少量 _global ptrs 传递给函数,然后将这些指针加载到本地或私有中(因为数量很少,而且我已经在内核中有一个本地内存围栏, 我不确定哪个是最快的,我将在让它工作后通过实验确定这一点)。所以我这样写内核:

__kernel foo(
  __global int* img,
  __global int** img_history,
  __private int** private_history,
  uint history_length)//could be local
{
    for (int i = 0; i < history_length; i++)
       private_history[i] = img_history[i];
}

澄清一下,在 cuda 中我是这样做的

__global__ foo(int* img, int** img_history, uint history_length)
{
   int* private_history[10];//max values 10
   for (int i = 0; i < history_length; i++)
      private_history[i] = img_history[i];
 }

并加载它

int** host_array = new int*[history_length];
for (int i = 0; i < history_length; i++)
    cudaMalloc(host_array+i,size);
int** device_array;
cudaMalloc(&device_array,sizeof(int*)*history_length);
cudaMemcpy(device_array, host_array,sizeof(int*)*history_length,cudaMemcpyHostToDevice)

但是,我收到错误error: invalid address space for pointee of pointer argument to __kernel function.正确的方法是什么?

我不知道你在 CUDA 中的表现如何。但这完全禁止作为 OpenCL 内核的参数。

不能将指针值复制到设备,然后直接使用它,因为内存地址不同。

为此,您需要:

  1. 仅复制引用图像表的img_history索引(而不是指针)。
  2. 随心所欲地使用thouse索引进行操作(整数操作)。
  3. 使用这些索引访问图像表或执行任何您想要的操作。如果您需要使用这些索引来访问 img,那么它必须是内核的参数。你必须复制所有这些。(全长 img 数组)

例:

__kernel foo(
  __global int* img,
  __global int* img_history,
  __private int* private_history,
  uint history_length)//could be local
{
    for (int i = 0; i < history_length; i++)
       private_history[i] = img_history[i];
    /* img[private_history[i]] */ //Use it as you wish
}