Question

So I'm trying to make heads and tails to get a certain piece of code to work in OpenCL.

And since I didn't get the originally intended results I've been trying various ways to figure out what went wrong. So I came up with the code attached below and after successful execution it didn't produce the intended results.

The original vision of this code was to execute a specified number of threads and copy the thread number into an array.

Threads: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

However the result that I get.

Threads: 0 0 0 3 0 0 0 7 0 0 0 11 0 0 0 15

With the results that I'm getting their is a pattern. So every

(n % 4)

it seems to put a number in my array. I started thinking if for some reason the code is being treated as int and converted to char.

OS: - (Snow Leopard)

gcc main.c -o threadsopencl -std=c99 -framework OpenCL



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

#include <stdlib.h>     // warning: implicit declaration of function ‘malloc’
#include <stdio.h>      // error: ‘stderr’ undeclared (first use in this function)

int main(int argc, char **argv)
{

    /* Retrieve Platforms */

    cl_uint Platforms = 0;

    printf("Checking for OpenCL platforms.\n");

    if (CL_SUCCESS == clGetPlatformIDs ( 0, NULL, &Platforms))
    {
        printf("Found %d platform.\n", Platforms);

        if (Platforms > 0)
        {

            /* Retrieve Platform ID */

            printf("Retrieving OpenCL platform details.\n");

            cl_platform_id *Platform = malloc((sizeof(cl_platform_id) * Platforms));

            clGetPlatformIDs( Platforms, Platform, &Platforms);


            /* Retrieve Devices on Platform */

            cl_uint GPUs = 0;

            printf("Retrieving GPU devices associated with the detected platform.\n");

            clGetDeviceIDs( Platform[0], CL_DEVICE_TYPE_GPU, 0, NULL, &GPUs);

            if (GPUs > 0)
            {

                printf("Found %d GPU device(s).\n", GPUs);

                cl_device_id  *GPU = malloc((sizeof(cl_device_id) * GPUs));

                clGetDeviceIDs( Platform[0], CL_DEVICE_TYPE_GPU, GPUs, GPU, &GPUs);


                cl_uint Error;


                printf("Creating OpenCL context and associating it with the detected GPU device.\n");
                                        //clCreateContext(NULL, 1, &devices[device_no], &pfn_notify, NULL, &_err)
                cl_context GPUcontext = clCreateContext(NULL, 1, &GPU[0], 0, NULL, &Error);
                //clCreateContextFromType( NULL, CL_DEVICE_TYPE_GPU, NULL, NULL, &Error);

                if (Error != CL_SUCCESS)
                {
                    printf("Failed to create an OpenCL context!\n");
                    return 1;
                }


                const char *program_source[] = {
                    "__kernel void NumberOfThreads( __global uchar *thread)\n",
                    "{\n",
                    "uchar id = convert_uchar(get_global_id(0));\n",
                    "thread[id] = id;\n",
                    "}\n"
                };


                printf("Creating a program for execution on the device.\n");

                cl_program AES = clCreateProgramWithSource( GPUcontext, sizeof(program_source)/sizeof(*program_source), program_source, NULL, &Error);

                if (Error != CL_SUCCESS)
                {
                    printf("Failed to create a program from source!\n");
                    return 1;
                }


                printf("Attempting compilation!\n");

                if (clBuildProgram( AES, GPUs, &GPU[0], "", NULL, NULL) != CL_SUCCESS) {
                    printf("Program compilation failed!\n");

                    char buffer[10240];

                    clGetProgramBuildInfo( AES, GPU[0], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, NULL);

                    fprintf(stderr, "CL Compilation failed:\n%s", buffer);
                    exit(2); // abort();
                }

                /* Since OpenCL compilation failed is due to incomplete code work */


                printf("Allocating space for the data to be executed within the context.\n");

                cl_mem Threads = clCreateBuffer( GPUcontext, CL_MEM_WRITE_ONLY, 16*sizeof(char), NULL, &Error);

                if (Error != CL_SUCCESS)
                {
                    printf("Failed to allocate buffer for State Matrix!\n");
                    return 1;
                }


                printf("Creating an OpenCL kernel!\n");

                cl_kernel ThreadsKernel = clCreateKernel( AES, "NumberOfThreads", &Error);
                clSetKernelArg( ThreadsKernel, 0, sizeof(cl_mem), &Threads);


                if (Error != CL_SUCCESS)
                {
                    printf("Failed to create kernel object!\n");
                    return 1;
                }


                printf("Setting up an execution queue.\n");

                cl_command_queue ExecutionQueue = clCreateCommandQueue( GPUcontext, GPU[0], 0, &Error);

                if (Error != CL_SUCCESS)
                {
                    printf("Failed to create command queue!\n");
                    return 1;
                }


                printf("Commencing with kernel execution!\n");

                cl_event ExecutionComplete;

                size_t global_work_size[1] = { 16 };

                if (clEnqueueNDRangeKernel( ExecutionQueue, ThreadsKernel, 1, NULL, global_work_size, NULL, 0, NULL, &ExecutionComplete) != CL_SUCCESS)
                {
                    //printf("Failed to execute kernel! Error %d\n", (unsigned int)Error);

                    switch(Error)
                    {
                        case CL_INVALID_PROGRAM_EXECUTABLE:
                        printf("CL_INVALID_PROGRAM_EXECUTABLE\n");
                        break;
                        case CL_INVALID_COMMAND_QUEUE:
                        printf("CL_INVALID_COMMAND_QUEUE\n");
                        break;
                        case CL_INVALID_KERNEL:
                        printf("CL_INVALID_KERNEL\n");
                        break;
                        case CL_INVALID_CONTEXT:
                        printf("CL_INVALID_CONTEXT\n");
                        break;
                        case CL_INVALID_KERNEL_ARGS:
                        printf("CL_INVALID_KERNEL_ARGS\n");
                        break;
                        case CL_INVALID_WORK_DIMENSION:
                        printf("CL_INVALID_WORK_DIMENSION\n");
                        break;
                        case CL_INVALID_GLOBAL_WORK_SIZE:
                        printf("CL_INVALID_GLOBAL_WORK_SIZE\n");
                        break;
                        case CL_INVALID_WORK_GROUP_SIZE:
                        printf("CL_INVALID_WORK_GROUP_SIZE\n");
                        break;
                        case CL_INVALID_WORK_ITEM_SIZE:
                        printf("CL_INVALID_WORK_ITEM_SIZE\n");
                        break;
                        case CL_INVALID_GLOBAL_OFFSET:
                        printf("CL_INVALID_GLOBAL_OFFSET\n");
                        break;
                        case CL_OUT_OF_RESOURCES:
                        printf("CL_OUT_OF_RESOURCES\n");
                        break;
                        case CL_MEM_OBJECT_ALLOCATION_FAILURE:
                        printf("CL_MEM_OBJECT_ALLOCATION_FAILURE\n");
                        break;
                        case CL_INVALID_EVENT_WAIT_LIST:
                        printf("CL_INVALID_EVENT_WAIT_LIST\n");
                        break;
                        case CL_OUT_OF_HOST_MEMORY:
                        printf("CL_OUT_OF_HOST_MEMORY\n");
                        break;
                        default:
                        printf("Failed to execute kernel! %u\n", (unsigned int)Error);
                    }

                    return 1;
                }
                clWaitForEvents( 1, &ExecutionComplete);
                clReleaseEvent( ExecutionComplete);


                printf("ThreadValue:");
                for ( char Loop = 0; Loop < 16; Loop++)
                {
                    unsigned char ThreadValue = 0;

                    if (clEnqueueReadBuffer( ExecutionQueue, Threads, CL_TRUE, Loop, 1, &ThreadValue, 0, NULL, NULL) != CL_SUCCESS)
                    {
                        printf("Failed to copy data back from Device to Host!\n");
                        return 1;
                    }

                    printf(" %d", ThreadValue);
                }

                printf("\n");

                printf("Freeing memory and exiting!\n");


                clReleaseMemObject(Threads);

                clReleaseKernel(ThreadsKernel);
                clReleaseProgram(AES);
                clReleaseContext(GPUcontext);


            }

        }

    }

    return 0;
}
Was it helpful?

Solution

Are you using OpenCL 1.0? That doesn't allow writes to char* by default. 1.0 only supports writes into ints.

You need 1.1 or the extension http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/cl_khr_byte_addressable_store.html

You can try adding

#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable

At the start of your kernel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top