在 OpenCL 循环中的主机和内核代码变量之间传递值
Passing values between variables of host and kernel Code in a loop in OpenCL
由于某些矢量数据类型,我无法在主机代码和内核代码之间传递值。下面的code/explanation只是为了参考我的问题,我的代码更大更复杂。希望通过这个小例子,我能够解释我遇到问题的地方。如果还需要什么,请告诉我。
std::vector<vector<double>> output;
for (int i = 0;i<2; i++)
{
auto& out = output[i];
sum =0;
for (int l =0;l<3;l++)
{
for (int j=0;j<4; j++)
{
if (some condition is true)
{ out[j+l] = 0.;}
sum+= .....some addition...
}
out[j+l] = sum
}
}
现在我想从第二个循环开始并行化这段代码。这是我在主机代码中所做的:
cl::buffer out = (context,CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, output.size(), &output, NULL)
然后,我设置了参数
cl::SetKernelArg(0, out);
然后循环,
for (int i = 0,i<2, i++)
{
auto& out = output[i];
// sending some more arguments(which are changing accrding to loop) for sum operations
queue.enqueueNDRangeKernel(.......)
queue.enqueuereadbuffer(.....,&out,...)
}
在内核代码中:
__kernel void sumout(__global double* out, ....)
{
int l = get_global_id(0);
int j = get_global_id(1);
if (some condition is true)
{ out[j+l] = 0.; // Here it goes out of the loop then
return}
sum+= .....some addition...
}
out[j+l] = sum
}
所以现在,在 if 条件下 out[j+l] 在 loop 中得到 0。所以输出值是有规律地变化的。在普通代码中,它是指向向量的引用指针。在我的内核和主机代码中,我无法从 out 中读取 output 中的值。我想为每个 out[j+l] 读取 output[i] 中的值。但是由于这个缓冲区和向量,我很困惑。
只是为了更清楚地说明,output 是向量的向量,out 是输出向量的参考向量。我需要为 out 中的每个更改更新 output 中的值。由于这些是矢量,我将 out 作为 cl 缓冲区传递。我希望它是清楚的。
请让我知道,如果需要代码,我会尽量提供。
您正在向 opencl 发送向量指针(当然它们在指针级别上是连续的)但是整个数据在内存中并不连续,因为每个内部向量指向不同的内存区域。 Opencl 无法将主机指针映射到设备内存,并且此 api.
中没有这样的命令
您可以使用数组向量(最新版本)或纯数组。
由于某些矢量数据类型,我无法在主机代码和内核代码之间传递值。下面的code/explanation只是为了参考我的问题,我的代码更大更复杂。希望通过这个小例子,我能够解释我遇到问题的地方。如果还需要什么,请告诉我。
std::vector<vector<double>> output;
for (int i = 0;i<2; i++)
{
auto& out = output[i];
sum =0;
for (int l =0;l<3;l++)
{
for (int j=0;j<4; j++)
{
if (some condition is true)
{ out[j+l] = 0.;}
sum+= .....some addition...
}
out[j+l] = sum
}
}
现在我想从第二个循环开始并行化这段代码。这是我在主机代码中所做的:
cl::buffer out = (context,CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, output.size(), &output, NULL)
然后,我设置了参数
cl::SetKernelArg(0, out);
然后循环,
for (int i = 0,i<2, i++)
{
auto& out = output[i];
// sending some more arguments(which are changing accrding to loop) for sum operations
queue.enqueueNDRangeKernel(.......)
queue.enqueuereadbuffer(.....,&out,...)
}
在内核代码中:
__kernel void sumout(__global double* out, ....)
{
int l = get_global_id(0);
int j = get_global_id(1);
if (some condition is true)
{ out[j+l] = 0.; // Here it goes out of the loop then
return}
sum+= .....some addition...
}
out[j+l] = sum
}
所以现在,在 if 条件下 out[j+l] 在 loop 中得到 0。所以输出值是有规律地变化的。在普通代码中,它是指向向量的引用指针。在我的内核和主机代码中,我无法从 out 中读取 output 中的值。我想为每个 out[j+l] 读取 output[i] 中的值。但是由于这个缓冲区和向量,我很困惑。
只是为了更清楚地说明,output 是向量的向量,out 是输出向量的参考向量。我需要为 out 中的每个更改更新 output 中的值。由于这些是矢量,我将 out 作为 cl 缓冲区传递。我希望它是清楚的。 请让我知道,如果需要代码,我会尽量提供。
您正在向 opencl 发送向量指针(当然它们在指针级别上是连续的)但是整个数据在内存中并不连续,因为每个内部向量指向不同的内存区域。 Opencl 无法将主机指针映射到设备内存,并且此 api.
中没有这样的命令您可以使用数组向量(最新版本)或纯数组。