Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Scheduler Class Reference

#include <Scheduler.h>

Inheritance diagram for Scheduler:
Collaboration diagram for Scheduler:

Classes

struct  ActionItem
 

Public Member Functions

 Scheduler ()
 Scheduler. More...
 
virtual ~Scheduler ()
 
virtual void add_action (Action *)
 
void reads (Action *, const char *attr, int idx)
 
void writes (Action *, const char *attr, int idx)
 
virtual void schedule ()
 
void init_actions (double t)
 
void restarting (double t)
 
void run_actions (double t, double dt)
 
void finalize_actions ()
 
void set_alpha (double alpha)
 
void print (const char *fname)
 
char * print (FILE *f, const char *container_name)
 
void printDDG (FILE *f)
 
bool isEmpty ()
 
const char * name ()
 
void set_name (const char *name)
 

Protected Types

typedef vector< ActionItem * > ActionList
 

Protected Member Functions

void buildDDG ()
 

Protected Attributes

std::string scheduler_name
 
ActionList actions
 
ActionList roots
 
ActionList sort
 
double alphaT
 
int scheduled
 
int inited
 

Private Member Functions

void topological_sort ()
 
void sanityCheck ()
 
void print_helper (FILE *f, ActionItem *aitem)
 
void print_toposort (FILE *f)
 
void printActions ()
 

Friends

class SchedulerAction
 
class UserSchedulerAction
 

Detailed Description

Definition at line 29 of file Scheduler.h.

Member Typedef Documentation

typedef vector< ActionItem* > ActionList
protected

Definition at line 54 of file Scheduler.h.

Constructor & Destructor Documentation

Scheduler ( )

Scheduler.

Definition at line 36 of file Scheduler.C.

References scheduler_name.

36  : alphaT(-1), scheduled(0), inited(0)
37 {
38  scheduler_name = "Scheduler";
39 }
double alphaT
Definition: Scheduler.h:82
std::string scheduler_name
Definition: Scheduler.h:52
int scheduled
Definition: Scheduler.h:84
int inited
Definition: Scheduler.h:85
virtual ~Scheduler ( )
inlinevirtual

Definition at line 35 of file Scheduler.h.

35 {}

Member Function Documentation

void add_action ( Action action)
virtual

Reimplemented in UserScheduler.

Definition at line 41 of file Scheduler.C.

References actions, and Action::declare().

Referenced by UserScheduler::add_action(), and main().

42 {
43  ActionItem *aitem = new ActionItem(action);
44  actions.push_back(aitem);
45  // call back action
46  action->declare(*this);
47 }
ActionList actions
Definition: Scheduler.h:78
virtual void declare(Scheduler &)
Definition: Action.C:77

Here is the call graph for this function:

Here is the caller graph for this function:

void buildDDG ( )
protected

Definition at line 79 of file Scheduler.C.

References actions, COM_assertion, Scheduler::ActionItem::hasOutput(), i, Scheduler::ActionItem::input, j, Scheduler::ActionItem::n_read(), Scheduler::ActionItem::output, Scheduler::ActionItem::read_attr, Scheduler::ActionItem::read_idx, and roots.

Referenced by schedule(), and UserScheduler::schedule().

