-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_jump.cpp
More file actions
226 lines (151 loc) · 5.81 KB
/
node_jump.cpp
File metadata and controls
226 lines (151 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include<iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
using namespace std;
#define NETWORK_SIZE 100000
struct Node{
uint id;
// The size of the struct is small enough so it can fit to stack
// I want to avoid vec use due to allocations overhead. (-1) - means no receiver defined.
// But it will require extra "if".
int receivers[3];
int got_msg;
int hop;
Node() : id(0), receivers{-1, -1, -1}, got_msg(0), hop(0) {}
};
void gen_rnd(int fan, Node* node){
for (int i = 0; i < fan; ++i) {
int rnd;
do {
rnd = rand() % NETWORK_SIZE;
} while (rnd == node->id);
node->receivers[i] = rnd;
}
}
void fill_array(Node nodes[], int fanout, int size){
for (int i = 0; i < size; ++i) {
nodes[i].id = i;
gen_rnd(fanout, &nodes[i]);
}
}
void start_msg(
Node nodes[],
int fanout,
int mother_node,
int size,
float *dublicates_amount,
float *total_messages_amount,
int *used_nodes_total,
float *total_hopes_amount){
//===============Init==============
float used_nodes_buff[10000];
for (int i = 0; i<size; ++i){
used_nodes_buff[i] = -1.0;
}
// immidiately define mother node as 1 because it has the message initially
used_nodes_buff[mother_node] = 1.0;
// mark the Node struct as
nodes[mother_node].got_msg = 1;
int current_node = mother_node;
//=====================================
// the stack border
int idx_buf_offset = 1;
//the Stack buffer exists only for the nodes which are allowed to send the message
//and Node.got_msg = 1
int idx_buf[10000];
for (int i = 0; i<size; ++i){
idx_buf[i] = -1;
}
//The first idx in the stack is the mother id put it there streight
idx_buf[0] = mother_node;
// This step will increase by 1 every iteration and it will go through the indexes of nodes untill it will meet "-1"
// What means there are no nodes who can send any messages left and we stop the process.
// Starting from 1 because 0 is buisy with mother
int step_in_stack = 1;
while (true){
for (int i = 0; i < fanout; ++i){
int idx = nodes[current_node].receivers[i];
// This logic is for counting dublicates
if (used_nodes_buff[idx]== -1.0){
used_nodes_buff[idx] = 0.0;
}
//
nodes[idx].got_msg += 1;
// increase dublicates only if it alredy got one message.
if (nodes[idx].got_msg > 1){
used_nodes_buff[idx] += 1.0;
}
//Next block is for adding the index of the node which can send messages
// It can not be added there if it is not allowed to send message
// If we did not yet get a message or got it 1 time==>
if (nodes[idx].got_msg <= 1)
{
// we put it in the queue that we need to send the message
idx_buf[idx_buf_offset] = idx;
// and we move the offset to the next memory block
idx_buf_offset++;
nodes[idx].hop = nodes[current_node].hop + 1;
}
}
// after we wrote all the receivers to the stack
// we step to the next id in the stack
//and then we get the next index from the "sender buffer" and assign it as a current node
if (step_in_stack >= idx_buf_offset || idx_buf[step_in_stack] == -1) {
break;
}
current_node = idx_buf[step_in_stack];
step_in_stack++;
}
//Statisitic variables
float dublicates = 0.0;
float total_messages = 0.0;
for(int i = 0; i < size; ++i){
if(used_nodes_buff[i]!= -1.0){
(*used_nodes_total)++;
dublicates += used_nodes_buff[i];
*total_hopes_amount += nodes[i].hop;
}
}
for (int i = 0; i< size; ++i){
total_messages += nodes[i].got_msg;
}
*dublicates_amount += dublicates;
*total_messages_amount += total_messages;
}
int main(){
srand(time(0));
Node nodes[NETWORK_SIZE];
int fanout;
cout<< "Enter a fanout number"<< endl;
cin >> fanout;
int size = sizeof(nodes) / sizeof(nodes[0]);
float dublicates_amount = 0.0;
float total_messages_amount = 0.0;
int used_nodes_total = 0;
float total_hopes_amount = 0;
int iter_times = 100;
int mother_node = rand() % NETWORK_SIZE;
auto bench_start = chrono::high_resolution_clock::now();
for(int i = 0; i < iter_times; ++i){
fill_array(nodes, fanout, size);
// Wipe old data from Node.got_msg
for (int j = 0; j < size; ++j) {
nodes[j].got_msg = 0;
}
start_msg(nodes, fanout, mother_node, size, &dublicates_amount, &total_messages_amount, &used_nodes_total, &total_hopes_amount);
}
auto bench_end = chrono::high_resolution_clock::now();
chrono::duration<double, std::milli> duration = bench_end - bench_start;
float avg_hops = total_hopes_amount/used_nodes_total;
float avg_dub = dublicates_amount/iter_times;
float avg_tot = total_messages_amount/iter_times;
float avg_used = (float)used_nodes_total / iter_times;
cout<<"Dublicates avg amount: "<<avg_dub<< endl;
cout<<"Messaes avg amount: "<<avg_tot<< endl;
cout<<"Percentage dub/total: "<< (avg_dub/avg_tot*100.00)<< endl;
cout<<" Avg percent of Used: "<< avg_used/float(NETWORK_SIZE)*100.0 << endl;
cout<<" Avg Hopes Amount: "<< avg_hops << endl;
cout << "Execution time: " << duration.count() << " ms" << endl;
return 0;
}