From 83f86b8dcc92c549f61984f0c03d2ca44198e218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Sun, 1 Sep 2013 02:25:41 +0200 Subject: [PATCH] Made the julia mode a command line option --- main.c | 119 ++++++++++++++++++++++++------------------- mandel_classic.c | 50 ++++++++++++------ mandelbrot_kernel.cl | 2 +- mandelclassic | Bin 12111 -> 12502 bytes 4 files changed, 103 insertions(+), 68 deletions(-) diff --git a/main.c b/main.c index 96f079f..4f39e02 100644 --- a/main.c +++ b/main.c @@ -25,14 +25,23 @@ int main(int argn, char **argv) { int res_x = 800; int res_y = 600; int current_line = 0; - int total_res = res_x * res_y; + int julia_mode = 0; + + if (argn == 1) + { + julia_mode = 0; + } + else if ((argn == 2) && (strcmp(argv[1], "-julia") == 0)) + { + julia_mode = 1; + printf("Julia mode activated...\n"); + } screen = SDL_SetVideoMode(res_x, res_y, 0, SDL_DOUBLEBUF); if(!screen) fprintf(stderr,"Could not set video mode: %s\n",SDL_GetError()); // Prepare the resolution and sizes and colors... - int i; const int ITERATIONS = 256; // Load the kernel source code into the array source_str @@ -40,7 +49,11 @@ int main(int argn, char **argv) { char *source_str; size_t source_size; - fp = fopen("mandelbrot_kernel.cl", "r"); + if (julia_mode == 0) + fp = fopen("mandelbrot_kernel.cl", "r"); + else + fp = fopen("julia_kernel.cl", "r"); + if (!fp) { fprintf(stderr, "Failed to load kernel.\n"); exit(1); @@ -101,19 +114,23 @@ int main(int argn, char **argv) { printf("program built\n"); // Create the OpenCL kernel - cl_kernel kernel = clCreateKernel(program, "mandelbrot_point", &ret); + cl_kernel kernel = clCreateKernel(program, "fractal_point", &ret); // Common kernel params ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &kernel_res_x); ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &kernel_res_y); ret = clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &graph_mem_obj); - // Our screen in a linear array - int graph_dots[total_res]; // = (int*)malloc(total_res * sizeof(int)); int graph_line[res_x]; // (int*)malloc(res_x * sizeof(int)); - float zoom; // Our current zoom level + float zoom = 1.0; // Our current zoom level + float stop_point; - for(zoom = 1.0; zoom > 0.00001; zoom = zoom * 0.98) + if (julia_mode == 0) + stop_point = 0.00001; + else + stop_point = -2.5; + + while(zoom > stop_point) { for (current_line = 0; current_line < res_y; current_line++) { @@ -137,8 +154,8 @@ int main(int argn, char **argv) { if (ret != CL_SUCCESS) { - printf("Error while executing kernel\n"); - printf("Error code %d\n", ret); + // printf("Error while executing kernel\n"); + // printf("Error code %d\n", ret); } // Wait for the computation to finish @@ -154,53 +171,51 @@ int main(int argn, char **argv) { clFinish(command_queue); int line_count; + Uint32 *pixel; + // Lock surface + // SDL_LockSurface(screen); + // rank = screen->pitch/sizeof(Uint32); + pixel = (Uint32*)screen->pixels; + int iteration; + for (line_count = 0; line_count < res_x; line_count++) { - int temp_val = graph_line[line_count]; - graph_dots[(current_line * res_x) + line_count] = temp_val; + // int temp_val = graph_line[line_count]; + // graph_dots[(current_line * res_x) + line_count] = temp_val; + // Get the iterations for the point + // printf("Point %d\n", i); + iteration = graph_line[line_count]; + if ((iteration < 128) && (iteration > 0)) { + pixel[(current_line * res_x) + line_count] = SDL_MapRGBA(screen->format, + 0, + 20 + iteration, + 0, + 255); + } + else if ((iteration >= 128) && (iteration < ITERATIONS)) + { + pixel[(current_line * res_x) + line_count] = SDL_MapRGBA(screen->format, + iteration, + 148, + iteration, + 255); + } + else + { + pixel[(current_line * res_x) + line_count] = SDL_MapRGBA(screen->format, + 0, + 0, + 0, + 255); + } } } + if (julia_mode == 0) + zoom = zoom * 0.98; + else + zoom -= 0.01; - int iteration; - Uint32 *pixel; - // Lock surface - // SDL_LockSurface(screen); - // rank = screen->pitch/sizeof(Uint32); - pixel = (Uint32*)screen->pixels; - /* Draw all dots */ - for(i = 0;i < total_res;i++) - { - // Get the iterations for the point - // printf("Point %d\n", i); - iteration = graph_dots[i]; - if ((iteration < 128) && (iteration > 0)) { - pixel[i] = SDL_MapRGBA(screen->format, - 0, - 20 + iteration, - 0, - 255); - } - else if ((iteration >= 128) && (iteration < ITERATIONS)) - { - pixel[i] = SDL_MapRGBA(screen->format, - iteration, - 148, - iteration, - 255); - } - else - { - pixel[i] = SDL_MapRGBA(screen->format, - 0, - 0, - 0, - 255); - } - } - // Unlock surface - // SDL_UnlockSurface(screen); - - // Draw to the scree + // Draw to the screen SDL_Flip(screen); } diff --git a/mandel_classic.c b/mandel_classic.c index 29c6e54..a92c5d8 100644 --- a/mandel_classic.c +++ b/mandel_classic.c @@ -38,6 +38,7 @@ struct piece_args int max_iteration; int total_threads; int thread_number; + int julia_mode; }; @@ -51,13 +52,15 @@ int get_y (int linear_point, int height) return floor(linear_point / height); } -float map_x(int x, int width, float zoom) +float map_x_mandelbrot(int x, int width, float zoom) { -#ifndef JULIA return (((float)x / (float)width) * (3.5 * zoom)) - (2.5 - (1.0 - zoom)); -#else return (((float)x / (float)width) * (3.5 * zoom)) - (1.75 - (1.0 - zoom)); -#endif +} + +float map_x_julia(int x, int width, float zoom) +{ + return (((float)x / (float)width) * (3.5 * zoom)) - (1.75 - (1.0 - zoom)); } float map_y(int y, int height, float zoom) @@ -124,7 +127,7 @@ void store_iteration(float pos_x, float pos_y, int iteration, float x, float y) int mandelbrot_point(int res_x, int res_y, int image_x, int image_y, float zoom, int max_iteration) { // Get the index of the current element - float pos_x = map_x(image_x, res_x, zoom); + float pos_x = map_x_mandelbrot(image_x, res_x, zoom); float pos_y = map_y(image_y, res_y, zoom); float x = 0.0; float y = 0.0; @@ -191,7 +194,7 @@ int mandelbrot_point(int res_x, int res_y, int image_x, int image_y, float zoom, int julia_point(int res_x, int res_y, int image_x, int image_y, float zoom, int max_iteration) { // Get the index of the current element - float pos_x = map_x(image_x, res_x, 1.0); + float pos_x = map_x_julia(image_x, res_x, 1.0); float pos_y = map_y(image_y, res_y, 1.0); float x = pos_x; float y = pos_y; @@ -288,11 +291,10 @@ void *thread_launcher(void *arguments) { for (x = init_x; x < limit_x; x++) { -#ifndef JULIA + if(args->julia_mode == 0) iteration_pixels[x + (y * args->res_x)] = mandelbrot_point(args->res_x, args->res_y, x, y, args->zoom, args->max_iteration); -#else + else iteration_pixels[x + (y * args->res_x)] = julia_point(args->res_x, args->res_y, x, y, args->zoom, args->max_iteration); -#endif } } } @@ -316,6 +318,16 @@ int main(int argn, char **argv) SDL_Surface *screen; int res_x = 800; int res_y = 600; + int julia_mode = 0; + + if(argn == 1) + { + julia_mode = 0; + } + else if ((argn == 2) && (strcmp(argv[1], "-julia") == 0)) + { + julia_mode = 1; + } int number_cores = get_cpus(); int number_threads = number_cores * number_cores; @@ -365,13 +377,15 @@ int main(int argn, char **argv) printf("Rendering...\n"); - float zoom; + float zoom = 1.0; + float stop_point; -#ifndef JULIA - for (zoom = 1.0; zoom > 0.0001 ; zoom = zoom * 0.98) -#else - for (zoom = 1.0; zoom > -2.5 ; zoom -= 0.01) -#endif + if (julia_mode == 0) + stop_point = 0.00001; + else + stop_point = -2.5; + + while(zoom > stop_point) { int iteration, max_iteration, x, y, res; if((zoom < -0.02) && (zoom > -1.0)) @@ -393,6 +407,7 @@ int main(int argn, char **argv) arguments[thread_count].max_iteration = max_iteration; arguments[thread_count].total_threads = number_threads; arguments[thread_count].thread_number = thread_count; + arguments[thread_count].julia_mode = julia_mode; pthread_create( &threads[thread_count], NULL, thread_launcher, (void*) &arguments[thread_count]); } @@ -441,6 +456,11 @@ int main(int argn, char **argv) } } + if(julia_mode == 0) + zoom = zoom * 0.99; + else + zoom -= 0.01; + SDL_Flip(screen); } diff --git a/mandelbrot_kernel.cl b/mandelbrot_kernel.cl index d42f85d..9a18e26 100644 --- a/mandelbrot_kernel.cl +++ b/mandelbrot_kernel.cl @@ -8,7 +8,7 @@ float map_y(int y, int height, float zoom) return (((float)y / (float)height) * (2.0 * zoom)) - (1.00001 - (1.0 - zoom)); } -__kernel void mandelbrot_point(__global const int *res_x, +__kernel void fractal_point(__global const int *res_x, __global const int *res_y, __global const int *line, __global const float *zoom, diff --git a/mandelclassic b/mandelclassic index 4c61ca940241435362b07d52600dfa4257e3ab69..ba3552fc7ea36e2a45d9dbf029c32c4922bd0bbb 100755 GIT binary patch delta 4692 zcmai2eQ;FO6~Avc3rXDMz1{3)HydD=n2_+bva}!{Vs>FyUNVUjfoK{NNYEfKfRt{4 zQDmdbDz9s3=%r(7r!_DwjU6j#aTKj%mOxC}+G()XMs%DQweuFF#w2`5(ARVBzNb&} zhj-?^bAP{c?z!ild*9uA_n+(EZESN|pUCzTEU7mkgVPSrIlk~MCnJd_3Gpz3gZC78 z22}lkvw`sgPHNz0S!I=$Ra$n^W!(CiHMz`))a3eEMU(qdG}%XA<*2Rp*x%FX;H4I^Lr3 zO;FjZQ&=?x$QB)+qvH?ic$gXu zC)*gY5)E*6lPwG{LyWVU+{y4l#5kMD28QP###u~OFq-109T9B zr-Azd_Xes1>&m3@wbDel2Xtcev%N9~3|Y^Q$j?AcjIT&QQ&H-9W1Zbkd{e2LrLy){C2%7#$uFXTJk528f8KhfJc>)FRFv5TgmJ7##xbSyB4k zUhpP={FS1_n~#j~5y6BrHpW{*ZLurOS!2AAwKhdmwgE7$1i;%8fj9mDPWjsYQQlH{ za1^XEK(UGURvz3f@Lp3fpi>WmNswPX60htG`92Zjzs9~|f8`Io2#R>9GhPinT2`e8 zr3Y+%MjXGd@k-ySG)#xIN+^5athClCwwhA|ka~jBPt4-!VG}fp;ff1H&vN4Izlrfy zbSy^COcS|tV!XOVY`vrBwGJ!H);1VeaCuWFgwC2YOEh41+I9gA(=AIM`V!qQLDuAs z8+5x_%-H1R-3K5#LSHe@C@#GLok&y5qJ3!b2J_K*s0Xl49|OIq(zqSyBG zPd-m?nDZjagx}@{wGze%8N8UeiFXGHiM=0g_zQmMV(VjF^1IM+EMW^@XnCy5 z9N>c=TuiECAhazjH*#+(u%s1o1bv5Kw(3mp!7|nQPCg788RHjQn+AP}`vY}>`oQ|Y zhWazUOJ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