80 {
81 #if 0
82  ActionList pool;
83  pool = actions;
84 
85  while (pool.size()!=0) {
86  // find one leaf with no output or output already fullfiled
87  ActionList::iterator act = pool.end()-1;
88  for (; act>=pool.begin(); act--)
89  {
90  if ((*act)->fullfilled()== 1) {
91 //printf("FOUND LEAF: %s\n", (*act)->name());
92  break;
93  }
94  }
95  if (act < pool.begin()) { // error with loop
96  printf("Loop detected!\n");
97  COM_assertion(false);
98  }
99 
100  ActionItem *item = *act;
101  pool.erase(act); // don't use iterator act afterwards
102 
103  if (item->n_read() == 0) { // this is a tree root
104  roots.push_back(item);
105  }
106  else
107  for (unsigned int i=0; i<item->n_read(); i++) {
108  if (item->input[i] == NULL) {
109  // find matching input actions for item from pool
110  ActionList::iterator a = pool.end()-1;
111  for (; a>=pool.begin(); a--) {
112  int inIdx = (*a)->hasOutput(item->read_attr[i], item->read_idx[i]);
113  if (inIdx != -1) { // set up double link
114 //printf("updated output of %s for %s %d\n", (*a)->name(), item->read_attr[i], item->read_idx[i]);
115  item->input[i] = *a;
116  (*a)->output[inIdx] = item;
117  break; // only do once
118  }
119  }
120  if (a < pool.begin()) { // a read without any matching write
121  printf("Error: can not find matching input for action %s of attr:%s index:%d. \n", (*a)->name(), item->read_attr[i], item->read_idx[i]);
122  }
123  } // end of for i
124  }
125  } // end of while
126 #else
127  // make links
128  ActionList pool;
129  pool = actions;
130 
131  unsigned int idx = 1;
132  while (idx<pool.size()) {
133 
134  ActionItem *item = pool[idx];
135  // search all input
136  for (unsigned int i=0; i<item->n_read(); i++) {
137  for (int j=idx-1; j>=0; j--) {
138  ActionItem *aitem = pool[j];
139  int inIdx = aitem->hasOutput(item->read_attr[i], item->read_idx[i]);
140  if (inIdx != -1) {
141  item->input[i] = aitem;
142  aitem->output[inIdx] = item;
143  }
144  }
145  }
146  idx ++;
147  }
148  // identify root nodes
149  for (idx=0; idx<pool.size(); idx++) {
150  ActionItem *item = pool[idx];
151  int isroot = 1;
152  for (unsigned int i=0; i<item->n_read(); i++)
153  if (item->input[i] != NULL) {
154  isroot = 0;
155  break;
156  }
157  if (isroot) roots.push_back(item);
158  }
159 #endif
160 }
#define COM_assertion(EX)
Error checking utility similar to the assert macro of the C language.
ActionList roots
Definition: Scheduler.h:79
ActionList actions
Definition: Scheduler.h:78
vector< ActionItem * > ActionList
Definition: Scheduler.h:54
blockLoc i
Definition: read.cpp:79
j indices j
Definition: Indexing.h:6

Here is the call graph for this function:

Here is the caller graph for this function:

void finalize_actions ( )

Definition at line 351 of file Scheduler.C.

References Scheduler::ActionItem::action(), COM_assertion_msg, Action::finalize(), scheduled, and sort.

Referenced by SchedulerAction::finalize(), UserSchedulerAction::finalize(), Coupling::finalize(), Agent::finalize(), and main().

352 {
353  COM_assertion_msg(scheduled, "ERROR: Scheduler has not been scheduled when calling finalize_actions.\n");
354  if (sort.size()==0) return;
355  // do at reversed order
356  ActionList::iterator aitem;
357  for (aitem = sort.end()-1; aitem>=sort.begin(); aitem--)
358  {
359  ActionItem *item = *aitem;
360  item->action()->finalize();
361  }
362 }
#define COM_assertion_msg(EX, msg)
int scheduled
Definition: Scheduler.h:84
ActionList sort
Definition: Scheduler.h:80

Here is the call graph for this function:

Here is the caller graph for this function:

void init_actions ( double  t)

Definition at line 324 of file Scheduler.C.

References Scheduler::ActionItem::action(), COM_assertion_msg, Action::init(), inited, scheduled, and sort.

Referenced by SchedulerAction::init(), UserSchedulerAction::init(), Coupling::init(), Agent::init_bcinitaction(), Agent::init_callback(), Agent::init_gmactions(), Agent::init_subscheduler(), and main().

