diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2013-01-01 22:44:12 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2013-01-01 22:44:12 +0000 |
commit | 01e21ba6afe2d16ba45b5a73f2777152fd27ffc3 (patch) | |
tree | e4ba10669fac95d66763c8d9c8812eda9da3b7b0 /src/testbed/standard_deviation.c | |
parent | 5200c6d68ff70c484c480794e5debbc50b62e2aa (diff) | |
download | gnunet-01e21ba6afe2d16ba45b5a73f2777152fd27ffc3.tar.gz gnunet-01e21ba6afe2d16ba45b5a73f2777152fd27ffc3.zip |
adaptive parallelism for overlay connect operations based on SD calculations
Diffstat (limited to 'src/testbed/standard_deviation.c')
-rw-r--r-- | src/testbed/standard_deviation.c | 129 |
1 files changed, 97 insertions, 32 deletions
diff --git a/src/testbed/standard_deviation.c b/src/testbed/standard_deviation.c index 362b50b26..4bdd6cef2 100644 --- a/src/testbed/standard_deviation.c +++ b/src/testbed/standard_deviation.c | |||
@@ -18,12 +18,41 @@ | |||
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #include <gnunet/platform.h> | 21 | #include "platform.h" |
22 | #include <gnunet/gnunet_common.h> | 22 | #include "gnunet_util_lib.h" |
23 | |||
24 | struct SDEntry | ||
25 | { | ||
26 | /** | ||
27 | * DLL next pointer | ||
28 | */ | ||
29 | struct SDEntry *next; | ||
30 | |||
31 | /** | ||
32 | * DLL prev pointer | ||
33 | */ | ||
34 | struct SDEntry *prev; | ||
35 | |||
36 | /** | ||
37 | * The value to store | ||
38 | */ | ||
39 | unsigned int amount; | ||
40 | }; | ||
41 | |||
23 | 42 | ||
24 | struct SDHandle | 43 | struct SDHandle |
25 | { | 44 | { |
26 | /** | 45 | /** |
46 | * DLL head for storing entries | ||
47 | */ | ||
48 | struct SDEntry *head; | ||
49 | |||
50 | /** | ||
51 | * DLL tail for storing entries | ||
52 | */ | ||
53 | struct SDEntry *tail; | ||
54 | |||
55 | /** | ||
27 | * Squared sum of data values | 56 | * Squared sum of data values |
28 | */ | 57 | */ |
29 | unsigned long long sqsum; | 58 | unsigned long long sqsum; |
@@ -36,43 +65,78 @@ struct SDHandle | |||
36 | /** | 65 | /** |
37 | * The average of data amounts | 66 | * The average of data amounts |
38 | */ | 67 | */ |
39 | unsigned int avg; | 68 | float avg; |
40 | 69 | ||
41 | /** | 70 | /** |
42 | * The variance | 71 | * The variance |
43 | */ | 72 | */ |
44 | unsigned int vr; | 73 | double vr; |
45 | 74 | ||
46 | /** | 75 | /** |
47 | * Number of data values | 76 | * Number of data values; also the length of DLL containing SDEntries |
48 | */ | 77 | */ |
49 | unsigned int cnt; | 78 | unsigned int cnt; |
79 | |||
80 | /** | ||
81 | * max number of entries we can have in the DLL | ||
82 | */ | ||
83 | unsigned int max_cnt; | ||
50 | }; | 84 | }; |
51 | 85 | ||
52 | 86 | ||
53 | struct SDHandle * | 87 | struct SDHandle * |
54 | GNUNET_TESTBED_SD_init () | 88 | SD_init (unsigned int max_cnt) |
55 | { | 89 | { |
56 | return GNUNET_malloc (sizeof (struct SDHandle)); | 90 | struct SDHandle *h; |
91 | |||
92 | GNUNET_assert (1 < max_cnt); | ||
93 | h = GNUNET_malloc (sizeof (struct SDHandle)); | ||
94 | h->max_cnt = max_cnt; | ||
95 | return h; | ||
57 | } | 96 | } |
58 | 97 | ||
59 | void | 98 | void |
60 | GNUNET_TESTBED_SD_destroy (struct SDHandle *h) | 99 | SD_destroy (struct SDHandle *h) |
61 | { | 100 | { |
101 | struct SDEntry *entry; | ||
102 | |||
103 | while (NULL != (entry = h->head)) | ||
104 | { | ||
105 | GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry); | ||
106 | GNUNET_free (entry); | ||
107 | } | ||
62 | GNUNET_free (h); | 108 | GNUNET_free (h); |
63 | } | 109 | } |
64 | 110 | ||
65 | void | 111 | void |
66 | GNUNET_TESTBED_SD_add_data (struct SDHandle *h, unsigned int amount) | 112 | SD_add_data (struct SDHandle *h, unsigned int amount) |
67 | { | 113 | { |
68 | unsigned long sqavg; | 114 | struct SDEntry *entry; |
69 | 115 | double sqavg; | |
116 | double sqsum_avg; | ||
117 | |||
118 | entry = NULL; | ||
119 | if (h->cnt == h->max_cnt) | ||
120 | { | ||
121 | entry = h->head; | ||
122 | GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry); | ||
123 | h->sum -= entry->amount; | ||
124 | h->sqsum -= ((unsigned long) entry->amount) * | ||
125 | ((unsigned long) entry->amount); | ||
126 | h->cnt--; | ||
127 | } | ||
128 | GNUNET_assert (h->cnt < h->max_cnt); | ||
129 | if (NULL == entry) | ||
130 | entry = GNUNET_malloc (sizeof (struct SDEntry)); | ||
131 | entry->amount = amount; | ||
132 | GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, entry); | ||
70 | h->sum += amount; | 133 | h->sum += amount; |
71 | h->cnt++; | 134 | h->cnt++; |
135 | h->avg = ((float) h->sum) / ((float) h->cnt); | ||
72 | h->sqsum += ((unsigned long) amount) * ((unsigned long) amount); | 136 | h->sqsum += ((unsigned long) amount) * ((unsigned long) amount); |
73 | h->avg = h->sum / h->cnt; | 137 | sqsum_avg = ((double) h->sqsum) / ((double) h->cnt); |
74 | sqavg = h->avg * h->avg; | 138 | sqavg = ((double) h->avg) * ((double) h->avg); |
75 | h->vr = (h->sqsum / h->cnt) - sqavg; | 139 | h->vr = sqsum_avg - sqavg; |
76 | } | 140 | } |
77 | 141 | ||
78 | 142 | ||
@@ -82,23 +146,24 @@ GNUNET_TESTBED_SD_add_data (struct SDHandle *h, unsigned int amount) | |||
82 | * @param h the SDhandle | 146 | * @param h the SDhandle |
83 | * @param amount the value for which the deviation is returned | 147 | * @param amount the value for which the deviation is returned |
84 | * @return the deviation from the average; GNUNET_SYSERR if the deviation cannot | 148 | * @return the deviation from the average; GNUNET_SYSERR if the deviation cannot |
85 | * be calculated | 149 | * be calculated; a maximum of 4 is returned for deviations equal to |
150 | * or larger than 4 | ||
86 | */ | 151 | */ |
87 | int | 152 | int |
88 | GNUNET_TESTBED_SD_deviation_factor (struct SDHandle *h, unsigned int amount) | 153 | SD_deviation_factor (struct SDHandle *h, unsigned int amount) |
89 | { | 154 | { |
90 | unsigned long diff; | 155 | double diff; |
91 | unsigned int n; | 156 | unsigned int n; |
92 | 157 | ||
93 | if (h->cnt < 2) | 158 | if (h->cnt < 2) |
94 | return GNUNET_SYSERR; | 159 | return GNUNET_SYSERR; |
95 | if (amount > h->avg) | 160 | if (((float) amount) > h->avg) |
96 | diff = amount - h->avg; | 161 | diff = ((float) amount) - h->avg; |
97 | else | 162 | else |
98 | diff = h->avg - amount; | 163 | diff = h->avg - ((float) amount); |
99 | diff *= diff; | 164 | diff *= diff; |
100 | for (n = 1; n < 4; n++) | 165 | for (n = 1; n < 4; n++) |
101 | if (diff < (n * n * h->vr)) | 166 | if (diff < (((double) (n * n)) * h->vr)) |
102 | break; | 167 | break; |
103 | return n; | 168 | return n; |
104 | } | 169 | } |
@@ -107,17 +172,17 @@ GNUNET_TESTBED_SD_deviation_factor (struct SDHandle *h, unsigned int amount) | |||
107 | int | 172 | int |
108 | main () | 173 | main () |
109 | { | 174 | { |
110 | struct SDHandle * h = GNUNET_TESTBED_SD_init (); | 175 | struct SDHandle * h = SD_init (20); |
111 | 176 | ||
112 | GNUNET_TESTBED_SD_add_data (h, 40); | 177 | SD_add_data (h, 40); |
113 | GNUNET_TESTBED_SD_add_data (h, 30); | 178 | SD_add_data (h, 30); |
114 | GNUNET_TESTBED_SD_add_data (h, 40); | 179 | SD_add_data (h, 40); |
115 | GNUNET_TESTBED_SD_add_data (h, 10); | 180 | SD_add_data (h, 10); |
116 | GNUNET_TESTBED_SD_add_data (h, 30); | 181 | SD_add_data (h, 30); |
117 | printf ("Average: %d\n", h->avg); | 182 | printf ("Average: %f\n", h->avg); |
118 | printf ("Variance: %d\n", h->vr); | 183 | printf ("Variance: %f\n", h->vr); |
119 | printf ("Standard Deviation: %d\n", (int) sqrt (h->vr)); | 184 | printf ("Standard Deviation: %f\n", sqrt (h->vr)); |
120 | printf ("Deviation factor: %d\n", GNUNET_TESTBED_SD_deviation (h, 40)); | 185 | printf ("Deviation factor: %d\n", SD_deviation_factor (h, 60)); |
121 | GNUNET_TESTBED_SD_destroy (h); | 186 | SD_destroy (h); |
122 | return 0; | 187 | return 0; |
123 | } | 188 | } |