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 5267 - (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 4958 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 5267 std::cerr << "Error: unable to create world" << std::endl;
20 : jhr 3912 exit (1);
21 :     }
22 :    
23 : jhr 5094 // initialize scheduler stuff
24 :     if (wrld->_verbose) {
25 :     std::cerr << "CPU info: " << wrld->_sched->_numHWCores << " cores / "
26 :     << wrld->_sched->_numHWThreads << " threads\n";
27 :     std::cerr << "initializing world ..." << std::endl;
28 :     }
29 :     if (wrld->init()) {
30 : jhr 5267 exit_with_error (wrld, "Error initializing world");
31 : jhr 4797 }
32 :    
33 : jhr 3912 #ifndef DIDEROT_NO_INPUTS
34 :     // initialize the default values for the inputs
35 : jhr 3931 cmd_line_inputs inputs;
36 : jhr 3912 init_defaults (&inputs);
37 :     #endif
38 :    
39 :     // handle command-line options
40 :     {
41 : jhr 4833 diderot::options *opts = new diderot::options ();
42 : jhr 4797 reqNumWorkers = wrld->_sched->_numHWCores;
43 : jhr 4668 opts->add ("l,limit", "specify limit on number of super-steps (0 means unlimited)",
44 :     &stepLimit, true);
45 :     #ifdef DIDEROT_EXEC_SNAPSHOT
46 :     opts->add ("s,snapshot",
47 :     "specify number of super-steps per snapshot (0 means no snapshots)",
48 :     &snapshotPeriod, true);
49 :     #endif
50 :     opts->add ("print", "specify where to direct printed output", &printFile, true);
51 : jhr 4317 opts->addFlag ("v,verbose", "enable runtime-system messages", &(wrld->_verbose));
52 :     opts->addFlag ("t,timing", "enable execution timing", &timingFlg);
53 : jhr 4797 opts->add ("n,nworkers", "specify number of worker threads", &reqNumWorkers, true);
54 : jhr 3912 #ifndef DIDEROT_NO_INPUTS
55 :     // register options for setting global inputs
56 : jhr 4317 register_inputs (&inputs, opts);
57 : jhr 3912 #endif
58 : jhr 4317 register_outputs (opts);
59 :     opts->process (argc, argv);
60 :     delete opts;
61 : jhr 3912 }
62 :    
63 : jhr 4633 // redirect printing (if necessary)
64 :     if (printFile.compare("-") != 0) {
65 :     wrld->_printTo = new std::ofstream (printFile);
66 :     if (wrld->_printTo->fail()) {
67 : jhr 5267 exit_with_error (wrld, "Error opening print file");
68 : jhr 4633 }
69 : jhr 5123 diderot::__details::config_ostream (*wrld->_printTo);
70 : jhr 4633 }
71 : jhr 5116 else {
72 : jhr 5123 diderot::__details::config_ostream (std::cout);
73 : jhr 5116 }
74 : jhr 4633
75 : jhr 4842 wrld->_sched->set_num_workers (reqNumWorkers);
76 :     #ifdef DIDEROT_ENABLE_LOGGING
77 :     // initialize logging
78 :     wrld->_log_file = new diderot::log::file("@LOG_FILE@", wrld->_sched);
79 :     #endif
80 :     if (wrld->_sched->create_workers (wrld)) {
81 : jhr 5267 exit_with_error (wrld, "Error creating workers");
82 : jhr 4797 }
83 : jhr 3912
84 :     #ifndef DIDEROT_NO_INPUTS
85 :     // initialize the input globals
86 : jhr 3916 if (init_inputs (wrld, &inputs)) {
87 : jhr 5267 exit_with_error (wrld, "Error initializing inputs");
88 : jhr 3912 }
89 :     #endif
90 :    
91 :     // run the generated global initialization code
92 : jhr 3916 if (wrld->_verbose) {
93 : jhr 4407 std::cerr << "initializing globals and creating strands ...\n";
94 : jhr 3912 }
95 : jhr 4418 if (wrld->create_strands()) {
96 : jhr 5267 exit_with_error (wrld, "Error in global initialization");
97 : jhr 3912 }
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 5267 if (write_snapshot (wrld, "-0000")) {
104 :     exit_with_error (wrld, "Error generating snapshot");
105 :     }
106 : jhr 4678 // run the program for `snapshotPeriod` steps at a time with a snapshot after each run
107 : jhr 4668 while (true) {
108 :     uint32_t n, limit;
109 :     // determine a step limit for the next run
110 :     if (stepLimit > 0) {
111 :     if (stepLimit <= nSteps) {
112 :     break;
113 :     }
114 :     limit = std::min(stepLimit - nSteps, snapshotPeriod);
115 :     }
116 :     else {
117 :     limit = snapshotPeriod;
118 :     }
119 :     // run the program for upto limit steps
120 :     if ((n = wrld->run (limit)) == 0) {
121 :     break;
122 :     }
123 : jhr 4664 nSteps += n;
124 : jhr 5142 if (wrld->_errors->errNum > 0) {
125 : jhr 4664 break;
126 :     }
127 : jhr 5142 else if (wrld->_strands.num_alive() == 0) {
128 :     wrld->error("no alive strands, so no snapshot at step %d", nSteps);
129 :     break;
130 :     }
131 : jhr 4664 // write a snapshot with the step count as a suffix
132 :     std::string suffix = std::to_string(nSteps);
133 :     if (suffix.length() < 4) {
134 :     suffix = std::string("0000").substr(0, 4 - suffix.length()) + suffix;
135 :     }
136 :     suffix = "-" + suffix;
137 : jhr 5267 if (write_snapshot (wrld, suffix)) {
138 :     exit_with_error (wrld, "Error generating snapshot");
139 :     }
140 : jhr 4664 }
141 :     }
142 :     else {
143 :     nSteps = wrld->run (stepLimit);
144 :     }
145 :    
146 :     #else // !DIDEROT_EXEC_SNAPSHOT
147 :    
148 :     nSteps = wrld->run (stepLimit);
149 :    
150 :     #endif // DIDEROT_EXEC_SNAPSHOT
151 :    
152 : jhr 4856 // shutdown the workers
153 :     wrld->_sched->shutdown (wrld);
154 :    
155 : jhr 3916 if (wrld->_errors->errNum > 0) {
156 : jhr 5267 exit_with_error (wrld, "Error during execution");
157 : jhr 3912 }
158 :    
159 : jhr 4753 if ((stepLimit != 0) && (wrld->_strands.num_active() > 0)) {
160 :     #ifdef DIDEROT_STRAND_ARRAY
161 :     if (wrld->_verbose) {
162 :     std::cerr << "Step limit expired; "
163 :     << wrld->_strands.num_active() << " active strands remaining" << std::endl;
164 :     }
165 :     #else
166 :     // step limit expired, so kill remaining strands
167 :     if (wrld->_verbose) {
168 :     std::cerr << "Step limit expired. Killing remaining "
169 :     << wrld->_strands.num_active() << " active strands" << std::endl;
170 :     }
171 :     wrld->kill_all();
172 :     #endif
173 :     }
174 :    
175 : jhr 3916 if (wrld->_verbose) {
176 : jhr 4990 std::cerr << "done: " << nSteps << " steps, in " << wrld->_run_time << " seconds";
177 :     #ifndef DIDEROT_STRAND_ARRAY
178 :     std::cerr << "; " << wrld->_strands.num_stable() << " stable strands" << std::endl;
179 :     #else
180 :     std::cerr << std::endl;
181 :     #endif
182 : jhr 3912 }
183 :     else if (timingFlg) {
184 : jhr 4317 std::cout << "usr=" << wrld->_run_time << std::endl;
185 : jhr 3912 }
186 :    
187 :     // output the final strand states
188 : jhr 5142 if (wrld->_strands.num_stable() > 0) {
189 : jhr 5267 if (write_output (wrld)) {
190 :     exit_with_error (wrld, "Error generating output");
191 :     }
192 : jhr 5142 }
193 :     else {
194 :     std::cerr << "Error: no stable strands at termination, so no output\n";
195 :     delete wrld;
196 :     return 1;
197 :     }
198 : jhr 3912
199 : jhr 3917 delete wrld;
200 : jhr 3912
201 :     return 0;
202 :    
203 :     } // main

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