SST/macro
app.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_APP_H_INCLUDED
13 #define SSTMAC_SOFTWARE_PROCESS_APP_H_INCLUDED
14 
22 
26 
27 namespace sstmac {
28 namespace sw {
29 
30 class mutex_t {
31  public:
32  std::string
33  to_string() const {
34  return "sstmac mutex";
35  }
36 
37  /** Blocking keys for those threads waiting on the mutex */
38  std::list<key*> waiters;
39  std::list<key*> conditionals;
40  bool locked;
41 
42  mutex_t() : locked(false)
43  {
44  }
45 };
46 
47 typedef std::map<long, mutex_t*> condition_t;
48 
49 /**
50  * The app derived class adds to the thread base class by providing
51  * facilities to allow applications to simulate computation.
52  * Messaging models are supported through an api class,
53  * which are stored by the app
54  */
55 class app :
56  public thread,
58 {
59  public:
60  typedef void (*destructor_fxn)(void*);
61 
62  typedef int (*main_fxn)(int argc, char** argv);
63  typedef int (*empty_main_fxn)();
64 
65  int
66  allocate_tls_key(destructor_fxn fnx);
67 
68  static sprockit::sim_parameters*
69  get_params();
70 
72  sid() const {
73  return id_;
74  }
75 
76  int
77  appnum() const {
78  return id_.app_;
79  }
80 
81  int
82  tasknum() const {
83  return id_.task_;
84  }
85 
86  app*
87  parent_app() const {
88  return const_cast<app*>(this);
89  }
90 
91  static void
92  delete_statics();
93 
94  void
95  sleep(timestamp time);
96 
97  // convenience functions for apps
98  // public to allow C interface
99  void
100  compute(timestamp time);
101 
102  void
103  compute_inst(compute_event* cmsg);
104 
105  void
106  compute_loop(uint64_t,
107  int nflops_per_loop,
108  int nintops_per_loop,
109  int bytes_per_loop);
110 
111  void
112  compute_detailed(long flops, long intops, long bytes);
113 
114  void
115  compute_block_read(long bytes);
116 
117  void
118  compute_block_write(long bytes);
119 
120  void
121  compute_block_memcpy(long bytes);
122 
124  compute_loops_lib();
125 
126  /// Goodbye.
127  virtual ~app();
128 
129  virtual void
130  consume_params(sprockit::sim_parameters* params) = 0;
131 
132  virtual app*
133  clone_type() const = 0;
134 
135  app*
136  clone(software_id newid);
137 
138  //called when killing the app, in case you want to check or clean anything up before destructor
139  virtual void kill();
140 
141  virtual void
142  skeleton_main() = 0;
143 
144  virtual void run();
145 
146  virtual void
147  init_factory_params(sprockit::sim_parameters *params);
148 
149  sprockit::sim_parameters*
150  params() const {
151  return params_;
152  }
153 
154  /**
155  * Let a parent application know about the existence of a subthread
156  * If thread does not have an initialized ID, a unique ID is allocated for the thread
157  * Can be called from a constructor. This method does NOT throw.
158  * @param thr
159  */
160  void add_subthread(thread* thr);
161 
162  /**
163  * Indicate to parent application that subthread is done running.
164  * This puts a null marker for the thread rather than deleting it completely
165  * @param thr
166  */
167  void set_subthread_done(thread* thr);
168 
169  /**
170  * Let a parent application know a subthread has finished.
171  * This completely erases the thread. There will be no record of this thread after calling this function.
172  * @param thr A thread with initialized ID
173  */
174  void remove_subthread(thread* thr);
175 
176  void remove_subthread(long thr_id);
177 
178  /**
179  * @brief get_subthread
180  * @param id
181  * @return
182  */
183  thread* get_subthread(long id);
184 
185  /**
186  * Allocate a unique ID for a mutex variable
187  * @return The unique ID
188  */
189  int allocate_mutex();
190 
191  /**
192  * Allocate a unique ID for a condition variable
193  * @return The unique ID
194  */
195  int allocate_condition();
196 
197  /**
198  * Fetch a mutex object corresponding to their ID
199  * @param id
200  * @return The mutex object corresponding to the ID. Return NULL if no mutex is found.
201  */
202  mutex_t* get_mutex(int id);
203 
204  /**
205  * Fetch a condition object corresponding to the ID
206  * @param id
207  * @return The condition object corresponding to the ID. Return NULL if not condition is found.
208  */
209  condition_t* get_condition(int id);
210 
211  bool erase_condition(int id);
212 
213  bool erase_mutex(int id);
214 
215  virtual void
216  clear_subthread_from_parent_app();
217 
218  protected:
219  friend class thread;
220 
221  app();
222 
223  api*
224  _get_api(const char* name);
225 
226  virtual void init_mem_lib();
227 
228  sprockit::sim_parameters* params_;
230 
231  private:
238 
241 
242  std::map<long, thread*> subthreads_;
243 
244  std::map<int, mutex_t> mutexes_;
245 
246  std::map<int, condition_t> conditions_;
247 
248  std::map<int, destructor_fxn> tls_key_fxns_;
249 
250 };
251 
253 {
254  public:
255  static void
256  register_main_fxn(const char* name, app::main_fxn fxn);
257 
258  void skeleton_main();
259 
260  virtual void
261  consume_params(sprockit::sim_parameters *params);
262 
263  static void
264  delete_statics();
265 
266  app*
267  clone_type() const {
268  return new user_app_cxx_full_main;
269  }
270 
271  struct argv_entry {
272  char** argv;
273  int argc;
274  argv_entry() : argv(0), argc(0) {}
275  };
276 
277  private:
278  void init_argv(argv_entry& entry);
279 
280  static std::map<std::string, app::main_fxn>* main_fxns_;
281  static std::map<app_id, argv_entry> argv_map_;
283 
284 };
285 
287 {
288  public:
289  static void
290  register_main_fxn(const char* name, app::empty_main_fxn fxn);
291 
292  virtual void
293  consume_params(sprockit::sim_parameters *params);
294 
295  app*
296  clone_type() const {
297  return new user_app_cxx_empty_main;
298  }
299 
300  void skeleton_main();
301 
302  private:
303  static std::map<std::string, app::empty_main_fxn>* empty_main_fxns_;
305 
306 };
307 
308 /** utility function for computing stuff */
309 void compute_time(double tsec);
310 
312 
313 }
314 } // end of namespace sstmac.
315 
316 #endif
317 
int(* empty_main_fxn)()
Definition: app.h:63
int tasknum() const
Definition: app.h:82
std::list< key * > conditionals
Definition: app.h:39
std::map< long, thread * > subthreads_
Definition: app.h:242
Input for processor models that use performance counter data.
Definition: compute_event.h:34
software_id sid() const
Definition: app.h:72
app * clone_type() const
Definition: app.h:267
std::map< long, mutex_t * > condition_t
Definition: app.h:47
std::map< int, condition_t > conditions_
Definition: app.h:246
sprockit::sim_parameters * params_
Definition: app.h:228
int(* empty_main_fxn)()
Definition: skeleton.h:9
std::map< int, mutex_t > mutexes_
Definition: app.h:244
lib_compute_loops * compute_loops_
Definition: app.h:235
The app derived class adds to the thread base class by providing facilities to allow applications to ...
Definition: app.h:55
software_id id_
Definition: app.h:229
void compute_time(double tsec)
utility function for computing stuff
sprockit::sim_parameters * params() const
Definition: app.h:150
int next_condition_
Definition: app.h:239
char ** argv
Definition: app.h:272
static std::map< std::string, app::main_fxn > * main_fxns_
Definition: app.h:280
static std::map< std::string, app::empty_main_fxn > * empty_main_fxns_
Definition: app.h:303
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...
lib_compute_memmove * compute_mem_move_
Definition: app.h:234
int argc
Definition: app.h:273
DeclareFactory(blas_kernel)
app * parent_app() const
Definition: app.h:87
int(* main_fxn)(int argc, char **argv)
Definition: app.h:62
std::list< key * > waiters
Blocking keys for those threads waiting on the mutex.
Definition: app.h:38
int appnum() const
Definition: app.h:77
long next_tls_key_
Definition: app.h:237
lib_sleep * sleep_lib_
Definition: app.h:236
app::empty_main_fxn fxn_
Definition: app.h:304
lib_compute_inst * compute_inst_
Definition: app.h:232
void compute(double sec)
void run(opts &oo, sstmac::parallel_runtime *rt, sprockit::sim_parameters *params, sim_stats &stats)
void sleep(double sec)
static std::map< app_id, argv_entry > argv_map_
Definition: app.h:281
Definition: app.h:271
std::string to_string() const
Definition: app.h:33
lib_compute_time * compute_time_
Definition: app.h:233
int next_mutex_
Definition: app.h:240
A wrapper for an appid, taskid pair.
Definition: software_id.h:28
int(* main_fxn)(int, char **)
Definition: skeleton.h:8
argv_entry()
Definition: app.h:274
std::map< int, destructor_fxn > tls_key_fxns_
Definition: app.h:248