Global: collectd → libsortnetwork
[sort-networks.git] / src / sn-evolution.c
index 62b01eb..2f3fba3 100644 (file)
@@ -1,6 +1,6 @@
 /**
- * collectd - src/sn-evolution.c
- * Copyright (C) 2008  Florian octo Forster
+ * libsortnetwork - src/sn-evolution.c
+ * Copyright (C) 2008-2010  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
@@ -16,7 +16,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
  * Authors:
- *   Florian octo Forster <octo at verplant.org>
+ *   Florian octo Forster <ff at octo.it>
  **/
 
 #ifndef _ISOC99_SOURCE
 #include "sn_network.h"
 #include "sn_random.h"
 
+#if !defined(__GNUC__) || !__GNUC__
+# define __attribute__(x) /**/
+#endif
+
 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
  * */
 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;
@@ -65,7 +70,7 @@ static int evolution_threads_num = 4;
 
 static int do_loop = 0;
 
-static void sigint_handler (int signal)
+static void sigint_handler (int signal __attribute__((unused)))
 {
   do_loop++;
 } /* void sigint_handler */
@@ -224,7 +229,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);
@@ -258,7 +263,7 @@ static int create_offspring (void)
   return (0);
 } /* int create_offspring */
 
-static void *evolution_thread (void *arg)
+static void *evolution_thread (void *arg __attribute__((unused)))
 {
   while (do_loop == 0)
   {
@@ -371,6 +376,7 @@ int main (int argc, char **argv)
 
   {
     sn_network_t *n;
+    int tmp;
 
     n = sn_network_read_file (initial_input_file);
     if (n == NULL)
@@ -381,6 +387,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);
   }