如何将字符串行作为cl_uint数组和文件详细信息传递给GPGPU

How to pass string lines as cl_uint array and file details to the GPGPU?

本文关键字:文件 数组 详细信息 GPGPU uint 字符串 cl      更新时间:2023-10-16

如何加载文本文件的行、行的长度和读取行的数量,以正确的方式进行强制转换并将其传递给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上下文和与该上下文关联的命令队列使用了上下文和队列。