From 569da6c94c9aa21cd35ccd19a9614c8a658106ec Mon Sep 17 00:00:00 2001 From: Tom Stellard Date: Fri, 27 Apr 2012 09:53:28 -0400 Subject: loop: Add argument for number of iterations --- loop.c | 26 +++++++++++++++++++------- loop.cl | 6 +++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/loop.c b/loop.c index af94dcb..151dfde 100644 --- a/loop.c +++ b/loop.c @@ -16,8 +16,19 @@ int main (int argc, char ** argv) cl_int error; cl_mem out_buffer; - int out_data[100]; - size_t global_work_size = 10; + int * out_data; + size_t global_work_size = 5; + int iterations; + unsigned out_data_size; + + if (argc != 2) { + fprintf(stderr, "Usage: loop iterations\n"); + return EXIT_FAILURE; + } + + iterations = atoi(argv[1]); + out_data_size = global_work_size * iterations * sizeof(int); + out_data = malloc(out_data_size); if (!clSimpleSimpleInit(&context, "loop")) { return EXIT_FAILURE; @@ -25,12 +36,13 @@ int main (int argc, char ** argv) out_buffer = clCreateBuffer(context.cl_ctx, CL_MEM_WRITE_ONLY, - sizeof(out_data), + out_data_size, NULL, &error); assert(error == CL_SUCCESS); - if (!clSimpleKernelSetArg(context.kernel, 0, sizeof(cl_mem), &out_buffer)) { + if (!clSimpleKernelSetArg(context.kernel, 0, sizeof(cl_mem), &out_buffer) + || !clSimpleKernelSetArg(context.kernel, 1, sizeof(int), &iterations)) { return EXIT_FAILURE; } @@ -48,15 +60,15 @@ int main (int argc, char ** argv) out_buffer, CL_TRUE, 0, - sizeof(out_data), + out_data_size, out_data, 0, NULL, NULL); assert(error == CL_SUCCESS); for (i = 0; i < global_work_size; i++) { - for (j = 0; j < 10; j++) { - fprintf(stderr, "%2u ", out_data[i * global_work_size + j]); + for (j = 0; j < iterations; j++) { + fprintf(stderr, "%2u ", out_data[(i * iterations) + j]); } fprintf(stderr, "\n"); } diff --git a/loop.cl b/loop.cl index f19c49d..0d2c1b8 100644 --- a/loop.cl +++ b/loop.cl @@ -1,8 +1,8 @@ -__kernel void loop(__global int * out) +__kernel void loop(__global int * out, int iterations) { unsigned i; - for (i = 0; i < 10; i++) { - unsigned id = get_global_id(0) + i; + for (i = 0; i < iterations; i++) { + unsigned id = (get_global_id(0) * iterations) + i; out[id] = id; } } -- cgit v1.2.3