from __future__ import annotations
import abc
import dataclasses
import enum
import textwrap
import typing
import weakref
from collections.abc import Hashable, Iterable
from functools import cached_property
from typing import Any
import loopy as lp
import loopy.tools
import numpy as np
from mpi4py import MPI
import pyop3.cc
import pyop3.expr
import pyop3.record
from pyop3 import utils
from pyop3.axis_tree import AxisTree
from pyop3.axis_tree.tree import (
UNIT_AXIS_TREE,
)
from pyop3.cache import (
cached_method,
with_self_heavy_cache,
)
from pyop3.collections import OrderedFrozenSet
from pyop3.constants import Intent
from pyop3.exceptions import InvalidScalarTypeException, Pyop3Exception
from pyop3.expr import Tensor
from pyop3.node import Node, Operator, Terminal
if typing.TYPE_CHECKING:
from pyop3.types import *
from .exec import InstructionExecutionContext
# TODO: This exception is not actually ever raised. We should check the
# intents of the kernel arguments and complain if something illegal is
# happening.
[docs]
class IntentMismatchError(Exception):
pass
_intent_merge_mapping = {
frozenset({Intent.READ, Intent.WRITE}): Intent.RW,
}
# TODO: Would probably be nicer as a dict-like class
def _add_named_terminal_intent(intents, arg, intent):
"""Add an object with a particular intent.
If the object is already in the dictionary the intent may change accordingly.
"""
if arg not in intents:
intents[arg] = intent
elif intents[arg] == intent:
pass
else:
new_intent = intent
orig_intent = intents[arg]
mix = frozenset({new_intent, orig_intent})
try:
shared_intent = _intent_merge_mapping[mix]
except KeyError:
raise ValueError(f"dont know how to deal with combination {mix}")
else:
intents[arg] = shared_intent
# FIXME: This is not a thing any more
[docs]
class KernelArgument(abc.ABC):
"""Abstract class for types that may be passed as arguments to kernels.
Note that some types that can be passed to *functions* are not in fact
kernel arguments. This is because they either wrap actual kernel arguments
(e.g. `Dat`), or because no argument is actually passed
(e.g. a temporary).
"""
# needed? the motivation is that one can consider arrays as having 2 dtypes. E.g.
# 'double*' or 'double' (the whole thing or the entries)
# @property
# @abc.abstractmethod
# def kernel_dtype(self):
# pass
[docs]
class UnprocessedExpressionException(Pyop3Exception):
"""Exception raised when pyop3 expected a preprocessed expression."""
[docs]
class Instruction(Node, abc.ABC):
@property
@abc.abstractmethod
def named_terminal_intents(self):
# TODO: rename .arguments to .argument_exprs
"""Mapping from named terminals to their intents.
For example consider the assignment
dat1[map1] <- 2*dat2[map2] + dat3[map3]
There are:
* 2 arguments: dat1 and '2*dat2 + dat3'
* 3 named arguments: dat1, dat2 and dat3
* 6 buffers: dat1, dat2, dat3, map1, map2, map3
"""
...
@property
def named_terminals(self) -> tuple:
return tuple(self.named_terminal_intents)
[docs]
@with_self_heavy_cache
def __call__(self, *, compiler_parameters=None, **kwargs) -> None:
self._get_execution_context(compiler_parameters)(**kwargs)
@cached_method()
def _get_execution_context(self, compiler_parameters) -> InstructionExecutionContext:
from .exec import InstructionExecutionContext
weak_self = weakref.ref(self)
return InstructionExecutionContext(weak_self, compiler_parameters)
[docs]
class NonTerminalInstruction(Instruction, Operator):
pass
[docs]
class TerminalInstruction(Instruction, Terminal, abc.ABC):
@property
@abc.abstractmethod
def argument_intents(self) -> tuple[Any, ...]:
pass
@property
def arguments(self) -> tuple[Any, ...]:
return tuple(a for a, _ in self.argument_intents)
@property
@abc.abstractmethod
def with_arguments(self, arguments):
pass
@property
@abc.abstractmethod
def compiler_options(self) -> pyop3.cc.CompilerOptions:
"""Extra options needed to compile this terminal."""
[docs]
@cached_property
def named_terminal_intents(self) -> dict:
from pyop3.expr.visitors import collect_arguments
intents = {}
for arg_expr, intent in self.argument_intents:
for term in collect_arguments(arg_expr):
_add_named_terminal_intent(intents, term, intent)
return intents
# TODO not a useful thing to have any more
_DEFAULT_LOOP_NAME = "pyop3_loop"
[docs]
@pyop3.record.frozenrecord()
class Loop(NonTerminalInstruction):
"""A loop."""
# {{{ instance attrs
index: pyop3.LoopIndex
statements: tuple[Instruction, ...]
[docs]
def collect_buffers(self, visitor):
return visitor(self.index).union(*(map(visitor, self.statements)))
[docs]
def get_disk_cache_key(self, visitor) -> Hashable:
with visitor.active_loop_index(self.index.label):
stmts_key = tuple(map(visitor, self.statements))
return (type(self), visitor(self.index), stmts_key)
[docs]
def get_instruction_executor_cache_key(self, visitor) -> Hashable:
with visitor.identity_hash_buffers():
index_key = visitor(self.index)
with visitor.active_loop_index(self.index.label):
stmts_key = tuple(map(visitor, self.statements))
return (type(self), index_key, stmts_key)
def __new__(cls, *args, **kwargs):
if args:
index, stmts = args
else:
index = kwargs.pop("index")
stmts = kwargs.pop("statements")
assert not kwargs
processed_stmts = []
for stmt in pyop3.collections.as_tuple(stmts):
if isinstance(stmt, NullInstruction):
continue
elif isinstance(stmt, InstructionList):
processed_stmts.extend(stmt)
else:
processed_stmts.append(stmt)
stmts = tuple(processed_stmts)
if len(stmts) == 0:
return NullInstruction()
else:
self = object.__new__(cls)
object.__setattr__(self, "index", index)
object.__setattr__(self, "statements", stmts)
return self
# }}}
# {{{ interface impls
child_attrs = ("statements",)
[docs]
@cached_property
def named_terminal_intents(self) -> dict:
intents = {}
for stmt in self.statements:
for term, intent in stmt.named_terminal_intents.items():
_add_named_terminal_intent(intents, term, intent)
return intents
# }}}
def __str__(self) -> str:
stmt_str = "\n".join(textwrap.indent(str(stmt), " ") for stmt in self.statements)
return f"""loop(
{self.index},
[
{stmt_str}
]
)"""
[docs]
@pyop3.record.frozenrecord()
class InstructionList(NonTerminalInstruction):
"""A list of instructions."""
# {{{ instance attrs
instructions: tuple[Instruction]
[docs]
def get_disk_cache_key(self, visitor):
return (type(self), tuple(visitor(insn) for insn in self.instructions))
get_instruction_executor_cache_key = get_disk_cache_key
[docs]
def collect_buffers(self, visitor):
return OrderedFrozenSet().union(*(map(visitor, self.instructions)))
def __new__(cls, *args, **kwargs):
if args:
insns = utils.just_one(args)
else:
insns = kwargs.pop("instructions")
assert not kwargs
processed_insns = []
for insn in insns:
if isinstance(insn, NullInstruction):
continue
elif isinstance(insn, InstructionList):
processed_insns.extend(insn)
else:
processed_insns.append(insn)
insns = tuple(processed_insns)
if len(insns) == 0:
return NullInstruction()
elif len(insns) == 1:
return utils.just_one(insns)
else:
self = object.__new__(cls)
object.__setattr__(self, "instructions", insns)
return self
# }}}
# {{{ interface impls
child_attrs = ("instructions",)
[docs]
@cached_property
def named_terminal_intents(self) -> OrderedFrozenSet[Tensor]:
intents = {}
for insn in self.instructions:
for term, intent in insn.named_terminal_intents.items():
_add_named_terminal_intent(intents, term, intent)
return intents
# }}}
def __iter__(self):
return iter(self.instructions)
def __str__(self) -> str:
return "\n".join(map(str, self.instructions))
[docs]
def enlist(insn: Instruction) -> InstructionList:
if isinstance(insn, InstructionList):
return insn
elif isinstance(insn, NullInstruction):
return InstructionList(())
else:
return InstructionList([insn])
[docs]
def maybe_enlist(instructions) -> Instruction:
flattened_insns = []
for insn in filter_null(instructions):
if isinstance(insn, InstructionList):
flattened_insns.extend(insn.instructions)
else:
flattened_insns.append(insn)
if not flattened_insns:
return NullInstruction()
elif len(flattened_insns) > 1:
return InstructionList(flattened_insns)
else:
return utils.just_one(flattened_insns)
[docs]
def non_null(instruction: Instruction) -> bool:
return not isinstance(instruction, NullInstruction)
[docs]
def filter_null(iterable: Iterable[Instruction]):
return filter(non_null, iterable)
[docs]
class NonEmptyTerminal(TerminalInstruction, metaclass=abc.ABCMeta):
@property
@abc.abstractmethod
def axis_trees(self):
pass
@property
@abc.abstractmethod
def shape(self):
pass
[docs]
@dataclasses.dataclass(frozen=True)
class ArgumentSpec:
intent: Intent
dtype: np.dtype
[docs]
class FunctionArgument(abc.ABC):
"""Abstract class for types that may be passed to functions."""
[docs]
@pyop3.record.frozenrecord()
class Function(pyop3.obj.Object):
"""A callable function.
Parameters
----------
loopy_kernel
The loopy kernel that implements the function.
access_descrs
The access descriptor of each kernel argument.
include_dirs
The include directories that are needed to compile the kernel.
lib_dirs
The library directories that are needed to link the kernel.
libs
The libraries that are needed to link the kernel.
"""
# {{{ instance attrs
code: Any
_access_descrs: tuple[Intent, ...]
_compiler_options: pyop3.cc.CompilerOptions
[docs]
def get_disk_cache_key(self, visitor) -> Hashable:
return (
type(self),
utils._loopy_key_builder(self.code),
self._access_descrs,
)
get_instruction_executor_cache_key = get_disk_cache_key
def __init__(
self,
loopy_kernel,
access_descrs,
*,
include_dirs: Iterable[str] = (),
lib_dirs: Iterable[str] = (),
libs: Iterable[str] = (),
):
lpy_args = loopy_kernel.default_entrypoint.args
if len(lpy_args) != len(access_descrs):
raise ValueError("Wrong number of access descriptors given")
loopy_kernel = fix_intents(loopy_kernel, access_descrs)
access_descrs = tuple(access_descrs)
compiler_options = pyop3.cc.CompilerOptions(
include_dirs=tuple(include_dirs),
lib_dirs=tuple(lib_dirs),
libs=tuple(libs),
)
object.__setattr__(self, "code", loopy_kernel)
object.__setattr__(self, "_access_descrs", access_descrs)
object.__setattr__(self, "_compiler_options", compiler_options)
# }}}
@property
def comm(self) -> MPI.Comm:
return MPI.COMM_SELF
# {{{ interface impls
compiler_options = pyop3.record.attr("_compiler_options")
# }}}
# {{{ factory methods
[docs]
@classmethod
def from_c_string(
cls,
/,
name: str,
c_code: str,
args: Iterable[tuple[str, np.dtype, Intent]],
*,
preambles=(),
**kwargs,
) -> Function:
from pyop3.compile.loopy import LOOPY_LANG_VERSION, LOOPY_TARGET
loopy_insn = lp.CInstruction(
(),
c_code,
frozenset((name_ for name_, _, _ in args)),
tuple(name_ for name_, _, intent in args if intent != Intent.READ),
)
loopy_args = []
for name_, dtype, intent in args:
match intent:
case Intent.READ:
is_input = True
is_output = False
case Intent.WRITE:
is_input = True # is this needed?
is_output = True
case Intent.RW | Intent.INC | Intent.MAX_RW | Intent.MAX_WRITE | Intent.MIN_RW | Intent.MIN_WRITE:
is_input = True
is_output = True
case _:
raise NotImplementedError
if isinstance(dtype, lp.types.OpaqueType):
# no packing, passthrough arg
loopy_arg = lp.ValueArg(name_, dtype, is_input=True, is_output=False)
else:
loopy_arg = lp.GlobalArg(name_, dtype, is_input=is_input, is_output=is_output)
loopy_args.append(loopy_arg)
loopy_kernel = lp.make_kernel(
[], # no extra loops
[loopy_insn],
loopy_args,
name=name,
preambles=[
("20_petsc", "#include <petsc.h>"),
*preambles,
],
target=LOOPY_TARGET,
lang_version=LOOPY_LANG_VERSION,
)
intents = [intent for _, _, intent in args]
return cls(loopy_kernel, intents, **kwargs)
# }}}
[docs]
def __call__(self, *args):
# if not all(isinstance(a, FunctionArgument) for a in args):
# raise TypeError("invalid kernel argument type")
if len(args) != len(self.argspec):
raise ValueError(
f"Wrong number of arguments provided, expected {len(self.argspec)} "
f"but received {len(args)}"
)
# if any(
# spec.dtype.numpy_dtype != arg.kernel_dtype
# for spec, arg in checked_zip(self.argspec, args)
# if arg.kernel_dtype is not auto
# ):
# raise ValueError("Arguments to the kernel have the wrong dtype")
return CalledFunction(self, args)
@property
def argspec(self):
spec = []
for access, arg in zip(
self._access_descrs, self.code.default_entrypoint.args, strict=True
):
spec.append(ArgumentSpec(access, arg.dtype))
return tuple(spec)
@property
def intents(self):
return tuple(a.intent for a in self.argspec)
@property
def name(self):
return self.code.default_entrypoint.name
@property
def num_flops(self) -> int:
import pyop3.debug
pyop3.debug.warn_todo("Function.num_flops isn't implemented, returning 666 for now")
return 666
[docs]
class AbstractCalledFunction(NonEmptyTerminal, metaclass=abc.ABCMeta):
__abstract_record_attrs = ("function", "_arguments")
# {{{ interface impls
# TODO: look at the arguments too
[docs]
@cached_property
def compiler_options(self) -> pyop3.cc.CompilerOptions:
return self.function.compiler_options
# }}}
def __str__(self) -> str:
return f"{self.name}({', '.join(arg.name for arg in self.arguments)})"
# or an empty tuple?
axis_trees = (UNIT_AXIS_TREE,)
shape = ()
@property
def name(self):
return self.function.name
@property
def argspec(self):
return self.function.argspec
[docs]
@cached_property
def function_arguments(self):
return tuple((arg, spec.intent) for arg, spec in zip(self.arguments, self.argspec, strict=True))
[docs]
@cached_property
def argument_intents(self):
return tuple(
(arg, intent)
for arg, intent in zip(self._arguments, self.function.intents, strict=True)
)
@property
def argument_shapes(self):
return tuple(
arg.shape if not isinstance(arg, lp.ValueArg) else ()
for arg in self.function.code.default_entrypoint.args
)
[docs]
def with_arguments(self, arguments):
return self.record_new(_arguments=tuple(arguments))
[docs]
@pyop3.record.frozenrecord()
class CalledFunction(AbstractCalledFunction):
# {{{ instance attrs
function: Function
_arguments: tuple[Any]
[docs]
def get_instruction_executor_cache_key(self, visitor) -> Hashable:
return (
type(self),
visitor(self.function),
tuple(map(visitor, self._arguments)),
)
def __init__(self, function: Function, arguments: Iterable) -> None:
for arg, intent in zip(arguments, function.intents, strict=True):
if (
isinstance(arg, pyop3.expr.Tensor)
and isinstance(arg.buffer, pyop3.buffer.PetscMatBuffer)
and intent not in [pyop3.constants.WRITE, pyop3.constants.INC]
):
raise pyop3.exceptions.InvalidIntentException(
f"PETSc mats can only be accessed using WRITE or INC, not {intent}"
)
function = self._fixup_function_argument_shapes(function, arguments)
arguments = tuple(arguments)
object.__setattr__(self, "function", function)
object.__setattr__(self, "_arguments", arguments)
# }}}
@classmethod
def _fixup_function_argument_shapes(cls, function, arguments):
loopy_kernel = function.code.default_entrypoint
if all(
a.shape is not None for a in loopy_kernel.args
if isinstance(a, lp.ArrayArg)
):
return function
new_loopy_args = []
for loopy_arg, arg in zip(loopy_kernel.args, arguments, strict=True):
if isinstance(loopy_arg, lp.ArrayArg):
loopy_arg = loopy_arg.copy(shape=(arg.size,), dim_tags=None)
new_loopy_args.append(loopy_arg)
new_loopy_args = tuple(new_loopy_args)
return function.record_new(
code=function.code.with_kernel(loopy_kernel.copy(args=new_loopy_args))
)
[docs]
@pyop3.record.frozenrecord()
class StandaloneCalledFunction(AbstractCalledFunction):
"""A called function whose arguments do not need packing/unpacking."""
function: Function
_arguments: Iterable[FunctionArgument]
[docs]
def get_disk_cache_key(self, visitor):
return (
type(self),
visitor(self.function),
tuple(map(visitor, self._arguments)),
)
[docs]
def collect_buffers(self, visitor):
return OrderedFrozenSet().union(*(map(visitor, self.arguments)))
def __init__(self, function: Function, arguments: Iterable) -> None:
arguments = tuple(arguments)
object.__setattr__(self, "function", function)
object.__setattr__(self, "_arguments", arguments)
# TODO: Make this a singleton like UNIT_AXIS_TREE
[docs]
class NullInstruction(TerminalInstruction):
"""An instruction that does nothing."""
# {{{ instance attrs (there aren't any)
[docs]
def collect_buffers(self, visitor):
return OrderedFrozenSet()
# }}}
arguments = ()
shape = ()
# TODO: With Python 3.11 can be made a StrEnum
[docs]
class AssignmentType(enum.Enum):
WRITE = "write"
INC = "inc"
MAX = "max"
MIN = "min"
[docs]
def assignment_type_as_intent(assignment_type: AssignmentType) -> Intent:
match assignment_type:
case AssignmentType.WRITE:
return Intent.WRITE
case AssignmentType.INC:
return Intent.INC
case AssignmentType.MAX:
return Intent.MAX_WRITE # not obvious why it's WRITE here
case AssignmentType.MIN:
return Intent.MIN_WRITE
case _:
raise AssertionError(f"{assignment_type} not recognised")
[docs]
class AbstractAssignmentLike(TerminalInstruction):
__abstract_record_attrs = ("_assignee", "expression")
@property
def arguments(self) -> tuple[Any, Any]:
return (self._assignee, self.expression)
[docs]
@cached_property
def compiler_options(self) -> pyop3.cc.CompilerOptions:
return pyop3.cc.CompilerOptions()
[docs]
@cached_property
def shape(self):
return pyop3.expr.visitors.get_shape(self.assignee)
[docs]
class AbstractAssignment(AbstractAssignmentLike):
__abstract_record_attrs = ("assignment_type",)
def __record_post_init(self) -> None:
# Complex numbers are not ordered, so min and max reductions are not defined for them.
if self.assignment_type in (AssignmentType.MAX, AssignmentType.MIN):
if np.issubdtype(self.assignee.dtype, np.complexfloating):
raise InvalidScalarTypeException(
f"'{self.assignment_type.value}' assignment is not supported "
f"for complex data (dtype '{self.assignee.dtype}')"
)
[docs]
@cached_property
def argument_intents(self) -> dict:
assignee_intent = assignment_type_as_intent(self.assignment_type)
return ((self._assignee, assignee_intent), (self.expression, Intent.READ))
def __str__(self) -> str:
match self.assignment_type:
case AssignmentType.WRITE:
operator = "="
case AssignmentType.INC:
operator = "+="
case AssignmentType.MAX:
operator = "max="
case AssignmentType.MIN:
operator = "min="
case _:
raise AssertionError(f"{self.assignment_type} not recognised")
# 'assignee' and 'expression' might be multi-component and thus have
# multi-line representations. We want to line these up.
# NOTE: This might not be the ideal solution, eagerly break the Assignment up?
expression_strs = str(self.expression).split("\n")
if isinstance(self._assignee, weakref.ReferenceType):
assignee = self._assignee()
else:
assignee = self._assignee
if assignee is None:
assignee_strs = ["<dead>" for _ in expression_strs]
else:
assignee_strs = str(self.assignee).split("\n")
if len(assignee_strs) > 1:
if len(expression_strs) > 1:
return "\n".join((
f"{assignee} {operator} {expression}"
for assignee, expression in zip(assignee_strs, expression_strs, strict=True)
))
else:
return "\n".join(
f"{assignee} {operator} {utils.just_one(expression_strs)}"
for assignee in assignee_strs
)
else:
if len(expression_strs) > 1:
return "\n".join(
f"{utils.just_one(assignee_strs)} {operator} {expr}"
for expr in expression_strs
)
else:
return f"{utils.just_one(assignee_strs)} {operator} {utils.just_one(expression_strs)}"
[docs]
@pyop3.record.frozenrecord()
class Assignment(AbstractAssignment):
# {{{ instance attrs
_assignee: Any
expression: Any
assignment_type: AssignmentType
@property
def assignee(self):
if isinstance(self._assignee, weakref.ReferenceType):
assignee_ = self._assignee()
assert assignee_ is not None
return assignee_
else:
return self._assignee
[docs]
def get_instruction_executor_cache_key(self, visitor) -> Hashable:
return (
type(self),
visitor(self.assignee),
visitor(self.expression),
self.assignment_type,
)
def __init__(self, assignee: Any, expression: Any, assignment_type: AssignmentType | str) -> None:
assignment_type = AssignmentType(assignment_type)
object.__setattr__(self, "_assignee", assignee)
object.__setattr__(self, "expression", expression)
object.__setattr__(self, "assignment_type", assignment_type)
# }}}
[docs]
def with_arguments(self, arguments):
assignee, expression = arguments
return self.record_new(_assignee=assignee, expression=expression)
[docs]
@cached_property
def comm(self) -> MPI.Comm:
import pyop3.visitors
return pyop3.visitors.common_comm([self.assignee, self.expression])
# TODO: rename NonEmptyAssignment, is this even needed?
[docs]
@pyop3.record.frozenrecord()
class NonEmptyArrayAssignment(AbstractAssignment, NonEmptyTerminal):
"""Assignment type."""
_assignee: Any
expression: Any
_axis_trees: tuple[AxisTree, ...]
assignment_type: AssignmentType
# is this still needed?
_comm: MPI.Comm = dataclasses.field(hash=False)
[docs]
def get_disk_cache_key(self, visitor) -> Hashable:
return (
type(self),
visitor(self.assignee),
visitor(self.expression),
*(map(visitor, self._axis_trees)),
self.assignment_type,
)
[docs]
def collect_buffers(self, visitor) -> OrderedFrozenSet[pyop3.buffer.ConcreteBuffer]:
return OrderedFrozenSet().union(
visitor(self.assignee),
visitor(self.expression),
*(visitor(tree) for tree in self._axis_trees),
)
def __init__(
self,
assignee: Any,
expression: Any,
axis_trees,
assignment_type: AssignmentType | str,
*,
comm: MPI.Comm,
) -> None:
assignment_type = AssignmentType(assignment_type)
object.__setattr__(self, "_assignee", assignee)
object.__setattr__(self, "expression", expression)
object.__setattr__(self, "_axis_trees", axis_trees)
object.__setattr__(self, "assignment_type", assignment_type)
object.__setattr__(self, "_comm", comm)
@property
def assignee(self):
return self._assignee
@property
def axis_trees(self):
return self._axis_trees
@property
def comm(self) -> MPI.Comm:
return self._comm
[docs]
def with_arguments(self, arguments):
a, e = arguments
return self.record_new(_assignee=a, expression=e)
[docs]
@pyop3.record.frozenrecord()
class Exscan(AbstractAssignmentLike):
# {{{ instance attrs
_assignee: Any
expression: ExpressionT
scan_type: Any
scan_axis: pyop3.Axis
_comm: MPI.Comm = dataclasses.field(hash=False)
[docs]
def collect_buffers(self, visitor):
return OrderedFrozenSet().union(
visitor(self.assignee),
visitor(self.expression),
visitor(self.scan_axis),
)
[docs]
def get_disk_cache_key(self, visitor) -> Hashable:
return (
type(self),
visitor(self.assignee),
visitor(self.expression),
self.scan_type,
visitor(self.scan_axis),
)
[docs]
def get_instruction_executor_cache_key(self, visitor) -> Hashable:
assignee_key = visitor(self.assignee)
expression_key = visitor(self.expression)
with visitor.identity_hash_buffers():
scan_axis_key = visitor(self.scan_axis)
return (
type(self),
assignee_key,
expression_key,
self.scan_type,
scan_axis_key,
)
# }}}
[docs]
@cached_property
def argument_intents(self) -> dict:
return ((self._assignee, Intent.RW), (self.expression, Intent.READ))
@property
def assignee(self):
return self._assignee
@property
def comm(self):
return self._comm
@property
def arguments(self) -> tuple[Any, Any]:
return (self._assignee, self.expression)
[docs]
def with_arguments(self, arguments):
a, e = arguments
return self.record_new(_assignee=a, expression=e)
[docs]
@cached_property
def extent(self):
return self.scan_axis.component.size - 1
[docs]
@cached_property
def compiler_options(self) -> pyop3.cc.CompilerOptions:
return pyop3.cc.CompilerOptions()
[docs]
def exscan(*args, eager: bool = False, **kwargs):
expr = Exscan(*args, **kwargs)
return expr() if eager else expr
# The idea is basically RW isn't allowed here but READ is (unlike AssignmentType)
[docs]
class ArrayAccessType(enum.StrEnum):
READ = "read"
WRITE = "write"
INC = "inc"
MAX = "max"
MIN = "min"
[docs]
def loop_(*args, eager: bool = False, **kwargs) -> Loop | None:
"""
Notes
-----
This function has a trailing underscore to avoid clashing with any variables
called ``loop``. It is exported as ``op3.loop``.
"""
if eager:
compiler_parameters = kwargs.pop("compiler_parameters", None)
loop_expr = Loop(*args, **kwargs)
return loop_expr(compiler_parameters=compiler_parameters) if eager else loop_expr
[docs]
def fix_intents(tunit, accesses):
"""
The local kernel has underspecified accessors (is_input, is_output).
Here coerce them to match the access descriptors provided.
This should arguably be done properly in TSFC.
Note that even if this isn't done in TSFC we need to guard against this properly
as the default error is very unclear.
"""
kernel = tunit.default_entrypoint
new_args = []
for arg, access in zip(kernel.args, accesses, strict=True):
if isinstance(arg.dtype, lp.types.OpaqueType):
# no packing, passthrough arg
is_input = True
is_output = False
else:
is_input = access in {Intent.READ, Intent.RW, Intent.INC, Intent.MIN_RW, Intent.MAX_RW}
is_output = access in {Intent.WRITE, Intent.RW, Intent.INC, Intent.MIN_RW, Intent.MIN_WRITE, Intent.MAX_WRITE, Intent.MAX_RW}
new_args.append(arg.copy(is_input=is_input, is_output=is_output))
return tunit.with_kernel(kernel.copy(args=new_args))