* libsortnetwork - src/sn_network.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
- * Free Software Foundation; only version 2 of the License is applicable.
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
*
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
*
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Florian octo Forster <ff at octo.it>
{
sn_network_t *n;
- n = sn_network_create (inputs_num);
-
assert (inputs_num > 0);
if (inputs_num == 1)
{
- return (n);
+ return (sn_network_create (inputs_num));
}
if (inputs_num == 2)
{
- sn_stage_t *s;
sn_comparator_t c;
+ n = sn_network_create (inputs_num);
+
+ memset (&c, 0, sizeof (c));
c.min = 0;
c.max = 1;
- s = sn_stage_create (/* depth = */ 0);
- sn_stage_comparator_add (s, &c);
- sn_network_stage_add (n, s);
+ sn_network_comparator_add (n, &c);
return (n);
}
}
} /* }}} sn_network_t *sn_network_create_odd_even_mergesort */
+sn_network_t *sn_network_create_bitonic_mergesort (int inputs_num) /* {{{ */
+{
+ sn_network_t *n;
+
+ assert (inputs_num > 0);
+ if (inputs_num == 1)
+ {
+ return (sn_network_create (inputs_num));
+ }
+ if (inputs_num == 2)
+ {
+ sn_comparator_t c;
+
+ n = sn_network_create (inputs_num);
+
+ memset (&c, 0, sizeof (c));
+ c.min = 0;
+ c.max = 1;
+
+ sn_network_comparator_add (n, &c);
+
+ return (n);
+ }
+ else
+ {
+ sn_network_t *n_left;
+ sn_network_t *n_right;
+ int inputs_left;
+ int inputs_right;
+
+ inputs_left = inputs_num / 2;
+ inputs_right = inputs_num - inputs_left;
+
+ n_left = sn_network_create_bitonic_mergesort (inputs_left);
+ if (n_left == NULL)
+ return (NULL);
+
+ if (inputs_left != inputs_right)
+ n_right = sn_network_create_bitonic_mergesort (inputs_right);
+ else
+ n_right = n_left;
+ if (n_right == NULL)
+ {
+ sn_network_destroy (n_left);
+ return (NULL);
+ }
+
+ n = sn_network_combine_bitonic_merge (n_left, n_right);
+
+ if (n_left != n_right)
+ sn_network_destroy (n_right);
+ sn_network_destroy (n_left);
+
+ if (n != NULL)
+ sn_network_compress (n);
+
+ return (n);
+ }
+} /* }}} sn_network_t *sn_network_create_bitonic_mergesort */
+
+static int sn_network_create_pairwise_internal (sn_network_t *n, /* {{{ */
+ int *inputs, int inputs_num)
+{
+ int i;
+ int inputs_copy[inputs_num];
+ int m;
+
+ for (i = 1; i < inputs_num; i += 2)
+ {
+ sn_comparator_t *c = sn_comparator_create (inputs[i-1], inputs[i]);
+ sn_network_comparator_add (n, c);
+ sn_comparator_destroy (c);
+ }
+
+ if (inputs_num <= 2)
+ return (0);
+
+ /* Sort "pairs" recursively. Like with odd-even mergesort, odd and even lines
+ * are handled recursively and later reunited. */
+ for (i = 0; i < inputs_num; i += 2)
+ inputs_copy[(int) (i / 2)] = inputs[i];
+ /* Recursive call #1 with first set of lines */
+ sn_network_create_pairwise_internal (n, inputs_copy,
+ (int) ((inputs_num + 1) / 2));
+
+ for (i = 1; i < inputs_num; i += 2)
+ inputs_copy[(int) (i / 2)] = inputs[i];
+ /* Recursive call #2 with second set of lines */
+ sn_network_create_pairwise_internal (n, inputs_copy,
+ (int) (inputs_num/ 2));
+
+ /* m is the "amplitude" of the sorted pairs. This is a bit tricky to read due
+ * to different indices being used in the paper, unfortunately. */
+ m = inputs_num / 2;
+ while (m > 1)
+ {
+ for (i = 1; (i + (m - 1)) < inputs_num; i += 2)
+ {
+ int left = i;
+ int right = i + (m - 1);
+ sn_comparator_t *c;
+
+ assert (left < right);
+ c = sn_comparator_create (inputs[left], inputs[right]);
+ sn_network_comparator_add (n, c);
+ sn_comparator_destroy (c);
+ }
+
+ m = m / 2;
+ } /* while (m > 1) */
+
+ return (0);
+} /* }}} int sn_network_create_pairwise_internal */
+
+sn_network_t *sn_network_create_pairwise (int inputs_num) /* {{{ */
+{
+ sn_network_t *n = sn_network_create (inputs_num);
+ int inputs[inputs_num];
+ int i;
+
+ if (n == NULL)
+ return (NULL);
+
+ for (i = 0; i < inputs_num; i++)
+ inputs[i] = i;
+
+ sn_network_create_pairwise_internal (n, inputs, inputs_num);
+ sn_network_compress (n);
+
+ return (n);
+} /* }}} sn_network_t *sn_network_create_pairwise */
+
+int sn_network_network_add (sn_network_t *n, sn_network_t *other) /* {{{ */
+{
+ int stages_num;
+ sn_stage_t **tmp;
+
+ if ((n == NULL) || (other == NULL))
+ return (EINVAL);
+
+ stages_num = n->stages_num + other->stages_num;
+ if (stages_num <= n->stages_num)
+ return (EINVAL);
+
+ tmp = realloc (n->stages, sizeof (*n->stages) * stages_num);
+ if (tmp == NULL)
+ return (ENOMEM);
+ n->stages = tmp;
+
+ memcpy (n->stages + n->stages_num, other->stages,
+ sizeof (*other->stages) * other->stages_num);
+ n->stages_num = stages_num;
+
+ free (other->stages);
+ free (other);
+
+ return (0);
+} /* }}} int sn_network_network_add */
+
int sn_network_stage_add (sn_network_t *n, sn_stage_t *s) /* {{{ */
{
sn_stage_t **temp;
return (0);
} /* }}} int sn_network_normalize */
+int sn_network_remove_input (sn_network_t *n, int input) /* {{{ */
+{
+ int i;
+
+ if ((n == NULL) || (input < 0) || (input >= n->inputs_num))
+ return (EINVAL);
+
+ for (i = 0; i < n->stages_num; i++)
+ sn_stage_remove_input (n->stages[i], input);
+
+ n->inputs_num--;
+
+ return (0);
+} /* }}} int sn_network_remove_input */
+
int sn_network_cut_at (sn_network_t *n, int input, /* {{{ */
enum sn_network_cut_dir_e dir)
{
assert (((dir == DIR_MIN) && (position == 0))
|| ((dir == DIR_MAX) && (position == (n->inputs_num - 1))));
+ sn_network_remove_input (n, position);
+
+ return (0);
+} /* }}} int sn_network_cut_at */
+
+int sn_network_cut (sn_network_t *n, int *mask) /* {{{ */
+{
+ int inputs_num;
+ int i;
+
for (i = 0; i < n->stages_num; i++)
- sn_stage_remove_input (n->stages[i], position);
+ {
+ sn_stage_t *s = n->stages[i];
- n->inputs_num--;
+ sn_stage_cut (s, mask, n->stages);
+ }
+
+ /* Use a copy of this member since it will be updated by
+ * sn_network_remove_input(). */
+ inputs_num = n->inputs_num;
+ for (i = 0; i < inputs_num; i++)
+ {
+ if (mask[i] < 0)
+ sn_network_remove_input (n, 0);
+ else if (mask[i] > 0)
+ sn_network_remove_input (n, n->inputs_num - 1);
+ }
return (0);
-} /* }}} int sn_network_cut_at */
+} /* }}} int sn_network_cut */
/* sn_network_concatenate
*
return (n);
} /* }}} sn_network_t *sn_network_concatenate */
-static int sn_network_add_bitonic_merger_recursive (sn_network_t *n, /* {{{ */
- int low, int num)
+static int sn_network_add_bitonic_merger (sn_network_t *n, /* {{{ */
+ int *indizes, int indizes_num)
{
- sn_stage_t *s;
- int m;
int i;
- if (num == 1)
+ if (indizes_num <= 1)
return (0);
- s = sn_stage_create (n->stages_num);
- if (s == NULL)
- return (-1);
-
- m = num / 2;
-
- for (i = low; i < (low + m); i++)
+ if (indizes_num > 2)
{
- sn_comparator_t c;
-
- c.min = i;
- c.max = i + m;
-
- sn_stage_comparator_add (s, &c);
- }
-
- sn_network_stage_add (n, s);
+ int even_indizes[indizes_num];
+ int even_indizes_num;
+ int odd_indizes[indizes_num];
+ int odd_indizes_num;
- sn_network_add_bitonic_merger_recursive (n, low, m);
- sn_network_add_bitonic_merger_recursive (n, low + m, m);
+ even_indizes_num = (indizes_num + 1) / 2;
+ odd_indizes_num = indizes_num / 2;
- return (0);
-} /* }}} int sn_network_add_bitonic_merger_recursive */
-
-static int sn_network_add_bitonic_merger (sn_network_t *n) /* {{{ */
-{
-#if 0
- sn_stage_t *s;
- int m;
- int i;
-
- s = sn_stage_create (n->stages_num);
- if (s == NULL)
- return (-1);
+ for (i = 0; i < even_indizes_num; i++)
+ even_indizes[i] = indizes[2 * i];
+ for (i = 0; i < odd_indizes_num; i++)
+ odd_indizes[i] = indizes[(2 * i) + 1];
- m = n->inputs_num / 2;
+ sn_network_add_bitonic_merger (n, even_indizes, even_indizes_num);
+ sn_network_add_bitonic_merger (n, odd_indizes, odd_indizes_num);
+ }
- for (i = 0; i < m; i++)
+ for (i = 1; i < indizes_num; i += 2)
{
sn_comparator_t c;
- c.min = i;
- c.max = n->inputs_num - (i + 1);
+ memset (&c, 0, sizeof (c));
+ c.min = indizes[i - 1];
+ c.max = indizes[i];
- sn_stage_comparator_add (s, &c);
+ sn_network_comparator_add (n, &c);
}
- sn_network_stage_add (n, s);
-
- sn_network_add_bitonic_merger_recursive (n, 0, m);
- sn_network_add_bitonic_merger_recursive (n, m, m);
-#else
- sn_network_add_bitonic_merger_recursive (n, 0, SN_NETWORK_INPUT_NUM (n));
-#endif
-
return (0);
} /* }}} int sn_network_add_bitonic_merger */
return (0);
} /* }}} int sn_network_add_odd_even_merger */
-static sn_network_t *sn_network_combine_bitonic_shift (sn_network_t *n0, /* {{{ */
- sn_network_t *n1, int do_shift)
+sn_network_t *sn_network_combine_bitonic_merge (sn_network_t *n0, /* {{{ */
+ sn_network_t *n1)
{
+ sn_network_t *n0_clone;
sn_network_t *n;
- sn_network_t *n1_clone;
- int shift;
+ int indizes_num = SN_NETWORK_INPUT_NUM (n0) + SN_NETWORK_INPUT_NUM (n1);
+ int indizes[indizes_num];
+ int i;
- n1_clone = sn_network_clone (n1);
- if (n1_clone == NULL)
+ /* We need to invert n0, because the sequence must be
+ * z_1 >= z_2 >= ... >= z_k <= z_{k+1} <= ... <= z_p
+ * and NOT the other way around! Otherwise the comparators added in
+ * sn_network_add_bitonic_merger() from comparing (z_0,z_1), (z_2,z_3), ...
+ * to comparing ..., (z_{n-4},z_{n-3}), (z_{n-2},z_{n-1}), i.e. bound to the
+ * end of the list, possibly leaving z_0 uncompared. */
+ n0_clone = sn_network_clone (n0);
+ if (n0_clone == NULL)
return (NULL);
+ sn_network_invert (n0_clone);
- sn_network_invert (n1_clone);
-
- n = sn_network_concatenate (n0, n1_clone);
+ n = sn_network_concatenate (n0_clone, n1);
if (n == NULL)
return (NULL);
+ sn_network_destroy (n0_clone);
- sn_network_destroy (n1_clone);
-
- if (do_shift)
- shift = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
- else
- shift = 0;
-
- if (shift > 0)
- {
- DPRINTF ("sn_network_combine_bitonic_shift: Shifting by %i.\n", shift);
- sn_network_shift (n, shift);
- }
+ for (i = 0; i < indizes_num; i++)
+ indizes[i] = i;
- sn_network_add_bitonic_merger (n);
+ sn_network_add_bitonic_merger (n, indizes, indizes_num);
return (n);
-} /* }}} sn_network_t *sn_network_combine_bitonic_shift */
-
-sn_network_t *sn_network_combine_bitonic_merge (sn_network_t *n0, /* {{{ */
- sn_network_t *n1)
-{
- return (sn_network_combine_bitonic_shift (n0, n1, /* do_shift = */ 0));
} /* }}} sn_network_t *sn_network_combine_bitonic_merge */
sn_network_t *sn_network_combine_odd_even_merge (sn_network_t *n0, /* {{{ */