2 * collectd - src/sn_network.c
3 * Copyright (C) 2008 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 <octo at verplant.org>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
36 #include "sn_network.h"
37 #include "sn_random.h"
39 sn_network_t *sn_network_create (int inputs_num)
43 n = (sn_network_t *) malloc (sizeof (sn_network_t));
46 memset (n, '\0', sizeof (sn_network_t));
48 n->inputs_num = inputs_num;
51 } /* sn_network_t *sn_network_create */
53 void sn_network_destroy (sn_network_t *n)
58 if (n->stages != NULL)
61 for (i = 0; i < n->stages_num; i++)
63 sn_stage_destroy (n->stages[i]);
71 } /* void sn_network_destroy */
73 int sn_network_stage_add (sn_network_t *n, sn_stage_t *s)
77 temp = (sn_stage_t **) realloc (n->stages, (n->stages_num + 1)
78 * sizeof (sn_stage_t *));
83 SN_STAGE_DEPTH (s) = n->stages_num;
84 n->stages[n->stages_num] = s;
88 } /* int sn_network_stage_add */
90 int sn_network_stage_remove (sn_network_t *n, int s_num)
92 int nmemb = n->stages_num - (s_num + 1);
95 assert (s_num < n->stages_num);
97 sn_stage_destroy (n->stages[s_num]);
98 n->stages[s_num] = NULL;
102 memmove (n->stages + s_num, n->stages + (s_num + 1),
103 nmemb * sizeof (sn_stage_t *));
104 n->stages[n->stages_num - 1] = NULL;
108 /* Free the unused memory */
109 if (n->stages_num == 0)
116 temp = (sn_stage_t **) realloc (n->stages,
117 n->stages_num * sizeof (sn_stage_t *));
124 } /* int sn_network_stage_remove */
126 sn_network_t *sn_network_clone (const sn_network_t *n)
128 sn_network_t *n_copy;
131 n_copy = sn_network_create (n->inputs_num);
135 for (i = 0; i < n->stages_num; i++)
140 s = sn_stage_clone (n->stages[i]);
144 status = sn_network_stage_add (n_copy, s);
149 if (i < n->stages_num)
151 sn_network_destroy (n_copy);
156 } /* sn_network_t *sn_network_clone */
158 int sn_network_show (sn_network_t *n)
162 for (i = 0; i < n->stages_num; i++)
163 sn_stage_show (n->stages[i]);
166 } /* int sn_network_show */
168 int sn_network_invert (sn_network_t *n)
172 for (i = 0; i < n->stages_num; i++)
173 sn_stage_invert (n->stages[i]);
176 } /* int sn_network_invert */
178 int sn_network_compress (sn_network_t *n)
184 for (i = 1; i < n->stages_num; i++)
190 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
192 sn_comparator_t *c = SN_STAGE_COMP_GET (s, j);
195 for (k = i - 1; k >= 0; k--)
199 conflict = sn_stage_comparator_check_conflict (n->stages[k], c);
214 sn_stage_comparator_add (n->stages[move_to], c);
215 sn_stage_comparator_remove (s, j);
221 while ((n->stages_num > 0)
222 && (SN_STAGE_COMP_NUM (n->stages[n->stages_num - 1]) == 0))
223 sn_network_stage_remove (n, n->stages_num - 1);
226 } /* int sn_network_compress */
228 int sn_network_normalize (sn_network_t *n)
232 for (i = n->stages_num - 1; i >= 0; i--)
239 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
245 c = SN_STAGE_COMP_GET (s, j);
254 for (k = i; k >= 0; k--)
255 sn_stage_swap (n->stages[k], min, max);
257 } /* for (j = 0 .. #comparators) */
258 } /* for (i = n->stages_num - 1 .. 0) */
261 } /* int sn_network_normalize */
263 int sn_network_cut_at (sn_network_t *n, int input, enum sn_network_cut_dir_e dir)
266 int position = input;
268 for (i = 0; i < n->stages_num; i++)
274 new_position = sn_stage_cut_at (s, position, dir);
276 if (position != new_position)
280 for (j = 0; j < i; j++)
281 sn_stage_swap (n->stages[j], position, new_position);
284 position = new_position;
287 assert (((dir == DIR_MIN) && (position == 0))
288 || ((dir == DIR_MAX) && (position == (n->inputs_num - 1))));
290 for (i = 0; i < n->stages_num; i++)
291 sn_stage_remove_input (n->stages[i], position);
296 } /* int sn_network_cut_at */
298 static int sn_network_add_bitonic_merger_recursive (sn_network_t *n,
308 s = sn_stage_create (n->stages_num);
314 for (i = low; i < (low + m); i++)
321 sn_stage_comparator_add (s, &c);
324 sn_network_stage_add (n, s);
326 sn_network_add_bitonic_merger_recursive (n, low, m);
327 sn_network_add_bitonic_merger_recursive (n, low + m, m);
330 } /* int sn_network_add_bitonic_merger_recursive */
332 static int sn_network_add_bitonic_merger (sn_network_t *n)
338 s = sn_stage_create (n->stages_num);
342 m = n->inputs_num / 2;
344 for (i = 0; i < m; i++)
349 c.max = n->inputs_num - (i + 1);
351 sn_stage_comparator_add (s, &c);
354 sn_network_stage_add (n, s);
356 sn_network_add_bitonic_merger_recursive (n, 0, m);
357 sn_network_add_bitonic_merger_recursive (n, m, m);
360 } /* int sn_network_add_bitonic_merger */
362 static int sn_network_add_odd_even_merger_recursive (sn_network_t *n,
363 int *indizes, int indizes_num)
369 int indizes_half_num;
374 indizes_half_num = indizes_num / 2;
375 indizes_half = (int *) malloc (indizes_num * sizeof (int));
376 if (indizes_half == NULL)
379 for (i = 0; i < indizes_half_num; i++)
381 indizes_half[i] = indizes[2 * i];
382 indizes_half[indizes_half_num + i] = indizes[(2 * i) + 1];
385 status = sn_network_add_odd_even_merger_recursive (n,
386 indizes_half, indizes_half_num);
393 status = sn_network_add_odd_even_merger_recursive (n,
394 indizes_half + indizes_half_num, indizes_half_num);
403 s = sn_stage_create (n->stages_num);
407 for (i = 1; i < (indizes_num - 2); i += 2)
410 c.max = indizes[i + 1];
412 sn_stage_comparator_add (s, &c);
415 sn_network_stage_add (n, s);
422 assert (indizes_num == 2);
427 s = sn_stage_create (n->stages_num);
431 sn_stage_comparator_add (s, &c);
432 sn_network_stage_add (n, s);
436 } /* int sn_network_add_odd_even_merger_recursive */
438 static int sn_network_add_odd_even_merger (sn_network_t *n)
445 indizes_num = n->inputs_num;
446 indizes = (int *) malloc (indizes_num * sizeof (int));
450 for (i = 0; i < indizes_num; i++)
453 status = sn_network_add_odd_even_merger_recursive (n,
454 indizes, indizes_num);
458 } /* int sn_network_add_bitonic_merger */
460 sn_network_t *sn_network_combine (sn_network_t *n0, sn_network_t *n1)
467 stages_num = (n0->stages_num > n1->stages_num)
471 n = sn_network_create (n0->inputs_num + n1->inputs_num);
475 for (i = 0; i < stages_num; i++)
477 sn_stage_t *s = sn_stage_create (i);
479 if (i < n0->stages_num)
480 for (j = 0; j < SN_STAGE_COMP_NUM (n0->stages[i]); j++)
482 sn_comparator_t *c = SN_STAGE_COMP_GET (n0->stages[i], j);
483 sn_stage_comparator_add (s, c);
486 if (i < n1->stages_num)
487 for (j = 0; j < SN_STAGE_COMP_NUM (n1->stages[i]); j++)
489 sn_comparator_t *c_orig = SN_STAGE_COMP_GET (n1->stages[i], j);
490 sn_comparator_t c_copy;
492 SN_COMP_MIN(&c_copy) = SN_COMP_MIN(c_orig) + n0->inputs_num;
493 SN_COMP_MAX(&c_copy) = SN_COMP_MAX(c_orig) + n0->inputs_num;
495 sn_stage_comparator_add (s, &c_copy);
498 sn_network_stage_add (n, s);
501 if (sn_bounded_random (0, 1) == 0)
503 sn_network_add_bitonic_merger (n);
507 sn_network_add_odd_even_merger (n);
510 sn_network_compress (n);
513 } /* sn_network_t *sn_network_combine */
515 int sn_network_sort (sn_network_t *n, int *values)
521 for (i = 0; i < n->stages_num; i++)
523 status = sn_stage_sort (n->stages[i], values);
529 } /* int sn_network_sort */
531 int sn_network_brute_force_check (sn_network_t *n)
533 int test_pattern[n->inputs_num];
534 int values[n->inputs_num];
538 memset (test_pattern, 0, sizeof (test_pattern));
544 /* Copy the current pattern and let the network sort it */
545 memcpy (values, test_pattern, sizeof (values));
546 status = sn_network_sort (n, values);
550 /* Check if the array is now sorted. */
551 previous = values[0];
552 for (i = 1; i < n->inputs_num; i++)
554 if (previous > values[i])
556 previous = values[i];
559 /* Generate the next test pattern */
561 for (i = 0; i < n->inputs_num; i++)
563 if (test_pattern[i] == 0)
576 /* Break out of the while loop if we tested all possible patterns */
581 /* All tests successfull */
583 } /* int sn_network_brute_force_check */
585 sn_network_t *sn_network_read (FILE *fh)
592 while (fgets (buffer, sizeof (buffer), fh) != NULL)
594 char *str_key = buffer;
595 char *str_value = NULL;
596 int buffer_len = strlen (buffer);
598 while ((buffer_len > 0) && ((buffer[buffer_len - 1] == '\n')
599 || (buffer[buffer_len - 1] == '\r')))
602 buffer[buffer_len] = '\0';
607 str_value = strchr (buffer, ':');
608 if (str_value == NULL)
610 printf ("Cannot parse line: %s\n", buffer);
614 *str_value = '\0'; str_value++;
615 while ((*str_value != '\0') && (isspace (*str_value) != 0))
618 if (strcasecmp ("Inputs", str_key) == 0)
619 opt_inputs = atoi (str_value);
621 printf ("Unknown key: %s\n", str_key);
622 } /* while (fgets) */
627 n = sn_network_create (opt_inputs);
633 s = sn_stage_read (fh);
637 sn_network_stage_add (n, s);
640 if (SN_NETWORK_STAGE_NUM (n) < 1)
642 sn_network_destroy (n);
647 } /* sn_network_t *sn_network_read */
649 sn_network_t *sn_network_read_file (const char *file)
654 fh = fopen (file, "r");
658 n = sn_network_read (fh);
663 } /* sn_network_t *sn_network_read_file */
665 int sn_network_write (sn_network_t *n, FILE *fh)
669 fprintf (fh, "Inputs: %i\n", n->inputs_num);
672 for (i = 0; i < n->stages_num; i++)
673 sn_stage_write (n->stages[i], fh);
676 } /* int sn_network_write */
678 int sn_network_write_file (sn_network_t *n, const char *file)
683 fh = fopen (file, "w");
687 status = sn_network_write (n, fh);
692 } /* int sn_network_write_file */
694 int sn_network_serialize (sn_network_t *n, char **ret_buffer,
695 size_t *ret_buffer_size)
702 buffer = *ret_buffer;
703 buffer_size = *ret_buffer_size;
705 #define SNPRINTF_OR_FAIL(...) \
706 status = snprintf (buffer, buffer_size, __VA_ARGS__); \
707 if ((status < 1) || (status >= buffer_size)) \
710 buffer_size -= status;
712 SNPRINTF_OR_FAIL ("Inputs: %i\r\n\r\n", n->inputs_num);
714 for (i = 0; i < n->stages_num; i++)
716 status = sn_stage_serialize (n->stages[i], &buffer, &buffer_size);
721 *ret_buffer = buffer;
722 *ret_buffer_size = buffer_size;
724 } /* int sn_network_serialize */
726 sn_network_t *sn_network_unserialize (char *buffer, size_t buffer_size)
731 if (buffer_size == 0)
734 /* Read options first */
735 while (buffer_size > 0)
744 endptr = strchr (buffer, '\n');
751 line_len = strlen (line);
753 if ((line_len > 0) && (line[line_len - 1] == '\r'))
755 line[line_len - 1] = 0;
763 str_value = strchr (line, ':');
764 if (str_value == NULL)
766 printf ("Cannot parse line: %s\n", line);
770 *str_value = '\0'; str_value++;
771 while ((*str_value != '\0') && (isspace (*str_value) != 0))
774 if (strcasecmp ("Inputs", str_key) == 0)
775 opt_inputs = atoi (str_value);
777 printf ("Unknown key: %s\n", str_key);
778 } /* while (fgets) */
783 n = sn_network_create (opt_inputs);
789 s = sn_stage_unserialize (&buffer, &buffer_size);
793 sn_network_stage_add (n, s);
796 if (SN_NETWORK_STAGE_NUM (n) < 1)
798 sn_network_destroy (n);
803 } /* sn_network_t *sn_network_unserialize */
805 /* vim: set shiftwidth=2 softtabstop=2 : */