在openCL中使用强力模式匹配

时间:2012-07-12 09:01:31

标签: opencl string-matching workgroup

我将文本存储在char数组中[textLength],并将模式存储在数组b [patternLength]

cl_char *a = (cl_char *) malloc(textLength*sizeof(cl_char));

for(int i =0; i<textLength;i++)
{
    a[i]=text[i];
    }

// A buffer object is a handle to a region of memory
cl_mem a_buffer = clCreateBuffer(context,
                                 CL_MEM_READ_ONLY | // buffer object read only for kernel
                                 CL_MEM_COPY_HOST_PTR, // copy data from memory referenced
                                 // by host pointer
                                 textLength*sizeof(cl_char), // size in bytes of buffer object
                                 a, // host pointer
                                 NULL); // no error code returned

// for text and pattern kernal arguments
cl_char *b = (cl_char *) malloc(patternLength*sizeof(cl_char));

for(int i =0; i<patternLength;i++)
{
   b[i]=pattern[i];
}

// A buffer object is a handle to a region of memory
/*cl_mem b_buffer = clCreateBuffer(context,
                                 CL_MEM_READ_ONLY | // buffer object read only for kernel
                                 CL_MEM_COPY_HOST_PTR, // copy data from memory referenced
                                 // by host pointer
                                 patternLength*sizeof(cl_char), // size in bytes of buffer object
                                 b, // host pointer
                                 NULL); // no error code returned */
cl_mem b_buffer = NULL;


    clSetKernelArg(kernel, 0, sizeof(a_buffer), (void*) &a_buffer);
clSetKernelArg(kernel, 1, sizeof(cl_mem), NULL);
clSetKernelArg(kernel, n, sizeof(cl_mem), &b_buffer);
    size_t global_work_size = numberofWorkItem;
    cl_int error= clEnqueueNDRangeKernel(queue, kernel,
                       1, NULL, // global work items dimensions and offset
                       &global_work_size, // number of global work items
                       &patternLength, // number of local work items
                       0, NULL, // don't wait on any events to complete
                       &timeEvent); // no event object returned

 I have read that in clSetKernelArg, for __local indentifiers, the arg_value should be NULL. I have done that by doing b_buffer=NULL;

但这样做会阻止b_buffer存储b [](模式)的值 我该怎么办?

另外, 如果我没有错,则local_work_size不能大于CL_DEVICE_MAX_WORK_ITEM_SIZES给出的值。因为local_work_size受底层设备/硬件的约束。 另一方面,global_work_size可以像人们想要的那样大。 它必须是local_work_size的倍数??? 如果是的话,为什么?

2 个答案:

答案 0 :(得分:0)

您的错误位于clSetKernelArg行:

//incorrect
clSetKernelArg(kernel, n, sizeof(cl_mem), &b_buffer);

//correct
clSetKernelArg(kernel, n, sizeof(cl_char)*patternLength, NULL);

内核执行后会清除本地内存,因此您无法使用您的方法获取b_buffer的副本。此外,主机不分配本地内存。您需要从全局参数进行复制以使其进入LDS。

要复制本地数据,您需要传入全局cl_mem以及local参数。副本可以在内核的末尾完成,并使用clEnqueueReadBuffer红色返回主机。

<强>更新

这是一个如何使用动态本地缓冲区并为其分配全局缓冲区内容的具体示例。

__kernel void copyBufferExample(__global int* srcBuff, __local int* localBuff, const int copyCount)
{
    int lid = get_local_id(0);
    int ls = get_local_size(0);
    int i;

    for(i=lid; i<copyCount; i+=ls){
        localBuff[i] = srcBuff[i];
    }

    //use localBuff here
    //copy result back to global memory if needed
}

答案 1 :(得分:0)

上面的代码不会并行复制...

这确实......

_ kernel void copyBufferExample( _global int * srcBuff,__ local int * localBuff,const int copyCount) {

int i = get_local_id(0);

if(i&lt; copyCount)        localBuff [i] = srcBuff [i]; //每个线程复制1个int。不需要循环

barrier(CLK_LOCAL_MEM_FENCE);  // synchronize all threads before using the local memory


//use localBuff here
//copy result back to global memory if needed

}