325 {
326  COM_assertion_msg(scheduled, "RROR: Scheduler has not been scheduled.\n");
327  if (inited) return;
328  inited = 1;
329 
330  // do at sorted order
331  ActionList::iterator aitem;
332  for (aitem = sort.begin(); aitem!=sort.end(); aitem++)
333  {
334  ActionItem *item = *aitem;
335  item->action()->init(t);
336  }
337 }
#define COM_assertion_msg(EX, msg)
int scheduled
Definition: Scheduler.h:84
int inited
Definition: Scheduler.h:85
ActionList sort
Definition: Scheduler.h:80

Here is the call graph for this function:

Here is the caller graph for this function:

bool isEmpty ( )
inline

Definition at line 48 of file Scheduler.h.

Referenced by Agent::init_callback(), and Agent::obtain_gm().

48 { return actions.size() == 0; }
ActionList actions
Definition: Scheduler.h:78

Here is the caller graph for this function:

const char* name ( )
inline

Definition at line 49 of file Scheduler.h.

Referenced by print(), and Scheduler::ActionItem::print().

49 { return scheduler_name.c_str(); }
std::string scheduler_name
Definition: Scheduler.h:52

Here is the caller graph for this function:

void print ( const char *  fname)

Definition at line 244 of file Scheduler.C.

References cimg_library::cimg::fclose(), and cimg_library::cimg::fopen().

Referenced by main(), SchedulerAction::print(), UserSchedulerAction::print(), Agent::print(), and printActions().

245 {
246  FILE *f = fopen(fname, "w");
247 
248  fprintf(f, "graph: { title: \"DependenceTree\" \n\
249  display_edge_labels: yes \n\
250  layoutalgorithm: tree \n\
251  scaling: maxspect \n\
252  node.color : green \n\
253  node.textcolor : black \n\
254  node.bordercolor: black \n\
255  node.borderwidth: 1 \n\
256  edge.color : blue \n\
257  edge.arrowsize : 7 \n\
258  edge.thickness : 2 \n\
259  edge.fontname:\"helvO08\" \n\
260  node.label: \"no type\" \n");
261 
262  print(f, "scheduler");
263 
264  fprintf(f, "} \n");
265  fclose(f);
266 }
int fclose(std::FILE *file)
Close a file, and check for possible errors.
Definition: CImg.h:5507
void print(const char *fname)
Definition: Scheduler.C:244
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494

Here is the call graph for this function:

Here is the caller graph for this function:

char * print ( FILE *  f,
const char *  container_name 
)

Definition at line 208 of file Scheduler.C.

References actions, i, name(), print_helper(), and roots.

209 {
210  if (actions.size() == 0) return NULL;
211 
212  std::string sched_name = container_name;
213  sched_name = sched_name + "-" + name();
214  fprintf(f, "graph: { title: \"%s\" label: \"%s\" \n\
215  status: folded \n\
216  display_edge_labels: yes \n\
217  layoutalgorithm: tree \n\
218  scaling: maxspect \n\
219  color : white \n\
220  node.color : lightblue \n\
221  node.textcolor : black \n\
222  node.bordercolor: black \n\
223  node.borderwidth: 1 \n\
224  edge.color : lightblue \n\
225  edge.arrowsize : 7 \n\
226  edge.thickness : 2 \n\
227  edge.fontname:\"helvO08\" \n\
228  node.label: \"no type\" \n", sched_name.c_str(), sched_name.c_str());
229 
230  unsigned int i;
231  for (i=0; i<actions.size(); i++)
232  actions[i]->print_flag = 0;
233 
234  for (i=0; i<roots.size(); i++) {
235  print_helper(f, roots[i]);
236  }
237 
238  fprintf(f, "}\n");
239 
240  return strdup(sched_name.c_str());
241 }
ActionList roots
Definition: Scheduler.h:79
void print_helper(FILE *f, ActionItem *aitem)
Definition: Scheduler.C:194
ActionList actions
Definition: Scheduler.h:78
blockLoc i
Definition: read.cpp:79
const char * name()
Definition: Scheduler.h:49

