aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2016-10-26 15:56:46 +1300
committerDavid Phillips <david@sighup.nz>2016-10-26 15:56:46 +1300
commit22ccf34f4960b355d55e2aad14008a4c50e65197 (patch)
tree89e56d93126db82712a981e450d5c0bc27389015
parentf0778d25e747092d59fa6b7d3a1e15bfc795fcd3 (diff)
parent14f76b353d681eb92fa0cd113c2e9c666aaea951 (diff)
downloadfractal-gen-22ccf34f4960b355d55e2aad14008a4c50e65197.tar.xz
Merge branch 'multicore-eff'
-rw-r--r--fractal-gen.c44
-rw-r--r--fractal-gen.h8
2 files changed, 48 insertions, 4 deletions
diff --git a/fractal-gen.c b/fractal-gen.c
index 7f4e326..c5930c9 100644
--- a/fractal-gen.c
+++ b/fractal-gen.c
@@ -56,6 +56,24 @@ defaultsd(double *who, double def)
*who = def;
}
+double
+timespec_diff(struct timespec start, struct timespec end) {
+ long weight = 1000000000;
+ time_t s = (end.tv_sec - start.tv_sec);
+ long ns = (end.tv_nsec - start.tv_nsec) % weight;
+
+ return s + ((double)ns)/weight;
+}
+
+void
+*generate(void *section) {
+ data_section *s = (data_section*)section;
+ clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(s->time_start));
+ (s->generator)(s);
+ clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(s->time_end));
+ return NULL;
+}
+
int
main(int argc, char **argv)
{
@@ -107,6 +125,10 @@ main(int argc, char **argv)
ram_nice /= 1024;
}
+ /* FIXME clean up */
+ struct timespec time_start, time_end;
+ clock_gettime(CLOCK_REALTIME, &time_start);
+
fprintf(stderr,
"Forecast resource use:\n"
" Threads: %d\n"
@@ -142,10 +164,11 @@ main(int argc, char **argv)
sections[i].width = width;
sections[i].parent_frame.y = f.y;
sections[i].parent_frame.x = f.x;
+ sections[i].generator = generator;
sections[i].parent_frame.scale = f.scale;
sections[i].datasize = toalloc;
fprintf(stderr, " -> Thread %lu\r", i);
- pthread_create(&sections[i].thread, NULL, generator, &(sections[i]));
+ pthread_create(&sections[i].thread, NULL, generate, &(sections[i]));
}
s = &(sections[cores-1]);
@@ -170,6 +193,24 @@ main(int argc, char **argv)
kill(child, SIGKILL);
+ clock_gettime(CLOCK_REALTIME, &time_end);
+
+ fprintf(stderr, "\nDone\n");
+
+ double time_wall = timespec_diff(time_start, time_end);
+ double time_ch = 0;
+
+ for (i = 0; i < cores; i++) {
+ data_section *s = &(sections[i]);
+ time_ch += (timespec_diff(s->time_start, s->time_end)) / cores;
+ }
+
+ fprintf(stderr,
+ "Wall-clock time: %.2f seconds\n"
+ "Average worker time: %.2f seconds\n"
+ "Multi-core efficiency: %.2f%%\n"
+ , time_wall, time_ch, 100*(time_ch)/time_wall);
+
/* Output PGM Header */
printf("P5\n%d\n%d\n255\n",size,size/clust_total);
@@ -182,7 +223,6 @@ main(int argc, char **argv)
putchar(s->data[y*(s->width) + x/cores]);
}
}
- fprintf(stderr, "\nDone\n");
/* Free the memory we allocated for point data */
for (i = 0; i < cores; i++)
diff --git a/fractal-gen.h b/fractal-gen.h
index 210d50f..8dcd167 100644
--- a/fractal-gen.h
+++ b/fractal-gen.h
@@ -28,16 +28,21 @@
#include <stdbool.h>
#include <pthread.h>
+typedef void* (*generator_func)(void *);
+
struct frame {
double x;
double y;
double scale;
};
-typedef struct {
+typedef struct data_section_s {
volatile unsigned long idx;
+ generator_func generator;
struct frame parent_frame;
unsigned long core;
+ struct timespec time_start;
+ struct timespec time_end;
unsigned long width;
unsigned long datasize;
char* data;
@@ -53,7 +58,6 @@ double power;
double thread_mult; /* number to multiply available cores by to get thread count */
char *argv0;
-typedef void* (*generator_func)(void *);
void defaultsd(double*, double);
int parse_args(int argc, char **argv);