Skip to content

Custom operations over user defined types that creates new instance of userdefined type #564

@gsvgit

Description

@gsvgit

Is it possible to define custom operation that creates instances of user defined type?

NP_UDT = np.dtype([("x", np.int64), ("y", np.float64)], align=True)
UDT = gb.dtypes.register_new("UDT", NP_UDT)

v1 = gb.Vector(Point, size=10)
v1[0] = (1, 4.5) # Tuple works here

# I want to define a function that creates new UDT from int and float passed as parameters
def udt_add_op(x, y):
    # When I try to use tuple here (similarly to assignment to v1[0]) it fails with exception below.
    return ??? # What should be here to create new UDT(x,y) ?

BinaryOp.register_new("udt_add", udt_add_op,is_udt=True)

def v_add(v1, v2):
    gb.Vector.ewise_add(v1, v2, "udt_add")
    ...

Exception on attempt to return tuple from udt_add_op:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "example.py", line 24, in v_add
    v3 << gb.Vector.ewise_add(v1, v2, "points_add")
  File "/miniconda3/lib/python3.10/site-packages/graphblas/core/vector.py", line 998, in ewise_add
    return self._ewise_add(other, op)
  File "/miniconda3/lib/python3.10/site-packages/graphblas/core/vector.py", line 1029, in _ewise_add
    op = get_typed_op(op, self.dtype, other.dtype, kind="binary")
  File "/miniconda3/lib/python3.10/site-packages/graphblas/core/operator/utils.py", line 112, in get_typed_op
    return get_typed_op(
  File "/miniconda3/lib/python3.10/site-packages/graphblas/core/operator/utils.py", line 43, in get_typed_op
    return op._compile_udt(dtype, dtype2)
  File "/miniconda3/lib/python3.10/site-packages/graphblas/core/operator/binary.py", line 609, in _compile_udt
    ret_type = lookup_dtype(numba_func.overloads[sig].signature.return_type)
  File "/miniconda3/lib/python3.10/site-packages/graphblas/core/dtypes.py", line 374, in lookup_dtype
    raise ValueError(f"Unknown dtype: {key} of type {type(key)}")
ValueError: Unknown dtype: Tuple(Literal[int](1), float64) of type <class 'numba.core.types.containers.Tuple'>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions