aboutsummaryrefslogtreecommitdiff
path: root/mbrot-gen.c
diff options
context:
space:
mode:
Diffstat (limited to 'mbrot-gen.c')
-rw-r--r--mbrot-gen.c112
1 files changed, 94 insertions, 18 deletions
diff --git a/mbrot-gen.c b/mbrot-gen.c
index cb79f54..08e438d 100644
--- a/mbrot-gen.c
+++ b/mbrot-gen.c
@@ -1,39 +1,115 @@
#include <stdio.h>
+#include <stdlib.h>
#include <complex.h>
#include <math.h>
-#include "config.h"
+#include <unistd.h>
+#include <pthread.h>
+
+typedef struct
+{
+ int core;
+ int cores;
+ int size;
+ double power;
+ unsigned int iterat;
+ char* data;
+ pthread_t thread;
+} data_section;
+
+
+void *generate_section(void *d);
+
+
int main(int argc, char **argv)
{
- unsigned int x, y, i;
- float a,b;
- float complex z,c;
+ int size, iterat, cores,i;
+ double power;
+
+ if (argc != 4)
+ {
+ fprintf(stderr, "%s size iterat power", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ size = atoi(argv[1]);
+ iterat = atoi(argv[2]);
+ power = atof(argv[3]);
- fprintf(stderr, "Creating %dx%d image (raw format)\n",SIZE,SIZE);
+ // Fetch number of cores available on machine, so we can use lots of them to speed things up
+ cores = sysconf (_SC_NPROCESSORS_ONLN);
+// cores = 8;
- y = 0;
- for (b = -1.75f; y < SIZE; b+=(3.5f/SIZE))
+
+ fprintf(stderr, "Found %d cores. I'll spawn as many threads and let the scheduler sort it out.\n", cores);
+
+
+
+
+
+ int x,y,s;
+
+ data_section* sections = malloc(sizeof(data_section)*cores);
+
+ fprintf(stderr, "Spawning threads:\n");
+ // Spawn all the threads! Something something interlacing
+ for (i = 0; i < cores; i++)
{
- x = 0;
- for (a = -2.5f; x < SIZE; a+=(3.5f/SIZE))
+ sections[i].core = i;
+ sections[i].cores = cores;
+ sections[i].size = size;
+ sections[i].power = power;
+ sections[i].iterat = iterat;
+
+ fprintf(stderr, " -> Thread #%d\n", i);
+ pthread_create(&sections[i].thread, NULL, &generate_section, &(sections[i]));
+ }
+
+
+ for (i = 0; i < cores; i++)
+ pthread_join(sections[i].thread, NULL);
+
+
+
+ for (y = 0; y < size; y++)
+ for (x = 0; x < size; x++)
+ printf("%c", sections[y%cores].data[(y/cores)*size + x]);
+
+
+ for (s = 0; s < cores; s++)
+ free(sections[s].data);
+
+ fprintf(stderr,"\n");
+ return 0;
+}
+
+
+void *generate_section(void *section)
+{
+ data_section *d = (data_section*)section;
+ d->data = malloc((d->size*d->size)/d->cores);
+ unsigned int x,y,i;
+ double a,b;
+ double complex z,c;
+
+ int idx = 0;
+
+ for (y = d->core, b = (d->core*(3.5f/d->size)-1.75f); y < d->size; b+=((d->cores*3.5f)/d->size), y+=d->cores)
+ {
+ for (x = 0, a = -2.5f; x < d->size; a+=(3.5f/d->size), x++)
{
z = 0;
c = a+I*b;
- for (i = 0; i < ITERATIONS; i++)
+ for (i = 0; i < d->iterat; i++)
{
if (cabsf(z) >= 2)
break;
- z = cpow(z,POWER)+c;
+ z = cpow(z, d->power)+c;
}
- printf("%c",( (255*i)/ITERATIONS ) );
- x++;
+ d->data[idx++] = (255*i)/d->iterat;
}
- y++;
- if ( (y%10) == 0 )
- fprintf(stderr,"\r%.3f%%",y/(float)SIZE*100);
}
- fprintf(stderr,"\n");
- return 0;
+ return NULL;
}