MADARA  3.2.3
Barrier.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 #include <sstream>
5 
6 #include "Barrier.h"
8 
10  const KnowledgeUpdateSettings & settings)
11 : BaseContainer ("", settings), context_ (0), id_ (0), participants_ (1)
12 {
13  init_noharm ();
14 }
15 
17  const std::string & name,
19  const KnowledgeUpdateSettings & settings)
20 : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
21  id_ (0), participants_ (1)
22 {
23  init_noharm ();
24  build_var ();
26 }
27 
29  const std::string & name,
31  const KnowledgeUpdateSettings & settings)
32 : BaseContainer (name, settings), context_ (knowledge.get_context ()),
33  id_ (0), participants_ (1)
34 {
35  init_noharm ();
36  build_var ();
38 }
39 
41  const std::string & name,
43  int id,
44  int participants,
45  const KnowledgeUpdateSettings & settings)
46 : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
47  id_ (id), participants_ (participants)
48 {
49  init_noharm ();
50  build_var ();
52 }
53 
55  const std::string & name,
57  int id,
58  int participants,
59  const KnowledgeUpdateSettings & settings)
60 : BaseContainer (name, settings), context_ (knowledge.get_context ()),
61  id_ (id), participants_ (participants)
62 {
63  init_noharm ();
64  build_var ();
66 }
67 
68 
70 : BaseContainer (rhs), context_ (rhs.context_),
71  variable_ (rhs.variable_),
72  id_ (rhs.id_),
76 {
77 
78 }
79 
80 
82 {
83 
84 }
85 
86 void
88 {
89  if (this != &rhs)
90  {
91  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
92 
93  this->context_ = rhs.context_;
94  this->name_ = rhs.name_;
95  this->id_ = rhs.id_;
96  this->participants_ = rhs.participants_;
97  this->settings_ = rhs.settings_;
98  this->variable_ = rhs.variable_;
100  this->variable_name_ = rhs.variable_name_;
101  }
102 }
103 
104 void
106 {
107  if (context_ && name_ != "")
108  {
109  ContextGuard context_guard (*context_);
110  MADARA_GUARD_TYPE guard (mutex_);
111 
112  std::stringstream buffer;
113  if (participants_ > 0)
114  {
115  // barrier logic is that everyone else is at least to your barrier
116  buffer << name_;
117  buffer << ".0 >= ";
118  buffer << name_;
119  buffer << ".";
120  buffer << id_;
121 
122  // add all other barrierer variables
123  for (size_t i = 1; i < participants_; ++i)
124  {
125  buffer << " && ";
126  buffer << name_;
127  buffer << ".";
128  buffer << i;
129  buffer << " >= ";
130  buffer << name_;
131  buffer << ".";
132  buffer << id_;
133  }
134  }
135 
136  aggregate_barrier_ = context_->compile (buffer.str ());
137 
138 
140  "Barrier::build_aggregate_barrier: building barrier string of %s\n",
141  buffer.str ().c_str ());
142  }
143  else if (name_ == "")
144  {
145  context_->print ("ERROR: Container::Barrier needs a name.\n", 0);
146  }
147  else if (!context_)
148  {
149  context_->print ("ERROR: Container::Barrier needs a context.\n", 0);
150  }
151 }
152 
153 void
155 {
156  if (context_ && name_ != "")
157  {
158  ContextGuard context_guard (*context_);
159  MADARA_GUARD_TYPE guard (mutex_);
160 
161  std::stringstream buffer;
162 
163  buffer << name_;
164  buffer << ".";
165  buffer << id_;
166 
167  variable_name_ = buffer.str ();
168 
170  "Barrier::build_var: settings variable reference to %s\n",
171  variable_name_.c_str ());
172 
173  variable_ = context_->get_ref (buffer.str (), no_harm);
174  }
175  else if (name_ == "")
176  {
177  context_->print ("ERROR: Container::Barrier needs a name.\n", 0);
178  }
179  else if (!context_)
180  {
181  context_->print ("ERROR: Container::Barrier needs a context.\n", 0);
182  }
183 }
184 
185 void
187 {
188  no_harm.always_overwrite = false;
190  no_harm.expand_variables = false;
191  no_harm.signal_changes = false;
194 }
195 
196 size_t
198 {
199  MADARA_GUARD_TYPE guard (mutex_);
200  return id_;
201 }
202 
203 size_t
205 {
206  MADARA_GUARD_TYPE guard (mutex_);
207  return participants_;
208 }
209 
210 void
212  const std::string & var_name,
214  int id,
215  int participants)
216 {
217  KnowledgeUpdateSettings keep_local (true);
218  context_ = &(knowledge.get_context ());
219 
220  ContextGuard context_guard (*context_);
221  MADARA_GUARD_TYPE guard (mutex_);
222 
223  name_ = var_name;
224  id_ = id;
225  participants_ = participants;
226 
227  this->build_var ();
228  this->build_aggregate_barrier ();
229 }
230 
231 void
233  const std::string & var_name,
235  int id,
236  int participants)
237 {
238  KnowledgeUpdateSettings keep_local (true);
239  context_ = knowledge.get_context ();
240 
241  ContextGuard context_guard (*context_);
242  MADARA_GUARD_TYPE guard (mutex_);
243 
244  name_ = var_name;
245  id_ = id;
246  participants_ = participants;
247 
248  this->build_var ();
249  this->build_aggregate_barrier ();
250 }
251 
252 void
254  const std::string & var_name,
256  int id,
257  int participants)
258 {
259  KnowledgeUpdateSettings keep_local (true);
260  context_ = &knowledge;
261 
262  ContextGuard context_guard (*context_);
263  MADARA_GUARD_TYPE guard (mutex_);
264 
265  name_ = var_name;
266  id_ = id;
267  participants_ = participants;
268 
269  this->build_var ();
270  this->build_aggregate_barrier ();
271 }
272 
273 void
274 madara::knowledge::containers::Barrier::resize (size_t id, size_t participants)
275 {
276  if (context_)
277  {
278  ContextGuard context_guard (*context_);
279  MADARA_GUARD_TYPE guard (mutex_);
280 
281  id_ = id;
282  participants_ = participants;
283 
284  this->build_var ();
285  this->build_aggregate_barrier ();
286  }
287 }
288 
291 {
292  if (context_)
293  {
294  ContextGuard context_guard (*context_);
295  MADARA_GUARD_TYPE guard (mutex_);
296  context_->set (variable_, value, settings_);
297  }
298 
299  return value;
300 }
301 
304 {
306 
307  if (context_)
308  {
309  ContextGuard context_guard (*context_);
310  MADARA_GUARD_TYPE guard (mutex_);
311  result = context_->get (variable_, no_harm);
312  }
313 
314  return result;
315 }
316 
319 {
321 
322  if (context_)
323  {
324  ContextGuard context_guard (*context_);
325  MADARA_GUARD_TYPE guard (mutex_);
326  result = context_->get (variable_, no_harm).to_integer ();
327  }
328 
329  return result;
330 }
331 
332 
333 void
335 {
336  if (context_)
337  {
338  ContextGuard context_guard (*context_);
339  MADARA_GUARD_TYPE guard (mutex_);
341  }
342 }
343 
344 bool
346 {
347  bool result = false;
348 
349  if (context_ && name_ != "")
350  {
351  ContextGuard context_guard (*context_);
352  MADARA_GUARD_TYPE guard (mutex_);
353 
355  "Barrier::is_done: checking barrier result for done\n");
356 
357  result = barrier_result () == 1;
358 
359  if (!result)
360  {
362  "Barrier::is_true: barrier is not true, remarking barrier variable\n");
363 
365 
367  }
368  else
369  {
371  "Barrier::is_true: barrier is true\n");
372  }
373  }
374 
375  return result;
376 }
377 
378 void
380 {
381  if (context_ && name_ != "")
382  {
383  context_->set (variable_, value, settings_);
384  }
385 }
386 
387 void
389 {
390  if (context_ && name_ != "")
391  {
393  }
394 }
395 
398 {
399  std::stringstream result;
400 
401  result << "Barrier: ";
402 
403  if (context_)
404  {
405  ContextGuard context_guard (*context_);
406  MADARA_GUARD_TYPE guard (mutex_);
407 
408  result << this->name_;
409  result << " = " << context_->get (variable_).to_string () << "\n";
410 
411  std::string prefix = name_ + ".";
412 
413  // add all other barrierer variables
414  for (size_t i = 0; i < participants_; ++i)
415  {
416  std::stringstream temp_buffer;
417  temp_buffer << prefix;
418  temp_buffer << i;
419 
420  std::string this_var = temp_buffer.str ();
421 
422  result << " " << this_var;
423  result << "={";
424  result << this_var;
425  result << "}\n";
426  }
427  }
428 
429  return result.str ();
430 }
431 
432 void
434 {
435  modify ();
436 }
437 
440 {
441  return get_debug_info ();
442 }
443 
446 {
447  return new Barrier (*this);
448 }
449 
450 double
452 {
453  double result (0.0);
454 
455  if (context_)
456  {
457  ContextGuard context_guard (*context_);
458  MADARA_GUARD_TYPE guard (mutex_);
459  result = context_->get (variable_, no_harm).to_double ();
460  }
461 
462  return result;
463 }
464 
467 {
468  std::string result;
469 
470  if (context_)
471  {
472  ContextGuard context_guard (*context_);
473  MADARA_GUARD_TYPE guard (mutex_);
474  result = context_->get (variable_, no_harm).to_string ();
475  }
476 
477  return result;
478 }
479 
480 void
482  uint32_t quality,
483  const KnowledgeReferenceSettings & settings)
484 {
485  if (context_)
486  {
487  ContextGuard context_guard (*context_);
488  MADARA_GUARD_TYPE guard (mutex_);
489  context_->set_quality (name_, quality, true, settings);
490  }
491 }
492 
493 bool
495 {
496  bool result (false);
497 
499  "Barrier::is_true: checking barrier result for truth\n");
500 
501  if (context_)
502  {
503  ContextGuard context_guard (*context_);
504  MADARA_GUARD_TYPE guard (mutex_);
505  result = barrier_result () == 1;
506  }
507 
509  "Barrier::is_true: final result is %d\n", (int)result);
510 
511  return result;
512 }
513 
514 bool
516 {
517  return !is_true ();
518 }
519 
520 
521 bool
523 {
524  return is_true ();
525 }
526 
527 bool
529 {
530  return is_false ();
531 }
532 
533 
534 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
bool expand_variables
Toggle for always attempting to expand variables (true) or never expanding variables (false) ...
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
size_t get_id(void) const
Returns the id of the barrier in the barrier ring.
Definition: Barrier.cpp:197
size_t get_participants(void) const
Returns the number of participants in the barrier ring.
Definition: Barrier.cpp:204
bool is_false(void) const
Determines if the barrier is false.
Definition: Barrier.cpp:515
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the barrier round number as an integer (same as *)
Definition: Barrier.cpp:318
std::string to_string(void) const
Returns the barrier round number as a string.
Definition: Barrier.cpp:466
void build_aggregate_barrier(void)
Builds the aggregate barrier logic.
Definition: Barrier.cpp:105
void operator=(const Barrier &rhs)
Assignment operator.
Definition: Barrier.cpp:87
void resize(size_t id=0, size_t participants=1)
Resizes the barrier, usually when number of participants change.
Definition: Barrier.cpp:274
double to_double(void) const
converts the value to a float/double.
std::string name_
Prefix of variable.
virtual BaseContainer * clone(void) const
Clones this container.
Definition: Barrier.cpp:445
bool signal_changes
Toggle whether to signal changes have happened.
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
Definition: Barrier.h:37
This class stores variables and their values for use by any entity needing state information in a thr...
void print(unsigned int level) const
Atomically prints all variables and values in the context.
void next(void)
Goes to the next barrier round.
Definition: Barrier.cpp:334
size_t id_
id of this barrier in the barrier ring
Definition: Barrier.h:358
MADARA_LOCK_TYPE mutex_
guard for access and changes
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Barrier.h:348
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: Barrier.cpp:433
CompiledExpression aggregate_barrier_
Expression for aggregating barrier in one atomic operation.
Definition: Barrier.h:368
static struct madara::knowledge::tags::string_t string
double to_double(void) const
Returns the barrier round number as a double.
Definition: Barrier.cpp:451
VariableReference variable_
Variable reference.
Definition: Barrier.h:353
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
virtual bool is_false_(void) const
Polymorphic is false method which can be used to determine if at least one value in the container is ...
Definition: Barrier.cpp:528
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: Barrier.cpp:439
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Barrier(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
Definition: Barrier.cpp:9
Integer to_integer(void) const
converts the value to an integer.
type barrier_result(void) const
Checks if current barrier is successful.
Definition: Barrier.h:330
bool always_overwrite
Toggle for always overwriting records, regardless of quality, clock values, etc.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the barrier variables.
Definition: Barrier.cpp:481
size_t participants_
the number of participants in the barrier ring
Definition: Barrier.h:363
std::string variable_name_
Holder for variable name to quickly refresh modified status.
Definition: Barrier.h:378
Provides functions and classes for the distributed knowledge base.
void init_noharm(void)
Initialize the no harm eval settings.
Definition: Barrier.cpp:186
KnowledgeUpdateSettings settings_
Settings for modifications.
bool is_true(void) const
Determines if the barrier is true.
Definition: Barrier.cpp:494
bool is_done(void)
Wait for all other participants to reach your barrier round.
Definition: Barrier.cpp:345
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int id, int participants)
Sets the variable name that this refers to.
Definition: Barrier.cpp:211
EvalSettings no_harm
Settings we&#39;ll use for all evaluations.
Definition: Barrier.h:373
Settings for applying knowledge updates.
bool treat_globals_as_locals
Toggle whether updates to global variables are treated as local variables and not marked as modified ...
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
uint32_t set_quality(const std::string &key, uint32_t quality, bool force_update, const KnowledgeReferenceSettings &settings)
Atomically sets quality of this process for a variable.
This class stores an integer within a variable context.
Definition: Barrier.h:33
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
void set(type value)
Sets the barrier to a specific round.
Definition: Barrier.cpp:379
knowledge::KnowledgeRecord to_record(void) const
Returns the barrier round number as a knowledge::KnowledgeRecord.
Definition: Barrier.cpp:303
bool delay_sending_modifieds
Toggle for sending modifieds in a single update event after each evaluation.
Definition: EvalSettings.h:115
bool track_local_changes
Toggle for checkpointing support.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
Definition: Barrier.cpp:522
Settings for applying knowledge updates.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Barrier.cpp:397
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
void build_var(void)
Builds the variable that is actually incremented.
Definition: Barrier.cpp:154
Provides an interface for external functions into the MADARA KaRL variable settings.
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
void modify(void)
Mark the value as modified.
Definition: Barrier.cpp:388
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.