Skip to content
Snippets Groups Projects
Commit b35ee506 authored by Frederik Hennig's avatar Frederik Hennig
Browse files

Fix linting. Fix docs on mixing types. Add docs on default types.

parent ac0dfad3
No related branches found
No related tags found
1 merge request!443Extended Support for Typing in the Symbolic Toolbox
Pipeline #72618 passed
......@@ -23,8 +23,30 @@ import pystencils as ps
import sympy as sp
```
## Changing the Default Data Types
The pystencils code generator defines two default data types:
- The default *numeric type*, which is applied to all numerical computations that are not
otherwise explicitly typed; the default is `float64`.
- The default *index type*, which is used for all loop and field index calculations; the default is `int64`.
These can be modified by setting the
{any}`default_dtype <CreateKernelConfig.default_dtype>` and
{any}`index_type <CreateKernelConfig.index_dtype>`
options of the code generator configuration:
```{code-cell} ipython3
cfg = ps.CreateKernelConfig()
cfg.default_dtype = "float32"
cfg.index_dtype = "int32"
```
Modifying these will change the way types for [untyped symbols](#untyped-symbols)
and [dynamically typed expressions](#dynamic-typing) are computed.
## Setting the Types of Fields and Symbols
(untyped-symbols)=
### Untyped Symbols
Symbols used inside a kernel are most commonly created using
......@@ -65,6 +87,7 @@ str(f.dtype), str(g.dtype)
When using `Field.create_generic` or `Field.create_fixed_size`, on the other hand,
you can set the data type via the `dtype` keyword argument.
(dynamic-typing)=
### Dynamically Typed Symbols and Fields
Apart from explicitly setting data types,
......@@ -92,9 +115,11 @@ if any type conflicts arise, it will terminate with an error.
Still, there are cases where you want to combine subexpressions of different types;
maybe you need to compute geometric information from loop counters or other integers,
or you are doing mixed-precision numerical computations.
In these cases, you might have to
1. Introduce explicit type casts when values move from one type context to another;
2. Annotate expressions with a specific data type to ensure computations are performed in that type.
In these cases, you might have to introduce explicit type casts when values move from one type context to another.
<!-- 2. Annotate expressions with a specific data type to ensure computations are performed in that type.
TODO: See #97 (https://i10git.cs.fau.de/pycodegen/pystencils/-/issues/97)
-->
(type_casts)=
### Type Casts
......@@ -116,11 +141,6 @@ and then introduce a runtime cast to the target type.
That target type must comply with the type computed for the outer expression,
which the cast is embedded in.
(explicit_expression_types)=
### Setting Explicit Types for Expressions
## Understanding the pystencils Type Inference System
To correctly apply varying data types to pystencils kernels, it is important to understand
......
......@@ -7,7 +7,7 @@ import pickle
import re
from enum import Enum
from itertools import chain
from typing import List, Optional, Sequence, Set, Tuple, Union
from typing import List, Optional, Sequence, Set, Tuple
from warnings import warn
import numpy as np
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment