X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsn-evolution.c;h=b6c37b473ca5345c4dd7ba806688783b0add0702;hb=9cbc448819152a9adde2b42439426ee874a30fc3;hp=48012a620b6d0d2b4cacd018dc6e98bbe886457c;hpb=6036d137f84b8ee853779ce7d3513f72f9f62621;p=sort-networks.git diff --git a/src/sn-evolution.c b/src/sn-evolution.c index 48012a6..b6c37b4 100644 --- a/src/sn-evolution.c +++ b/src/sn-evolution.c @@ -1,3 +1,24 @@ +/** + * collectd - src/sn-evolution.c + * Copyright (C) 2008 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + #define _ISOC99_SOURCE #define _POSIX_C_SOURCE 200112L @@ -14,6 +35,8 @@ #include #include +#include + #include "sn_network.h" #include "sn_population.h" #include "sn_random.h" @@ -250,25 +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; + } -#if 0 - if ((stats_interval > 0) && ((i % stats_interval) == 0)) - population_print_stats (i); -#endif + 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; + + status = pthread_create (&threads[i], /* attr = */ NULL, + evolution_thread, /* arg = */ NULL); + if (status != 0) + { + 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) { @@ -312,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;