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/seq-main.in
ViewVC logotype

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

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