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_network_add (sn_network_t *n, sn_network_t *other) /* {{{ */
215 if ((n == NULL) || (other == NULL))
218 stages_num = n->stages_num + other->stages_num;
219 if (stages_num <= n->stages_num)
222 tmp = realloc (n->stages, sizeof (*n->stages) * stages_num);
227 memcpy (n->stages + n->stages_num, other->stages,
228 sizeof (*other->stages) * other->stages_num);
229 n->stages_num = stages_num;
231 free (other->stages);
235 } /* }}} int sn_network_network_add */
237 int sn_network_stage_add (sn_network_t *n, sn_stage_t *s) /* {{{ */
241 if ((n == NULL) || (s == NULL))
244 temp = (sn_stage_t **) realloc (n->stages, (n->stages_num + 1)
245 * sizeof (sn_stage_t *));
250 SN_STAGE_DEPTH (s) = n->stages_num;
251 n->stages[n->stages_num] = s;
255 } /* }}} int sn_network_stage_add */
257 int sn_network_stage_remove (sn_network_t *n, int s_num) /* {{{ */
259 int nmemb = n->stages_num - (s_num + 1);
262 if ((n == NULL) || (s_num >= n->stages_num))
265 sn_stage_destroy (n->stages[s_num]);
266 n->stages[s_num] = NULL;
270 memmove (n->stages + s_num, n->stages + (s_num + 1),
271 nmemb * sizeof (sn_stage_t *));
272 n->stages[n->stages_num - 1] = NULL;
276 /* Free the unused memory */
277 if (n->stages_num == 0)
284 temp = (sn_stage_t **) realloc (n->stages,
285 n->stages_num * sizeof (sn_stage_t *));
292 } /* }}} int sn_network_stage_remove */
294 sn_network_t *sn_network_clone (const sn_network_t *n) /* {{{ */
296 sn_network_t *n_copy;
299 n_copy = sn_network_create (n->inputs_num);
303 for (i = 0; i < n->stages_num; i++)
308 s = sn_stage_clone (n->stages[i]);
312 status = sn_network_stage_add (n_copy, s);
317 if (i < n->stages_num)
319 sn_network_destroy (n_copy);
324 } /* }}} sn_network_t *sn_network_clone */
326 int sn_network_comparator_add (sn_network_t *n, /* {{{ */
327 const sn_comparator_t *c)
331 if ((n == NULL) || (c == NULL))
334 if (n->stages_num > 0)
336 s = n->stages[n->stages_num - 1];
338 if (sn_stage_comparator_check_conflict (s, c) == 0)
340 sn_stage_comparator_add (s, c);
345 s = sn_stage_create (n->stages_num);
346 sn_stage_comparator_add (s, c);
347 sn_network_stage_add (n, s);
350 } /* }}} int sn_network_comparator_add */
352 int sn_network_get_comparator_num (const sn_network_t *n) /* {{{ */
361 for (i = 0; i < n->stages_num; i++)
362 num += n->stages[i]->comparators_num;
365 } /* }}} int sn_network_get_comparator_num */
367 int sn_network_show (sn_network_t *n) /* {{{ */
371 for (i = 0; i < n->stages_num; i++)
372 sn_stage_show (n->stages[i]);
375 } /* }}} int sn_network_show */
377 int sn_network_invert (sn_network_t *n) /* {{{ */
384 for (i = 0; i < n->stages_num; i++)
385 sn_stage_invert (n->stages[i]);
388 } /* }}} int sn_network_invert */
390 int sn_network_shift (sn_network_t *n, int sw) /* {{{ */
394 if ((n == NULL) || (sw < 0))
400 for (i = 0; i < n->stages_num; i++)
401 sn_stage_shift (n->stages[i], sw, SN_NETWORK_INPUT_NUM (n));
404 } /* }}} int sn_network_shift */
406 int sn_network_compress (sn_network_t *n) /* {{{ */
412 for (i = 1; i < n->stages_num; i++)
418 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
420 sn_comparator_t *c = SN_STAGE_COMP_GET (s, j);
423 for (k = i - 1; k >= 0; k--)
427 conflict = sn_stage_comparator_check_conflict (n->stages[k], c);
442 sn_stage_comparator_add (n->stages[move_to], c);
443 sn_stage_comparator_remove (s, j);
449 while ((n->stages_num > 0)
450 && (SN_STAGE_COMP_NUM (n->stages[n->stages_num - 1]) == 0))
451 sn_network_stage_remove (n, n->stages_num - 1);
454 } /* }}} int sn_network_compress */
456 int sn_network_normalize (sn_network_t *n) /* {{{ */
460 for (i = 0; i < n->stages_num; i++)
467 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
473 c = SN_STAGE_COMP_GET (s, j);
482 for (k = i; k < n->stages_num; k++)
483 sn_stage_swap (n->stages[k], min, max);
488 } /* for (j = 0 .. #comparators) */
489 } /* for (i = n->stages_num - 1 .. 0) */
492 } /* }}} int sn_network_normalize */
494 int sn_network_remove_input (sn_network_t *n, int input) /* {{{ */
498 if ((n == NULL) || (input < 0) || (input >= n->inputs_num))
501 for (i = 0; i < n->stages_num; i++)
502 sn_stage_remove_input (n->stages[i], input);
507 } /* }}} int sn_network_remove_input */
509 int sn_network_cut_at (sn_network_t *n, int input, /* {{{ */
510 enum sn_network_cut_dir_e dir)
513 int position = input;
515 for (i = 0; i < n->stages_num; i++)
521 new_position = sn_stage_cut_at (s, position, dir);
523 if (position != new_position)
527 for (j = 0; j < i; j++)
528 sn_stage_swap (n->stages[j], position, new_position);
531 position = new_position;
534 assert (((dir == DIR_MIN) && (position == 0))
535 || ((dir == DIR_MAX) && (position == (n->inputs_num - 1))));
537 sn_network_remove_input (n, position);
540 } /* }}} int sn_network_cut_at */
542 int sn_network_cut (sn_network_t *n, int *mask) /* {{{ */
547 for (i = 0; i < n->stages_num; i++)
549 sn_stage_t *s = n->stages[i];
551 sn_stage_cut (s, mask, n->stages);
554 /* Use a copy of this member since it will be updated by
555 * sn_network_remove_input(). */
556 inputs_num = n->inputs_num;
557 for (i = 0; i < inputs_num; i++)
560 sn_network_remove_input (n, 0);
561 else if (mask[i] > 0)
562 sn_network_remove_input (n, n->inputs_num - 1);
566 } /* }}} int sn_network_cut */
568 /* sn_network_concatenate
570 * `Glues' two networks together, resulting in a comparator network with twice
571 * as many inputs but one that doesn't really sort anymore. It produces a
572 * bitonic sequence, though, that can be used by the mergers below. */
573 static sn_network_t *sn_network_concatenate (sn_network_t *n0, /* {{{ */
581 stages_num = (n0->stages_num > n1->stages_num)
585 n = sn_network_create (n0->inputs_num + n1->inputs_num);
589 for (i = 0; i < stages_num; i++)
591 sn_stage_t *s = sn_stage_create (i);
593 if (i < n0->stages_num)
594 for (j = 0; j < SN_STAGE_COMP_NUM (n0->stages[i]); j++)
596 sn_comparator_t *c = SN_STAGE_COMP_GET (n0->stages[i], j);
597 sn_stage_comparator_add (s, c);
600 if (i < n1->stages_num)
601 for (j = 0; j < SN_STAGE_COMP_NUM (n1->stages[i]); j++)
603 sn_comparator_t *c_orig = SN_STAGE_COMP_GET (n1->stages[i], j);
604 sn_comparator_t c_copy;
606 SN_COMP_MIN(&c_copy) = SN_COMP_MIN(c_orig) + n0->inputs_num;
607 SN_COMP_MAX(&c_copy) = SN_COMP_MAX(c_orig) + n0->inputs_num;
609 sn_stage_comparator_add (s, &c_copy);
612 sn_network_stage_add (n, s);
616 } /* }}} sn_network_t *sn_network_concatenate */
618 static int sn_network_add_bitonic_merger_recursive (sn_network_t *n, /* {{{ */
628 s = sn_stage_create (n->stages_num);
634 for (i = low; i < (low + m); i++)
641 sn_stage_comparator_add (s, &c);
644 sn_network_stage_add (n, s);
646 sn_network_add_bitonic_merger_recursive (n, low, m);
647 sn_network_add_bitonic_merger_recursive (n, low + m, m);
650 } /* }}} int sn_network_add_bitonic_merger_recursive */
652 static int sn_network_add_bitonic_merger (sn_network_t *n) /* {{{ */
659 s = sn_stage_create (n->stages_num);
663 m = n->inputs_num / 2;
665 for (i = 0; i < m; i++)
670 c.max = n->inputs_num - (i + 1);
672 sn_stage_comparator_add (s, &c);
675 sn_network_stage_add (n, s);
677 sn_network_add_bitonic_merger_recursive (n, 0, m);
678 sn_network_add_bitonic_merger_recursive (n, m, m);
680 sn_network_add_bitonic_merger_recursive (n, 0, SN_NETWORK_INPUT_NUM (n));
684 } /* }}} int sn_network_add_bitonic_merger */
686 static int sn_network_add_odd_even_merger (sn_network_t *n, /* {{{ */
687 int *indizes_left, int indizes_left_num,
688 int *indizes_right, int indizes_right_num)
690 int tmp_left[indizes_left_num];
692 int tmp_right[indizes_left_num];
698 if ((indizes_left_num == 0) || (indizes_right_num == 0))
702 else if ((indizes_left_num == 1) && (indizes_right_num == 1))
707 c.min = *indizes_left;
708 c.max = *indizes_right;
710 s = sn_stage_create (n->stages_num);
714 sn_stage_comparator_add (s, &c);
715 sn_network_stage_add (n, s);
720 /* Merge odd sequences */
721 tmp_left_num = (indizes_left_num + 1) / 2;
722 for (i = 0; i < tmp_left_num; i++)
723 tmp_left[i] = indizes_left[2 * i];
725 tmp_right_num = (indizes_right_num + 1) / 2;
726 for (i = 0; i < tmp_right_num; i++)
727 tmp_right[i] = indizes_right[2 * i];
729 sn_network_add_odd_even_merger (n,
730 tmp_left, tmp_left_num,
731 tmp_right, tmp_right_num);
733 /* Merge even sequences */
734 tmp_left_num = indizes_left_num / 2;
735 for (i = 0; i < tmp_left_num; i++)
736 tmp_left[i] = indizes_left[(2 * i) + 1];
738 tmp_right_num = indizes_right_num / 2;
739 for (i = 0; i < tmp_right_num; i++)
740 tmp_right[i] = indizes_right[(2 * i) + 1];
742 sn_network_add_odd_even_merger (n,
743 tmp_left, tmp_left_num,
744 tmp_right, tmp_right_num);
746 /* Apply ``comparison-interchange'' operations. */
747 s = sn_stage_create (n->stages_num);
749 max_index = indizes_left_num + indizes_right_num;
750 if ((max_index % 2) == 0)
755 for (i = 1; i <= max_index; i += 2)
759 if (i < indizes_left_num)
760 c.min = indizes_left[i];
762 c.min = indizes_right[i - indizes_left_num];
764 if ((i + 1) < indizes_left_num)
765 c.max = indizes_left[i + 1];
767 c.max = indizes_right[i + 1 - indizes_left_num];
769 sn_stage_comparator_add (s, &c);
772 sn_network_stage_add (n, s);
775 } /* }}} int sn_network_add_odd_even_merger */
777 static sn_network_t *sn_network_combine_bitonic_shift (sn_network_t *n0, /* {{{ */
778 sn_network_t *n1, int do_shift)
781 sn_network_t *n1_clone;
784 n1_clone = sn_network_clone (n1);
785 if (n1_clone == NULL)
788 sn_network_invert (n1_clone);
790 n = sn_network_concatenate (n0, n1_clone);
794 sn_network_destroy (n1_clone);
797 shift = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
803 DPRINTF ("sn_network_combine_bitonic_shift: Shifting by %i.\n", shift);
804 sn_network_shift (n, shift);
807 sn_network_add_bitonic_merger (n);
810 } /* }}} sn_network_t *sn_network_combine_bitonic_shift */
812 sn_network_t *sn_network_combine_bitonic_merge (sn_network_t *n0, /* {{{ */
815 return (sn_network_combine_bitonic_shift (n0, n1, /* do_shift = */ 0));
816 } /* }}} sn_network_t *sn_network_combine_bitonic_merge */
818 sn_network_t *sn_network_combine_odd_even_merge (sn_network_t *n0, /* {{{ */
822 int indizes_left[n0->inputs_num];
823 int indizes_left_num;
824 int indizes_right[n1->inputs_num];
825 int indizes_right_num;
829 indizes_left_num = n0->inputs_num;
830 indizes_right_num = n1->inputs_num;
831 for (i = 0; i < indizes_left_num; i++)
833 for (i = 0; i < indizes_right_num; i++)
834 indizes_right[i] = indizes_left_num + i;
836 n = sn_network_concatenate (n0, n1);
840 status = sn_network_add_odd_even_merger (n,
841 indizes_left, indizes_left_num,
842 indizes_right, indizes_right_num);
845 sn_network_destroy (n);
849 sn_network_compress (n);
851 } /* }}} sn_network_t *sn_network_combine_odd_even_merge */
853 sn_network_t *sn_network_combine (sn_network_t *n0, /* {{{ */
856 return (sn_network_combine_odd_even_merge (n0, n1));
857 } /* }}} sn_network_t *sn_network_combine */
859 int sn_network_sort (sn_network_t *n, int *values) /* {{{ */
865 for (i = 0; i < n->stages_num; i++)
867 status = sn_stage_sort (n->stages[i], values);
873 } /* }}} int sn_network_sort */
875 int sn_network_brute_force_check (sn_network_t *n) /* {{{ */
877 int test_pattern[n->inputs_num];
878 int values[n->inputs_num];
882 memset (test_pattern, 0, sizeof (test_pattern));
888 /* Copy the current pattern and let the network sort it */
889 memcpy (values, test_pattern, sizeof (values));
890 status = sn_network_sort (n, values);
894 /* Check if the array is now sorted. */
895 previous = values[0];
896 for (i = 1; i < n->inputs_num; i++)
898 if (previous > values[i])
900 previous = values[i];
903 /* Generate the next test pattern */
905 for (i = 0; i < n->inputs_num; i++)
907 if (test_pattern[i] == 0)
920 /* Break out of the while loop if we tested all possible patterns */
925 /* All tests successfull */
927 } /* }}} int sn_network_brute_force_check */
929 sn_network_t *sn_network_read (FILE *fh) /* {{{ */
936 while (fgets (buffer, sizeof (buffer), fh) != NULL)
938 char *str_key = buffer;
939 char *str_value = NULL;
940 int buffer_len = strlen (buffer);
942 while ((buffer_len > 0) && ((buffer[buffer_len - 1] == '\n')
943 || (buffer[buffer_len - 1] == '\r')))
946 buffer[buffer_len] = '\0';
951 str_value = strchr (buffer, ':');
952 if (str_value == NULL)
954 printf ("Cannot parse line: %s\n", buffer);
958 *str_value = '\0'; str_value++;
959 while ((*str_value != '\0') && (isspace (*str_value) != 0))
962 if (strcasecmp ("Inputs", str_key) == 0)
963 opt_inputs = atoi (str_value);
965 printf ("Unknown key: %s\n", str_key);
966 } /* while (fgets) */
971 n = sn_network_create (opt_inputs);
977 s = sn_stage_read (fh);
981 sn_network_stage_add (n, s);
984 if (SN_NETWORK_STAGE_NUM (n) < 1)
986 sn_network_destroy (n);
991 } /* }}} sn_network_t *sn_network_read */
993 sn_network_t *sn_network_read_file (const char *file) /* {{{ */
998 fh = fopen (file, "r");
1002 n = sn_network_read (fh);
1007 } /* }}} sn_network_t *sn_network_read_file */
1009 int sn_network_write (sn_network_t *n, FILE *fh) /* {{{ */
1013 fprintf (fh, "Inputs: %i\n", n->inputs_num);
1016 for (i = 0; i < n->stages_num; i++)
1017 sn_stage_write (n->stages[i], fh);
1020 } /* }}} int sn_network_write */
1022 int sn_network_write_file (sn_network_t *n, const char *file) /* {{{ */
1027 fh = fopen (file, "w");
1031 status = sn_network_write (n, fh);
1036 } /* }}} int sn_network_write_file */
1038 int sn_network_serialize (sn_network_t *n, char **ret_buffer, /* {{{ */
1039 size_t *ret_buffer_size)
1046 buffer = *ret_buffer;
1047 buffer_size = *ret_buffer_size;
1049 #define SNPRINTF_OR_FAIL(...) \
1050 status = snprintf (buffer, buffer_size, __VA_ARGS__); \
1051 if ((status < 1) || (((size_t) status) >= buffer_size)) \
1054 buffer_size -= status;
1056 SNPRINTF_OR_FAIL ("Inputs: %i\r\n\r\n", n->inputs_num);
1058 for (i = 0; i < n->stages_num; i++)
1060 status = sn_stage_serialize (n->stages[i], &buffer, &buffer_size);
1065 *ret_buffer = buffer;
1066 *ret_buffer_size = buffer_size;
1068 } /* }}} int sn_network_serialize */
1070 sn_network_t *sn_network_unserialize (char *buffer, /* {{{ */
1076 if (buffer_size == 0)
1079 /* Read options first */
1080 while (buffer_size > 0)
1089 endptr = strchr (buffer, '\n');
1096 line_len = strlen (line);
1098 if ((line_len > 0) && (line[line_len - 1] == '\r'))
1100 line[line_len - 1] = 0;
1108 str_value = strchr (line, ':');
1109 if (str_value == NULL)
1111 printf ("Cannot parse line: %s\n", line);
1115 *str_value = '\0'; str_value++;
1116 while ((*str_value != '\0') && (isspace (*str_value) != 0))
1119 if (strcasecmp ("Inputs", str_key) == 0)
1120 opt_inputs = atoi (str_value);
1122 printf ("Unknown key: %s\n", str_key);
1123 } /* while (fgets) */
1128 n = sn_network_create (opt_inputs);
1134 s = sn_stage_unserialize (&buffer, &buffer_size);
1138 sn_network_stage_add (n, s);
1141 if (SN_NETWORK_STAGE_NUM (n) < 1)
1143 sn_network_destroy (n);
1148 } /* }}} sn_network_t *sn_network_unserialize */
1150 /* vim: set sw=2 sts=2 et fdm=marker : */