-
Notifications
You must be signed in to change notification settings - Fork 1
/
measure-action.py
120 lines (91 loc) · 3.67 KB
/
measure-action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from __future__ import absolute_import, division, print_function
from six.moves import range
import os
import sys
import pandas
from argparse import ArgumentParser
from numpy import cbrt, floor, sqrt
from mpi4py import MPI
from firedrake.petsc import PETSc
from firedrake import COMM_WORLD, ExtrudedMesh, UnitCubeMesh, UnitSquareMesh, assemble
import form
parser = ArgumentParser(description="""Profile assembly""", add_help=False)
parser.add_argument("--mode", action="store", default="spectral",
help="Which TSFC mode to use?")
parser.add_argument("--help", action="store_true",
help="Show help")
args, _ = parser.parse_known_args()
if args.help:
help = parser.format_help()
PETSc.Sys.Print("%s\n" % help)
sys.exit(0)
filepath = os.path.abspath("matfree.csv")
num_matvecs = 40
PETSc.Log.begin()
parloop_event = PETSc.Log.Event("ParLoopExecute")
matmult_event = PETSc.Log.Event("MatMult")
simplex_range = list(range(1, 8))
cube_range = list(range(1, 9))
test_cases = [
# (form.stokes_momentum, False, 0.5),
# (form.elasticity, False, 0.1),
(form.poisson, True, 1),
# (form.mass_gll, True, 2),
# (form.poisson_gll, True, 1),
(form.hyperelasticity, True, 0.1),
(form.curl_curl, True, 0.3),
]
def run(problem, tensor, size_factor, degree):
formname = problem.__name__
cellname = 'cube' if tensor else 'simplex'
PETSc.Sys.Print("%s: %s, degree=%d" % (formname, cellname, degree))
num_cells = COMM_WORLD.size * max(1, 1e7 * size_factor / (degree + 1)**{'spectral': 4, 'coffee': 6}[args.mode])
h = int(floor(cbrt(num_cells / COMM_WORLD.size)))
w = int(floor(sqrt(num_cells / h)))
d = int(round(num_cells / (w * h)))
num_cells = w * d * h
if tensor:
mesh = ExtrudedMesh(UnitSquareMesh(w, d, quadrilateral=True), h)
else:
mesh = UnitCubeMesh(w, d, h)
comm = mesh.comm
J = problem(mesh, int(degree))
# Warmup and allocate
A = assemble(J, mat_type="matfree", form_compiler_parameters={'mode': args.mode})
A.force_evaluation()
Ap = A.petscmat
x, y = Ap.createVecs()
assert x.size == y.size
num_dofs = x.size
Ap.mult(x, y)
stage = PETSc.Log.Stage("%s(%d) %s" % (formname, degree, cellname))
with stage:
assemble(J, mat_type="matfree", form_compiler_parameters={'mode': args.mode}, tensor=A)
A.force_evaluation()
Ap = A.petscmat
for _ in range(num_matvecs):
Ap.mult(x, y)
parloop = parloop_event.getPerfInfo()
matmult = matmult_event.getPerfInfo()
assert parloop["count"] == 2 * num_matvecs
assert matmult["count"] == num_matvecs
parloop_time = comm.allreduce(parloop["time"], op=MPI.SUM) / (comm.size * num_matvecs)
matmult_time = comm.allreduce(matmult["time"], op=MPI.SUM) / (comm.size * num_matvecs)
matfree_overhead = (1 - parloop_time / matmult_time)
PETSc.Sys.Print("Matrix-free action overhead: %.1f%%" % (matfree_overhead * 100,))
if COMM_WORLD.rank == 0:
header = not os.path.exists(filepath)
data = {"num_cells": num_cells,
"num_dofs": num_dofs,
"num_procs": comm.size,
"tsfc_mode": args.mode,
"problem": formname,
"cell_type": cellname,
"degree": degree,
"matmult_time": matmult_time,
"parloop_time": parloop_time}
df = pandas.DataFrame(data, index=[0])
df.to_csv(filepath, index=False, mode='a', header=header)
for problem, tensor, size_factor in test_cases:
for degree in (cube_range if tensor else simplex_range):
run(problem, tensor, size_factor, degree)