From 4adc2814ca1f9c44ababd043280b91d2738136c8 Mon Sep 17 00:00:00 2001 From: Dominic Kempf <dominic.kempf@iwr.uni-heidelberg.de> Date: Wed, 29 Mar 2017 15:55:10 +0200 Subject: [PATCH] Some fixes to option parsing --- python/dune/perftool/options.py | 39 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/python/dune/perftool/options.py b/python/dune/perftool/options.py index 55bbddab..b751e420 100644 --- a/python/dune/perftool/options.py +++ b/python/dune/perftool/options.py @@ -13,13 +13,15 @@ class PerftoolOption(ImmutableRecord): default=None, helpstr="Undocumented feature!", process=lambda x: x, - type=type(None), + _type=type(None), ): + _type = type(default) + if _type is type(None): + _type = str ImmutableRecord.__init__(self, helpstr=helpstr, default=default, - process=process, - type=type, + type=_type, ) @@ -27,12 +29,12 @@ class PerftoolOptionsArray(ImmutableRecord): """ A collection of form compiler arguments """ def __init__(self, **kwargs): opts = {k: v.default for k, v in PerftoolOptionsArray.__dict__.items() if isinstance(v, PerftoolOption)} - opts.update(kwargs) + opts.update(**kwargs) ImmutableRecord.__init__(self, **opts) - uflfile = PerftoolOption(helpstr="the UFL file to compile", process=abspath) - driver_file = PerftoolOption(helpstr="The filename for the generated driver header", process=abspath) - operator_file = PerftoolOption(helpstr="The filename for the generated local operator header", process=abspath) + uflfile = PerftoolOption(helpstr="the UFL file to compile") + driver_file = PerftoolOption(helpstr="The filename for the generated driver header") + operator_file = PerftoolOption(helpstr="The filename for the generated local operator header") numerical_jacobian = PerftoolOption(default=False, helpstr="use numerical jacobians (only makes sense, if uflpdelab for some reason fails to generate analytic jacobians)") matrix_free = PerftoolOption(default=False, helpstr="Use iterative solver with matrix free jacobian application") explicit_time_stepping = PerftoolOption(default=False, helpstr="use explicit time stepping") @@ -41,15 +43,15 @@ class PerftoolOptionsArray(ImmutableRecord): compare_l2errorsquared = PerftoolOption(helpstr="maximal allowed l2 error squared of difference between numerical solution and interpolation of exact solution (NOTE: requires --exact-solution-expression)") interactive = PerftoolOption(default=False, helpstr="whether the optimization process should be guided interactively (also useful for debugging)") print_transformations = PerftoolOption(default=False, helpstr="print out dot files after ufl tree transformations") - print_transformations_dir = PerftoolOption(default=".", helpstr="place where to put dot files (can be omitted)", process=abspath) - quadrature_order = PerftoolOption(type=int, helpstr="Quadrature order used for all integrals.") + print_transformations_dir = PerftoolOption(default=".", helpstr="place where to put dot files (can be omitted)") + quadrature_order = PerftoolOption(_type=int, helpstr="Quadrature order used for all integrals.") diagonal_transformation_matrix = PerftoolOption(default=False, helpstr="set option if the jacobian of the transformation is diagonal (axiparallel grids)") constant_transformation_matrix = PerftoolOption(default=False, helpstr="set option if the jacobian of the transformation is constant on a cell") - ini_file = PerftoolOption(helpstr="An inifile to use. A generated driver will be hard-coded to it, a [formcompiler] section will be used as default values to form compiler arguments (use snake case)", process=abspath) + ini_file = PerftoolOption(helpstr="An inifile to use. A generated driver will be hard-coded to it, a [formcompiler] section will be used as default values to form compiler arguments (use snake case)") opcounter = PerftoolOption(default=False, helpstr="Count operations. Note: In this case only oparor applications are generated since solving and operator counting does not work. You probably want to set instrumentation level>0.") time_opcounter = PerftoolOption(default=False, helpstr="Generate opcounter codepath. Can be used for timing opcounter programs without setting the opcounter option.") instrumentation_level = PerftoolOption(default=0, helpstr="Control time/opcounter measurements. 0-do nothing, 1-measure program as a whole, 2-operator applications, 3-measure kernel (eg. alpha-volume, ...), 4-parts of kernel (eg. stage 1-3 of SF)") - project_basedir = PerftoolOption(helpstr="The base (build) directory of the dune-perftool project", process=abspath) + project_basedir = PerftoolOption(helpstr="The base (build) directory of the dune-perftool project") fastdg = PerftoolOption(default=False, helpstr="Use FastDGGridOperator from PDELab.") sumfact = PerftoolOption(default=False, helpstr="Use sumfactorization") vectorize_quad = PerftoolOption(default=False, helpstr="whether to generate code with explicit vectorization") @@ -78,12 +80,7 @@ def update_options_from_commandline(opt): for k, v in type(opt).__dict__.items(): if isinstance(v, PerftoolOption): cmdopt = "--{}".format(k.replace('_', '-')) - _type = v.type - if _type is type(None): - _type = type(v.default) - if _type is type(None): - _type = str - parser.add_argument(cmdopt, help=v.helpstr, type=_type) + parser.add_argument(cmdopt, help=v.helpstr, type=v.type) parsedargs = {k: v for k, v in vars(parser.parse_args()).items() if v is not None} return opt.copy(**parsedargs) @@ -91,7 +88,13 @@ def update_options_from_commandline(opt): def update_options_from_inifile(opt): """ Return an options array object with updated values from an inifile """ if opt.ini_file: - opt = opt.copy(**parse_ini_file(opt.ini_file).get("formcompiler", {})) + def _fix_bool(k, v): + if hasattr(type(opt), k) and getattr(type(opt), k).type is bool: + return bool(eval(v)) + return v + ini = parse_ini_file(opt.ini_file).get("formcompiler", {}) + ini = {k: _fix_bool(k, v) for k, v in ini.items()} + opt = opt.copy(**ini) return opt -- GitLab