SST/macro
operating_system.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SSTMAC_SOFTWARE_PROCESS_operatingsystem_H_INCLUDED
13 #define SSTMAC_SOFTWARE_PROCESS_operatingsystem_H_INCLUDED
14 
15 
23 
28 
29 
35 
37 
38 #include <sprockit/unordered.h>
39 #include <sprockit/debug.h>
40 #include <stack>
41 #include <queue>
42 
44 
45 namespace sstmac {
46 namespace sw {
47 
49  public event_subscheduler
50 {
51  friend class service;
52  friend class thread;
53 
54  public:
57  std::list<thread*> to_delete;
62  };
63 
64  virtual ~operating_system();
65 
66  static operating_system*
67  construct(sprockit::sim_parameters* params);
68 
69  static inline os_thread_context&
71  if (cxa_finalizing_){
72  abort();
73  }
74  #if SSTMAC_USE_MULTITHREAD
76  return os_thread_contexts_[thr];
77  #else
78  return os_thread_context_;
79  #endif
80  }
81 
82  virtual void
83  init_factory_params(sprockit::sim_parameters* params);
84 
85  virtual void
86  finalize_init();
87 
88  static void
90 
91  static void
92  switch_to_context(int aid, int tid);
93 
94  static operating_system*
95  current_os();
96 
97  static app_id
98  current_aid();
99 
100  static task_id
101  current_tid();
102 
103  static library*
104  current_library(const std::string& name);
105 
106  static node_id
107  current_node_id();
108 
109  static node_id
110  remote_node(int tid);
111 
112  /**
113  * @brief execute Execute a compute function.
114  * This function MUST begin on a user-space thread
115  * since it may block and context switch until completion.
116  * To invoke compute operations for the main DES thread,
117  * use execute_kernel
118  * @param func The function to perform
119  * @param data Event carrying all the data describing the compute
120  * @param cat An optional category labeling the type of
121  * operation
122  */
123  void
126 
127  /**
128  * @brief execute Execute a communication function.
129  * This function MUST begin on a user-space thread
130  * since it may block and context switch until completion.
131  * To invoke compute operations for the main DES thread,
132  * use execute_kernel
133  * @param data Event carrying all the data describing the compute
134  */
135  void
137  message* data){
138  execute_kernel(func, data);
139  }
140 
141  /**
142  * @brief execute Execute a compute function.
143  * This function takes place in "kernel" land
144  * and will never block and context switch.
145  * This function can therefore run on the main DES thread
146  * @param func The function to perform
147  * @param data Event carrying all the data describing the compute
148  */
149  void
151  message* data);
152 
153  /**
154  * @brief execute Execute a communication function.
155  * This function takes place in "kernel" land
156  * and will never block and context switch.
157  * This function can therefore run on the main DES thread
158  * @param func The function to perform
159  * @param data Event carrying all the data describing the compute
160  * @param cb The callback to invoke when the kernel is complete
161  */
162  void
164  event* data,
165  callback* cb);
166  /**
167  * @brief execute Enqueue an operation to perform
168  * This function takes place in "kernel" land
169  * and will never block and context switch.
170  * This function can therefore run on the main DES thread.
171  * The function must run asynchronously and immediately return
172  * with no virtual time advancing.
173  * @param func The function to perform
174  * @param data Event carrying all the data describing the compute
175  */
176  void
178  event* data);
179 
180  static void
181  stack_check();
182 
183  timestamp
184  block(key* req);
185 
186  timestamp
187  unblock(key* req);
188 
189  void
190  start_thread(thread* t);
191 
192  void
193  join_thread(thread* t);
194 
195  void
196  complete_thread(bool succ);
197 
198  void
199  register_lib(void* owner, library* lib);
200 
201  void
202  unregister_all_libs(void* owner);
203 
204  library*
205  lib(const std::string& name) const;
206 
207  void
208  set_ncores(int ncores, int nsocket);
209 
210  void
212 
213  void
214  add_application(app* a);
215 
216  void
217  start_app(app* a);
218 
219  void
220  handle_event(event* ev);
221 
222  std::list<app*>
223  app_ptrs(app_id aid);
224 
225  app*
226  app_ptr(software_id sid);
227 
229  current_context() const {
230  return threadstack_.top();
231  }
232 
233  static void
236  }
237 
238  void
239  print_libs(std::ostream& os = std::cout) const;
240 
241  long
242  current_threadid() const;
243 
244  void
246  node_ = n;
247  }
248 
249  hw::node*
250  node() const {
251  return node_;
252  }
253 
254  node_id
255  addr() const {
256  return my_addr_;
257  }
258 
259  void
261  my_addr_ = addr;
262  init_loc_id(event_loc_id(addr));
263  }
264 
265  void
266  start_api_call();
267 
268  void
269  schedule_timeout(timestamp delay, key* k);
270 
271  void
272  free_thread_stack(void* stack);
273 
274  static size_t
276  return stacksize_;
277  }
278 
279  static thread*
280  current_thread();
281 
282  static void
283  simulation_done();
284 
285  sprockit::sim_parameters*
286  params() const {
287  return params_;
288  }
289 
290  void
291  sleep(timestamp t);
292 
293  void
294  compute(timestamp t);
295 
296  void kill_node();
297 
298  private:
300 
301  void
302  add_thread(thread* t);
303 
304  void
306 
307  void
308  init_threading();
309 
310  void
311  init_services();
312 
315 
316  void
317  register_lib(library* lib);
318 
319  void
320  unregister_lib(library* lib);
321 
322  void
323  local_shutdown();
324 
325  private:
327  spkt_unordered_map<std::string, library*> libs_;
328  spkt_unordered_map<library*, int> lib_refcounts_;
329  spkt_unordered_map<void*, std::list<library*> > libs_by_owner_;
330  spkt_unordered_set<std::string> deleted_libs_;
331 
333 
334  std::list<thread*> threads_;
335 
336  std::vector<std::string> startup_libs_;
337 
338  std::list<api*> services_;
339 
340  std::stack<thread_data_t> threadstack_;
341 
343 
345 
346  spkt_unordered_map<task_id, long> task_to_thread_;
347 
348  std::queue<thread*> to_awake_; // from thread join
349 
350  /// The caller context (main DES thread). We go back
351  /// to this context on every context switch.
353 
354  sprockit::sim_parameters* params_;
355 
357 
359 
361 
363 
364 #if SSTMAC_USE_MULTITHREAD
365  static std::vector<operating_system::os_thread_context> os_thread_contexts_;
366 #else
368 #endif
369 
370 
371 #if SSTMAC_SANITY_CHECK
372  std::set<key*> valid_keys_;
373 #endif
374 
375  private:
376  static size_t stacksize_;
377  static bool cxa_finalizing_;
379 
380 };
381 
382 }
383 } //end of namespace sstmac
384 #endif
void execute_kernel(ami::COMM_FUNC func, message *data)
execute Execute a compute function.
void print_libs(std::ostream &os=std::cout) const
A class describing an event.
Definition: sst_message.h:42
sprockit::sim_parameters * params() const
virtual void init_factory_params(sprockit::sim_parameters *params)
thread_data_t current_context() const
static os_thread_context & static_os_thread_context()
app * app_ptr(software_id sid)
SERVICE_FUNC
Functions that do not block and should return asynchronously.
Definition: ami.h:45
COMP_FUNC
Functions that block and must complete before returning.
Definition: ami.h:38
spkt_unordered_map< library *, int > lib_refcounts_
os_thread_context & current_os_thread_context()
sprockit::sim_parameters * params_
DeclareDebugSlot(os)
static operating_system::os_thread_context os_thread_context_
spkt_unordered_map< void *, std::list< library * > > libs_by_owner_
std::stack< thread_data_t > threadstack_
void set_node(sstmac::hw::node *n)
A management type to handle dividing mmap-ed memory for use as ucontext stack(s). ...
Definition: stack_alloc.h:30
void init_loc_id(event_loc_id id)
Definition: event_handler.h:90
void start_thread(thread *t)
void async_kernel(ami::SERVICE_FUNC func, event *data)
execute Enqueue an operation to perform This function takes place in "kernel" land and will never blo...
std::list< app * > app_ptrs(app_id aid)
The app derived class adds to the thread base class by providing facilities to allow applications to ...
Definition: app.h:55
int task_id
Definition: task_id.h:20
std::list< thread * > threads_
void free_thread_stack(void *stack)
COMM_FUNC
Definition: ami.h:33
compute_scheduler * compute_sched_
spkt_unordered_set< std::string > deleted_libs_
void register_lib(void *owner, library *lib)
void execute(ami::COMP_FUNC, event *data, key::category cat=key::general)
execute Execute a compute function.
static node_id remote_node(int tid)
static category general
Definition: key.h:66
A basic container for time (subject to future transplant).
Definition: timestamp.h:29
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
timestamp unblock(key *req)
void unregister_lib(library *lib)
static void switch_to_context(int aid, int tid)
timestamp block(key *req)
std::vector< std::string > startup_libs_
static library * current_library(const std::string &name)
static operating_system * construct(sprockit::sim_parameters *params)
static os_thread_context cxa_finalize_context_
A base type and default (empty) implementation of a handle to block and unblock processes.
Definition: key.h:30
void switch_to_thread(thread_data_t tothread)
std::queue< thread * > to_awake_
void handle_event(event *ev)
static node_id current_node_id()
The interface for something that can schedule messages.
library * lib(const std::string &name) const
static graph_viz * call_graph_
threading_interface * des_context_
The caller context (main DES thread).
spkt_unordered_map< task_id, long > task_to_thread_
endpoint_id node_id
Definition: node_address.h:20
void set_ncores(int ncores, int nsocket)
void set_event_parent(event_scheduler *man)
Set the eventmanager for this scheduler.
std::pair< threading_interface *, thread * > thread_data_t
Definition: thread_data.h:14
void schedule_timeout(timestamp delay, key *k)
A wrapper for an appid, taskid pair.
Definition: software_id.h:28
static int current_physical_thread_id()
Definition: thread_info.h:18
int app_id
Definition: app_id.h:20
void complete_thread(bool succ)
void compute(timestamp t)
void unregister_all_libs(void *owner)
spkt_unordered_map< std::string, library * > libs_
void execute(ami::COMM_FUNC func, message *data)
execute Execute a communication function.