From c19cffcae57ff68d99eaa38f6d0547942dce5eca Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sun, 12 Feb 2017 20:31:13 +1300 Subject: Remove more hardcoded crap --- fractal-gen.c | 45 +++++++++++++++++++++++++++++++++++++-------- test.cl | 5 ++--- trampoline.c | 16 ++++++++++++---- trampoline.h | 2 +- 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/fractal-gen.c b/fractal-gen.c index 4401fa1..091f7d5 100644 --- a/fractal-gen.c +++ b/fractal-gen.c @@ -1,16 +1,11 @@ #include #include +#include #include "trampoline.h" -static unsigned long size; -static unsigned long iterations; - -int main() +int run(unsigned int size, unsigned int iterations) { - size = 1024; - iterations = 102400; - fprintf(stderr, "Building CL trampoline... "); if (tramp_init()) { fprintf(stderr, "Failed.\n"); @@ -52,7 +47,7 @@ int main() return 1; } fprintf(stderr, "Reading data from device... "); - if (tramp_copy_data(&buffer, size*size)) { + if (tramp_copy_data((void*)&buffer, size*size)) { fprintf(stderr, "Failed.\n"); return 1; } @@ -64,6 +59,40 @@ int main() printf("P5\n%d\n%d\n255\n",size,size); fwrite(buffer, size*size, 1, stdout); +} + +void die_help() +{ + fprintf(stderr, "Syntax:\n%s [-s size] [-i max_iteratons]\n"); + exit(1); +} + +int main(int argc, char **argv) +{ + unsigned int size = 0; + unsigned int iterations = 0; + char c = '\0'; + + while ((c = getopt(argc, argv, "s:i:")) != -1) { + switch (c) { + case 's': + size = atoi(optarg); + break; + case 'i': + iterations = atoi(optarg); + break; + case '?': + die_help(); + return 1; /* mostly unreachable */ + break; /* unreachable */ + } + } + + if (size == 0 || iterations == 0) { + die_help(); + return 1; /* mostly unreachable */ + } + run(size, iterations); return 0; } diff --git a/test.cl b/test.cl index a2ecc67..2652108 100644 --- a/test.cl +++ b/test.cl @@ -11,8 +11,7 @@ __kernel void fractal_gen( float b = -1.75+(((float)y)/(float)size)*3.5; float2 z = (0.0, 0.0); - - + for (i = 0; i < iterations; i++) { // if abs(z) > 2 if (z.x*z.x + z.y*z.y > 4) @@ -22,5 +21,5 @@ __kernel void fractal_gen( z.x = z.x*z.x - z.y*z.y + a; z.y = 2*oldx*z.y + b; } - buffer[x+size*y] = (i*255)/iterations; + buffer[(size*y)+x] = (i*255)/iterations; } diff --git a/trampoline.c b/trampoline.c index 306e217..2dbccc9 100644 --- a/trampoline.c +++ b/trampoline.c @@ -15,6 +15,10 @@ static cl_mem device_buffer; static cl_kernel kernel; static cl_program program; +static unsigned int size; +static unsigned int iterations; + + /* FIXME print cl error messages with oclErrorString */ int tramp_init() { @@ -146,11 +150,15 @@ int tramp_compile_kernel() return ret != CL_SUCCESS; } -int tramp_set_kernel_args(unsigned int size, unsigned int iterations) +int tramp_set_kernel_args(unsigned int s, unsigned int it) { cl_int ret = 0; - device_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, 8192*8192, NULL, &ret); + size = s; + iterations = it; + + + device_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, size*size, NULL, &ret); if (ret != CL_SUCCESS) return 1; @@ -170,8 +178,8 @@ int tramp_run_kernel() cl_event event; cl_int ret = 0; size_t workgroup_sizes[2]; - workgroup_sizes[0] = 8192; - workgroup_sizes[1] = 8192; + workgroup_sizes[0] = size; + workgroup_sizes[1] = size; ret = clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, workgroup_sizes, NULL, 0, NULL, &event); if (ret != CL_SUCCESS) { diff --git a/trampoline.h b/trampoline.h index 3fe983c..fbcc453 100644 --- a/trampoline.h +++ b/trampoline.h @@ -3,6 +3,6 @@ void tramp_destroy(void); int tramp_load_kernel(const char *filename); char *tramp_get_build_log(void); int tramp_compile_kernel(void); -int tramp_set_kernel_args(unsigned long *size, unsigned long *iterations); +int tramp_set_kernel_args(unsigned long size, unsigned long iterations); int tramp_run_kernel(void); int tramp_copy_data(void **buffer, size_t size); -- cgit v1.1