devito.tools package

Submodules

devito.tools.abc module

class devito.tools.abc.Tag(name, val=None)[source]

Bases: abc.ABC

An abstract class to define categories of object decorators.

Note

This class must be subclassed for each new category.

class devito.tools.abc.ArgProvider[source]

Bases: object

A base class for types that can provide runtime values for dynamically executed (JIT-compiled) code.

class devito.tools.abc.Signer[source]

Bases: object

A base class for types that can emit a unique, deterministic string representing their internal state. Subclasses may be mutable or immutable.

Note

Subclasses must implement the method __signature_items___().

Note

Regardless of whether an object is mutable or immutable, the returned signature must be immutable, and thus hashable.

class devito.tools.abc.Pickable[source]

Bases: object

A base class for types that require pickling. There are several complications that this class tries to handle:

* Packages such as SymPy have their own way of handling pickling -- though
  still based upon Python's pickle module. This may get in conflict with
  other packages, or simply with Devito itself. For example, most of Devito
  symbolic objects are created via ``def __new__(..., **kwargs)``; SymPy1.1
  pickling does not cope nicely with ``new`` and ``kwargs``, since it is
  based on the low-level copy protocol (__reduce__, __reduce_ex__) and
  simply end up ignoring ``__getnewargs_ex__``, the function responsible
  for processing __new__'s kwargs.

Note

All sub-classes using multiple inheritance may have to explicitly set __reduce_ex__ = Pickable.__reduce_ex__ depending on the MRO.

devito.tools.algorithms module

devito.tools.algorithms.toposort(data)[source]

Given items that depend on other items, a topological sort arranges items in order that no one item precedes an item it depends on.

data captures the various dependencies. It may be:

  • A dictionary whose keys are items and whose values are a set of dependent items. The dictionary may contain self-dependencies (which are ignored), and dependent items that are not also dict keys.
  • An iterable of dependences as expected by build_dependence_lists().

Readapted from:

http://code.activestate.com/recipes/577413/

devito.tools.data_structures module

class devito.tools.data_structures.Bunch(**kwargs)[source]

Bases: object

Bind together an arbitrary number of generic items. This is a mutable alternative to a namedtuple.

From:

http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of        -a-bunch-of-named/?in=user-97991
class devito.tools.data_structures.EnrichedTuple[source]

Bases: tuple

A tuple with an arbitrary number of additional attributes.

class devito.tools.data_structures.ReducerMap[source]

Bases: multidict._multidict.MultiDict

Specialised MultiDict object that maps a single key to a list of potential values and provides a reduction method for retrieval.

reduce(key, op=None)[source]

Returns a reduction of all candidate values for a given key.

Parameters:
  • key – Key for which to retrieve candidate values
  • op – Operator for reduction among candidate values. If not provided, a unique value will be returned, or a ValueError raised if no unique value exists.
reduce_all()[source]

Returns a dictionary with reduced/unique values for all keys.

unique(key)[source]

Returns a unique value for a given key, if such a value exists, and raises a ValueError if it does not.

Parameters:key – Key for which to retrieve a unique value
update(values)[source]

Update internal mapping with standard dictionary semantics.

class devito.tools.data_structures.DefaultOrderedDict(default_factory=None, *a, **kw)[source]

Bases: collections.OrderedDict

copy() → a shallow copy of od[source]
class devito.tools.data_structures.PartialOrderTuple[source]

Bases: tuple

A tuple whose elements are ordered according to a set of relations.

Parameters:
  • items – The elements of the tuple.
  • relations – (Optional) an iterable of binary relations between elements in items. If not provided, then items is interpreted as a totally ordered sequence. If provided, then a (partial) ordering is computed and all elements in items for which a relation is not provided are appended.
generate_ordering()[source]
relations
classmethod reorder(items, relations)[source]

devito.tools.memoization module

class devito.tools.memoization.memoized_func(func)[source]

Bases: object

Decorator. Caches a function’s return value each time it is called. If called later with the same arguments, the cached value is returned (not reevaluated). This decorator may also be used on class methods, but it will cache at the class level; to cache at the instance level, use memoized_meth.

Adapted from:

https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
class devito.tools.memoization.memoized_meth(func)[source]

Bases: object

Decorator. Cache the return value of a class method.

Unlike memoized_func, the return value of a given method invocation will be cached on the instance whose method was invoked. All arguments passed to a method decorated with memoize must be hashable.

If a memoized method is invoked directly on its class the result will not be cached. Instead the method will be invoked like a static method:

class Obj(object):
    @memoize
    def add_to(self, arg):
        return self + arg