Here is the call graph for this function:

void print_helper ( FILE *  f,
ActionItem aitem 
)
private

Definition at line 194 of file Scheduler.C.

References Scheduler::ActionItem::action(), i, Scheduler::ActionItem::input, Scheduler::ActionItem::n_read(), Scheduler::ActionItem::n_write(), Scheduler::ActionItem::name(), Scheduler::ActionItem::output, Action::print(), Scheduler::ActionItem::print_flag, Scheduler::ActionItem::read_attr, and Scheduler::ActionItem::read_idx.

Referenced by print().

195 {
196  unsigned int i;
197  if (aitem->print_flag == 1) return;
198  aitem->print_flag = 1;
199  aitem->action()->print(f);
200  for (i=0; i<aitem->n_read(); i++) {
201  if (aitem->input[i])
202  fprintf(f, "edge: { sourcename: \"%s\" targetname: \"%s\" label: \"%s,%d\"}\n", aitem->input[i]->name(), aitem->name(), aitem->read_attr[i], aitem->read_idx[i]);
203  }
204  for (i=0; i<aitem->n_write(); i++)
205  if ( aitem->output[i]) print_helper(f, aitem->output[i]);
206 }
void print_helper(FILE *f, ActionItem *aitem)
Definition: Scheduler.C:194
blockLoc i
Definition: read.cpp:79

Here is the call graph for this function:

Here is the caller graph for this function:

void print_toposort ( FILE *  f)
private

Definition at line 302 of file Scheduler.C.

References i, and sort.

Referenced by SchedulerAction::print_toposort(), and schedule().

303 {
304  for (unsigned int i=0; i<sort.size(); i++) sort[i]->action()->print_toposort(f);
305 }
blockLoc i
Definition: read.cpp:79
ActionList sort
Definition: Scheduler.h:80

Here is the caller graph for this function:

void printActions ( )
private

Definition at line 187 of file Scheduler.C.

References actions, i, and print().

Referenced by schedule().

188 {
189  for (unsigned int i=0; i<actions.size(); i++)
190  actions[i]->print(stdout);
191  printf("\n");
192 }
ActionList actions
Definition: Scheduler.h:78
void print(const char *fname)
Definition: Scheduler.C:244
blockLoc i
Definition: read.cpp:79

Here is the call graph for this function:

Here is the caller graph for this function:

void printDDG ( FILE *  f)
void reads ( Action a,
const char *  attr,
int  idx 
)

Definition at line 49 of file Scheduler.C.

References actions, and Action::name().

Referenced by ActionB::declare(), ActionC::declare(), Action::declare(), ActionD::declare(), ActionE::declare(), and ActionG::declare().

50 {
51  // FIXME: locate this action, doing linear search now
52  ActionList::iterator aitem;
53  for (aitem = actions.begin(); aitem != actions.end(); aitem++)
54  if ((*aitem)->action() == a) break;
55  if (aitem == actions.end()) {
56  printf("ERROR: action '%s' not registered to scheduler. \n", a->name());
57  exit(1);
58  }
59  (*aitem)->read_attr.push_back(strdup(attr)); // copy the string
60  (*aitem)->read_idx.push_back(idx);
61  (*aitem)->input.push_back(NULL);
62 }
virtual char * name()
Definition: Action.h:53
ActionList actions
Definition: Scheduler.h:78

Here is the call graph for this function:

Here is the caller graph for this function:

void restarting ( double  t)
inline

Definition at line 41 of file Scheduler.h.

Referenced by Coupling::init().

41 { inited = 0; } // clear init state for restarting
int inited
Definition: Scheduler.h:85

Here is the caller graph for this function:

void run_actions ( double  t,
double  dt 
)

Definition at line 339 of file Scheduler.C.

References Scheduler::ActionItem::action(), alphaT, COM_assertion_msg, Action::run(), scheduled, and sort.

