[ create a new paste ] login | about

Link: http://codepad.org/J8nFTwzM    [ raw code | fork ]

C, pasted on Apr 29:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <CL/cl.h>

// inlined OpenCL C code
char* simple_opencl_program = (
    "__kernel void can_move("
        "__global const char *board,"
        "const char2 board_size,"
        "__global char *can_move)"
    "{"
        //"*can_move = 1;"
    "}");

int main() {
    int e = 0; // error code
    // create a compute context with GPU device
    cl_platform_id platform;
    e = clGetPlatformIDs(1, &platform, NULL);
    printf("%d - platform-ids\n", e);
    cl_device_id device_id = NULL;
    e = clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, NULL );
    printf("%d - device-ids\n", e);
    cl_context_properties props[3];
    props[0] = (cl_context_properties)CL_CONTEXT_PLATFORM;
    props[1] = (cl_context_properties)platform;
    props[2] = (cl_context_properties)0;
    cl_context context = clCreateContextFromType(props, CL_DEVICE_TYPE_CPU, NULL, NULL, &e);
    printf("%d - context\n", e);
    
    // create a command queue
    cl_command_queue queue = clCreateCommandQueue(context, device_id, 0, &e);
    printf("%d - queue\n", e);

    // allocate the buffer memory objects
    char* board_source[240];
    memset(board_source, 0, sizeof(board_source));
    
    int can_move_source_prim = 0;
    int* can_move_source = &can_move_source_prim;
    
    cl_mem board = clCreateBuffer(
        context,
        CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
        sizeof(board_source),
        board_source,
        NULL);
    cl_mem can_move = clCreateBuffer(
        context,
        CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
        sizeof(can_move_source_prim),
        can_move_source,
        NULL);
    int x = 3;
    // create the compute program
    cl_program program = clCreateProgramWithSource(
        context,
        1,
        (const char**)&simple_opencl_program,
        NULL,
        NULL);

    // build the compute program executable
    clBuildProgram(program, 0, NULL, NULL, NULL, NULL);

    // create the compute kernel
    cl_kernel kernel = clCreateKernel(program, "can_move", NULL);

    // set the args values
    clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&board);
    clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&can_move);
    
    char size[2] = {10, 24};
    clSetKernelArg(kernel, 1, sizeof(size), size);

    // create N-D range object with work-item dimensions and execute kernel
    const size_t global_work_size[1] = {1};
    cl_event k_event;
    e= clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, &k_event);
    
    if (e != 0) {
        printf("%d??? - 1\n", e);
        return 1;
    }
    
    printf("welp.\n");
    const cl_event events[1] = {k_event};
    e = clWaitForEvents(1, events);
    if (e != 0) {
        printf("%d??? - 2\n", e);
        return 1;
    }
    printf("WOO!\n");
    
    return 0;
}


Create a new paste based on this one


Comments: