Composer API (pystencilssfg.composer)¶
- class pystencilssfg.composer.SfgComposer(sfg)¶
Primary interface for constructing source files in pystencils-sfg.
The SfgComposer combines the SfgBasicComposer for the basic components (kernel namespaces, includes, definitions, and functions) and the SfgClassComposer for constructing
struct
s andclass
es.- Parameters:
sfg (SfgContext | SfgBasicComposer) –
- class pystencilssfg.composer.SfgIComposer(ctx)¶
- Parameters:
ctx (SfgContext) –
- class pystencilssfg.composer.SfgBasicComposer(sfg)¶
Composer for basic source components, and base class for all composer mix-ins.
- Parameters:
sfg (SfgContext | SfgIComposer) –
- prelude(content)¶
Append a string to the prelude comment, to be printed at the top of both generated files.
The string should not contain C/C++ comment delimiters, since these will be added automatically during code generation.
- Parameters:
content (str) –
- define(*definitions)¶
Add custom definitions to the generated header file.
- Parameters:
definitions (str) –
- define_once(*definitions)¶
Same as define, but only adds definitions only if the same code string was not already added.
- Parameters:
definitions (str) –
- namespace(namespace)¶
Set the inner code namespace. Throws an exception if a namespace was already set.
- Parameters:
namespace (str) –
- generate(generator)¶
Invoke a custom code generator with the underlying context.
- Parameters:
generator (CustomGenerator) –
- property kernels: SfgKernelNamespace¶
The default kernel namespace.
Add kernels like:
sfg.kernels.add(ast, "kernel_name") sfg.kernels.create(assignments, "kernel_name", config)
- kernel_namespace(name)¶
Return the kernel namespace of the given name, creating it if it does not exist yet.
- Return type:
- Parameters:
name (str) –
- include(header_file, private=False)¶
Include a header file.
- numpy_struct(name, dtype, add_constructor=True)¶
Add a numpy structured data type as a C++ struct
- kernel_function(name, ast_or_kernel_handle)¶
Create a function comprising just a single kernel call.
- Parameters:
ast_or_kernel_handle (
KernelFunction
|SfgKernelHandle
) – Either a pystencils AST, or a kernel handle for an already registered AST.name (str) –
- function(name)¶
Add a function.
The syntax of this function adder uses a chain of two calls to mimic C++ syntax:
sfg.function("FunctionName")( # Function Body )
The function body is constructed via sequencing (see make_sequence).
- Parameters:
name (str) –
- call(kernel_handle)¶
Use inside a function body to directly call a kernel.
When using call, the given kernel will simply be called as a function. To invoke a GPU kernel on a specified launch grid, use cuda_invoke or the interfaces of pystencilssfg.extensions.sycl instead.
- Parameters:
kernel_handle (
SfgKernelHandle
) – Handle to a kernel previously added to some kernel namespace.- Return type:
- seq(*args)¶
Syntax sequencing. For details, see make_sequence
- Return type:
- Parameters:
args (tuple | str | SfgCallTreeNode | SfgNodeBuilder) –
- params(*args)¶
Use inside a function body to add parameters to the function.
- Return type:
- Parameters:
args (SfgVar) –
- var(name, dtype)¶
Create a variable with given name and data type.
- init(lhs)¶
Create a C++ in-place initialization.
Usage:
obj = sfg.var("obj", "SomeClass") sfg.init(obj)(arg1, arg2, arg3)
becomes
SomeClass obj { arg1, arg2, arg3 };
- Return type:
SfgInplaceInitBuilder
- Parameters:
lhs (SfgVar) –
- property branch: SfgBranchBuilder¶
Use inside a function body to create an if/else conditonal branch.
The syntax is:
sfg.branch("condition")( # then-body )( # else-body (may be omitted) )
- map_field(field, index_provider)¶
Map a pystencils field to a field data structure, from which pointers, sizes and strides should be extracted.
- Parameters:
field (
Field
) – The pystencils field to be mappedsrc_object – A IFieldIndexingProvider object representing a field data structure.
index_provider (IFieldExtraction | SrcField) –
- Return type:
SfgDeferredFieldMapping
- map_param(lhs, rhs, mapping)¶
Arbitrary parameter mapping: Add a single line of code to define a left-hand side object from one or multiple right-hand side dependencies.
- class pystencilssfg.composer.SfgClassComposer¶
Composer for classes and structs.
This class cannot be instantiated on its own but must be mixed in with
SfgBasicComposer
. Its interface is exposed bySfgComposer
.- class VisibilityContext(visibility)¶
Represent a visibility block in the composer syntax.
Returned by private, public, and protected.
- Parameters:
visibility (SfgVisibility) –
- class ConstructorBuilder(*params)¶
Composer syntax for constructor building.
Returned by constructor.
- Parameters:
params (SfgVar) –
- klass(class_name, bases=())¶
Create a class and add it to the underlying context.
- struct(class_name, bases=())¶
Create a struct and add it to the underlying context.
- property public: VisibilityContext¶
Create a public visibility block in a class body
- property protected: VisibilityContext¶
Create a protected visibility block in a class or struct body
- property private: VisibilityContext¶
Create a private visibility block in a class or struct body
- constructor(*params)¶
In a class or struct body or visibility block, add a constructor.
- Parameters:
params (
SfgVar
) – List of constructor parameters
- method(name, returns=CustomType(void, const=False), inline=False, const=False)¶
In a class or struct body or visibility block, add a method. The usage is similar to
SfgBasicComposer.function
.
- pystencilssfg.composer.make_sequence(*args)¶
Construct a sequence of C++ code from various kinds of arguments.
make_sequence is ubiquitous throughout the function building front-end; among others, it powers the syntax of SfgComposer.function and SfgComposer.branch.
make_sequence constructs an abstract syntax tree for code within a function body, accepting various types of arguments which then get turned into C++ code. These are :rtype:
SfgSequence
Strings (str) are printed as-is
Tuples (tuple) signify blocks, i.e. C++ code regions enclosed in
{ }
Sub-ASTs and AST builders, which are often produced by the syntactic sugar and factory methods of SfgComposer.
Its usage is best shown by example:
tree = make_sequence( "int a = 0;", "int b = 1;", ( "int tmp = b;", "b = a;", "a = tmp;" ), SfgKernelCall(kernel_handle) ) sfg.context.add_function("myFunction", tree)
will translate to
void myFunction() { int a = 0; int b = 0; { int tmp = b; b = a; a = tmp; } kernels::kernel( ... ); }
- Parameters:
args (tuple | str | AugExpr | SfgCallTreeNode | SfgNodeBuilder) –
- Return type: