replicode
RR.h
Go to the documentation of this file.
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ HUMANOBS - mBrane - Perf
4 //_/_/
5 //_/_/ Eric Nivel
6 //_/_/ Center for Analysis and Design of Intelligent Agents
7 //_/_/ Reykjavik University, Menntavegur 1, 101 Reykjavik, Iceland
8 //_/_/ http://cadia.ru.is
9 //_/_/ Copyright(c)2012
10 //_/_/
11 //_/_/ This software was developed by the above copyright holder as part of
12 //_/_/ the HUMANOBS EU research project, in collaboration with the
13 //_/_/ following parties:
14 //_/_/
15 //_/_/ Autonomous Systems Laboratory
16 //_/_/ Technical University of Madrid, Spain
17 //_/_/ http://www.aslab.org/
18 //_/_/
19 //_/_/ Communicative Machines
20 //_/_/ Edinburgh, United Kingdom
21 //_/_/ http://www.cmlabs.com/
22 //_/_/
23 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
24 //_/_/ University of Lugano and SUPSI, Switzerland
25 //_/_/ http://www.idsia.ch/
26 //_/_/
27 //_/_/ Institute of Cognitive Sciences and Technologies
28 //_/_/ Consiglio Nazionale delle Ricerche, Italy
29 //_/_/ http://www.istc.cnr.it/
30 //_/_/
31 //_/_/ Dipartimento di Ingegneria Informatica
32 //_/_/ University of Palermo, Italy
33 //_/_/ http://roboticslab.dinfo.unipa.it/index.php/Main/HomePage
34 //_/_/
35 //_/_/
36 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
37 //_/_/
38 //_/_/ Redistribution and use in source and binary forms, with or without
39 //_/_/ modification, is permitted provided that the following conditions
40 //_/_/ are met:
41 //_/_/
42 //_/_/ - Redistributions of source code must retain the above copyright
43 //_/_/ and collaboration notice, this list of conditions and the
44 //_/_/ following disclaimer.
45 //_/_/
46 //_/_/ - Redistributions in binary form must reproduce the above copyright
47 //_/_/ notice, this list of conditions and the following
48 //_/_/ disclaimer in the documentation and/or other materials provided
49 //_/_/ with the distribution.
50 //_/_/
51 //_/_/ - Neither the name of its copyright holders nor the names of its
52 //_/_/ contributors may be used to endorse or promote products
53 //_/_/ derived from this software without specific prior written permission.
54 //_/_/
55 //_/_/ - CADIA Clause: The license granted in and to the software under this
56 //_/_/ agreement is a limited-use license. The software may not be used in
57 //_/_/ furtherance of:
58 //_/_/ (i) intentionally causing bodily injury or severe emotional distress
59 //_/_/ to any person;
60 //_/_/ (ii) invading the personal privacy or violating the human rights of
61 //_/_/ any person; or
62 //_/_/ (iii) committing or preparing for any act of war.
63 //_/_/
64 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
65 //_/_/ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
66 //_/_/ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
67 //_/_/ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
68 //_/_/ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
69 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
70 //_/_/ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
71 //_/_/ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
72 //_/_/ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
73 //_/_/ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
74 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 //_/_/
76 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
77 
78 #ifndef Perf_RR_h
79 #define Perf_RR_h
80 #include <thread>
81 
82 uint64_t t1;
83 
85 private:
86 uint64_t tStart;
87 uint64_t tEnd;
88 uint64_t t2;
91 public:
92 void loadParameters(const std::vector<int32_t> &numbers, const std::vector<std::string> &strings)
93 {
94  int32_t a = numbers[0];
95  float f = *reinterpret_cast<float *>(&a);
96  std::cout << "RR Module got parameters: " << f << " " << numbers[1] << " " << strings[0] << std::endl;
97 }
98 void start()
99 {
100  tStart = 0;
101  tEnd = 0;
102  t1 = t2 = 0;
103  cRun = 0;
104  cc = 0;
105  NODE->addConstantObject(new Shared(), "a_constant");
106 }
107 void stop() { }
108 template<class T> Decision decide(T *p)
109 {
110  return WAIT;
111 }
112 template<class T> void react(T *p) {}
113 
114 void react(SystemReady *p)
115 {
116  // Thread::Sleep(3000);
117  printf("RRMaster starting RoundRobin test, please wait...\n");
118  tStart = t1 = NODE->time();
119  NODE->send(this, new Ball1(0), N::PRIMARY);
120 }
121 
122 void react(Ball9 *p)
123 {
124  uint32_t t;
125  int32_t counter = p->num;
126 
127  if (counter == 1) {
128  printf("RoundRobin test %u running...\n", cRun);
129  }
130 
131  t2 = NODE->time();
132 
133  // printf("Module 9 Send/Rec time: %u\n", (uint32) (t2 - t1));
134  // if ((t = (uint32)(t2-t1)) > 10000)
135  // printf("*** RR[%u] single run test took %uus for the %dth cycle, %.3fus per msg (%p)\n",
136  // cRun, t, counter, ((double)t)/((double)9), p);
137  if (counter == 10000) {
138  tEnd = t2;
139  t = (uint32_t)(tEnd - tStart);
140  printf("RR[%u] test took %uus for %d cycle, %.3fus per cycles, %.3fus per msg (%p)\n", cRun, t, counter, ((double)t) / ((double)counter), ((double)t) / ((double)counter * 9), p);
141  // OUTPUT<<"RR[" << cRun << "] test took "<<t<<"us for "<<counter<<" msgs, "<<((double)t)/((double)counter)<<"us per msg ("<<<<")"<<std::endl;
142  //OUTPUT<<"Test got to '"<<counter*runCount<<"' so far..."<<std::endl;
143  cRun++;
144  tStart = tEnd;
145  t1 = t2;
146  NODE->send(this, new Ball1(0), N::PRIMARY);
147  } else {
148  // if (counter % 1000 == 0)
149  // printf("RR[%u] %d msgs so far (%p)\n",cRun,counter, p);
150  // if (counter % 100 == 0)
151  // OUTPUT<<"RR[" << cRun << "] "<<counter<<" msgs so far..."<<std::endl;
152  t1 = t2;
153  NODE->send(this, new Ball1(counter + 1), N::PRIMARY);
154  }
155 }
156 MODULE_CLASS_END(RRMaster)
157 
158 MODULE_CLASS_BEGIN(RRModule, Module<RRModule>)
159 void start() {}
160 void stop() { }
161 template<class T> Decision decide(T *p)
162 {
163  return WAIT;
164 }
165 template<class T> void react(T *p) {}
166 
167 void react(Ball1 *p)
168 {
169  uint64_t now = NODE->time();
170  //printf("Module Rec time (%d,%d,%d,%d): 1 %u\n",
171  // (uint32) (now - p->send_ts()),
172  // (uint32) (now - p->node_send_ts()),
173  // (uint32) (now - p->node_recv_ts()),
174  // (uint32) (now - p->recv_ts()),
175  // (uint32) (now - t1));
176  int32_t counter = p->num;
177 // printf( "Ball1 triggered...\n");
178  // if (counter % 1000 == 0)
179  // printf("RR5 %d msgs so far (%p)\n",
180  // counter, ball);
181  NODE->send(this, new Ball2(counter), N::PRIMARY);
182 }
183 void react(Ball2 *p)
184 {
185  uint64_t now = NODE->time();
186  //printf("Module Rec time (%d,%d,%d,%d): 2 %u\n",
187  // (uint32) (now - p->send_ts()),
188  // (uint32) (now - p->node_send_ts()),
189  // (uint32) (now - p->node_recv_ts()),
190  // (uint32) (now - p->recv_ts()),
191  // (uint32) (now - t1));
192 // printf( "Ball2 triggered...\n");
193  int32_t counter = p->num;
194  NODE->send(this, new Ball3(counter), N::PRIMARY);
195 }
196 void react(Ball3 *p)
197 {
198  uint64_t now = NODE->time();
199  //printf("Module Rec time (%d,%d,%d,%d): 3 %u\n",
200  // (uint32) (now - p->send_ts()),
201  // (uint32) (now - p->node_send_ts()),
202  // (uint32) (now - p->node_recv_ts()),
203  // (uint32) (now - p->recv_ts()),
204  // (uint32) (now - t1));
205 // printf( "Ball3 triggered...\n");
206  int32_t counter = p->num;
207  NODE->send(this, new Ball4(counter), N::PRIMARY);
208 }
209 void react(Ball4 *p)
210 {
211  uint64_t now = NODE->time();
212  //printf("Module Rec time (%d,%d,%d,%d): 4 %u\n",
213  // (uint32) (now - p->send_ts()),
214  // (uint32) (now - p->node_send_ts()),
215  // (uint32) (now - p->node_recv_ts()),
216  // (uint32) (now - p->recv_ts()),
217  // (uint32) (now - t1));
218 // printf( "Ball4 triggered...\n");
219  int32_t counter = p->num;
220 // printf( "Ball4 triggered %d...\n", p->num);
221  NODE->send(this, new Ball5(counter), N::PRIMARY);
222 }
223 void react(Ball5 *p)
224 {
225  uint64_t now = NODE->time();
226  //printf("Module Rec time (%d,%d,%d,%d): 5 %u\n",
227  // (uint32) (now - p->send_ts()),
228  // (uint32) (now - p->node_send_ts()),
229  // (uint32) (now - p->node_recv_ts()),
230  // (uint32) (now - p->recv_ts()),
231  // (uint32) (now - t1));
232 // printf( "Ball5 triggered...\n");
233 // printf( "Ball5 triggered %d...\n", p->num);
234  int32_t counter = p->num;
235  //ActivateModule* am = new ActivateModule();
236  //am->host_id = 1;
237  //am->module_cid = 2;
238  //am->module_id = 3;
239  //am->space_id = 4;
240  //am->activationLevel = 5;
241  //Error::PrintBinary(am, am->size(), true, "ActivateModule Message Structure");
242  //Error::PrintBinary(p, p->size(), true, "Ball5 remote Message Structure");
243  //Ball5* ball = new Ball5(2);
244  //Error::PrintBinary(ball, ball->size(), true, "Ball5 local Message Structure");
245  NODE->send(this, new Ball6(counter), N::PRIMARY);
246 }
247 void react(Ball6 *p)
248 {
249  uint64_t now = NODE->time();
250  //printf("Module Rec time (%d,%d,%d,%d): 6 %u\n",
251  // (uint32) (now - p->send_ts()),
252  // (uint32) (now - p->node_send_ts()),
253  // (uint32) (now - p->node_recv_ts()),
254  // (uint32) (now - p->recv_ts()),
255  // (uint32) (now - t1));
256  int32_t counter = p->num;
257  NODE->send(this, new Ball7(counter), N::PRIMARY);
258 }
259 void react(Ball7 *p)
260 {
261  uint64_t now = NODE->time();
262  //printf("Module Rec time (%d,%d,%d,%d): 7 %u\n",
263  // (uint32) (now - p->send_ts()),
264  // (uint32) (now - p->node_send_ts()),
265  // (uint32) (now - p->node_recv_ts()),
266  // (uint32) (now - p->recv_ts()),
267  // (uint32) (now - t1));
268 // printf( "Ball7 triggered...\n");
269  int32_t counter = p->num;
270  NODE->send(this, new Ball8(counter), N::PRIMARY);
271 }
272 void react(Ball8 *p)
273 {
274  uint64_t now = NODE->time();
275  //printf("Module Rec time (%d,%d,%d,%d): 8 %u\n",
276  // (uint32) (now - p->send_ts()),
277  // (uint32) (now - p->node_send_ts()),
278  // (uint32) (now - p->node_recv_ts()),
279  // (uint32) (now - p->recv_ts()),
280  // (uint32) (now - t1));
281 // printf( "Ball8 triggered %d...\n", p->num);
282  int32_t counter = p->num;
283  NODE->send(this, new Ball9(counter), N::PRIMARY);
284 }
285 MODULE_CLASS_END(RRModule)
286 
287 MODULE_CLASS_BEGIN(SizeTest, Module<SizeTest>)
288 public:
289 void start() { }
290 void stop() { }
291 template<class T> Decision decide(T *p)
292 {
293  return WAIT;
294 }
295 template<class T> void react(T *p) {}
296 
297 void react(SystemReady *p)
298 {
299  ActivateModule *am = new ActivateModule();
300  am->host_id = 1;
301  am->module_cid = 2;
302  am->module_id = 3;
303  am->space_id = 4;
304  am->activationLevel = 5;
305  Error::PrintBinary(am, am->size(), true, "ActivateModule Message Structure");
306  Ball1 *ball = new Ball1();
307  ball->d1 = 1;
308  ball->num = 2;
309  ball->num2 = 3;
310  Error::PrintBinary(ball, ball->size(), true, "Ball1 Message Structure");
311 }
312 
313 MODULE_CLASS_END(SizeTest)
314 
315 typedef char *(*LoomOutput)(uint32_t);
316 typedef bool (*LoomInput)(char *);
317 
318 MODULE_CLASS_BEGIN(Loom, Module<Loom>)
319 public:
321 SharedLibrary *lib;
324 
325 void start()
326 {
327  lib = SharedLibrary::New("modules/mBrane_d.dll");
328 
329  if (lib) {
330  waitForEvent = lib->getFunction<LoomOutput>("WaitForEvent");
331  processEvent = lib->getFunction<LoomInput>("ProcessEvent");
332  } else {
333  waitForEvent = NULL;
334  processEvent = NULL;
335  }
336 }
337 void stop()
338 {
339  if (thread.joinable()) {
340  thread.join();
341  }
342 
343  delete(lib);
344  lib = NULL;
345  waitForEvent = NULL;
346  processEvent = NULL;
347 }
348 template<class T> Decision decide(T *p)
349 {
350  return WAIT;
351 }
352 template<class T> void react(T *p) {}
353 
354 void react(SystemReady *p)
355 {
356  thread = std::thread(run, this);
357  char *data = NULL;
358 
359  // Output data to Loom
360  if (processEvent) {
361  processEvent(data);
362  }
363 }
364 
365 void react(Ball1 *p)
366 {
367  char *data = NULL;
368 
369  // Output data to Loom
370  if (processEvent) {
371  processEvent(data);
372  }
373 }
374 
375 static thread_ret thread_function_call run(void *args)
376 {
377  Loom *_this = (Loom *) args;
378 
379  if (!_this) {
380  thread_ret_val(0);
381  }
382 
383  uint32_t type = 0;
384  char *data = NULL;
385  uint32_t dataSize = 0;
386 
387  while (_this->waitForEvent) {
388  // wait for new input from lib function
389  if ((data = _this->waitForEvent(100))) {
390  // if there, post output
391  }
392  }
393 
394  thread_ret_val(0);
395 }
396 
397 MODULE_CLASS_END(Loom)
398 
399 
400 #endif /* Perf_RR_h */
static thread_ret thread_function_call run(void *args)
Definition: RR.h:375
Definition: Perf_class.h:158
uint64_t tEnd
Definition: RR.h:87
int32_t num
Definition: Perf_class.h:116
Definition: Perf_class.h:196
LoomOutput waitForEvent
Definition: RR.h:322
int32_t num
Definition: Perf_class.h:125
void start()
Definition: RR.h:98
#define MODULE_CLASS_END(C)
Definition: application.h:204
std::thread thread
Definition: RR.h:320
Decision decide(T *p)
Definition: RR.h:108
int32_t num
Definition: Perf_class.h:179
Definition: Perf_class.h:149
uint64_t t1
Definition: RR.h:82
SharedLibrary * lib
Definition: RR.h:321
int32_t num
Definition: Perf_class.h:134
uint32_t cRun
Definition: RR.h:89
Definition: Perf_class.h:185
Definition: Perf_class.h:122
Definition: application.h:98
int32_t num
Definition: Perf_class.h:152
void loadParameters(const std::vector< int32_t > &numbers, const std::vector< std::string > &strings)
Definition: RR.h:92
Definition: Perf_class.h:140
int32_t num
Definition: Perf_class.h:161
char *(* LoomOutput)(uint32_t)
Definition: RR.h:315
#define MODULE_CLASS_BEGIN(C, S)
Definition: application.h:190
Definition: Perf_class.h:113
void react(T *p)
Definition: RR.h:112
int32_t num
Definition: Perf_class.h:170
void stop()
Definition: RR.h:107
Definition: Perf_class.h:176
int32_t num
Definition: Perf_class.h:143
int32_t num
Definition: Perf_class.h:188
uint32_t cc
Definition: RR.h:90
Definition: Perf_class.h:131
#define NODE
Definition: Perf_modules.h:85
bool(* LoomInput)(char *)
Definition: RR.h:316
LoomInput processEvent
Definition: RR.h:323
uint64_t t2
Definition: RR.h:88
Definition: Perf_class.h:167
uint64_t tStart
Definition: RR.h:86