内核没有给出每个核心的预期结果

时间:2014-02-28 11:25:25

标签: kernel opencl

我正处于OpenCl的学习阶段。

问题:创建两个数组:使用值“-1”初始化的50个元素的输入和输出。

我将两个数组作为输入和输出缓冲区传递给GPU并解决(递增数组的每个元素)5次迭代。因此, 所有元素的值应该增加到4.因为,我将此作业分为10个核心

size_t global_work_size[1] = {10};

我的内核根据get_global_id(0)值解析所有数组,这意味着解决每个核心中的每5个元素。

所以,我的最终输出应该是:

output[0] = 4.0000
output[1] = 4.0000
...
...
output[48] = 4.0000
output[49] = 4.0000

但是,我的输出即将到来:

output[0] = 9.0000
output[1] = 9.0000
output[2] = 9.0000
output[3] = 9.0000
output[4] = 9.0000
output[5] = 4.0000
output[6] = 4.0000
output[7] = 4.0000
output[8] = 4.0000
output[9] = 4.0000
...
...
output[48] = 4.0000
output[49] = 4.0000

正如我们所看到的,输出的差异是前10个元素。这可能是因为我的第一个内核的内核运行了两次。 但是,我无法理解确切的原因。请建议我,我哪里出错了?可能逻辑错了,或者我在OpenCl中错过了一件大事?

program.c 档案:

#include <stdio.h>
#include <stdlib.h> 
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

#define MEM_SIZE (50)
#define MAX_SOURCE_SIZE (0x100000)

int main() {
    float input[MEM_SIZE], output[MEM_SIZE];
    int go, i;
    for (i = 0; i < MEM_SIZE; i++) {
        input[i] = -1.0;
        output[i] = -1.0;
    }
    FILE *fp;
    cl_device_id device_id = NULL;
    cl_context context = NULL;
    cl_command_queue command_queue = NULL;
    cl_program program = NULL;
    cl_kernel kernel = NULL;
    cl_platform_id platform_id = NULL;
    cl_uint ret_num_devices;
    cl_uint ret_num_platforms;
    cl_int ret;
    size_t source_size;
    cl_event event;
    char *source_str;
    fp = fopen("algebra.cl", "r");
    if (!fp) {
        fprintf(stderr, "Failed to load kernel.\n");
        exit(1);
    }
    source_str = (char*)malloc(MAX_SOURCE_SIZE);
    source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
    fclose(fp);

    /*Initialization*/
    /* Get Platform and Device Info */
    ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
    ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);

    /* Create OpenCL context */
    context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);

    /* Create Command Queue */
    command_queue = clCreateCommandQueue(context, device_id, 0, &ret);

    /*Initialization complete*/

    cl_mem inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, MEM_SIZE * sizeof(float),(void *) input, NULL);
    cl_mem outputBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY , MEM_SIZE * sizeof(float), NULL, NULL);



    ret = clEnqueueWriteBuffer(command_queue,
                                  inputBuffer,
                                  CL_FALSE,
                                  0,
                                  MEM_SIZE * sizeof(float),
                                  input,
                                  0,
                                  NULL,
                                  &event);
    ret = clWaitForEvents(1, &event);
    clReleaseEvent(event);

    ret = clEnqueueWriteBuffer(command_queue,
                                  outputBuffer,
                                  CL_FALSE,
                                  0,
                                  MEM_SIZE * sizeof(float),
                                  output,
                                  0,
                                  NULL,
                                  &event);

    ret = clWaitForEvents(1, &event);
    clReleaseEvent(event);

    /* Create Kernel Program from the source */
    program = clCreateProgramWithSource(context, 1, (const char **)&source_str,(const size_t *)&source_size, &ret);

    /* Build Kernel Program */
    ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);

    /* Create OpenCL Kernel */
    kernel = clCreateKernel(program, "calc", &ret);

    /* Set OpenCL Kernel Parameters */
    ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&inputBuffer);
    ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&outputBuffer);

    /* Execute OpenCL Kernel*/
    ret = clEnqueueTask(command_queue, kernel, 0, NULL,NULL);
    double io;
    size_t global_work_size[1] = {10};
    inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, MEM_SIZE * sizeof(float),(void *) output, NULL);
    ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&inputBuffer);
    ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, NULL);
    ret = clEnqueueReadBuffer(command_queue, outputBuffer, CL_TRUE, 0, MEM_SIZE * sizeof(float), output, 0, NULL, NULL);

    for (go = 0; go < MEM_SIZE; go++) {
        printf("output[%d] = %f\n", go, output[go]);
    }
    printf("\n\n");

    /* Finalization */
    ret = clFlush(command_queue);
    ret = clFinish(command_queue);
    ret = clReleaseKernel(kernel);
    ret = clReleaseProgram(program);
    ret = clReleaseMemObject(inputBuffer);
    ret = clReleaseMemObject(outputBuffer);
    ret = clReleaseCommandQueue(command_queue);
    ret = clReleaseContext(context);
    return 1;
}

algebra.cl (内核文件):

__kernel void euler(__global float* in, __global float* out)
{
    int idx = get_global_id(0) * 5;
    int end_idx = idx + 5;
    double j = 0;
    int i;
    while (j < 5.0) {
        j++;
        for (i = idx; i < end_idx; i++) {
            out[i] = out[i] + 1;
        }
    }
}

gcc编译:

gcc program.c -o test -l OpenCL -I /usr/local/opencl/AMD-APP-SDK-v2.9-RC-lnx32/include/ -L /usr/local/opencl/AMD-APP-SDK-v2.9-RC-lnx32/lib/x86

1 个答案:

答案 0 :(得分:1)

你对自己在做什么有完全错误的理解,所以,你期待一种永远不会发生的行为。

  1. clEnqueueTask(command_queue, kernel, 0, NULL,NULL);这将仅使用1个工作项运行内核,所以,是的。第一个工作项目正在运行两次。这是你的主要问题。
  2. size_t global_work_size[1] = {10};确实意味着10个工作项。但这不是使用OpenCL的方式。您永远不应该放置10M的物品来处理和运行10个工作项。您应该处理10M的项目以及尽可能多的工作项目(即:10M,1 work-item -> 1 simple action),否则GPU将99%空闲。此外,这将更容易理解和编程,因为您的内核代码中几乎没有循环。
  3. 通常,您不应重复使用输出或增加输出值out[i] = out[i] +1;。因为如果许多工作项改变相同的值,则可能导致竞争条件或错误的结果。请记住,所有工作项可能同时独立运行。