This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

opencl kernel 中调用printf 无输出

设置kernel中的printf,按照教程试了一下,没有任何输出,代码如下所示,请问可能是什么原因?

#include <stdio.h>
#include <CL/cl.h>
#include <CL/cl_ext.h>


const char *opencl =
"__kernel void hello() \n"
"{\n"
" printf(\"Hello, World!\\n\");\n"
"}\n";


void callback( const char *buffer, unsigned int length, size_t final, void *user_data)
{
printf("%.*s", length, buffer);
}


int main()
{
cl_platform_id platform;
cl_device_id device;
cl_context context;

cl_context_properties context_properties[] =
{
CL_PRINTF_CALLBACK_ARM, (cl_context_properties)callback,
CL_PRINTF_BUFFERSIZE_ARM, 0x100000,
CL_CONTEXT_PLATFORM, 0,
0
};

cl_command_queue queue;
cl_program program;
cl_kernel kernel;

clGetPlatformIDs(1, &platform, NULL);

context_properties[5] = (cl_context_properties)platform;

clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);

context = clCreateContext(context_properties, 1, &device, NULL, NULL, NULL);

queue = clCreateCommandQueue(context, device, 0, NULL);

program = clCreateProgramWithSource(context, 1, &opencl, NULL, NULL);
kernel = clCreateKernel(program, "hello", NULL);
clEnqueueTask(queue, kernel, 0, NULL, NULL);

clFinish(queue);
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(queue);
clReleaseContext(context);

return 0;
}

Parents
  • I find the reason. I forget to call "clBuildProgram". Now it works.

    #include <stdio.h>
    #include <CL/cl.h>
    #include <CL/cl_ext.h>


    const char *opencl =
    "__kernel void hello() \n"
    "{\n"
    " printf(\"Hello, World!\\n\");\n"
    "}\n";


    void callback( const char *buffer, unsigned int length, size_t final, void *user_data)
    {
    printf("%.*s", length, buffer);
    }


    int main()
    {
    cl_platform_id platform;
    cl_device_id device;
    cl_context context;

    cl_context_properties context_properties[] =
    {
    CL_PRINTF_CALLBACK_ARM, (cl_context_properties)callback,
    CL_PRINTF_BUFFERSIZE_ARM, 0x100000,
    CL_CONTEXT_PLATFORM, 0,
    0
    };

    cl_command_queue queue;
    cl_program program;
    cl_kernel kernel;

    clGetPlatformIDs(1, &platform, NULL);

    context_properties[5] = (cl_context_properties)platform;

    clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);

    context = clCreateContext(context_properties, 1, &device, NULL, NULL, NULL);

    queue = clCreateCommandQueue(context, device, 0, NULL);

    program = clCreateProgramWithSource(context, 1, &opencl, NULL, NULL);

    clBuildProgram(program, 0, NULL, NULL, NULL, NULL);


    kernel = clCreateKernel(program, "hello", NULL);
    clEnqueueTask(queue, kernel, 0, NULL, NULL);

    clFinish(queue);
    clReleaseKernel(kernel);
    clReleaseProgram(program);
    clReleaseCommandQueue(queue);
    clReleaseContext(context);

    return 0;
    }

Reply
  • I find the reason. I forget to call "clBuildProgram". Now it works.

    #include <stdio.h>
    #include <CL/cl.h>
    #include <CL/cl_ext.h>


    const char *opencl =
    "__kernel void hello() \n"
    "{\n"
    " printf(\"Hello, World!\\n\");\n"
    "}\n";


    void callback( const char *buffer, unsigned int length, size_t final, void *user_data)
    {
    printf("%.*s", length, buffer);
    }


    int main()
    {
    cl_platform_id platform;
    cl_device_id device;
    cl_context context;

    cl_context_properties context_properties[] =
    {
    CL_PRINTF_CALLBACK_ARM, (cl_context_properties)callback,
    CL_PRINTF_BUFFERSIZE_ARM, 0x100000,
    CL_CONTEXT_PLATFORM, 0,
    0
    };

    cl_command_queue queue;
    cl_program program;
    cl_kernel kernel;

    clGetPlatformIDs(1, &platform, NULL);

    context_properties[5] = (cl_context_properties)platform;

    clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);

    context = clCreateContext(context_properties, 1, &device, NULL, NULL, NULL);

    queue = clCreateCommandQueue(context, device, 0, NULL);

    program = clCreateProgramWithSource(context, 1, &opencl, NULL, NULL);

    clBuildProgram(program, 0, NULL, NULL, NULL, NULL);


    kernel = clCreateKernel(program, "hello", NULL);
    clEnqueueTask(queue, kernel, 0, NULL, NULL);

    clFinish(queue);
    clReleaseKernel(kernel);
    clReleaseProgram(program);
    clReleaseCommandQueue(queue);
    clReleaseContext(context);

    return 0;
    }

Children
No data