Debian Codegraph @

brian2/devices/cpp_standalone/templates/network.cpp

raw
1 {% macro cpp_file() %}
2
3 #include "network.h"
4 #include<stdlib.h>
5 #include<iostream>
6 #include <ctime>
7 #include<utility>
8 #include<chrono>
9
10 {{ openmp_pragma('include') }}
11
12 #define Clock_epsilon 1e-14
13
14 double Network::_last_run_time = 0.0;
15 double Network::_last_run_completed_fraction = 0.0;
16 bool Network::_globally_stopped = false;
17 bool Network::_globally_running = false;
18
19 Network::Network()
20 {
21 t = 0.0;
22 }
23
24 void Network::clear()
25 {
26 objects.clear();
27 }
28
29 void Network::add(BaseClock* clock, codeobj_func func)
30 {
31 #if defined(_MSC_VER) && (_MSC_VER>=1700)
32 objects.push_back(std::make_pair(std::move(clock), std::move(func)));
33 #else
34 objects.push_back(std::make_pair(clock, func));
35 #endif
36 }
37
38 void Network::run(const double duration, void (*report_func)(const double, const double, const double, const double), const double report_period)
39 {
40 std::chrono::time_point<std::chrono::high_resolution_clock> start, current;
41 const double t_start = t;
42 const double t_end = t + duration;
43 double next_report_time = report_period;
44 // compute the set of clocks
45 compute_clocks();
46 // set interval for all clocks
47
48 for(std::set<BaseClock*>::iterator i=clocks.begin(); i!=clocks.end(); i++)
49 (*i)->set_interval(t, t_end);
50
51 start = std::chrono::high_resolution_clock::now();
52 if (report_func)
53 {
54 report_func(0.0, 0.0, t_start, duration);
55 }
56
57 BaseClock* clock = next_clocks();
58 double elapsed_realtime;
59 bool did_break_early = false;
60
61 Network::_globally_running = true;
62 Network::_globally_stopped = false;
63 while(clock && clock->running() && !Network::_globally_stopped)
64 {
65 t = clock->t[0];
66
67 for(size_t i=0; i<objects.size(); i++)
68 {
69 if (report_func)
70 {
71 current = std::chrono::high_resolution_clock::now();
72 const double elapsed = std::chrono::duration<double>(current - start).count();
73 if (elapsed > next_report_time)
74 {
75 report_func(elapsed, (clock->t[0]-t_start)/duration, t_start, duration);
76 next_report_time += report_period;
77 }
78 }
79 BaseClock *obj_clock = objects[i].first;
80 // Only execute the object if it uses the right clock for this step
81 if (curclocks.find(obj_clock) != curclocks.end())
82 {
83 codeobj_func func = objects[i].second;
84 if (func) // code objects can be NULL in cases where we store just the clock
85 func();
86 }
87 }
88 for(std::set<BaseClock*>::iterator i=curclocks.begin(); i!=curclocks.end(); i++)
89 (*i)->tick();
90 clock = next_clocks();
91
92 {% if maximum_run_time is not none %}
93 current = std::chrono::high_resolution_clock::now();
94 elapsed_realtime = std::chrono::duration<double>(current - start).count();
95 if(elapsed_realtime>{{maximum_run_time}})
96 {
97 did_break_early = true;
98 break;
99 }
100 {% endif %}
101
102 }
103 Network::_globally_running = false;
104 current = std::chrono::high_resolution_clock::now();
105 elapsed_realtime = std::chrono::duration<double>(current - start).count();
106
107 if(!did_break_early && !Network::_globally_stopped)
108 t = t_end;
109 else
110 t = clock->t[0];
111
112 _last_run_time = elapsed_realtime;
113 if(duration>0)
114 {
115 _last_run_completed_fraction = (t-t_start)/duration;
116 } else {
117 _last_run_completed_fraction = 1.0;
118 }
119 if (report_func)
120 {
121 report_func(elapsed_realtime, _last_run_completed_fraction, t_start, duration);
122 }
123 }
124
125 void Network::compute_clocks()
126 {
127 clocks.clear();
128 for(int i=0; i<objects.size(); i++)
129 {
130 BaseClock *clock = objects[i].first;
131 clocks.insert(clock);
132 }
133 }
134
135 BaseClock* Network::next_clocks()
136 {
137 if (clocks.empty())
138 return NULL;
139 // find minclock, clock with smallest t value
140 BaseClock *minclock = *clocks.begin();
141
142 for(std::set<BaseClock*>::iterator i=clocks.begin(); i!=clocks.end(); i++)
143 {
144 BaseClock *clock = *i;
145 if(clock->t[0]<minclock->t[0])
146 minclock = clock;
147 }
148 // find set of equal clocks
149 curclocks.clear();
150
151 double t = minclock->t[0];
152 for(std::set<BaseClock*>::iterator i=clocks.begin(); i!=clocks.end(); i++)
153 {
154 BaseClock *clock = *i;
155 double s = clock->t[0];
156 if(s==t || fabs(s-t)<=Clock_epsilon)
157 curclocks.insert(clock);
158 }
159 return minclock;
160 }
161
162 {% endmacro %}
163
164 {% macro h_file() %}
165
166 #ifndef _BRIAN_NETWORK_H
167 #define _BRIAN_NETWORK_H
168
169 #include<vector>
170 #include<utility>
171 #include<set>
172 #include "brianlib/clocks.h"
173
174 typedef void (*codeobj_func)();
175
176 class Network
177 {
178 std::set<BaseClock*> clocks, curclocks;
179 void compute_clocks();
180 BaseClock* next_clocks();
181 public:
182 std::vector< std::pair< BaseClock*, codeobj_func > > objects;
183 double t;
184 static double _last_run_time;
185 static double _last_run_completed_fraction;
186 static bool _globally_stopped;
187 static bool _globally_running;
188
189 Network();
190 void clear();
191 void add(BaseClock *clock, codeobj_func func);
192 void run(const double duration, void (*report_func)(const double, const double, const double, const double), const double report_period);
193 };
194
195 #endif
196
197 {% endmacro %}