在OpenCL内核中传递vector的值 [英] passing values of vector in the OpenCL kernel

查看:91
本文介绍了在OpenCL内核中传递vector的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了带有某些值的向量.然后为该向量创建一个cl_buffer,并使用内核参数将其传递给OpenCL内核.像这样:

I have created a vector with some values. Then Created a cl_buffer for that vector and pass it to the OpenCL kernel using kernel Arguments. Like this:

在主机代码中:

std::vector<cl_double> inp;
inp.resize(1024);
for( int i = 0; i<1024;i++)
{
  inp[i] = i;
}
        filter_kernel = cl::Buffer(context,CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR,sizeof(cl_double)*inp.size(),(void*)&inp[0],&err); // also tried (void*)inp.data()

kernel.setArg(0, filter_kernel);

内核代码中:

__kernel void test(__global double* inp)
 {
   for(int m = 0;m<10;m++)
   {
    printf("inp values are : %d \n",inp[m]);
   }
 }

这只是展示如何将向量中的值传递到程序中的OpenCL内核的示例.这有什么问题吗?当我打印这些值时,每次都会得到一些随机的垃圾值.

This is just and example of showing, how I am passing values in the vector to OpenCL kernel in my program. Is there something wrong with it? As when I print the values, I am getting some random garbage values every time.

我正在使用MacOS系统和Xcode.设备是Intel HD graphics 4000

I am using MacOS system and Xcode. Device is Intel HD graphics 4000

推荐答案

我正在编译答案,因为所有要点(在注释中)使它一起工作.

I am just compiling the answer, as all the points(in comments) made it work together.

1)将数据类型更改为从double浮点型,因为Intel无法正确使用double并给出错误的结果.

1) change the data type to float from double as Intel does not work with double properly and gives incorrect result.

2)另一个错误是大小,在缓冲区中传递大小和在读缓冲区期间传递大小时,应该是inp * sizeof(float),因为我们正在使用float而不是现在加倍!

2) Other mistake was of size, while passing the size in buffer and during readbuffer, it should be inp*sizeof(float) as we are using float and not double now!

这篇关于在OpenCL内核中传递vector的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