clSetKernelArg,size参数(clSetKernelArg, size parameter)

编程入门 行业动态 更新时间:2024-10-26 20:27:51
clSetKernelArg,size参数(clSetKernelArg, size parameter)

我是否必须了解clSetKernelArg()的OpenCL文档中arg_size参数的描述,或者我可以安全地输入:clSetKernelArg([parameter index],sizeof(A),(void *)&A)? ......独立于A是什么? 在我的情况下,A可能是一个结构,我不确定是否存在填充问题。

谢谢Daniel Dekkers

Do I have to understand the description of the arg_size parameter in the OpenCL documentation of clSetKernelArg() or could I safely just type: clSetKernelArg([parameter index], sizeof(A), (void*) &A)? ...independent of what A is? In my case A might be a struct, I'm not sure if there could be padding problems.

Thanks, Daniel Dekkers

最满意答案

你必须通过这个:

clSetKernelArg(kernel, Arg_index, sizeof(Arg), &Arg)

哪里:

内核 :您要设置参数的内核 Arg_index :索引(第一个为0,第二个为1,依此类推),有时您只想更改1个参数 Arg :你要设置的参数。 通常只是一个cl_mem缓冲区对象,它包含一个大数据数组,但它也可能是一个常量值。

注意:如果是常量值,则不得超过设备的常量内存。 通常,此处仅使用单个整数/字符/浮点数或简单结构。

示例 :对于此内核:

__kernel void mykernel (__global float *inout, int num){ inout[get_global_id(0)] = num; }

您可以设置如下参数:

cl_mem my_buffer = clCreateBuffer(...); clSetKernelArg(kernel, 0, sizeof(my_buffer), &my_buffer); int my_int = 50; clSetKernelArg(kernel, 1, sizeof(my_int), &my_int);

关于结构的问题,你不能使用结构...:

不使用标准cl数据类型( cl_int -> OK , int -> unsafe! )。 使用任何类型的指针。 使用嵌套结构。 里面有任何其他类(即:std :: vector <>) 他们需要某种特殊的对齐方式。

这个结构是有效的:

//Host side struct my_struct{ cl_int objectid; cl_float3 speed; cl_float3 direction; }; //Kernel arg my_struct a; a.objectid = 100; ... clSetKernelArg(kernel, 1, sizeof(my_struct), &a); //Kernel side typedef struct { int objectid; float3 speed; float3 direction; } my_struct; //Kernel declaration __kernel void mykernel (__global float *inout, my_struct data){ inout[get_global_id(0)] = (float)data.objectid; }

You have to pass this:

clSetKernelArg(kernel, Arg_index, sizeof(Arg), &Arg)

Where:

Kernel: The kernel you want to set up an argument Arg_index: The index (0 for first, 1 for second, and so on), sometimes you just want to change 1 arg Arg: The argument you want to set up. Typically is just a cl_mem buffer object that hold a larg array of data, but it might also be a constant value.

NOTE: If it is a constant value, it must not exceed the constant memory of your device. Typically only single integers/char/floats or simple struct are used here.

Example: For this kernel:

__kernel void mykernel (__global float *inout, int num){ inout[get_global_id(0)] = num; }

You would set the arguments like:

cl_mem my_buffer = clCreateBuffer(...); clSetKernelArg(kernel, 0, sizeof(my_buffer), &my_buffer); int my_int = 50; clSetKernelArg(kernel, 1, sizeof(my_int), &my_int);

Regarding your question of structs you cannot use struct that..:

Are not using standard cl data types (cl_int -> OK, int -> unsafe!). Use any kind of pointers. Use nested structs. Have any other class inside (ie: std::vector<>) They need some kind of special alignment.

This struct would be valid:

//Host side struct my_struct{ cl_int objectid; cl_float3 speed; cl_float3 direction; }; //Kernel arg my_struct a; a.objectid = 100; ... clSetKernelArg(kernel, 1, sizeof(my_struct), &a); //Kernel side typedef struct { int objectid; float3 speed; float3 direction; } my_struct; //Kernel declaration __kernel void mykernel (__global float *inout, my_struct data){ inout[get_global_id(0)] = (float)data.objectid; }

更多推荐

本文发布于:2023-07-26 18:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279331.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   clSetKernelArg   size   parameter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!