0

我写了一个内核,它只正确执行一次!我正在尝试执行此内核代码:

__kernel void calculate2dim(__global int *g_idata, 
__local int *sdata, __global int *g_odata) {

unsigned int tid = get_local_id(0);
unsigned int i = get_global_id(0);
sdata[tid] = g_idata[i];
barrier(CLK_LOCAL_MEM_FENCE);

for(unsigned int s = 1; s < get_local_size(0); s *= 2) {
    if(tid % (2*s) == 0) {
        sdata[tid] += sdata[tid + s];
    }
    barrier(CLK_LOCAL_MEM_FENCE);
}

if(tid == 0)
    g_odata[get_group_id(0)] = sdata[0];
}

这是主机代码的片段(请记住,它是草稿,尚未完美编写代码):

cl_int err;
size_t global;  // global domain size for our calculation
size_t local;   // local domain size for our calculation

int* data = new int[200];
int *res = new int[200];
int result = 0;

for(int i = 0; i < 200; i++) {
    data[i] = i % 14;
    res[i] = 0;
}

unsigned int correct;               // number of correct results returned
cl_mem input1;                       // device memory used for the input array
cl_mem output;

unsigned int count = 200;

input1 = clCreateBuffer(context,  CL_MEM_READ_ONLY, sizeof(int) * count, NULL, NULL);
output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int) * count, NULL, NULL);

if (!input1)
{
    cerr << "Error: Failed to allocate device memory!" << endl;
    exit(1);
}

err = clEnqueueWriteBuffer(commands, input1,
                           CL_TRUE, 0, sizeof(int) * count,
                           data, 0, NULL, NULL);

if (err != CL_SUCCESS)
{
    cerr << "Error: Failed to write to source array!" << endl;
    exit(1);
}

err = 0;
err  = clSetKernelArg(kernel_av, 0, sizeof(cl_mem), &input1);
err |= clSetKernelArg(kernel_av, 1, count * sizeof(int), NULL);
err |= clSetKernelArg(kernel_av, 2, sizeof(cl_mem), &output);
if (err != CL_SUCCESS)
{
    cerr << "Error: Failed to set kernel arguments! " << err << endl;
    exit(1);
}

err = clGetKernelWorkGroupInfo(kernel_av, device_id,
                               CL_KERNEL_WORK_GROUP_SIZE,
                               sizeof(local), &local, NULL);
if (err != CL_SUCCESS)
{
    cerr << "Error: Failed to retrieve kernel work group info! "
         <<  err << endl;
    exit(1);
}

global = count;

if(local > global) {
    local = global;
}
cout << "global = " << global << " local = " << local << endl;
local = 200;

err = clEnqueueNDRangeKernel(commands, kernel_av,
                             1, NULL, &global, &local,
                             0, NULL, NULL);
if (err)
{
    cerr << "Error: Failed to execute kernel!" << endl;
    return EXIT_FAILURE;
}

// Wait for all commands to complete
clFinish(commands);

// Read back the results from the device to verify the output
//
err = clEnqueueReadBuffer(commands, output,
                           CL_TRUE, 0, sizeof(int) * count,
                           res, 0, NULL, NULL );

if (err != CL_SUCCESS)
{
    cerr << "Error: Failed to read output array! " <<  err << endl;
    exit(1);
}

cout << res[0] << endl;

int x = 0;
for(int i = 0; i < 200; i++) {
    x += data[i];
    cout << res[i] << endl;
}
cout << x << endl;

// Shutdown and cleanup
delete [] data;
delete [] res;

clReleaseMemObject(input1);
clReleaseMemObject(output);

第一次执行后,我在主机代码中有正确的答案 1280,但随后(第二次执行和其他一些)我得到类似 1073743104 的东西,而不是正确的答案。也许我忘记清理一些变量或什么?

谢谢!

4

1 回答 1

0

最后我得到了它!

当你使用这个算法(Parallel Reduction Tree)时,你应该记住,线程数应该是 2 的幂!

否则(让我们取 10 个数字)它将是这样的:

步骤 0:1 2 3 4 5 6 7 8 9 10

第 1 步:3 2 7 4 11 6 15 8 19 10

第 2 步:10 (3 + 7) 2 7 4 18 (11 + 15) 6 15 8 ??? 10 //因为每一步的活动线程数应该是偶数!

于 2013-03-11T13:09:36.590 回答