Type System¶
The pystencils.types
module contains the set of classes used by pystencils
to model data types. Data types are used extensively within the code generator,
but can safely be ignored by most users unless you wish to force certain types on
symbols, generate mixed-precision kernels, et cetera.
Basic Functions¶
- pystencils.types.create_type(type_spec)¶
Create a pystencils type object from a variety of specifications.
This function converts several possible representations of data types to an instance of PsType. The
type_spec
argument can be any of the following:Strings (str): will be parsed as common C types, throwing an exception if that fails. To construct a PsCustomType instead, use the constructor of PsCustomType or its abbreviation types.quick.Custom.
- Python builtin data types (instances of type): Attempts to interpret Python numeric types like so:
int becomes a signed 64-bit integer
float becomes a double-precision IEEE-754 float
No others are supported at the moment
Supported Numpy scalar data types (see https://numpy.org/doc/stable/reference/arrays.scalars.html) are converted to pystencils scalar data types
Instances of numpy.dtype: Attempt to interpret scalar types like above, and structured types as structs.
Instances of PsType will be returned as they are
- pystencils.types.create_numeric_type(type_spec)¶
Like create_type, but only for numeric types.
- Return type:
- Parameters:
- pystencils.types.constify(t)¶
Adds the const qualifier to a given type.
Data Type Class Hierarchy¶
- class pystencils.types.PsType(const=False)¶
Base class for all pystencils types.
- Parameters:
const (
bool
) – Const-qualification of this typeargs (Any) –
kwargs (Any) –
- Return type:
Any
- class pystencils.types.types.PsCustomType(name, const=False)¶
Class to model custom types by their names.
- Parameters:
name (
str
) – Name of the custom type.args (Any) –
kwargs (Any) –
- Return type:
Any
- class pystencils.types.types.PsDereferencableType(base_type, const=False)¶
Base class for subscriptable types.
PsDereferencableType represents any type that may be dereferenced and may occur as the base of a subscript, that is, before the C
[]
operator.
- final class pystencils.types.types.PsPointerType(base_type, restrict=True, const=False)¶
A C pointer with arbitrary base type.
PsPointerType models C pointer types to arbitrary data, with support for
restrict
-qualified pointers.- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- class pystencils.types.types.PsArrayType(base_type, length=None, const=False)¶
C array type of known or unknown size.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- class pystencils.types.types.PsStructType(members, name=None, const=False)¶
Named or anonymous structured data type.
A struct type is defined by its sequence of members. The struct may optionally have a name, although the code generator currently does not support named structs and treats them the same way as anonymous structs.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- find_member(member_name)¶
Find a member by name
- class pystencils.types.types.PsNumericType(const=False)¶
Numeric data type, i.e. any type that may occur inside arithmetic-logical expressions.
Constants
Every numeric type has to act as a factory for compile-time constants of that type. The PsConstant class relies on create_constant to instantiate constants of a given numeric type. The object returned by create_constant must implement the necessary arithmetic operations, and its arithmetic behaviour must match the given type.
create_constant should fail whenever its input cannot safely be interpreted as the given type.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- class pystencils.types.types.PsScalarType(const=False)¶
Scalar numeric type.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- abstract create_literal(value)¶
Create a C numerical literal for a constant of this type.
- class pystencils.types.types.PsVectorType(scalar_type, vector_entries, const=False)¶
Packed vector of numeric type.
- Parameters:
element_type – Underlying scalar data type
num_entries – Number of entries in the vector
args (Any) –
kwargs (Any) –
- Return type:
Any
- property numpy_dtype¶
A np.dtype object representing this data type.
Available both for backward compatibility and for interaction with the numpy-based runtime system.
- class pystencils.types.types.PsBoolType(const=False)¶
Boolean type.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- property numpy_dtype: dtype | None¶
A np.dtype object representing this data type.
Available both for backward compatibility and for interaction with the numpy-based runtime system.
- create_literal(value)¶
Create a C numerical literal for a constant of this type.
- class pystencils.types.types.PsIntegerType(width, signed=True, const=False)¶
Signed and unsigned integer types.
PsIntegerType cannot be instantiated on its own, but only through PsSignedIntegerType and PsUnsignedIntegerType. This distinction is meant mostly to help in pattern matching.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- property numpy_dtype: dtype | None¶
A np.dtype object representing this data type.
Available both for backward compatibility and for interaction with the numpy-based runtime system.
- create_literal(value)¶
Create a C numerical literal for a constant of this type.
- final class pystencils.types.types.PsSignedIntegerType(width, const=False)¶
Signed integer types.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- final class pystencils.types.types.PsUnsignedIntegerType(width, const=False)¶
Unsigned integer types.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- final class pystencils.types.types.PsIeeeFloatType(width, const=False)¶
IEEE-754 floating point data types
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
Any
- property numpy_dtype: dtype | None¶
A np.dtype object representing this data type.
Available both for backward compatibility and for interaction with the numpy-based runtime system.
- property required_headers: set[str]¶
The set of header files required when this type occurs in generated code.
- create_literal(value)¶
Create a C numerical literal for a constant of this type.
Data Type Abbreviations¶
Quick access to the pystencils data type system.
- pystencils.types.quick.Custom¶
Custom data types are modelled only by their name.
- pystencils.types.quick.Scalar¶
Scalar()
matches any subclass ofPsScalarType
- pystencils.types.quick.Ptr¶
Ptr(t)
matchesPsPointerType(base_type=t)
- pystencils.types.quick.Arr¶
Arr(t, s)
matchesPsArrayType(base_type=t, size=s)
- pystencils.types.quick.Bool¶
Bool()
matchesPsBoolType()
- pystencils.types.quick.AnyInt¶
AnyInt(width)
matches bothPsUnsignedIntegerType(width)
andPsSignedIntegerType(width)
- pystencils.types.quick.UInt¶
UInt(width)
matchesPsUnsignedIntegerType(width)
- pystencils.types.quick.Int¶
Int(width)
matchesPsSignedIntegerType(width)
- pystencils.types.quick.SInt¶
SInt(width)
matchesPsSignedIntegerType(width)
- pystencils.types.quick.Fp¶
Fp(width)
matchesPsIeeeFloatType(width)
Implementation Details¶
Caching of Instances¶
To handle and compare types more efficiently, the pystencils type system customizes class instantiation to cache and reuse existing instances of types. This means, for example, if a 32-bit const unsigned integer type gets created in two places in the program, the resulting objects are exactly the same:
>>> from pystencils.types import PsUnsignedIntegerType
>>> t1 = PsUnsignedIntegerType(32, const=True)
>>> t2 = PsUnsignedIntegerType(32, const=True)
>>> t1 is t2
True
This mechanism is implemented by the metaclass PsTypeMeta. It is not perfect, however; some parts of Python that bypass the regular object creation sequence, such as pickle and copy.copy, may create additional instances of types.
- class pystencils.types.meta.PsTypeMeta(name, bases, namespace, **kwargs)¶
Metaclass for the PsType hierarchy.
PsTypeMeta holds an internal cache of all created instances of PsType and overrides object creation such that whenever a type gets instantiated more than once with the same argument list, instead of creating a new object, the existing object is returned.
Extending the Type System¶
When extending the type system’s class hierarchy, new classes need to implement at least the internal method __args__. This method, when called on a type object, must return a hashable sequence of arguments – not including the const-qualifier – that can be used to recreate that exact type. It is used internally to compute hashes and compare equality of types, as well as for const-conversion.
- pystencils.types.PsType.__args__(self)¶
Return the arguments used to create this instance, in canonical order, excluding the const-qualifier.
The tuple returned by this method must be hashable and for each instantiable subclass
MyType
ofPsType
, the following must hold:t = MyType(< arguments >) assert MyType(*t.__args__(), const=t.const) == t