/usr/share/pyshared/ffc/quadrature/quadratureoptimization.py is in python-ffc 1.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | # Copyright (C) 2010 Kristian B. Oelgaard
#
# This file is part of FFC.
#
# FFC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FFC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FFC. If not, see <http://www.gnu.org/licenses/>.
#
# First added: 2010-02-08
# Last changed: 2011-01-11
# FFC modules
from ffc.log import info
from ffc.cpp import format
from ffc.quadrature.symbolics import optimise_code, BASIS, IP, GEO, CONST
from ffc.quadrature.symbolics import create_product, create_sum, create_symbol, create_fraction
def optimize_integral_ir(ir, parameters):
"Compute optimized intermediate representation of integral."
# FIXME: input argument "parameters" has been added to optimize_integral_ir
# FIXME: which shadows a local parameter
parameters = ir["optimise_parameters"]
if parameters["optimisation"]:
integrals = ir["trans_integrals"]
domain_type = ir["domain_type"]
num_facets = ir["num_facets"]
geo_consts = ir["geo_consts"]
psi_tables_map = ir["psi_tables_map"]
if domain_type == "cell":
info("Optimising expressions for cell integral")
if parameters["optimisation"] in ("precompute_ip_const", "precompute_basis_const"):
_precompute_expressions(integrals, geo_consts, parameters["optimisation"])
else:
_simplify_expression(integrals, geo_consts, psi_tables_map)
elif domain_type == "exterior_facet":
for i in range(num_facets):
info("Optimising expressions for facet integral %d" % i)
if parameters["optimisation"] in ("precompute_ip_const", "precompute_basis_const"):
_precompute_expressions(integrals[i], geo_consts, parameters["optimisation"])
else:
_simplify_expression(integrals[i], geo_consts, psi_tables_map)
elif domain_type == "interior_facet":
for i in range(num_facets):
for j in range(num_facets):
info("Optimising expressions for facet integral (%d, %d)" % (i, j))
if parameters["optimisation"] in ("precompute_ip_const", "precompute_basis_const"):
_precompute_expressions(integrals[i][j], geo_consts,parameters["optimisation"])
else:
_simplify_expression(integrals[i][j], geo_consts, psi_tables_map)
else:
error("Unhandled domain type: " + str(domain_type))
return ir
def _simplify_expression(integral, geo_consts, psi_tables_map):
for points, terms, functions, ip_consts, coordinate, conditionals in integral:
# NOTE: sorted is needed to pass the regression tests on the buildbots
# but it might be inefficient for speed.
# A solution could be to only compare the output of evaluating the
# integral, not the header files.
for loop, (data, entry_vals) in sorted(terms.iteritems()):
t_set, u_weights, u_psi_tables, u_nzcs, basis_consts = data
new_entry_vals = []
psi_tables = set()
# NOTE: sorted is needed to pass the regression tests on the buildbots
# but it might be inefficient for speed.
# A solution could be to only compare the output of evaluating the
# integral, not the header files.
for entry, val, ops in sorted(entry_vals):
value = optimise_code(val, ip_consts, geo_consts, t_set)
# Check if value is zero
if value.val:
new_entry_vals.append((entry, value, value.ops()))
psi_tables.update(set([psi_tables_map[b] for b in value.get_unique_vars(BASIS)]))
terms[loop][0][2] = psi_tables
terms[loop][1] = new_entry_vals
def _precompute_expressions(integral, geo_consts, optimisation):
for points, terms, functions, ip_consts, coordinate, conditionals in integral:
for loop, (data, entry_vals) in terms.iteritems():
t_set, u_weights, u_psi_tables, u_nzcs, basis_consts = data
new_entry_vals = []
for entry, val, ops in entry_vals:
value = _extract_variables(val, basis_consts, ip_consts, geo_consts, t_set, optimisation)
# Check if value is zero
if value.val:
new_entry_vals.append((entry, value, value.ops()))
terms[loop][1] = new_entry_vals
def _extract_variables(val, basis_consts, ip_consts, geo_consts, t_set, optimisation):
f_G = format["geometry constant"]
f_I = format["ip constant"]
f_B = format["basis constant"]
if val._prec == 0:
return val
elif val._prec == 1:
if val.base_expr is None:
return val
new_base = _extract_variables(val.base_expr, basis_consts, ip_consts, geo_consts, t_set, optimisation)
new_sym = create_symbol(val.v, val.t, new_base, val.base_op, val.exp, val.cond)
new_sym.exp = val.exp
if new_sym.t == BASIS:
return _reduce_expression(new_sym, [], basis_consts, f_B, True)
elif new_sym.t == IP:
return _reduce_expression(new_sym, [], ip_consts, f_I, True)
elif new_sym.t == GEO:
return _reduce_expression(new_sym, [], geo_consts, f_G, True)
# First handle child classes of product and sum.
elif val._prec in (2, 3):
new_vars = []
for v in val.vrs:
new_vars.append(_extract_variables(v, basis_consts, ip_consts, geo_consts, t_set, optimisation))
if val._prec == 2:
new_val = create_product(new_vars)
if val._prec == 3:
new_val = create_sum(new_vars)
elif val._prec == 4:
num = _extract_variables(val.num, basis_consts, ip_consts, geo_consts, t_set, optimisation)
denom = _extract_variables(val.denom, basis_consts, ip_consts, geo_consts, t_set, optimisation)
return create_fraction(num, denom)
else:
error("Unknown symbolic type: %s" % repr(val))
# Sort variables of product and sum.
b_c, i_c, g_c = [], [], []
for v in new_val.vrs:
if v.t == BASIS:
if optimisation == "precompute_basis_const":
b_c.append(v)
elif v.t == IP:
i_c.append(v)
else:
g_c.append(v)
vrs = new_val.vrs[:]
for v in g_c + i_c + b_c:
vrs.remove(v)
i_c.extend(_reduce_expression(new_val, g_c, geo_consts, f_G))
vrs.extend(_reduce_expression(new_val, i_c, ip_consts, f_I))
vrs.extend(_reduce_expression(new_val, b_c, basis_consts, f_B))
# print "b_c: "
# for b in b_c:
# print b
# print "basis"
# for k,v in basis_consts.items():
# print "k: ", k
# print "v: ", v
# print "geo"
# for k,v in geo_consts.items():
# print "k: ", k
# print "v: ", v
# print "ret val: ", val
if len(vrs) > 1:
if new_val._prec == 2:
new_object = create_product(vrs)
elif new_val._prec == 3:
new_object = create_sum(vrs)
else:
error("Must have product or sum here: %s" % repr(new_val))
if new_object.t == BASIS:
if optimisation == "precompute_ip_const":
return new_object
elif optimisation == "precompute_basis_const":
return _reduce_expression(new_object, [], basis_consts, f_B, True)
elif new_object.t == IP:
return _reduce_expression(new_object, [], ip_consts, f_I, True)
elif new_object.t == GEO:
return _reduce_expression(new_object, [], geo_consts, f_G, True)
return vrs[0]
# if new_val._prec == 2:
# if len(vrs) > 1:
# new_prod = create_product(vrs)
# if new_prod.t == BASIS:
# if optimisation == "precompute_ip_const":
# return new_prod
# elif optimisation == "precompute_basis_const":
# return _reduce_expression(new_prod, [], basis_consts, f_B, True)
# elif new_prod.t == IP:
# return _reduce_expression(new_prod, [], ip_consts, f_I, True)
# elif new_prod.t == GEO:
# return _reduce_expression(new_prod, [], geo_consts, f_G, True)
# return vrs[0]
# elif new_val._prec == 3:
# if len(vrs) > 1:
# new_sum = create_sum(vrs)
# if new_sum.t == BASIS:
# return new_sum
## return _reduce_expression(new_sum, [], basis_consts, f_B, True)
# elif new_sum.t == IP:
# return _reduce_expression(new_sum, [], ip_consts, f_I, True)
# elif new_sum.t == GEO:
# return _reduce_expression(new_sum, [], geo_consts, f_G, True)
# return vrs[0]
# else:
# error("Must have product or sum here: %s" % repr(new_val))
def _reduce_expression(expr, symbols, const_dict, f_name, use_expr_type=False):
if use_expr_type:
if expr not in const_dict:
const_dict[expr] = len(const_dict)
return create_symbol(f_name(const_dict[expr]), expr.t)
# Only something to be done if we have more than one symbol.
if len(symbols) > 1:
sym_type = symbols[0].t
# Create new symbol.
if expr._prec == 2:
new_sym = create_product(symbols)
elif expr._prec == 3:
new_sym = create_sum(symbols)
if new_sym not in const_dict:
const_dict[new_sym] = len(const_dict)
s = create_symbol(f_name(const_dict[new_sym]), sym_type)
return [s]
return symbols
|