2 * libsortnetwork - src/sn-batcher.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
32 #include "sn_network.h"
34 static sn_network_t *create_batcher_sort (size_t inputs_num)
37 sn_network_t *n_small;
44 n = sn_network_create (2);
47 fprintf (stderr, "create_batcher_sort: sn_network_create failed.\n");
51 s = sn_stage_create (0);
54 sn_network_destroy (n);
55 fprintf (stderr, "create_batcher_sort: sn_stage_create failed.\n");
62 sn_stage_comparator_add (s, &c);
63 sn_network_stage_add (n, s);
67 else if ((inputs_num < 2) || ((inputs_num % 2) != 0))
69 fprintf (stderr, "create_batcher_sort: Inputs must be a power of two, "
74 n_small = create_batcher_sort (inputs_num / 2);
78 n = sn_network_combine_bitonic_merge (n_small, n_small);
81 sn_network_destroy (n_small);
82 fprintf (stderr, "create_batcher_sort: sn_network_combine_bitonic_merge "
87 sn_network_destroy (n_small);
88 sn_network_compress (n);
91 } /* sn_network_t *create_batcher_sort */
93 int main (int argc, char **argv)
100 printf ("Usage: %s <num inputs>\n", argv[0]);
104 inputs_num = (size_t) atoi (argv[1]);
107 fprintf (stderr, "Invalid number of inputs: %zu\n", inputs_num);
111 n = create_batcher_sort (inputs_num);
114 printf ("n == NULL!\n");
118 sn_network_write (n, stdout);
123 /* vim: set shiftwidth=2 softtabstop=2 : */