From 9cbc448819152a9adde2b42439426ee874a30fc3 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 10 May 2008 07:57:28 +0200 Subject: [PATCH] src/sn-evolution.c: Added pthread support for (local) parallelization. --- src/Makefile | 4 ++-- src/sn-evolution.c | 61 ++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/Makefile b/src/Makefile index 55f1861..41dc03b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ CC = gcc -#CFLAGS = -Wall -Werror -std=c99 -O3 -CFLAGS = -Wall -Werror -std=c99 -O0 -g +CFLAGS = -Wall -Werror -std=c99 -O3 -pthread +#CFLAGS = -Wall -Werror -std=c99 -O0 -g -pthread all: sn-apply sn-cut sn-evolution sn-merge sn-normalize sn-show sn-tex diff --git a/src/sn-evolution.c b/src/sn-evolution.c index 9699fcb..b6c37b4 100644 --- a/src/sn-evolution.c +++ b/src/sn-evolution.c @@ -35,6 +35,8 @@ #include #include +#include + #include "sn_network.h" #include "sn_population.h" #include "sn_random.h" @@ -271,26 +273,62 @@ static int create_offspring (void) return (0); } /* int create_offspring */ -static int start_evolution (void) +static void *evolution_thread (void *arg) { - uint64_t i; - while (do_loop == 0) { create_offspring (); - + /* XXX: Not synchronized! */ iteration_counter++; - i = iteration_counter; + } + + return ((void *) 0); +} /* int start_evolution */ + +static int evolution_start (int threads_num) +{ + pthread_t threads[threads_num]; /* C99 ftw! */ + int i; + + for (i = 0; i < threads_num; i++) + { + int status; - if ((i % 1000) == 0) + status = pthread_create (&threads[i], /* attr = */ NULL, + evolution_thread, /* arg = */ NULL); + if (status != 0) { - int rating = sn_population_best_rating (population); - printf ("After %10llu iterations: Best rating: %4i\n", i, rating); + fprintf (stderr, "evolution_start: pthread_create[%i] failed " + "with status %i.\n", + i, status); + threads[i] = 0; } } + while (do_loop == 0) + { + int status; + + status = sleep (1); + if (status == 0) + { + int best_rating; + i = iteration_counter; + + best_rating = sn_population_best_rating (population); + printf ("After approximately %i iterations: Currently best rating: %i\n", i, best_rating); + } + } + + for (i = 0; i < threads_num; i++) + { + if (threads[i] == 0) + continue; + pthread_join (threads[i], /* value_ptr = */ NULL); + } + return (0); -} /* int start_evolution */ +} /* int evolution_start */ int main (int argc, char **argv) { @@ -334,9 +372,10 @@ int main (int argc, char **argv) "=======================\n", initial_input_file, inputs_num, max_population_size); - start_evolution (); + evolution_start (3); - printf ("Exiting after %llu iterations.\n", iteration_counter); + printf ("Exiting after %llu iterations.\n", + (unsigned long long) iteration_counter); { sn_network_t *n; -- 2.11.0