Made the julia mode a command line option

This commit is contained in:
José Carlos Cuevas 2013-09-01 02:25:41 +02:00
parent 32cc86aac7
commit 83f86b8dcc
4 changed files with 103 additions and 68 deletions

119
main.c
View file

@ -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);
}

View file

@ -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);
}

View file

@ -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,

Binary file not shown.