Skip to content

Commit 46c5941

Browse files
authored
Follow changes from #3959 (#362)
The timers labels changed
1 parent 304b64e commit 46c5941

2 files changed

Lines changed: 111 additions & 87 deletions

File tree

bindings/SofaRuntime/src/SofaPython3/SofaRuntime/Timer/Submodule_Timer.cpp

Lines changed: 106 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ py::dict getRecords(const std::string & id) {
3838
using sofa::helper::system::thread::CTime;
3939

4040
static auto timer_freq = CTime::getTicksPerSec();
41-
auto getTime = [](ctime_t t) {
42-
return 1000.0 * t / timer_freq;
41+
auto getTime = [](ctime_t t)
42+
{
43+
return 1000 * t / timer_freq;
4344
};
4445

4546
const auto records = AdvancedTimer::getRecords(id);
@@ -49,97 +50,118 @@ py::dict getRecords(const std::string & id) {
4950
tokens.push(token);
5051
ctime_t t0;
5152

52-
for (const auto & r : records) {
53-
switch (r.type) {
54-
case Record::RNONE:
55-
break;
56-
case Record::RBEGIN: // Timer begins
57-
token = tokens.top();
58-
if (token.contains(r.label.c_str())) {
59-
if (py::list::check_(token[r.label.c_str()])) {
60-
token_temp = py::dict();
61-
py::list(token[r.label.c_str()]).append(token_temp);
62-
token = token_temp;
63-
} else if (py::dict::check_(token[r.label.c_str()])) {
64-
token_temp = token[r.label.c_str()];
65-
token[r.label.c_str()] = py::list();
66-
py::list(token[r.label.c_str()]).append(token_temp);
67-
token_temp = py::dict();
68-
py::list(token[r.label.c_str()]).append(token_temp);
69-
token = token_temp;
70-
} else {
71-
msg_error("Timer::getRecords") << "Got an unexpected token of type '" << std::string(py::str(token.get_type())) << "'.";
72-
break;
73-
}
74-
} else {
75-
token[r.label.c_str()] = py::dict();
76-
token = token[r.label.c_str()];
53+
for (const auto& r : records)
54+
{
55+
switch (r.type)
56+
{
57+
case Record::RNONE:
58+
break;
59+
case Record::RBEGIN: // Timer begins
60+
token = tokens.top();
61+
if (token.contains(r.label.c_str()))
62+
{
63+
if (py::list::check_(token[r.label.c_str()]))
64+
{
65+
token_temp = py::dict();
66+
py::list(token[r.label.c_str()]).append(token_temp);
67+
token = token_temp;
7768
}
78-
t0 = r.time;
79-
token["start_time"] = getTime(r.time - t0);
80-
tokens.push(token);
81-
break;
82-
case Record::REND: // Timer ends
83-
token = tokens.top();
84-
token["end_time"] = getTime(r.time - t0);
85-
token["total_time"] = getTime(r.time - t0) - py::cast<float>(token["start_time"]);
86-
tokens.pop();
87-
break;
88-
case Record::RSTEP_BEGIN: // Step begins
89-
token = tokens.top();
90-
if (token.contains(r.label.c_str())) {
91-
if (py::list::check_(token[r.label.c_str()])) {
92-
token_temp = py::dict();
93-
py::list(token[r.label.c_str()]).append(token_temp);
94-
token = token_temp;
95-
} else if (py::dict::check_(token[r.label.c_str()])) {
96-
token_temp = token[r.label.c_str()];
97-
token[r.label.c_str()] = py::list();
98-
py::list(token[r.label.c_str()]).append(token_temp);
99-
token_temp = py::dict();
100-
py::list(token[r.label.c_str()]).append(token_temp);
101-
token = token_temp;
102-
} else {
103-
msg_error("Timer::getRecords") << "Got an unexpected token of type '" << std::string(py::str(token.get_type())) << "'.";
104-
break;
105-
}
106-
} else {
107-
token[r.label.c_str()] = py::dict();
108-
token = token[r.label.c_str()];
69+
else if (py::dict::check_(token[r.label.c_str()]))
70+
{
71+
token_temp = token[r.label.c_str()];
72+
token[r.label.c_str()] = py::list();
73+
py::list(token[r.label.c_str()]).append(token_temp);
74+
token_temp = py::dict();
75+
py::list(token[r.label.c_str()]).append(token_temp);
76+
token = token_temp;
10977
}
110-
token["start_time"] = getTime(r.time - t0);
111-
tokens.push(token);
112-
break;
113-
case Record::RSTEP_END: // Step ends
114-
token = tokens.top();
115-
token["end_time"] = getTime(r.time - t0);
116-
token["total_time"] = getTime(r.time - t0) - py::cast<float>(token["start_time"]);
117-
tokens.pop();
118-
break;
119-
case Record::RVAL_SET: // Sets a value
120-
token = tokens.top();
121-
token[r.label.c_str()] = r.val;
122-
break;
123-
case Record::RVAL_ADD: // Sets a value
124-
token = tokens.top();
125-
token[r.label.c_str()] = r.val;
126-
break;
127-
default:
128-
token = tokens.top();
129-
token[r.label.c_str()] = py::list();
78+
else
79+
{
80+
msg_error("Timer::getRecords") << "Got an unexpected token of type '" << std::string(py::str(token.get_type())) << "'.";
81+
break;
82+
}
83+
}
84+
else
85+
{
86+
token[r.label.c_str()] = py::dict();
13087
token = token[r.label.c_str()];
131-
token["start_time"] = r.time;
132-
break;
88+
}
89+
t0 = r.time;
90+
token["start_time"] = getTime(r.time - t0);
91+
tokens.push(token);
92+
break;
93+
case Record::REND: // Timer ends
94+
token = tokens.top();
95+
token["end_time"] = getTime(r.time - t0);
96+
token["total_time"] = getTime(r.time - t0) - py::cast<float>(token["start_time"]);
97+
tokens.pop();
98+
break;
99+
case Record::RSTEP_BEGIN: // Step begins
100+
token = tokens.top();
101+
if (token.contains(r.label.c_str()))
102+
{
103+
if (py::list::check_(token[r.label.c_str()]))
104+
{
105+
token_temp = py::dict();
106+
py::list(token[r.label.c_str()]).append(token_temp);
107+
token = token_temp;
108+
}
109+
else if (py::dict::check_(token[r.label.c_str()]))
110+
{
111+
token_temp = token[r.label.c_str()];
112+
token[r.label.c_str()] = py::list();
113+
py::list(token[r.label.c_str()]).append(token_temp);
114+
token_temp = py::dict();
115+
py::list(token[r.label.c_str()]).append(token_temp);
116+
token = token_temp;
117+
}
118+
else
119+
{
120+
msg_error("Timer::getRecords") << "Got an unexpected token of type '" << std::string(py::str(token.get_type())) << "'.";
121+
break;
122+
}
123+
}
124+
else
125+
{
126+
token[r.label.c_str()] = py::dict();
127+
token = token[r.label.c_str()];
128+
}
129+
token["start_time"] = getTime(r.time - t0);
130+
tokens.push(token);
131+
break;
132+
case Record::RSTEP_END: // Step ends
133+
token = tokens.top();
134+
token["end_time"] = getTime(r.time - t0);
135+
token["total_time"] = getTime(r.time - t0) - py::cast<float>(token["start_time"]);
136+
tokens.pop();
137+
break;
138+
case Record::RVAL_SET: // Sets a value
139+
token = tokens.top();
140+
token[r.label.c_str()] = r.val;
141+
break;
142+
case Record::RVAL_ADD: // Sets a value
143+
token = tokens.top();
144+
token[r.label.c_str()] = r.val;
145+
break;
146+
default:
147+
token = tokens.top();
148+
token[r.label.c_str()] = py::list();
149+
token = token[r.label.c_str()];
150+
token["start_time"] = r.time;
151+
break;
133152
}
134153
}
135154

136155
// There should be two remaining records: Top level "record" + "timer starts". The "timer starts" record remains in
137156
// the stack since we normally get the records before the timer ends (ending the timer in Sofa destroys the
138157
// records...)
139-
if (tokens.size() == 2) {
158+
if (tokens.size() == 2)
159+
{
140160
token = tokens.top();
141161
tokens.pop();
142-
} else if (tokens.size() == 1) {
162+
}
163+
else if (tokens.size() == 1)
164+
{
143165
// This should not happen unless we successfully got the timer records AFTER the timer has ends, which would mean
144166
// that Sofa's advanced timer has improved, let not warn the user for that.
145167
token = tokens.top();
@@ -150,7 +172,9 @@ py::dict getRecords(const std::string & id) {
150172

151173
// The stack should be empty by now
152174
if (!tokens.empty())
175+
{
153176
msg_error("Timer::getRecords") << "Records stack leaked.";
177+
}
154178

155179
return token;
156180
}

examples/advanced_timer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def onAnimateEndEvent(self, event):
2525
else:
2626
records = Timer.getRecords("cg_timer")
2727

28-
step_time = records['AnimateVisitor']['Mechanical (meca)']['total_time']
28+
step_time = records['solve']['Mechanical (meca)']['total_time']
2929
print(f"Step took {step_time:.2f} ms")
3030

31-
nb_iterations = records['AnimateVisitor']['Mechanical (meca)']['StaticSolver::Solve']['nb_iterations']
31+
nb_iterations = records['solve']['Mechanical (meca)']['StaticSolver::Solve']['nb_iterations']
3232
for i in range(int(nb_iterations)):
33-
total_time = records['AnimateVisitor']['Mechanical (meca)']['StaticSolver::Solve']['NewtonStep'][i]['total_time']
34-
CG_iterations = records['AnimateVisitor']['Mechanical (meca)']['StaticSolver::Solve']['NewtonStep'][i]['MBKSolve']['CG iterations']
33+
total_time = records['solve']['Mechanical (meca)']['StaticSolver::Solve']['NewtonStep'][i]['total_time']
34+
CG_iterations = records['solve']['Mechanical (meca)']['StaticSolver::Solve']['NewtonStep'][i]['MBKSolve']['CG iterations']
3535
print(f" Newton iteration #{i} took {total_time:.2f} ms using {int(CG_iterations)} CG iterations")
3636

3737
if not self.use_sofa_profiler_timer:
@@ -66,7 +66,7 @@ def createScene(root):
6666
root.addObject( TimerController() )
6767

6868
# Create a grid topology of 10x10x60 centered on (0,0,0)
69-
root.addObject('RegularGridTopology', name='grid', min=[-5, -5, -30], max=[5, 5, 30], n=[11, 11, 61])
69+
root.addObject('RegularGridTopology', name='grid', min=[-5, -5, -30], max=[5, 5, 30], n=[6, 6, 16])
7070

7171
# Create our mechanical node
7272
root.addChild("meca")

0 commit comments

Comments
 (0)