From: Florian Forster Date: Tue, 1 Feb 2011 06:30:43 +0000 (+0100) Subject: sn-evolution-cut: Implement the "-n" and "-r" options. X-Git-Tag: v1.1.0~11 X-Git-Url: https://git.octo.it/?p=sort-networks.git;a=commitdiff_plain;h=ea03c7deb7078b4e08af1d248ba78a32d40ddd3c sn-evolution-cut: Implement the "-n" and "-r" options. --- diff --git a/src/sn-evolution-cut.c b/src/sn-evolution-cut.c index 0e4c1e8..bb3c9ab 100644 --- a/src/sn-evolution-cut.c +++ b/src/sn-evolution-cut.c @@ -34,6 +34,7 @@ #include #include #include +#include #include @@ -65,6 +66,9 @@ static int evolution_threads_num = 4; static int do_loop = 0; static uint64_t iteration_counter = 0; +static uint64_t max_iterations = 0; + +static int target_rating = 0; static void sigint_handler (int signal __attribute__((unused))) { @@ -82,6 +86,9 @@ static void exit_usage (const char *name) " -p Size of the population (default: 128)\n" " -P Send individuals to (may be repeated)\n" " -t Number of threads (default: 4)\n" + " -n Maximum number of steps (iterations) to perform\n" + " -r Target rating: Stop loop when rating is less then or equal\n" + " to this number.\n" "\n", name); exit (1); @@ -91,7 +98,7 @@ int read_options (int argc, char **argv) /* {{{ */ { int option; - while ((option = getopt (argc, argv, "i:o:O:p:P:s:t:h")) != -1) + while ((option = getopt (argc, argv, "i:o:O:p:P:s:t:n:r:h")) != -1) { switch (option) { @@ -159,6 +166,29 @@ int read_options (int argc, char **argv) /* {{{ */ break; } + case 'n': + { + errno = 0; + max_iterations = (uint64_t) strtoull (optarg, + /* endptr = */ NULL, + /* base = */ 0); + if (errno != 0) + { + fprintf (stderr, "Parsing integer argument failed: %s\n", + strerror (errno)); + exit_usage (argv[0]); + } + break; + } + + case 'r': + { + int tmp = atoi (optarg); + if (tmp > 0) + target_rating = tmp; + break; + } + case 'h': default: exit_usage (argv[0]); @@ -171,14 +201,9 @@ int read_options (int argc, char **argv) /* {{{ */ static int rate_network (const sn_network_t *n) /* {{{ */ { int rate; - int i; - rate = SN_NETWORK_STAGE_NUM (n); - for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++) - { - sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i); - rate += 2 * SN_STAGE_COMP_NUM (s); - } + rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n) + + sn_network_get_comparator_num (n); return (rate); } /* }}} int rate_network */ @@ -409,7 +434,13 @@ static int evolution_start (int threads_num) /* {{{ */ printf ("Best after approximately %i iterations: " "%i comparators in %i stages. Rating: %i.\n", iter, comparators_num, stages_num, rating); + + if ((target_rating > 0) && (rating <= target_rating)) + do_loop++; } + + if ((max_iterations > 0) && (iteration_counter >= max_iterations)) + do_loop++; } for (i = 0; i < threads_num; i++)