How to create OpenCL command queue?

妖精的绣舞 提交于 2019-12-24 20:03:08

问题


I'm trying to learn OpenCL but I can't even make a simple kernel to work.

The code below comes from the book "OpenCL Programming by Example", which I modified, modified, modified... and still, I have no clues what's the problem.

Every time I execute the program in my PC (AMD Athlon 5350 APU with Radeon R3), it prints the result as "0.0000". If I run the same executable, in my other machine (which is a clone of this HD, so everything is the same) with a NVIDIA 1080 TI, the program outputs "3.000" as the result.

I noticed a warning in the compiler output, so I changed the obsolete clCreateCommandQueue call to clCreateCommandQueueWithProperties().

And now... it just segfaults (with a printf() test I know it segfaults during/after the clCreateCommandQueueWithProperties).

On the system with the NVIDIA GPU, it just works.

What am I missing?

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <malloc.h>

#include <CL/cl.h>

#define VECTOR_NS 4096
#define VECTOR_SIZE (VECTOR_NS*sizeof(float))

static const char* saxpy_kernel =
    "__kernel void saxpy_kernel(__global float *A, __global float *B, __global float *C)\n"
    "{\n"
        "int index = get_global_id(0);\n"
        "C[0] = 3;\n"
    "}\n"
    ;

int main(void)
{
    int i;
    float total;
    float* A;
    float* B;
    float* C;
    float* Cmapped;
    cl_mem Acl;
    cl_mem Bcl;
    cl_mem Ccl;
    cl_context context;
    cl_platform_id* platforms;
    cl_uint num_platforms;
    cl_uint num_devices;
    cl_command_queue queue;
    cl_kernel kernel;
    cl_int clStatus;

    // Get platform and device information
    platforms = NULL;
    //Set up the Platform
    clStatus = clGetPlatformIDs(0, NULL, &num_platforms);
    platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id)*num_platforms);
    clStatus = clGetPlatformIDs(num_platforms, platforms, NULL);
    //Get the devices list and choose the device you want to run on
    cl_device_id* device_list = NULL;

    clStatus = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
    device_list = (cl_device_id *) malloc(sizeof(cl_device_id)*num_devices);

    clStatus = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_GPU, num_devices, device_list, NULL);
    // Create one OpenCL context for each device in the platform
    context = clCreateContext( NULL, num_devices, device_list, NULL, NULL, &clStatus);

    /* Create the command queue */
    //queue = clCreateCommandQueue(context, device_list[0], 0, &clStatus);
    queue = clCreateCommandQueueWithProperties(context, device_list[0], NULL, &clStatus);

    if(clStatus != CL_SUCCESS){
        fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
        exit(1);
    }

    /* */
    if((A = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
        fprintf(stderr, "ERROR: failed to allocate memory.\n");
        exit(1);
    }
    if((B = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
        fprintf(stderr, "ERROR: failed to allocate memory.\n");
        exit(1);
    }
    if((C = aligned_alloc(sysconf(_SC_PAGESIZE), VECTOR_SIZE)) == NULL){
        fprintf(stderr, "ERROR: failed to allocate memory.\n");
        exit(1);
    }

    /* Initialize it */
    i = 0;
    do {
        A[i] = 1;
        B[i] = 2;
        C[i] = 0;
    } while(++i != VECTOR_NS);

    Acl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, A, &clStatus); // CL_MEM_READ_ONLY
    Bcl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, B, &clStatus); // CL_MEM_READ_ONLY
    Ccl = clCreateBuffer(context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, VECTOR_SIZE, C, &clStatus); // CL_MEM_WRITE_ONLY

    // Create a program from the kernel source
    // Build the program
    cl_program program = clCreateProgramWithSource(context, 1, (const char**)&saxpy_kernel, NULL, &clStatus);

    if(clStatus != CL_SUCCESS){
        fprintf(stderr, "ERROR: failed to compile the OpenCL code.\n");
        exit(1);
    }

    clStatus = clBuildProgram(program, 1, device_list, NULL, NULL, NULL);

    if(clStatus != CL_SUCCESS){
        fprintf(stderr, "ERROR: failed to compile the OpenCL code.\n");
        exit(1);
    }

    // Create the OpenCL kernel
    kernel = clCreateKernel(program, "saxpy_kernel", &clStatus);
    // Set the arguments of the kernel
    clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&Acl);
    clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&Bcl);
    clSetKernelArg(kernel, 2, sizeof(cl_mem), (void*)&Ccl);

    // Execute the OpenCL kernel on the list
    size_t global_size = VECTOR_NS; // Process the entire lists
    size_t local_size = 1;

    // Process one item at a time
    clStatus = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, &local_size, 0, NULL, NULL);

    if(clStatus != CL_SUCCESS){
        fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
        exit(1);
    }

    //* Clean up and wait for all the comands to complete. */
    clFlush(queue);

    /* Display the result to the screen */
    Cmapped = (float*) clEnqueueMapBuffer(queue, Ccl, CL_TRUE, CL_MAP_READ, 0, VECTOR_SIZE, 0, NULL, NULL, &clStatus); // CL_MEM_USE_HOST_PTR

    if(clStatus != CL_SUCCESS){
        fprintf(stderr, "ERROR: failed to execute the kernel: %d.\n", clStatus);
        exit(1);
    }

    total = 0;
    for(i = 0; i < VECTOR_NS; i++)
        total += C[i];

    printf("TOTAL: %f\n", total);

    /* Clean up and wait for all the comands to complete. */
    clFlush(queue);
    clFinish(queue);

    /* Finally release all OpenCL allocated objects and host buffers. */
    clReleaseKernel(kernel);
    clReleaseProgram(program);
    clReleaseMemObject(Acl);
    clReleaseMemObject(Bcl);
    clReleaseMemObject(Ccl);
    clReleaseCommandQueue(queue);
    clReleaseContext(context);

    free(platforms);
    free(device_list);

    return 0;
}

回答1:


While your code works as-is on my machine, I think is a possible issue that could cause your problem. The call

Cmapped = (float*)clEnqueueMapBuffer(queue, Ccl, CL_TRUE, CL_MAP_READ, 0, VECTOR_SIZE, 0, NULL, NULL, &clStatus);

changes Cmapped but you try to read from the original C buffer in

total = 0;
for(i = 0; i < VECTOR_NS; i++)
    total += C[i];

This should probably read

total = 0;
for(i = 0; i < VECTOR_NS; i++)
    total += Cmapped[i];

However, since you created the buffer with the CL_MEM_USE_HOST_PTR flag, your OpenCL driver should also be able to optimize the copy operation if you use clEnqueueReadBuffer instead, if you use the original pointer as the destination:

clEnqueueReadBuffer(queue, Ccl, CL_TRUE, 0, VECTOR_SIZE, C, 0, NULL, NULL);

This should be a no-op if the data has not been cached to device memory by the OpenCL implementation.



来源:https://stackoverflow.com/questions/48271444/how-to-create-opencl-command-queue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!