Debian Codegraph @

python/demo/undocumented/timing/demo_timing.py

raw
1 # Copyright (C) 2015 Jan Blechta
2 #
3 # This file is part of DOLFIN.
4 #
5 # DOLFIN is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # DOLFIN is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18 from dolfin import *
19
20 # Let's solve some variational problem to get non-trivial timings
21 mesh = UnitSquareMesh(32, 32)
22 V = FunctionSpace(mesh, "Lagrange", 1)
23 bc = DirichletBC(V, 0.0, lambda x: near(x[0], 0.0) or near(x[0], 1.0))
24 u, v = TrialFunction(V), TestFunction(V)
25 f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)
26 g = Expression("sin(5*x[0])", degree=2)
27 a = inner(grad(u), grad(v))*dx
28 L = f*v*dx + g*v*ds
29 u = Function(V)
30 solve(a == L, u, bc)
31
32 # List timings; average across processes in parallel
33 list_timings(TimingClear.keep, [TimingType.wall, TimingType.system])
34
35 # Get Table object with timings
36 t = timings(TimingClear.keep,
37 [TimingType.wall, TimingType.user, TimingType.system])
38
39 # Use different MPI reductions
40 t_sum = MPI.sum(MPI.comm_world, t)
41 t_min = MPI.min(MPI.comm_world, t)
42 t_max = MPI.max(MPI.comm_world, t)
43 t_avg = MPI.avg(MPI.comm_world, t)
44
45 # Print aggregate timings to screen
46 print('\n'+t_sum.str(True))
47 print('\n'+t_min.str(True))
48 print('\n'+t_max.str(True))
49 print('\n'+t_avg.str(True))
50
51 # Store to XML file on rank 0
52 if MPI.rank(MPI.comm_world) == 0:
53 f = File(MPI.comm_self, "timings_aggregate.xml")
54 f << t_sum
55 f << t_min
56 f << t_max
57 f << t_avg
58
59 # Store timings of each rank separately
60 f = File(MPI.comm_self, "timings_rank_%d.xml"
61 % MPI.rank(MPI.comm_world))
62 f << t
63
64 # Helper function for storing rank-wise average, min and max
65 dump_timings_to_xml("timings_avg_min_max.xml", TimingClear.clear)