Referenced by Agent::init_callback(), main(), Agent::obtain_bc(), Agent::obtain_gm(), SchedulerAction::run(), UserSchedulerAction::run(), Coupling::run(), Agent::run_bcinitaction(), and Coupling::run_initactions().

340 {
341  COM_assertion_msg(scheduled, "RROR: Scheduler has not been scheduled when calling run_actions.\n");
342  // do at sorted order
343  ActionList::iterator aitem;
344  for (aitem = sort.begin(); aitem!=sort.end(); aitem++)
345  {
346  ActionItem *item = *aitem;
347  item->action()->run(t, dt, alphaT);
348  }
349 }
double alphaT
Definition: Scheduler.h:82
#define COM_assertion_msg(EX, msg)
int scheduled
Definition: Scheduler.h:84
ActionList sort
Definition: Scheduler.h:80

Here is the call graph for this function:

Here is the caller graph for this function:

void sanityCheck ( )
private

Definition at line 307 of file Scheduler.C.

References actions, COM_assertion_msg, i, Scheduler::ActionItem::input, Scheduler::ActionItem::n_read(), Scheduler::ActionItem::n_write(), and Scheduler::ActionItem::output.

Referenced by schedule().

308 {
309  // make sure all input and output are not null
310  ActionList::iterator aitem;
311  for (aitem = actions.begin(); aitem!=actions.end(); aitem++)
312  {
313  ActionItem *item = *aitem;
314  unsigned int i;
315  COM_assertion_msg(item->n_read() == item->input.size(), "ERROR: Scheduler::sanityCheck failed.\n");
316  for (i=0; i<item->n_read(); i++)
317  COM_assertion_msg(item->input[i] != NULL, "ERROR: Scheduler::sanityCheck failed at 2.\n");
318  COM_assertion_msg(item->n_write() == item->output.size(), "ERROR: Scheduler::sanityCheck failed at 3.\n");
319  for (i=0; i<item->n_write(); i++)
320  COM_assertion_msg(item->output[i] != NULL, "ERROR: Scheduler::sanityCheck failed at 4.\n");
321  }
322 }
#define COM_assertion_msg(EX, msg)
ActionList actions
Definition: Scheduler.h:78
blockLoc i
Definition: read.cpp:79

Here is the call graph for this function:

Here is the caller graph for this function:

void schedule ( )
virtual

Reimplemented in UserScheduler.

Definition at line 162 of file Scheduler.C.

References actions, buildDDG(), COM_assertion_msg, i, MAN_DEBUG, print_toposort(), printActions(), sanityCheck(), scheduled, and topological_sort().

Referenced by main(), and SchedulerAction::schedule().

163 {
164  MAN_DEBUG(1, ("Scheduler::schedule called.\n"));
165  COM_assertion_msg(!scheduled, "ERROR: Scheduler has already been scheduled.\n");
166 
167  // schedule all sub-schedulers
168  for (unsigned int i=0; i<actions.size(); i++)
169  actions[i]->action()->schedule();
170 
171  // debugging
172  printActions();
173 
174  buildDDG();
175 
176  sanityCheck();
177 
179 
180  printf("Topological sort:\n");
181  print_toposort(stdout);
182  printf("\n");
183 
184  scheduled = 1;
185 }
void printActions()
Definition: Scheduler.C:187
#define COM_assertion_msg(EX, msg)
ActionList actions
Definition: Scheduler.h:78
void buildDDG()
Definition: Scheduler.C:79
void sanityCheck()
Definition: Scheduler.C:307
void print_toposort(FILE *f)
Definition: Scheduler.C:302
int scheduled
Definition: Scheduler.h:84
blockLoc i
Definition: read.cpp:79
#define MAN_DEBUG(l, x)
Definition: rocman.h:98
void topological_sort()
Definition: Scheduler.C:268

Here is the call graph for this function:

Here is the caller graph for this function:

void set_alpha ( double  alpha)
inline

Definition at line 44 of file Scheduler.h.

