Symbols, Constants and Arrays¶
- class pystencils.backend.symbols.PsSymbol(name, dtype=None)¶
A mutable symbol with name and data type.
Do not create objects of this class directly unless you know what you are doing; instead obtain them from a
KernelCreationContext
throughKernelCreationContext.get_symbol
. This way, the context can keep track of all symbols used in the translation run, and uniqueness of symbols is ensured.
- class pystencils.backend.constants.PsConstant(value, dtype=None)¶
Type-safe representation of typed numerical constants.
This class models constants in the backend representation of kernels. A constant may be untyped, in which case its
value
may be any Python object.If the constant is typed (i.e. its
dtype
is notNone
), its data type is used to check the validity of itsvalue
and to convert it into the type’s internal representation.Instances of PsConstant are immutable.
- Parameters:
value (
Any
) – The constant’s valuedtype (
Optional
[PsNumericType
]) – The constant’s data type, orNone
if untyped.
- interpret_as(dtype)¶
Interprets this untyped constant with the given data type.
If this constant is already typed, raises an error.
- Return type:
- Parameters:
dtype (PsNumericType) –
- reinterpret_as(dtype)¶
Reinterprets this constant with the given data type.
Other than interpret_as, this method also works on typed constants.
- Return type:
- Parameters:
dtype (PsNumericType) –
- property dtype: PsNumericType | None¶
This constant’s data type, or
None
if it is untyped.The data type of a constant always has
const == True
.
- get_dtype()¶
Retrieve this constant’s data type, throwing an exception if the constant is untyped.
- Return type:
- class pystencils.backend.literals.PsLiteral(text, dtype)¶
Representation of literal code.
Instances of this class represent code literals inside the AST. These literals are not to be confused with C literals; the name Literal refers to the fact that the code generator takes them “literally”, printing them as they are.
Each literal has to be annotated with a type, and is considered constant within the scope of a kernel. Instances of PsLiteral are immutable.
- class pystencils.backend.arrays.PsLinearizedArray(name, element_type, shape, strides, index_dtype=PsIntegerType(width=64, signed=True, const=False))¶
Class to model N-dimensional contiguous arrays.
Memory Layout, Shape and Strides
The memory layout of an array is defined by its shape and strides. Both shape and stride entries may either be constants or special variables associated with exactly one array.
Shape and strides may be specified at construction in the following way. For constant entries, their value must be given as an integer. For variable shape entries and strides, the Ellipsis … must be passed instead. Internally, the passed
index_dtype
will be used to create typed constants (PsConstant) and variables (PsArrayShapeSymbol and PsArrayStrideSymbol) from the passed values.- Parameters:
name (str) –
element_type (PsType) –
shape (Sequence[int | EllipsisType]) –
strides (Sequence[int | EllipsisType]) –
index_dtype (PsIntegerType) –
- property name¶
The array’s name
- property base_pointer: PsArrayBasePointer¶
The array’s base pointer
- property shape: tuple[PsArrayShapeSymbol | PsConstant, ...]¶
The array’s shape, expressed using PsConstant and PsArrayShapeSymbol
- property strides: tuple[PsArrayStrideSymbol | PsConstant, ...]¶
The array’s strides, expressed using PsConstant and PsArrayStrideSymbol
- class pystencils.backend.arrays.PsArrayAssocSymbol(name, dtype, array)¶
A variable that is associated to an array.
Instances of this class represent pointers and indexing information bound to a particular array.
- Parameters:
name (str) –
dtype (PsType) –
array (PsLinearizedArray) –
- class pystencils.backend.arrays.PsArrayBasePointer(name, array)¶
- Parameters:
name (str) –
array (PsLinearizedArray) –
- class pystencils.backend.arrays.TypeErasedBasePointer(name, array)¶
Base pointer for arrays whose element type has been erased.
Used primarily for arrays of anonymous structs.
- Parameters:
name (str) –
array (PsLinearizedArray) –
- class pystencils.backend.arrays.PsArrayShapeSymbol(array, coordinate, dtype)¶
Variable that represents an array’s shape in one coordinate.
Do not instantiate this class yourself, but only use its instances as provided by PsLinearizedArray.shape.
- Parameters:
array (PsLinearizedArray) –
coordinate (int) –
dtype (PsIntegerType) –
- class pystencils.backend.arrays.PsArrayStrideSymbol(array, coordinate, dtype)¶
Variable that represents an array’s stride in one coordinate.
Do not instantiate this class yourself, but only use its instances as provided by PsLinearizedArray.strides.
- Parameters:
array (PsLinearizedArray) –
coordinate (int) –
dtype (PsIntegerType) –