2 * libsortnetwork - src/sn_network.c
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <ff at octo.it>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
30 # define DPRINTF(...) fprintf (stderr, "sn_network: " __VA_ARGS__)
32 # define DPRINTF(...) /**/
43 #include "sn_network.h"
44 #include "sn_random.h"
46 sn_network_t *sn_network_create (int inputs_num) /* {{{ */
50 n = (sn_network_t *) malloc (sizeof (sn_network_t));
53 memset (n, '\0', sizeof (sn_network_t));
55 n->inputs_num = inputs_num;
58 } /* }}} sn_network_t *sn_network_create */
60 void sn_network_destroy (sn_network_t *n) /* {{{ */
65 if (n->stages != NULL)
68 for (i = 0; i < n->stages_num; i++)
70 sn_stage_destroy (n->stages[i]);
78 } /* }}} void sn_network_destroy */
80 sn_network_t *sn_network_create_odd_even_mergesort (int inputs_num) /* {{{ */
84 n = sn_network_create (inputs_num);
86 assert (inputs_num > 0);
99 s = sn_stage_create (/* depth = */ 0);
100 sn_stage_comparator_add (s, &c);
101 sn_network_stage_add (n, s);
107 sn_network_t *n_left;
108 sn_network_t *n_right;
112 inputs_left = inputs_num / 2;
113 inputs_right = inputs_num - inputs_left;
115 n_left = sn_network_create_odd_even_mergesort (inputs_left);
119 n_right = sn_network_create_odd_even_mergesort (inputs_right);
122 sn_network_destroy (n_left);
126 n = sn_network_combine_odd_even_merge (n_left, n_right);
128 sn_network_destroy (n_left);
129 sn_network_destroy (n_right);
132 sn_network_compress (n);
136 } /* }}} sn_network_t *sn_network_create_odd_even_mergesort */
138 static int sn_network_create_pairwise_internal (sn_network_t *n, /* {{{ */
139 int *inputs, int inputs_num)
142 int inputs_copy[inputs_num];
145 for (i = 1; i < inputs_num; i += 2)
147 sn_comparator_t *c = sn_comparator_create (inputs[i-1], inputs[i]);
148 sn_network_comparator_add (n, c);
149 sn_comparator_destroy (c);
155 /* Sort "pairs" recursively. Like with odd-even mergesort, odd and even lines
156 * are handled recursively and later reunited. */
157 for (i = 0; i < inputs_num; i += 2)
158 inputs_copy[(int) (i / 2)] = inputs[i];
159 /* Recursive call #1 with first set of lines */
160 sn_network_create_pairwise_internal (n, inputs_copy,
161 (int) ((inputs_num + 1) / 2));
163 for (i = 1; i < inputs_num; i += 2)
164 inputs_copy[(int) (i / 2)] = inputs[i];
165 /* Recursive call #2 with second set of lines */
166 sn_network_create_pairwise_internal (n, inputs_copy,
167 (int) (inputs_num/ 2));
169 /* m is the "amplitude" of the sorted pairs. This is a bit tricky to read due
170 * to different indices being used in the paper, unfortunately. */
174 for (i = 1; (i + (m - 1)) < inputs_num; i += 2)
177 int right = i + (m - 1);
180 assert (left < right);
181 c = sn_comparator_create (inputs[left], inputs[right]);
182 sn_network_comparator_add (n, c);
183 sn_comparator_destroy (c);
187 } /* while (m > 1) */
190 } /* }}} int sn_network_create_pairwise_internal */
192 sn_network_t *sn_network_create_pairwise (int inputs_num) /* {{{ */
194 sn_network_t *n = sn_network_create (inputs_num);
195 int inputs[inputs_num];
201 for (i = 0; i < inputs_num; i++)
204 sn_network_create_pairwise_internal (n, inputs, inputs_num);
205 sn_network_compress (n);
208 } /* }}} sn_network_t *sn_network_create_pairwise */
210 int sn_network_stage_add (sn_network_t *n, sn_stage_t *s) /* {{{ */
214 if ((n == NULL) || (s == NULL))
217 temp = (sn_stage_t **) realloc (n->stages, (n->stages_num + 1)
218 * sizeof (sn_stage_t *));
223 SN_STAGE_DEPTH (s) = n->stages_num;
224 n->stages[n->stages_num] = s;
228 } /* }}} int sn_network_stage_add */
230 int sn_network_stage_remove (sn_network_t *n, int s_num) /* {{{ */
232 int nmemb = n->stages_num - (s_num + 1);
235 if ((n == NULL) || (s_num >= n->stages_num))
238 sn_stage_destroy (n->stages[s_num]);
239 n->stages[s_num] = NULL;
243 memmove (n->stages + s_num, n->stages + (s_num + 1),
244 nmemb * sizeof (sn_stage_t *));
245 n->stages[n->stages_num - 1] = NULL;
249 /* Free the unused memory */
250 if (n->stages_num == 0)
257 temp = (sn_stage_t **) realloc (n->stages,
258 n->stages_num * sizeof (sn_stage_t *));
265 } /* }}} int sn_network_stage_remove */
267 sn_network_t *sn_network_clone (const sn_network_t *n) /* {{{ */
269 sn_network_t *n_copy;
272 n_copy = sn_network_create (n->inputs_num);
276 for (i = 0; i < n->stages_num; i++)
281 s = sn_stage_clone (n->stages[i]);
285 status = sn_network_stage_add (n_copy, s);
290 if (i < n->stages_num)
292 sn_network_destroy (n_copy);
297 } /* }}} sn_network_t *sn_network_clone */
299 int sn_network_comparator_add (sn_network_t *n, /* {{{ */
300 const sn_comparator_t *c)
304 if ((n == NULL) || (c == NULL))
307 if (n->stages_num > 0)
309 s = n->stages[n->stages_num - 1];
311 if (sn_stage_comparator_check_conflict (s, c) == 0)
313 sn_stage_comparator_add (s, c);
318 s = sn_stage_create (n->stages_num);
319 sn_stage_comparator_add (s, c);
320 sn_network_stage_add (n, s);
323 } /* }}} int sn_network_comparator_add */
325 int sn_network_get_comparator_num (const sn_network_t *n) /* {{{ */
334 for (i = 0; i < n->stages_num; i++)
335 num += n->stages[i]->comparators_num;
338 } /* }}} int sn_network_get_comparator_num */
340 int sn_network_show (sn_network_t *n) /* {{{ */
344 for (i = 0; i < n->stages_num; i++)
345 sn_stage_show (n->stages[i]);
348 } /* }}} int sn_network_show */
350 int sn_network_invert (sn_network_t *n) /* {{{ */
357 for (i = 0; i < n->stages_num; i++)
358 sn_stage_invert (n->stages[i]);
361 } /* }}} int sn_network_invert */
363 int sn_network_shift (sn_network_t *n, int sw) /* {{{ */
367 if ((n == NULL) || (sw < 0))
373 for (i = 0; i < n->stages_num; i++)
374 sn_stage_shift (n->stages[i], sw, SN_NETWORK_INPUT_NUM (n));
377 } /* }}} int sn_network_shift */
379 int sn_network_compress (sn_network_t *n) /* {{{ */
385 for (i = 1; i < n->stages_num; i++)
391 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
393 sn_comparator_t *c = SN_STAGE_COMP_GET (s, j);
396 for (k = i - 1; k >= 0; k--)
400 conflict = sn_stage_comparator_check_conflict (n->stages[k], c);
415 sn_stage_comparator_add (n->stages[move_to], c);
416 sn_stage_comparator_remove (s, j);
422 while ((n->stages_num > 0)
423 && (SN_STAGE_COMP_NUM (n->stages[n->stages_num - 1]) == 0))
424 sn_network_stage_remove (n, n->stages_num - 1);
427 } /* }}} int sn_network_compress */
429 int sn_network_normalize (sn_network_t *n) /* {{{ */
433 for (i = 0; i < n->stages_num; i++)
440 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
446 c = SN_STAGE_COMP_GET (s, j);
455 for (k = i; k < n->stages_num; k++)
456 sn_stage_swap (n->stages[k], min, max);
461 } /* for (j = 0 .. #comparators) */
462 } /* for (i = n->stages_num - 1 .. 0) */
465 } /* }}} int sn_network_normalize */
467 int sn_network_cut_at (sn_network_t *n, int input, /* {{{ */
468 enum sn_network_cut_dir_e dir)
471 int position = input;
473 for (i = 0; i < n->stages_num; i++)
479 new_position = sn_stage_cut_at (s, position, dir);
481 if (position != new_position)
485 for (j = 0; j < i; j++)
486 sn_stage_swap (n->stages[j], position, new_position);
489 position = new_position;
492 assert (((dir == DIR_MIN) && (position == 0))
493 || ((dir == DIR_MAX) && (position == (n->inputs_num - 1))));
495 for (i = 0; i < n->stages_num; i++)
496 sn_stage_remove_input (n->stages[i], position);
501 } /* }}} int sn_network_cut_at */
503 /* sn_network_concatenate
505 * `Glues' two networks together, resulting in a comparator network with twice
506 * as many inputs but one that doesn't really sort anymore. It produces a
507 * bitonic sequence, though, that can be used by the mergers below. */
508 static sn_network_t *sn_network_concatenate (sn_network_t *n0, /* {{{ */
516 stages_num = (n0->stages_num > n1->stages_num)
520 n = sn_network_create (n0->inputs_num + n1->inputs_num);
524 for (i = 0; i < stages_num; i++)
526 sn_stage_t *s = sn_stage_create (i);
528 if (i < n0->stages_num)
529 for (j = 0; j < SN_STAGE_COMP_NUM (n0->stages[i]); j++)
531 sn_comparator_t *c = SN_STAGE_COMP_GET (n0->stages[i], j);
532 sn_stage_comparator_add (s, c);
535 if (i < n1->stages_num)
536 for (j = 0; j < SN_STAGE_COMP_NUM (n1->stages[i]); j++)
538 sn_comparator_t *c_orig = SN_STAGE_COMP_GET (n1->stages[i], j);
539 sn_comparator_t c_copy;
541 SN_COMP_MIN(&c_copy) = SN_COMP_MIN(c_orig) + n0->inputs_num;
542 SN_COMP_MAX(&c_copy) = SN_COMP_MAX(c_orig) + n0->inputs_num;
544 sn_stage_comparator_add (s, &c_copy);
547 sn_network_stage_add (n, s);
551 } /* }}} sn_network_t *sn_network_concatenate */
553 static int sn_network_add_bitonic_merger_recursive (sn_network_t *n, /* {{{ */
563 s = sn_stage_create (n->stages_num);
569 for (i = low; i < (low + m); i++)
576 sn_stage_comparator_add (s, &c);
579 sn_network_stage_add (n, s);
581 sn_network_add_bitonic_merger_recursive (n, low, m);
582 sn_network_add_bitonic_merger_recursive (n, low + m, m);
585 } /* }}} int sn_network_add_bitonic_merger_recursive */
587 static int sn_network_add_bitonic_merger (sn_network_t *n) /* {{{ */
594 s = sn_stage_create (n->stages_num);
598 m = n->inputs_num / 2;
600 for (i = 0; i < m; i++)
605 c.max = n->inputs_num - (i + 1);
607 sn_stage_comparator_add (s, &c);
610 sn_network_stage_add (n, s);
612 sn_network_add_bitonic_merger_recursive (n, 0, m);
613 sn_network_add_bitonic_merger_recursive (n, m, m);
615 sn_network_add_bitonic_merger_recursive (n, 0, SN_NETWORK_INPUT_NUM (n));
619 } /* }}} int sn_network_add_bitonic_merger */
621 static int sn_network_add_odd_even_merger (sn_network_t *n, /* {{{ */
622 int *indizes_left, int indizes_left_num,
623 int *indizes_right, int indizes_right_num)
625 int tmp_left[indizes_left_num];
627 int tmp_right[indizes_left_num];
633 if ((indizes_left_num == 0) || (indizes_right_num == 0))
637 else if ((indizes_left_num == 1) && (indizes_right_num == 1))
642 c.min = *indizes_left;
643 c.max = *indizes_right;
645 s = sn_stage_create (n->stages_num);
649 sn_stage_comparator_add (s, &c);
650 sn_network_stage_add (n, s);
655 /* Merge odd sequences */
656 tmp_left_num = (indizes_left_num + 1) / 2;
657 for (i = 0; i < tmp_left_num; i++)
658 tmp_left[i] = indizes_left[2 * i];
660 tmp_right_num = (indizes_right_num + 1) / 2;
661 for (i = 0; i < tmp_right_num; i++)
662 tmp_right[i] = indizes_right[2 * i];
664 sn_network_add_odd_even_merger (n,
665 tmp_left, tmp_left_num,
666 tmp_right, tmp_right_num);
668 /* Merge even sequences */
669 tmp_left_num = indizes_left_num / 2;
670 for (i = 0; i < tmp_left_num; i++)
671 tmp_left[i] = indizes_left[(2 * i) + 1];
673 tmp_right_num = indizes_right_num / 2;
674 for (i = 0; i < tmp_right_num; i++)
675 tmp_right[i] = indizes_right[(2 * i) + 1];
677 sn_network_add_odd_even_merger (n,
678 tmp_left, tmp_left_num,
679 tmp_right, tmp_right_num);
681 /* Apply ``comparison-interchange'' operations. */
682 s = sn_stage_create (n->stages_num);
684 max_index = indizes_left_num + indizes_right_num;
685 if ((max_index % 2) == 0)
690 for (i = 1; i <= max_index; i += 2)
694 if (i < indizes_left_num)
695 c.min = indizes_left[i];
697 c.min = indizes_right[i - indizes_left_num];
699 if ((i + 1) < indizes_left_num)
700 c.max = indizes_left[i + 1];
702 c.max = indizes_right[i + 1 - indizes_left_num];
704 sn_stage_comparator_add (s, &c);
707 sn_network_stage_add (n, s);
710 } /* }}} int sn_network_add_odd_even_merger */
712 static sn_network_t *sn_network_combine_bitonic_shift (sn_network_t *n0, /* {{{ */
713 sn_network_t *n1, int do_shift)
716 sn_network_t *n1_clone;
719 n1_clone = sn_network_clone (n1);
720 if (n1_clone == NULL)
723 sn_network_invert (n1_clone);
725 n = sn_network_concatenate (n0, n1_clone);
729 sn_network_destroy (n1_clone);
732 shift = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
738 DPRINTF ("sn_network_combine_bitonic_shift: Shifting by %i.\n", shift);
739 sn_network_shift (n, shift);
742 sn_network_add_bitonic_merger (n);
745 } /* }}} sn_network_t *sn_network_combine_bitonic_shift */
747 sn_network_t *sn_network_combine_bitonic_merge (sn_network_t *n0, /* {{{ */
750 return (sn_network_combine_bitonic_shift (n0, n1, /* do_shift = */ 0));
751 } /* }}} sn_network_t *sn_network_combine_bitonic_merge */
753 sn_network_t *sn_network_combine_odd_even_merge (sn_network_t *n0, /* {{{ */
757 int indizes_left[n0->inputs_num];
758 int indizes_left_num;
759 int indizes_right[n1->inputs_num];
760 int indizes_right_num;
764 indizes_left_num = n0->inputs_num;
765 indizes_right_num = n1->inputs_num;
766 for (i = 0; i < indizes_left_num; i++)
768 for (i = 0; i < indizes_right_num; i++)
769 indizes_right[i] = indizes_left_num + i;
771 n = sn_network_concatenate (n0, n1);
775 status = sn_network_add_odd_even_merger (n,
776 indizes_left, indizes_left_num,
777 indizes_right, indizes_right_num);
780 sn_network_destroy (n);
784 sn_network_compress (n);
786 } /* }}} sn_network_t *sn_network_combine_odd_even_merge */
788 sn_network_t *sn_network_combine (sn_network_t *n0, /* {{{ */
791 return (sn_network_combine_odd_even_merge (n0, n1));
792 } /* }}} sn_network_t *sn_network_combine */
794 int sn_network_sort (sn_network_t *n, int *values) /* {{{ */
800 for (i = 0; i < n->stages_num; i++)
802 status = sn_stage_sort (n->stages[i], values);
808 } /* }}} int sn_network_sort */
810 int sn_network_brute_force_check (sn_network_t *n) /* {{{ */
812 int test_pattern[n->inputs_num];
813 int values[n->inputs_num];
817 memset (test_pattern, 0, sizeof (test_pattern));
823 /* Copy the current pattern and let the network sort it */
824 memcpy (values, test_pattern, sizeof (values));
825 status = sn_network_sort (n, values);
829 /* Check if the array is now sorted. */
830 previous = values[0];
831 for (i = 1; i < n->inputs_num; i++)
833 if (previous > values[i])
835 previous = values[i];
838 /* Generate the next test pattern */
840 for (i = 0; i < n->inputs_num; i++)
842 if (test_pattern[i] == 0)
855 /* Break out of the while loop if we tested all possible patterns */
860 /* All tests successfull */
862 } /* }}} int sn_network_brute_force_check */
864 sn_network_t *sn_network_read (FILE *fh) /* {{{ */
871 while (fgets (buffer, sizeof (buffer), fh) != NULL)
873 char *str_key = buffer;
874 char *str_value = NULL;
875 int buffer_len = strlen (buffer);
877 while ((buffer_len > 0) && ((buffer[buffer_len - 1] == '\n')
878 || (buffer[buffer_len - 1] == '\r')))
881 buffer[buffer_len] = '\0';
886 str_value = strchr (buffer, ':');
887 if (str_value == NULL)
889 printf ("Cannot parse line: %s\n", buffer);
893 *str_value = '\0'; str_value++;
894 while ((*str_value != '\0') && (isspace (*str_value) != 0))
897 if (strcasecmp ("Inputs", str_key) == 0)
898 opt_inputs = atoi (str_value);
900 printf ("Unknown key: %s\n", str_key);
901 } /* while (fgets) */
906 n = sn_network_create (opt_inputs);
912 s = sn_stage_read (fh);
916 sn_network_stage_add (n, s);
919 if (SN_NETWORK_STAGE_NUM (n) < 1)
921 sn_network_destroy (n);
926 } /* }}} sn_network_t *sn_network_read */
928 sn_network_t *sn_network_read_file (const char *file) /* {{{ */
933 fh = fopen (file, "r");
937 n = sn_network_read (fh);
942 } /* }}} sn_network_t *sn_network_read_file */
944 int sn_network_write (sn_network_t *n, FILE *fh) /* {{{ */
948 fprintf (fh, "Inputs: %i\n", n->inputs_num);
951 for (i = 0; i < n->stages_num; i++)
952 sn_stage_write (n->stages[i], fh);
955 } /* }}} int sn_network_write */
957 int sn_network_write_file (sn_network_t *n, const char *file) /* {{{ */
962 fh = fopen (file, "w");
966 status = sn_network_write (n, fh);
971 } /* }}} int sn_network_write_file */
973 int sn_network_serialize (sn_network_t *n, char **ret_buffer, /* {{{ */
974 size_t *ret_buffer_size)
981 buffer = *ret_buffer;
982 buffer_size = *ret_buffer_size;
984 #define SNPRINTF_OR_FAIL(...) \
985 status = snprintf (buffer, buffer_size, __VA_ARGS__); \
986 if ((status < 1) || (((size_t) status) >= buffer_size)) \
989 buffer_size -= status;
991 SNPRINTF_OR_FAIL ("Inputs: %i\r\n\r\n", n->inputs_num);
993 for (i = 0; i < n->stages_num; i++)
995 status = sn_stage_serialize (n->stages[i], &buffer, &buffer_size);
1000 *ret_buffer = buffer;
1001 *ret_buffer_size = buffer_size;
1003 } /* }}} int sn_network_serialize */
1005 sn_network_t *sn_network_unserialize (char *buffer, /* {{{ */
1011 if (buffer_size == 0)
1014 /* Read options first */
1015 while (buffer_size > 0)
1024 endptr = strchr (buffer, '\n');
1031 line_len = strlen (line);
1033 if ((line_len > 0) && (line[line_len - 1] == '\r'))
1035 line[line_len - 1] = 0;
1043 str_value = strchr (line, ':');
1044 if (str_value == NULL)
1046 printf ("Cannot parse line: %s\n", line);
1050 *str_value = '\0'; str_value++;
1051 while ((*str_value != '\0') && (isspace (*str_value) != 0))
1054 if (strcasecmp ("Inputs", str_key) == 0)
1055 opt_inputs = atoi (str_value);
1057 printf ("Unknown key: %s\n", str_key);
1058 } /* while (fgets) */
1063 n = sn_network_create (opt_inputs);
1069 s = sn_stage_unserialize (&buffer, &buffer_size);
1073 sn_network_stage_add (n, s);
1076 if (SN_NETWORK_STAGE_NUM (n) < 1)
1078 sn_network_destroy (n);
1083 } /* }}} sn_network_t *sn_network_unserialize */
1085 /* vim: set sw=2 sts=2 et fdm=marker : */