X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fsn-evolution.c;h=86bdfba7cd292d0a394a143da939ebb834237d70;hb=79789cfc25a65312d8b91000abc7eb9209322555;hp=64da7026697b859f103999e3e730b014b4f05946;hpb=f98242c73c053bf818cea2cb27690a1888f72eff;p=sort-networks.git diff --git a/src/sn-evolution.c b/src/sn-evolution.c index 64da702..86bdfba 100644 --- a/src/sn-evolution.c +++ b/src/sn-evolution.c @@ -1,6 +1,6 @@ /** * collectd - src/sn-evolution.c - * Copyright (C) 2008 Florian octo Forster + * Copyright (C) 2008,2009 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 @@ -52,6 +52,7 @@ char *strdup (const char *s); static uint64_t iteration_counter = 0; static int inputs_num = 16; +static int inputs_num_is_power_of_two = 1; static char *initial_input_file = NULL; static char *best_output_file = NULL; @@ -61,6 +62,8 @@ static int stats_interval = 0; static int max_population_size = 128; static population_t *population; +static int evolution_threads_num = 4; + static int do_loop = 0; static void sigint_handler (int signal) @@ -76,6 +79,8 @@ static void exit_usage (const char *name) " -i Initial input file (REQUIRED)\n" " -o Write the current best solution to \n" " -p Size of the population (default: 128)\n" + " -P Send individuals to (may be repeated)\n" + " -t Number of threads (default: 4)\n" "\n", name); exit (1); @@ -85,7 +90,7 @@ int read_options (int argc, char **argv) { int option; - while ((option = getopt (argc, argv, "i:o:p:P:s:h")) != -1) + while ((option = getopt (argc, argv, "i:o:p:P:s:t:h")) != -1) { switch (option) { @@ -109,7 +114,23 @@ int read_options (int argc, char **argv) { int tmp = atoi (optarg); if (tmp > 0) + { max_population_size = tmp; + population_set_size (population, (size_t) max_population_size); + } + break; + } + + case 'P': + { + int status; + + status = population_add_peer (population, optarg, /* port = */ NULL); + if (status != 0) + { + fprintf (stderr, "population_add_peer failed with status %i.\n", + status); + } break; } @@ -121,6 +142,14 @@ int read_options (int argc, char **argv) break; } + case 't': + { + int tmp = atoi (optarg); + if (tmp >= 1) + evolution_threads_num = tmp; + break; + } + case 'h': default: exit_usage (argv[0]); @@ -196,7 +225,7 @@ static int create_offspring (void) assert (p1 != NULL); /* combine the two parents */ - n = sn_network_combine (p0, p1); + n = sn_network_combine (p0, p1, inputs_num_is_power_of_two); sn_network_destroy (p0); sn_network_destroy (p1); @@ -220,7 +249,7 @@ static int create_offspring (void) assert (SN_NETWORK_INPUT_NUM (n) == inputs_num); - if (sn_bounded_random (0, 100) <= 1) + if ((SN_NETWORK_INPUT_NUM (n) <= 16) && (sn_bounded_random (0, 100) <= 1)) mutate_network (n); population_insert (population, n); @@ -270,17 +299,32 @@ static int evolution_start (int threads_num) if (status == 0) { sn_network_t *n; + int stages_num; + int comparators_num; int rating; + int iter; - i = iteration_counter; + iter = iteration_counter; n = population_get_fittest (population); + rating = rate_network (n); + + stages_num = SN_NETWORK_STAGE_NUM (n); + comparators_num = 0; + for (i = 0; i < stages_num; i++) + { + sn_stage_t *s; + + s = SN_NETWORK_STAGE_GET (n, i); + comparators_num += SN_STAGE_COMP_NUM (s); + } + sn_network_destroy (n); - printf ("After approximately %i iterations: " - "Currently best rating: %i\n", - i, rating); + printf ("Best after approximately %i iterations: " + "%i comparators in %i stages. Rating: %i.\n", + iter, comparators_num, stages_num, rating); } } @@ -299,6 +343,19 @@ int main (int argc, char **argv) struct sigaction sigint_action; struct sigaction sigterm_action; + population = population_create ((pi_rate_f) rate_network, + (pi_copy_f) sn_network_clone, + (pi_free_f) sn_network_destroy); + if (population == NULL) + { + fprintf (stderr, "population_create failed.\n"); + return (1); + } + + population_set_serialization (population, + (pi_serialize_f) sn_network_serialize, + (pi_unserialize_f) sn_network_unserialize); + read_options (argc, argv); if (initial_input_file == NULL) exit_usage (argv[0]); @@ -311,17 +368,11 @@ int main (int argc, char **argv) sigterm_action.sa_handler = sigint_handler; sigaction (SIGTERM, &sigterm_action, NULL); - population = population_create ((pi_rate_f) rate_network, - (pi_copy_f) sn_network_clone, - (pi_free_f) sn_network_destroy); - if (population == NULL) - { - fprintf (stderr, "population_create failed.\n"); - return (1); - } + population_start_listen_thread (population, NULL, NULL); { sn_network_t *n; + int tmp; n = sn_network_read_file (initial_input_file); if (n == NULL) @@ -332,6 +383,23 @@ int main (int argc, char **argv) inputs_num = SN_NETWORK_INPUT_NUM(n); + /* Determine if `inputs_num' is a power of two. If so, more merge + * algorithms can be used. */ + tmp = inputs_num; + inputs_num_is_power_of_two = 1; + while (tmp > 0) + { + if ((tmp % 2) != 0) + { + if (tmp == 1) + inputs_num_is_power_of_two = 1; + else + inputs_num_is_power_of_two = 0; + break; + } + tmp = tmp >> 1; + } + population_insert (population, n); sn_network_destroy (n); } @@ -343,7 +411,7 @@ int main (int argc, char **argv) "=======================\n", initial_input_file, inputs_num, max_population_size); - evolution_start (3); + evolution_start (evolution_threads_num); printf ("Exiting after %llu iterations.\n", (unsigned long long) iteration_counter); @@ -356,8 +424,7 @@ int main (int argc, char **argv) { if (best_output_file != NULL) sn_network_write_file (n, best_output_file); - else - sn_network_show (n); + sn_network_show (n); sn_network_destroy (n); } }