Obj.add_to(1) # not enough arguments
Obj.add_to(1, 2) # returns 3, result is not cached

Adapted from:

code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/

devito.tools.os_helper module

class devito.tools.os_helper.change_directory(newPath)[source]

Bases: object

Context manager for changing the current working directory.

Adapted from:

https://stackoverflow.com/questions/431684/how-do-i-cd-in-python/
devito.tools.os_helper.make_tempdir(prefix=None)[source]

Create a temporary directory having a deterministic name. The directory is created within the default OS temporary directory.

devito.tools.utils module

devito.tools.utils.prod(iterable)[source]
devito.tools.utils.as_tuple(item, type=None, length=None)[source]

Force item to a tuple.

Partly extracted from: https://github.com/OP2/PyOP2/.

devito.tools.utils.is_integer(value)[source]

A thorough instance comparison for all integer types.

devito.tools.utils.generator()[source]

Return a function f that generates integer numbers starting at 0 with stepping 1.

devito.tools.utils.grouper(iterable, n)[source]

Split an interable into groups of size n, plus a reminder

devito.tools.utils.split(iterable, f)[source]

Split an iterable I into two iterables I1 and I2 of the same type as I. I1 contains all elements e in I for which f(e) returns True; I2 is the complement of I1.

devito.tools.utils.roundm(x, y)[source]

Return x rounded up to the closest multiple of y.

devito.tools.utils.powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)[source]
devito.tools.utils.invert(mapper)[source]

Invert a dict of lists preserving the order.

devito.tools.utils.flatten(l)[source]

Flatten a hierarchy of nested lists into a plain list.

devito.tools.utils.single_or(l)[source]

Return True iff only one item is different than None, False otherwise. Note that this is not a XOR function, according to the truth table of the XOR boolean function with n > 2 inputs. Hence the name single_or.

devito.tools.utils.filter_ordered(elements, key=None)[source]

Filter elements in a list while preserving order.

Parameters:key – Optional conversion key used during equality comparison.
devito.tools.utils.filter_sorted(elements, key=None)[source]

Filter elements in a list and sort them by key. The default key is operator.attrgetter('name').

devito.tools.utils.numpy_to_ctypes(dtype)[source]

Map numpy types to ctypes types.

devito.tools.utils.numpy_to_mpitypes(dtype)[source]

Map numpy types to MPI datatypes.

devito.tools.utils.ctypes_to_C(ctype)[source]

Map ctypes types to C types.

devito.tools.utils.ctypes_pointer(name)[source]

Create a ctypes type representing a C pointer to a custom data type name.

devito.tools.utils.pprint(node, verbose=True)[source]

Shortcut to pretty print Iteration/Expression trees.

devito.tools.utils.sweep(parameters, keys=None)[source]

Generator to create a parameter sweep from a dictionary of values or value lists.

devito.tools.utils.as_mapper(iterable, key=None)[source]

Rearrange an iterable into a dictionary of lists in which keys are produced by the function key.

devito.tools.validators module

class devito.tools.validators.validate_type(*checks)[source]

Bases: devito.tools.validators.validate_base

Decorator to validate argument types.

The decorator expects one or more arguments, which are 3-tuples of (name, type, exception), where name is the argument name in the function being decorated, type is the argument type to be validated and exception is the exception type to be raised if validation fails.

Readapted from:

https://github.com/OP2/PyOP2/
check_arg(arg, argtype, exception)[source]

devito.tools.visitors module

class devito.tools.visitors.GenericVisitor[source]

Bases: object

A generic visitor.

To define handlers, subclasses should define visit_Foo methods for each class Foo they want to handle. If a specific method for a class Foo is not found, the MRO of the class is walked in order until a matching method is found.

The method signature is:

The handler is responsible for visiting the children (if any) of the node o. *args and **kwargs may be used to pass information up and down the call stack. You can also pass named keyword arguments, e.g.:

default_args = {}
classmethod default_retval()[source]

A method that returns an object to use to populate return values.

If your visitor combines values in a tree-walk, it may be useful to provide a object to combine the results into. default_retval() may be defined by the visitor to be called to provide an empty object of appropriate type.

lookup_method(instance)[source]

Look up a handler method for a visitee.

Parameters:instance – The instance to look up a method for.
visit(o, *args, **kwargs)[source]

Apply this Visitor to an object.

Parameters:
  • o – The Node to visit.
  • args – Optional arguments to pass to the visit methods.
  • kwargs – Optional keyword arguments to pass to the visit methods.
visit_object(o, **kwargs)[source]

Module contents