Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/target-cpu/fragments/par-main.in
ViewVC logotype

Annotation of /branches/vis15/src/compiler/target-cpu/fragments/par-main.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4797 - (view) (download)

1 : jhr 3924 using namespace @PREFIX@;
2 :    
3 : jhr 3912 //! Main function for standalone parallel C target
4 :     //
5 :     int main (int argc, const char **argv)
6 :     {
7 :     bool timingFlg = false; //! true if timing computation
8 :     uint32_t stepLimit = 0; //! limit on number of execution steps (0 means unlimited)
9 : jhr 4633 std::string printFile = "-"; //! file to direct printed output into
10 : jhr 4797 uint32_t reqNumWorkers; //! requested number of worker threads
11 : jhr 4664 #ifdef DIDEROT_EXEC_SNAPSHOT
12 :     uint32_t snapshotPeriod = 1; //! supersteps per snapshot
13 :     #endif
14 :     uint32_t nSteps = 0; //! number of supersteps taken
15 : jhr 3912
16 :     // create the world
17 : jhr 3916 world *wrld = new (std::nothrow) world();
18 : jhr 3912 if (wrld == nullptr) {
19 : jhr 3916 std::cerr << "unable to create world" << std::endl;
20 : jhr 3912 exit (1);
21 :     }
22 :    
23 : jhr 4797 // get CPU info
24 :     if (wrld->_sched->get_cpu_info (wrld)) {
25 :     std::cerr << "Error creating workers:\n" << wrld->get_errors() << std::endl;
26 :     exit (1);
27 :     }
28 :    
29 : jhr 3912 #ifndef DIDEROT_NO_INPUTS
30 :     // initialize the default values for the inputs
31 : jhr 3931 cmd_line_inputs inputs;
32 : jhr 3912 init_defaults (&inputs);
33 :     #endif
34 :    
35 :     // handle command-line options
36 :     {
37 : jhr 4317 diderot::options<@REALTY@,@INTTY@> *opts = new diderot::options<@REALTY@,@INTTY@> ();
38 : jhr 4797 reqNumWorkers = wrld->_sched->_numHWCores;
39 : jhr 4668 opts->add ("l,limit", "specify limit on number of super-steps (0 means unlimited)",
40 :     &stepLimit, true);
41 :     #ifdef DIDEROT_EXEC_SNAPSHOT
42 :     opts->add ("s,snapshot",
43 :     "specify number of super-steps per snapshot (0 means no snapshots)",
44 :     &snapshotPeriod, true);
45 :     #endif
46 :     opts->add ("print", "specify where to direct printed output", &printFile, true);
47 : jhr 4317 opts->addFlag ("v,verbose", "enable runtime-system messages", &(wrld->_verbose));
48 :     opts->addFlag ("t,timing", "enable execution timing", &timingFlg);
49 : jhr 4797 opts->add ("n,nworkers", "specify number of worker threads", &reqNumWorkers, true);
50 : jhr 3912 #ifndef DIDEROT_NO_INPUTS
51 :     // register options for setting global inputs
52 : jhr 4317 register_inputs (&inputs, opts);
53 : jhr 3912 #endif
54 : jhr 4317 register_outputs (opts);
55 :     opts->process (argc, argv);
56 :     delete opts;
57 : jhr 3912 }
58 :    
59 : jhr 4633 // redirect printing (if necessary)
60 :     if (printFile.compare("-") != 0) {
61 :     wrld->_printTo = new std::ofstream (printFile);
62 :     if (wrld->_printTo->fail()) {
63 :     std::cerr << "Error opening print file" << std::endl;
64 :     exit(1);
65 :     }
66 :     }
67 :    
68 : jhr 3912 // initialize scheduler stuff
69 : jhr 3916 if (wrld->_verbose) {
70 : jhr 3912 std::cerr << "initializing world ..." << std::endl;
71 :     }
72 :     if (wrld->init()) {
73 : jhr 4797 std::cerr << "Error initializing world:\n" << wrld->get_errors() << std::endl;
74 : jhr 3912 exit(1);
75 :     }
76 : jhr 4797 if (wrld->_sched->create_workers (wrld, reqNumWorkers)) {
77 :     std::cerr << "Error creating workers:\n" << wrld->get_errors() << std::endl;
78 :     exit(1);
79 :     }
80 : jhr 3912
81 :     #ifndef DIDEROT_NO_INPUTS
82 :     // initialize the input globals
83 : jhr 3916 if (init_inputs (wrld, &inputs)) {
84 : jhr 4797 std::cerr << "Error initializing inputs:\n" << wrld->get_errors() << std::endl;
85 : jhr 4317 exit(1);
86 : jhr 3912 }
87 :     #endif
88 :    
89 :     // run the generated global initialization code
90 : jhr 3916 if (wrld->_verbose) {
91 : jhr 4407 std::cerr << "initializing globals and creating strands ...\n";
92 : jhr 3912 }
93 : jhr 4418 if (wrld->create_strands()) {
94 : jhr 4063 std::cerr << "Error in global initialization:\n"
95 : jhr 4797 << wrld->get_errors() << std::endl;
96 : jhr 3912 exit(1);
97 :     }
98 :    
99 : jhr 4664 #ifdef DIDEROT_EXEC_SNAPSHOT
100 : jhr 3912
101 : jhr 4664 if (snapshotPeriod > 0) {
102 : jhr 4779 // write initial state as snapshot 0
103 : jhr 4678 write_snapshot (wrld, "-0000");
104 :     // run the program for `snapshotPeriod` steps at a time with a snapshot after each run
105 : jhr 4668 while (true) {
106 :     uint32_t n, limit;
107 :     // determine a step limit for the next run
108 :     if (stepLimit > 0) {
109 :     if (stepLimit <= nSteps) {
110 :     break;
111 :     }
112 :     limit = std::min(stepLimit - nSteps, snapshotPeriod);
113 :     }
114 :     else {
115 :     limit = snapshotPeriod;
116 :     }
117 :     // run the program for upto limit steps
118 :     if ((n = wrld->run (limit)) == 0) {
119 :     break;
120 :     }
121 : jhr 4664 nSteps += n;
122 :     if ((wrld->_errors->errNum > 0) || (wrld->_strands.num_alive() == 0)) {
123 :     break;
124 :     }
125 :     // write a snapshot with the step count as a suffix
126 :     std::string suffix = std::to_string(nSteps);
127 :     if (suffix.length() < 4) {
128 :     suffix = std::string("0000").substr(0, 4 - suffix.length()) + suffix;
129 :     }
130 :     suffix = "-" + suffix;
131 :     write_snapshot (wrld, suffix);
132 :     }
133 :     }
134 :     else {
135 :     nSteps = wrld->run (stepLimit);
136 :     }
137 :    
138 :     #else // !DIDEROT_EXEC_SNAPSHOT
139 :    
140 :     nSteps = wrld->run (stepLimit);
141 :    
142 :     #endif // DIDEROT_EXEC_SNAPSHOT
143 :    
144 : jhr 3916 if (wrld->_errors->errNum > 0) {
145 : jhr 4797 std::cerr << "Error during execution:\n" << wrld->get_errors() << std::endl;
146 : jhr 3912 exit(1);
147 :     }
148 :    
149 : jhr 4753 if ((stepLimit != 0) && (wrld->_strands.num_active() > 0)) {
150 :     #ifdef DIDEROT_STRAND_ARRAY
151 :     if (wrld->_verbose) {
152 :     std::cerr << "Step limit expired; "
153 :     << wrld->_strands.num_active() << " active strands remaining" << std::endl;
154 :     }
155 :     #else
156 :     // step limit expired, so kill remaining strands
157 :     if (wrld->_verbose) {
158 :     std::cerr << "Step limit expired. Killing remaining "
159 :     << wrld->_strands.num_active() << " active strands" << std::endl;
160 :     }
161 :     wrld->kill_all();
162 :     #endif
163 :     }
164 :    
165 : jhr 3916 if (wrld->_verbose) {
166 : jhr 3917 std::cerr << "done: " << nSteps << " steps, in " << wrld->_run_time << " seconds"
167 : jhr 4317 << std::endl;
168 : jhr 3912 }
169 :     else if (timingFlg) {
170 : jhr 4317 std::cout << "usr=" << wrld->_run_time << std::endl;
171 : jhr 3912 }
172 :    
173 :     // output the final strand states
174 : jhr 4259 write_output (wrld);
175 : jhr 3912
176 : jhr 3917 delete wrld;
177 : jhr 3912
178 :     return 0;
179 :    
180 :     } // main

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0