如何将字符串行作为cl_uint数组和文件详细信息传递给GPGPU
How to pass string lines as cl_uint array and file details to the GPGPU?
如何加载文本文件的行、行的长度和读取行的数量,以正确的方式进行强制转换并将其传递给GPU?
含义:
输入文本文件:
Line1
Line2
..
LineN
app.cl
#define UP_LIMIT 256
typedef struct {
uint bar; // amount of read lines
} foomatic;
typedef struct {
uint len; // length of line
uchar s[UP_LIMIT]; // line
} foo;
__kernel foobar(__global foo * kernel_in, __global foomatic kernel_in2){
// do something
}
main.cpp
#define LLEN 256
typedef struct {
cl_uint bar; // amount of read lines
} foomatic;
typedef struct {
cl_uint len; // length of line
cl_uchar s[LLEN]; // line
} foo;
int main(){
// load from file
// count lines of file
foo * input = new foo[N]; // N is the amount of lines of source text file
// cast line to cl_uchar
// pass lines, their lengths and number of lines to ocl kernel
delete [] input;
}
在OpenCL中使用struct似乎很棘手,因为字段在CPU和设备上的封装可能不同。此外,它在GPU上效率不高,因为它会影响对内存访问进行分组的能力。更好的方法是使用多个数组。我想要这样的东西:
//N is the number of strings.
cl_uint* strlens = new cl_uint[N];
cl_uchar* input = new cl_uchar[N * LLEN];
cl_int err_code = CL_SUCCESS;
//Remember to check error codes, omitted here for convenience.
cl_mem strlens_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY, N*sizeof(cl_uint), NULL, &err_code);
cl_mem input_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY, N*LLEN*sizeof(cl_uchar), NULL, &err_code);
//Some initialisation code...
//Send to device.
err_code = clEnqueueWriteBuffer(queue, strlens_buffer, CL_TRUE, 0, N*sizeof(cl_uint), strlens, 0, NULL, NULL);
err_code = clEnqueueWriteBuffer(queue, input_buffer, CL_TRUE, 0, N*LLEN*sizeof(cl_uchar), 0 NULL, NULL);
//Send work to the GPU...
//Clean up.
delete[] strlens;
delete[] input;
我已经为与您的设备关联的OpenCL上下文和与该上下文关联的命令队列使用了上下文和队列。
相关文章:
- 如何将内容数组写入文本文件?
- C++将文本文件中的数据读取到结构数组中
- 将值从二维数组输出到文本文件
- C++数据文件、数组和计算赋值
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从txt文件中读取多个不同长度的数组?
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- C++编程从外部文本文件定义数组大小
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 将系数存储在头文件的数组中("does not name a type"错误)
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 从文件读取,并写入函数C++中的数组
- 如何使用QJSONARRAY在QT5(C )中解析JSON文件(数组)
- 从文件/数组C++聚合数据