replicode
group.h
Go to the documentation of this file.
1 // group.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef group_h
32 #define group_h
33 
34 #include <set>
35 
36 #include "object.h"
37 #include "view.h"
38 #include <unordered_map>
39 
40 namespace r_exec {
41 
42 class _Mem;
43 class HLPController;
44 
45 // Shared resources:
46 // all parameters: accessed by Mem::update and reduction cores (via overlay mod/set).
47 // all views: accessed by Mem::update and reduction cores.
48 // viewing_groups: accessed by Mem::injectNow and Mem::update.
50  public LObject {
51 private:
52 // Ctrl values.
54  double acc_sln_thr;
56  double acc_act_thr;
58  double acc_vis_thr;
60  double acc_c_sln;
62  double acc_c_act;
64  double acc_c_sln_thr;
66  double acc_c_act_thr;
67  void reset_ctrl_values();
68 
69 // Stats.
70  double avg_sln;
71  double high_sln;
72  double low_sln;
73  double avg_act;
74  double high_act;
75  double low_act;
78 
79 // Decay.
80  double sln_decay;
81  double sln_thr_decay;
84  double decay_target; // -1: none, 0: sln, 1:sln_thr
85  void reset_decay_values();
86 
87 // Notifications.
88  int64_t sln_change_monitoring_periods_to_go = 0;
89  int64_t act_change_monitoring_periods_to_go = 0;
90 
91  void _mod_0_positive(uint16_t member_index, float value);
92  void _mod_0_plus1(uint16_t member_index, float value);
93  void _mod_minus1_plus1(uint16_t member_index, float value);
94  void _set_0_positive(uint16_t member_index, float value);
95  void _set_0_plus1(uint16_t member_index, float value);
96  void _set_minus1_plus1(uint16_t member_index, float value);
97  void _set_0_1(uint16_t member_index, float value);
98 
99  bool is_active_pgm(View *view);
100  bool is_eligible_input(View *view);
101 
103  void inject(View *view);
104 
105  void notifyNew(View *view);
106  void cov(View *view);
107 
108  class GroupState {
109  public:
115  GroupState(double former_sln_thr,
116  bool was_c_active,
117  bool is_c_active,
118  bool was_c_salient,
119  bool is_c_salient): former_sln_thr(former_sln_thr), was_c_active(was_c_active), is_c_active(is_c_active), was_c_salient(was_c_salient), is_c_salient(is_c_salient) {}
120  };
121 
122  void _update_saliency(GroupState *state, View *view);
123  void _update_activation(GroupState *state, View *view);
124  void _update_visibility(GroupState *state, View *view);
125 
126  void _initiate_sln_propagation(Code *object, double change, double source_sln_thr) const;
127  void _initiate_sln_propagation(Code *object, double change, double source_sln_thr, std::vector<Code *> &path) const;
128  void _propagate_sln(Code *object, double change, double source_sln_thr, std::vector<Code *> &path) const;
129 public:
130  std::mutex mutex;
131 // xxx_views are meant for erasing views with res==0. They are specialized by type to ease update operations.
132 // Active overlays are to be found in xxx_ipgm_views.
133  std::unordered_map<uint64_t, P<View> > ipgm_views;
134  std::unordered_map<uint64_t, P<View> > anti_ipgm_views;
135  std::unordered_map<uint64_t, P<View> > input_less_ipgm_views;
136  std::unordered_map<uint64_t, P<View> > notification_views;
137  std::unordered_map<uint64_t, P<View> > group_views;
138  std::unordered_map<uint64_t, P<View> > other_views;
139 
140 // Defined to create reduction jobs in the viewing groups from the viewed group.
141 // Empty when the viewed group is invisible (this means that visible groups can be non c-active or non c-salient).
142 // Maintained by the viewing groups (at update time).
143 // Viewing groups are c-active and c-salient. the bool is the cov.
144  std::unordered_map<Group *, bool> viewing_groups;
145 
146 // Populated within update; ordered by increasing ijt; cleared at the beginning of update.
147  std::multiset<P<View>, r_code::View::Less> newly_salient_views;
148 
149 // Populated upon ipgm injection; used at update time; cleared afterward.
150  std::vector<Controller *> new_controllers;
151 
152  class Operation {
153  protected:
154  Operation(uint64_t oid): oid(oid) {}
155  public:
156  virtual ~Operation() {};
157  const uint64_t oid; // of the view.
158  virtual void execute(Group *g) const = 0;
159  };
160 
161  class ModSet:
162  public Operation {
163  protected:
164  ModSet(uint64_t oid, uint16_t member_index, double value): Operation(oid), member_index(member_index), value(value) {}
165  const uint16_t member_index;
166  const double value;
167  };
168 
169  class Mod:
170  public ModSet {
171  public:
172  Mod(uint64_t oid, uint16_t member_index, double value): ModSet(oid, member_index, value) {}
173  void execute(Group *g) const {
174 
175  View *v = g->get_view(oid);
176  if (v)
177  v->mod(member_index, value);
178  }
179  };
180 
181  class Set:
182  public ModSet {
183  public:
184  Set(uint64_t oid, uint16_t member_index, double value): ModSet(oid, member_index, value) {}
185  void execute(Group *g) const {
186 
187  View *v = g->get_view(oid);
188  if (v)
189  v->set(member_index, value);
190  }
191  };
192 
193 // Pending mod/set operations on the group's view, exploited and cleared at update time.
194  std::vector<Operation *> pending_operations;
195 
196  Group(r_code::Mem *m = NULL);
197  Group(r_code::SysObject *source);
198  virtual ~Group();
199 
200  bool invalidate(); // removes all views of itself and of any other object.
201 
202  bool all_views_cond(uint8_t &selector, std::unordered_map<uint64_t, P<View> >::const_iterator &it, std::unordered_map<uint64_t, P<View> >::const_iterator &end) {
203  while (it == end) {
204  switch (selector++) {
205  case 0:
206  it = anti_ipgm_views.begin();
207  end = anti_ipgm_views.end();
208  break;
209  case 1:
210  it = input_less_ipgm_views.begin();
211  end = input_less_ipgm_views.end();
212  break;
213  case 2:
214  it = notification_views.begin();
215  end = notification_views.end();
216  break;
217  case 3:
218  it = group_views.begin();
219  end = group_views.end();
220  break;
221  case 4:
222  it = other_views.begin();
223  end = other_views.end();
224  break;
225  case 5:
226  selector = 0;
227  return false;
228  }
229  }
230  return true;
231  }
232 
233 #define FOR_ALL_VIEWS_BEGIN(g,it) { \
234  uint8_t selector; \
235  std::unordered_map<uint64_t,P<View> >::const_iterator it=g->ipgm_views.begin(); \
236  std::unordered_map<uint64_t,P<View> >::const_iterator end=g->ipgm_views.end(); \
237  for(selector=0;g->all_views_cond(selector,it,end);++it){
238 
239 #define FOR_ALL_VIEWS_BEGIN_NO_INC(g,it) { \
240  uint8_t selector; \
241  std::unordered_map<uint64_t,P<View> >::const_iterator it=g->ipgm_views.begin(); \
242  std::unordered_map<uint64_t,P<View> >::const_iterator end=g->ipgm_views.end(); \
243  for(selector=0;g->all_views_cond(selector,it,end);){
244 
245 #define FOR_ALL_VIEWS_END } \
246 }
247 
248  bool views_with_inputs_cond(uint8_t &selector, std::unordered_map<uint64_t, P<View> >::const_iterator &it, std::unordered_map<uint64_t, P<View> >::const_iterator &end) {
249  while (it == end) {
250  switch (selector++) {
251  case 0:
252  it = anti_ipgm_views.begin();
253  end = anti_ipgm_views.end();
254  break;
255  case 1:
256  selector = 0;
257  return false;
258  }
259  }
260  return true;
261  }
262 
263 #define FOR_ALL_VIEWS_WITH_INPUTS_BEGIN(g,it) { \
264  uint8_t selector; \
265  std::unordered_map<uint64_t,P<View> >::const_iterator it=g->ipgm_views.begin(); \
266  std::unordered_map<uint64_t,P<View> >::const_iterator end=g->ipgm_views.end(); \
267  for(selector=0;g->views_with_inputs_cond(selector,it,end);++it){
268 
269 #define FOR_ALL_VIEWS_WITH_INPUTS_END } \
270 }
271 
272  bool non_ntf_views_cond(uint8_t &selector, std::unordered_map<uint64_t, P<View> >::const_iterator &it, std::unordered_map<uint64_t, P<View> >::const_iterator &end) {
273  while (it == end) {
274  switch (selector++) {
275  case 0:
276  it = anti_ipgm_views.begin();
277  end = anti_ipgm_views.end();
278  break;
279  case 1:
280  it = input_less_ipgm_views.begin();
281  end = input_less_ipgm_views.end();
282  break;
283  case 2:
284  it = group_views.begin();
285  end = group_views.end();
286  break;
287  case 3:
288  it = other_views.begin();
289  end = other_views.end();
290  break;
291  case 4:
292  selector = 0;
293  return false;
294  }
295  }
296  return true;
297  }
298 
299 #define FOR_ALL_NON_NTF_VIEWS_BEGIN(g,it) { \
300  uint8_t selector; \
301  std::unordered_map<uint64_t,P<View> >::const_iterator it=g->ipgm_views.begin(); \
302  std::unordered_map<uint64_t,P<View> >::const_iterator end=g->ipgm_views.end(); \
303  for(selector=0;g->non_ntf_views_cond(selector,it,end);++it){
304 
305 #define FOR_ALL_NON_NTF_VIEWS_END } \
306 }
307 
308  View *get_view(uint64_t OID);
309 
310  uint32_t get_upr() const;
311 
312  float get_sln_thr() const;
313  float get_act_thr() const;
314  float get_vis_thr() const;
315 
316  float get_c_sln() const;
317  float get_c_act() const;
318 
319  float get_c_sln_thr() const;
320  float get_c_act_thr() const;
321 
322  void mod_sln_thr(double value);
323  void set_sln_thr(double value);
324  void mod_act_thr(double value);
325  void set_act_thr(double value);
326  void mod_vis_thr(double value);
327  void set_vis_thr(double value);
328  void mod_c_sln(double value);
329  void set_c_sln(double value);
330  void mod_c_act(double value);
331  void set_c_act(double value);
332  void mod_c_sln_thr(double value);
333  void set_c_sln_thr(double value);
334  void mod_c_act_thr(double value);
335  void set_c_act_thr(double value);
336 
337  double update_sln_thr(); // computes and applies decay on sln thr if any.
338  double update_act_thr();
339  double update_vis_thr();
340  double update_c_sln();
341  double update_c_act();
342  double update_c_sln_thr();
343  double update_c_act_thr();
344 
345  float get_sln_chg_thr();
346  float get_sln_chg_prd();
347  float get_act_chg_thr();
348  float get_act_chg_prd();
349 
350  float get_avg_sln();
351  float get_high_sln();
352  float get_low_sln();
353  float get_avg_act();
354  float get_high_act();
355  float get_low_act();
356 
357  float get_high_sln_thr();
358  float get_low_sln_thr();
359  float get_sln_ntf_prd();
360  float get_high_act_thr();
361  float get_low_act_thr();
362  float get_act_ntf_prd();
363  float get_low_res_thr();
364 
365  float get_ntf_new();
366 
367  uint16_t get_ntf_grp_count();
368  Group *get_ntf_grp(uint16_t i); // i starts at 1.
369 
370 // Delegate to views; update stats and notifies.
371  double update_res(View *v);
372  double update_sln(View *v); // applies decay if any.
373  double update_act(View *v);
374 
375 // Target upr, spr, c_sln, c_act, sln_thr, act_thr, vis_thr, c_sln_thr, c_act_thr, sln_chg_thr,
376 // sln_chg_prd, act_chg_thr, act_chg_prd, high_sln_thr, low_sln_thr, sln_ntf_prd, high_act_thr, low_act_thr, act_ntf_prd, low_res_thr, res_ntf_prd, ntf_new,
377 // dcy_per, dcy-tgt, dcy_prd.
378  void mod(uint16_t member_index, float value);
379  void set(uint16_t member_index, float value);
380 
381  void reset_stats(); // called at the begining of an update.
382  void update_stats(); // at the end of an update; may produce notifcations.
383 
384  bool load(View *view, Code *object);
385 
386 // Called at each update period.
387 // - set the final resilience value, if 0, delete.
388 // - set the final saliency.
389 // - set the final activation.
390 // - set the final visibility, cov.
391 // - propagate saliency changes.
392 // - inject next update job for the group.
393 // - inject new signaling jobs if act pgm with no input or act |pgm.
394 // - notify high and low values.
395  void update(uint64_t planned_time);
396 
397  void inject_new_object(View *view);
398  void inject_existing_object(View *view);
399 
401  void inject_group(View *view);
402  void inject_notification(View *view, bool lock);
403  void inject_hlps(std::vector<View *> &views);
405  void inject_reduction_jobs(View *view);
406 
407  void cov();
408 
409  class Hash {
410  public:
411  size_t operator()(Group *g) const {
412  return (size_t)g;
413  }
414  };
415 
416  class Equal {
417  public:
418  bool operator()(const Group *lhs, const Group *rhs) const {
419  return lhs == rhs;
420  }
421  };
422 
423  void delete_view(View *v);
424  void delete_view(std::unordered_map<uint64_t, P<View> >::const_iterator &v);
425 
426  Group *get_secondary_group();
427  void load_secondary_mdl_controller(View *view);
428  void inject_secondary_mdl_controller(View *view);
429 
430  uint64_t get_next_upr_time(uint64_t now) const;
431  uint64_t get_prev_upr_time(uint64_t now) const;
432 };
433 }
434 
435 
436 #include "object.tpl.cpp"
437 
438 #endif
double low_act
Definition: group.h:75
double acc_c_act
Definition: group.h:62
void mod(uint16_t member_index, double value)
Definition: view.cpp:245
uint64_t c_act_thr_changes
Definition: group.h:65
Mod(uint64_t oid, uint16_t member_index, double value)
Definition: group.h:172
bool views_with_inputs_cond(uint8_t &selector, std::unordered_map< uint64_t, P< View > >::const_iterator &it, std::unordered_map< uint64_t, P< View > >::const_iterator &end)
Definition: group.h:248
bool is_c_salient
Definition: group.h:114
Definition: group.h:409
Definition: group.h:181
double former_sln_thr
Definition: group.h:110
double acc_sln_thr
Definition: group.h:54
double acc_act_thr
Definition: group.h:56
bool now(const Context &context, uint16_t &index)
Definition: operator.cpp:55
double low_sln
Definition: group.h:72
double decay_target
Definition: group.h:84
double high_sln
Definition: group.h:71
double decay_percentage_per_period
Definition: group.h:83
#define dll_export
Definition: dll.h:44
const uint16_t member_index
Definition: group.h:165
Definition: object.h:328
int64_t decay_periods_to_go
Definition: group.h:82
uint64_t vis_thr_changes
Definition: group.h:57
std::mutex mutex
Definition: group.h:130
Definition: group.h:108
std::vector< Operation * > pending_operations
Definition: group.h:194
Definition: group.h:152
Definition: group.h:169
Definition: _context.cpp:34
double high_act
Definition: group.h:74
virtual ~Operation()
Definition: group.h:156
bool was_c_salient
Definition: group.h:113
Definition: base.h:47
Definition: group.h:49
uint64_t c_sln_thr_changes
Definition: group.h:63
std::multiset< P< View >, r_code::View::Less > newly_salient_views
Definition: group.h:147
bool is_c_active
Definition: group.h:112
Set(uint64_t oid, uint16_t member_index, double value)
Definition: group.h:184
std::unordered_map< Group *, bool > viewing_groups
Definition: group.h:144
Definition: view.h:47
Definition: group.h:416
std::unordered_map< uint64_t, P< View > > ipgm_views
Definition: group.h:133
double sln_thr_decay
Definition: group.h:81
uint64_t sln_thr_changes
Definition: group.h:53
std::vector< Controller * > new_controllers
Definition: group.h:150
std::unordered_map< uint64_t, P< View > > group_views
Definition: group.h:137
double acc_c_sln_thr
Definition: group.h:64
double avg_act
Definition: group.h:73
double acc_c_sln
Definition: group.h:60
void set(uint16_t member_index, double value)
Definition: view.cpp:263
std::unordered_map< uint64_t, P< View > > other_views
Definition: group.h:138
bool non_ntf_views_cond(uint8_t &selector, std::unordered_map< uint64_t, P< View > >::const_iterator &it, std::unordered_map< uint64_t, P< View > >::const_iterator &end)
Definition: group.h:272
std::unordered_map< uint64_t, P< View > > notification_views
Definition: group.h:136
Definition: object.h:76
uint64_t act_thr_changes
Definition: group.h:55
std::unordered_map< uint64_t, P< View > > input_less_ipgm_views
Definition: group.h:135
bool operator()(const Group *lhs, const Group *rhs) const
Definition: group.h:418
std::unordered_map< uint64_t, P< View > > anti_ipgm_views
Definition: group.h:134
Definition: object.h:100
void execute(Group *g) const
Definition: group.h:185
uint64_t act_updates
Definition: group.h:77
const double value
Definition: group.h:166
GroupState(double former_sln_thr, bool was_c_active, bool is_c_active, bool was_c_salient, bool is_c_salient)
Definition: group.h:115
void execute(Group *g) const
Definition: group.h:173
uint64_t c_act_changes
Definition: group.h:61
Definition: object.h:172
View * get_view(uint64_t OID)
Definition: group.cpp:497
double acc_c_act_thr
Definition: group.h:66
Definition: group.h:161
Definition: object.h:133
uint64_t sln_updates
Definition: group.h:76
Definition: object.h:164
ModSet(uint64_t oid, uint16_t member_index, double value)
Definition: group.h:164
bool all_views_cond(uint8_t &selector, std::unordered_map< uint64_t, P< View > >::const_iterator &it, std::unordered_map< uint64_t, P< View > >::const_iterator &end)
Definition: group.h:202
size_t operator()(Group *g) const
Definition: group.h:411
Operation(uint64_t oid)
Definition: group.h:154
bool was_c_active
Definition: group.h:111
double avg_sln
Definition: group.h:70
double sln_decay
Definition: group.h:80
double acc_vis_thr
Definition: group.h:58
uint64_t c_sln_changes
Definition: group.h:59