Referenced by Agent::init_callback(), Agent::obtain_bc(), Agent::obtain_gm(), UserSchedulerAction::run(), and Coupling::run_initactions().

44 { alphaT = alpha; }
double alphaT
Definition: Scheduler.h:82

Here is the caller graph for this function:

void set_name ( const char *  name)
inline

Definition at line 50 of file Scheduler.h.

References Action::name().

Referenced by Agent::add_bcaction(), and Agent::Agent().

50 { scheduler_name = name; }
std::string scheduler_name
Definition: Scheduler.h:52
const char * name()
Definition: Scheduler.h:49

Here is the call graph for this function:

Here is the caller graph for this function:

void topological_sort ( )
private

Definition at line 268 of file Scheduler.C.

References Scheduler::ActionItem::action(), actions, i, Scheduler::ActionItem::input, s, and sort.

Referenced by schedule().

269 {
270  ActionList pool;
271  pool = actions;
272  while (1) {
273  ActionList::iterator aitem = pool.begin();
274  for (; aitem!=pool.end(); aitem++) {
275  if ((*aitem)->n_read() == 0) { break; }
276  // check every input
277  int flag = 1;
278  for (unsigned int i=0; i<(*aitem)->n_read(); i++) {
279  ActionItem *in = (*aitem)->input[i];
280  // search in sort
281  ActionList::iterator s;
282  for (s = sort.begin(); s!=sort.end(); ++s)
283  if ((*s)->action() == in->action()) break;
284  if (s == sort.end()) { flag = 0; break; }
285  }
286  if (flag) break;
287  }
288  if (aitem != pool.end()) {
289  sort.push_back(*aitem);
290  pool.erase(aitem);
291  }
292  else
293  break;
294  }
295 
296  if (pool.size() != 0) {
297  printf("ERROR in sorting!\n");
298  exit (1);
299  }
300 }
double s
Definition: blastest.C:80
ActionList actions
Definition: Scheduler.h:78
vector< ActionItem * > ActionList
Definition: Scheduler.h:54
blockLoc i
Definition: read.cpp:79
ActionList sort
Definition: Scheduler.h:80

Here is the call graph for this function:

Here is the caller graph for this function:

void writes ( Action a,
const char *  attr,
int  idx 
)

Definition at line 64 of file Scheduler.C.

References actions, COM_assertion, and Action::name().

Referenced by ActionA::declare(), ActionB::declare(), ActionC::declare(), Action::declare(), and ActionF::declare().

65 {
66  // locate this action
67  ActionList::iterator aitem = actions.begin();
68  for (; aitem != actions.end(); aitem++)
69  if ((*aitem)->action() == a) break;
70  if (aitem == actions.end()) {
71  printf("ERROR: action '%s' not registered to scheduler. \n", a->name());
72  COM_assertion(false);
73  }
74  (*aitem)->write_attr.push_back(strdup(attr)); // copy the string
75  (*aitem)->write_idx.push_back(idx);
76  (*aitem)->output.push_back(NULL);
77 }
#define COM_assertion(EX)
Error checking utility similar to the assert macro of the C language.
virtual char * name()
Definition: Action.h:53
ActionList actions
Definition: Scheduler.h:78

Here is the call graph for this function:

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class SchedulerAction
friend

Definition at line 31 of file Scheduler.h.

friend class UserSchedulerAction
friend

Definition at line 32 of file Scheduler.h.

Member Data Documentation

double alphaT
protected

Definition at line 82 of file Scheduler.h.

Referenced by run_actions().

int inited
protected

Definition at line 85 of file Scheduler.h.

Referenced by init_actions().

ActionList roots
protected

Definition at line 79 of file Scheduler.h.

Referenced by UserScheduler::add_action(), buildDDG(), and print().

int scheduled
protected
std::string scheduler_name
protected

Definition at line 52 of file Scheduler.h.

Referenced by Scheduler().


The documentation for this class was generated from the following files: