From 8dc546eced1d35424853d25834e482c611e9f6ca Mon Sep 17 00:00:00 2001 From: Tom Stellard Date: Wed, 14 Mar 2012 14:43:56 -0400 Subject: Add more helper functions --- get_global_id.c | 55 ++++++++++++------------------------------------------- util.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ util.h | 19 ++++++++++++------- 3 files changed, 70 insertions(+), 50 deletions(-) diff --git a/get_global_id.c b/get_global_id.c index 9f347fc..0f64192 100644 --- a/get_global_id.c +++ b/get_global_id.c @@ -11,58 +11,27 @@ int main(int argc, char ** argv) struct cltest_context context; - cl_mem out_buffer; - unsigned i; int out_data[10]; size_t global_work_size = 10; clTestSimpleInit(&context, "global_id"); - out_buffer = clCreateBuffer(context.cl_ctx, - CL_MEM_WRITE_ONLY, /* Flags */ - sizeof(out_data), /* Size of buffer */ - NULL, /* Pointer to the data */ - &error); /* error code */ - - if (error != CL_SUCCESS) { - fprintf(stderr, "clCreateBuffer() failed: %s\n", clTestErrorString(error)); - return EXIT_FAILURE; - } - - fprintf(stderr, "clCreateBuffer() succeeded.\n"); - - if ( !clTestKernelSetArg(context.kernel, 0, sizeof(cl_mem), &out_buffer)) { - return EXIT_FAILURE; - } - - error = clEnqueueNDRangeKernel(context.command_queue, - context.kernel, - 1, /* Number of dimensions */ - NULL, /* Global work offset */ - &global_work_size, - &global_work_size, /* local work size */ - 0, /* Events in wait list */ - NULL, /* Wait list */ - NULL); /* Event object for this event */ - - if (error != CL_SUCCESS) { - fprintf(stderr, "clEnqueueNDRangeKernel() failed: %s\n", - clTestErrorString(error)); - return EXIT_FAILURE; - } + clTestSetOutputBuffer(&context, sizeof(out_data)); - fprintf(stderr, "clEnqueueNDRangeKernel() suceeded.\n"); + clTestEnqueueNDRangeKernel(context.command_queue, + context.kernel, + 1, &global_work_size, &global_work_size); error = clEnqueueReadBuffer(context.command_queue, - out_buffer, - CL_TRUE, /* TRUE means it is a blocking read. */ - 0, /* Buffer offset to read from. */ - sizeof(out_data), /* Bytes to read */ - out_data, /* Pointer to store the data */ - 0, /* Events in wait list */ - NULL, /* Wait list */ - NULL); /* Event object */ + context.out_buffer, + CL_TRUE, /* TRUE means it is a blocking read. */ + 0, /* Buffer offset to read from. */ + sizeof(out_data), /* Bytes to read */ + out_data, /* Pointer to store the data */ + 0, /* Events in wait list */ + NULL, /* Wait list */ + NULL); /* Event object */ if (error != CL_SUCCESS) { diff --git a/util.c b/util.c index c8e476b..766f1c4 100644 --- a/util.c +++ b/util.c @@ -255,6 +255,30 @@ unsigned clTestSimpleInit(struct cltest_context * context, const char * kernel_n return 1; } +unsigned clTestSetOutputBuffer(struct cltest_context * context, + unsigned buffer_size) +{ + cl_int error; + + context->out_buffer = clCreateBuffer(context->cl_ctx, + CL_MEM_WRITE_ONLY, /* Flags */ + buffer_size, /* Size of buffer */ + NULL, /* Pointer to the data */ + &error); /* error code */ + + if (error != CL_SUCCESS) { + fprintf(stderr, "clCreateBuffer() failed: %s\n", clTestErrorString(error)); + return 0; + } + + if (!clTestKernelSetArg(context->kernel, 0, sizeof(cl_mem), + &context->out_buffer)) { + return 0; + } + + return 1; +} + unsigned clTestKernelSetArg(cl_kernel kernel, cl_uint index, size_t size, const void * value) { @@ -271,3 +295,25 @@ unsigned clTestKernelSetArg(cl_kernel kernel, cl_uint index, size_t size, return 1; } + +unsigned clTestEnqueueNDRangeKernel(cl_command_queue command_queue, + cl_kernel kernel, cl_uint work_dim, const size_t * global_work_size, + const size_t * local_work_size) +{ + cl_int error = clEnqueueNDRangeKernel(command_queue, + kernel, + work_dim, /* Number of dimensions */ + NULL, /* Global work offset */ + global_work_size, + local_work_size, /* local work size */ + 0, /* Events in wait list */ + NULL, /* Wait list */ + NULL); /* Event object for this event */ + if (error != CL_SUCCESS) { + fprintf(stderr, "clEnqueueNDRangeKernel() failed: %s\n", + clTestErrorString(error)); + return 0; + } + + return 1; +} diff --git a/util.h b/util.h index d2977bf..ecaab60 100644 --- a/util.h +++ b/util.h @@ -3,16 +3,21 @@ struct cltest_context { cl_device_id device_id; cl_context cl_ctx; cl_command_queue command_queue; + cl_mem out_buffer; cl_kernel kernel; }; -const char * cltestErrorString(cl_int error); -unsigned cltestInitGpuDevice(cl_device_id * device_id); -unsigned cltestCreateContext(cl_context * context, cl_device_id device_id); -unsigned cltestCreateCommandQueue(cl_command_queue * command_queue, +const char * clTestErrorString(cl_int error); +unsigned clTestInitGpuDevice(cl_device_id * device_id); +unsigned clTestCreateContext(cl_context * context, cl_device_id device_id); +unsigned clTestCreateCommandQueue(cl_command_queue * command_queue, cl_context context, cl_device_id device_id); -unsigned cltestCreateKernel(cl_context context, cl_device_id device_id, +unsigned clTestCreateKernel(cl_context context, cl_device_id device_id, cl_kernel * kernel, const char * kernel_name); -unsigned cltestSimpleInit(struct cltest_context * context, const char * kernel_name); -unsigned cltestKernelSetArg(cl_kernel kernel, cl_uint index, size_t size, +unsigned clTestSimpleInit(struct cltest_context * context, const char * kernel_name); +unsigned clTestSetOutputBuffer(struct cltest_context * context, unsigned buffer_size); +unsigned clTestKernelSetArg(cl_kernel kernel, cl_uint index, size_t size, const void * value); +unsigned clTestEnqueueNDRangeKernel(cl_command_queue command_queue, + cl_kernel kernel, cl_uint work_dim, const size_t * global_work_size, + const size_t * local_work_size); -- cgit v1.2.3