aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile28
-rw-r--r--config.h3
-rwxr-xr-xgenerate-pow-imagery37
-rwxr-xr-xgenerate-samples32
-rw-r--r--mbrot-gen.c112
-rw-r--r--raw-to-png.cpp111
6 files changed, 100 insertions, 223 deletions
diff --git a/Makefile b/Makefile
index 6e18fac..f952224 100644
--- a/Makefile
+++ b/Makefile
@@ -1,25 +1,9 @@
-all: config mbrot-gen raw-to-png raw-to-png-bw
+all: mbrot-gen
-mbrot-gen:
- - gcc -o mbrot-gen mbrot-gen.c ./config.so -lm -Wall
+mbrot-gen: mbrot-gen.c
+ $(CC) -o $@ $@.c -lm -lpthread -Wall
-raw-to-png:
- - g++ -o raw-to-png raw-to-png.cpp -lm -lGL -lX11 -lpthread -Wall
-raw-to-png-bw:
- - g++ -o raw-to-png-bw raw-to-png.cpp -DBW -lm -lGL -lX11 -lpthread -Wall
-
-
-clean: clean-config clean-exec
-clean-exec:
- - rm mbrot-gen raw-to-png -f
-
-clean-config:
- - rm config.so -f
-
-config:
- - gcc -shared -o config.so config.c
-
-
-samples:
- - ./generate-samples
+.PHONY: all clean
+clean:
+ - rm mbrot-gen -f
diff --git a/config.h b/config.h
deleted file mode 100644
index 12b650d..0000000
--- a/config.h
+++ /dev/null
@@ -1,3 +0,0 @@
-unsigned int SIZE;
-unsigned int ITERATIONS;
-float POWER;
diff --git a/generate-pow-imagery b/generate-pow-imagery
deleted file mode 100755
index 4906933..0000000
--- a/generate-pow-imagery
+++ /dev/null
@@ -1,37 +0,0 @@
-#!/bin/sh
-
-OUT_DIR="pow_imagery"
-
-mkdir -p $OUT_DIR
-
-
-
-make_sample () {
- N="$1-$2-$3pow"
-
- if [ -e $OUT_DIR/$N.png ]; then
- echo $OUT_DIR/$N : Already exists, skipping.
- else
- # Configure the generator
- echo unsigned int SIZE=$1\;unsigned int ITERATIONS=$2\;float POWER=$3\; > config.c
- make config
-
- # Generate raw data
- ./mbrot-gen > $OUT_DIR/$N.raw
-
- # Make normal colour image
- ./raw-to-png $OUT_DIR/$N.raw $1 $1
- mv 0,0.png $OUT_DIR/$N.png
-
- # Make (hypnotic) bw image to show boundaries clearly
- ./raw-to-png-bw $OUT_DIR/$N.raw $1 $1
- mv 0,0.png $OUT_DIR/$N-bw.png
-
- rm $OUT_DIR/$N.raw
- fi
-}
-
-for pow in $(seq 0.01 0.01 2); do
- make_sample 560 50 $pow
-done
-
diff --git a/generate-samples b/generate-samples
deleted file mode 100755
index 6cdd2fb..0000000
--- a/generate-samples
+++ /dev/null
@@ -1,32 +0,0 @@
-#!/bin/sh
-
-OUT_DIR="samples"
-
-mkdir -p $OUT_DIR
-
-
-
-make_sample () {
- # Configure the generator
- echo unsigned int SIZE=$1\;unsigned int ITERATIONS=$2\;float POWER=$3\; > config.c
- make config
-
- # Generate raw data
- ./mbrot-gen > $OUT_DIR/$1-$2-$3pow.raw
-
- # Make normal colour image
- ./raw-to-png $OUT_DIR/$1-$2-$3pow.raw $1 $1
- mv 0,0.png $OUT_DIR/$1-$2.png
-
- # Make (hypnotic) bw image to show boundaries clearly
- ./raw-to-png-bw $OUT_DIR/$1-$2-$3pow.raw $1 $1
- mv 0,0.png $OUT_DIR/$1-$2-$3-pow-bw.png
-
- rm $OUT_DIR/$1-$2-$3pow.raw
-}
-
-for it in 10 50 100; do
- for res in 35 70 140 280 560 1120 2240; do
- make_sample $res $it 2
- done
-done
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;
}
diff --git a/raw-to-png.cpp b/raw-to-png.cpp
deleted file mode 100644
index 0a6746e..0000000
--- a/raw-to-png.cpp
+++ /dev/null
@@ -1,111 +0,0 @@
-#include <iostream>
-#include <stdio.h>
-#include <CImg.h>
-
-//#define TILE_WIDTH 200
-//#define TILE_HEIGHT 200
-
-//#define IMAGE_WIDTH 12200
-//#define IMAGE_HEIGHT 12200
-
-using namespace cimg_library;
-
-int main(int argc, char **argv)
-{
- unsigned int TILE_SIZE, IMAGE_SIZE, x, y, tile_x, tile_y;
- char out_name[] = "________.png";
-
- if (argc != 4)
- {
- printf("%s in_file image_square tile_square\n",argv[0]);
- return 1;
- }
-
- IMAGE_SIZE = atoi(argv[2]);
- TILE_SIZE = atoi(argv[3]);
-
-
- unsigned char *buf = (unsigned char*)malloc(TILE_SIZE);
-
- FILE *fp = fopen(argv[1],"r");
- if (!fp)
- {
- printf("ERROR: Cannot open file\n");
- return 1;
- }
-
- CImg<unsigned char> buffer(TILE_SIZE, TILE_SIZE,1,3,1);
- for (tile_y = 0; tile_y < IMAGE_SIZE/TILE_SIZE; tile_y++)
- {
- for (tile_x = 0; tile_x < IMAGE_SIZE/TILE_SIZE; tile_x++)
- {
- // Seek to data for start of this tile
- #define DATA_Y (tile_y * (TILE_SIZE * TILE_SIZE) * (IMAGE_SIZE / TILE_SIZE))
- #define DATA_X (tile_x * TILE_SIZE)
-
- fseek(fp, DATA_Y+DATA_X, SEEK_SET);
-
- printf("Creating tile (%d,%d)...\n", tile_x, tile_y);
-
- for (y = 0; y < TILE_SIZE; y++)
- {
- fread(buf, TILE_SIZE, 1, fp);
- for (x = 0; x < TILE_SIZE; x++)
- {
-#ifdef BW
- if (buf[x] % 2)
- {
- buffer(x,y,0) = buffer(x,y,1) = buffer(x,y,2) = 255;
- } else {
- buffer(x,y,0) = buffer(x,y,1) = buffer(x,y,2) = 0;
- }
-#else
- buffer(x,y,0) = 0;
- buffer(x,y,1) = buf[x];
- buffer(x,y,2) = 0;
-#endif
- }
- // Seek and wrap around to next Y pos for tile
- fseek(fp, IMAGE_SIZE-TILE_SIZE, SEEK_CUR);
- }
- sprintf(out_name,"%d,%d.png",tile_x,tile_y);
- buffer.save(out_name);
- }
- }
- fclose(fp);
- free(buf);
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-//#ifdef BLACK
-// if (buf[x] == 255)
-// {
-// buffer(x,y,0) = 0;
-// buffer(x,y,1) = 0;
-// buffer(x,y,2) = 0;
-// } else {
-//#endif
-
-//#ifdef BLACK
-// }
-//#endif
-// printf("%d,%d\n",x,y);