Exemple #1
0
    def __new__(meta, type_name, bases, members):
        if members.get('base'):
            cls = super().__new__(meta, type_name, bases, members)
            return cls
        m = members['members']
        cls_ffi = members['ffi'] = FFI()
        cls_ffi.cdef(build_udt_def(type_name, m))
        t = ffi.new('GrB_Type*')
        _check(lib.GrB_Type_new(t, cls_ffi.sizeof(type_name)))
        cffi_support.map_type(cls_ffi.typeof(type_name), use_record_dtype=True)
        members['gb_type'] = t[0]

        cls = super().__new__(meta, type_name, bases, members)
        cls.member_def = list(map(methodcaller('split'), m))
        cls.C = type_name
        cls.ptr = type_name + '*'
        get = partial(getattr, lib)
        cls.Matrix_setElement = lib.GrB_Matrix_setElement_UDT
        cls.Matrix_extractElement = lib.GrB_Matrix_extractElement_UDT
        cls.Matrix_extractTuples = lib.GrB_Matrix_extractTuples_UDT
        cls.Matrix_assignScalar = lib.GrB_Matrix_assign_UDT
        cls.Vector_setElement = lib.GrB_Vector_setElement_UDT
        cls.Vector_extractElement = lib.GrB_Vector_extractElement_UDT
        cls.Vector_extractTuples = lib.GrB_Vector_extractTuples_UDT
        cls.Vector_assignScalar = lib.GrB_Vector_assign_UDT
        cls.Scalar_setElement = lib.GxB_Scalar_setElement_UDT
        cls.Scalar_extractElement = lib.GxB_Scalar_extractElement_UDT
        cls.identity = cls.from_value(cls.identity)
        for op_name in ['eq_op', 'add_op', 'mult_op']:
            if not hasattr(cls, op_name):
                setattr(cls, op_name, cls.ffi.NULL)
        return cls
Exemple #2
0
 def new_semiring(cls, monoid, op):
     from .semiring import Semiring
     semiring = core_ffi.new('GrB_Semiring[1]')
     _check(
         lib.GrB_Semiring_new(semiring, monoid[0],
                              op.get_binaryop(core_ffi.NULL)))
     return Semiring('PLUS', 'TIMES', cls.__name__, semiring[0])
Exemple #3
0
 def new_monoid(cls, op, identity):
     monoid = core_ffi.new('GrB_Monoid[1]')
     if cls.base_name == 'UDT':
         i = cls.ffi.new(cls.ptr)
         i[0] = identity
         identity = i
     _check(cls.Monoid_new(monoid, op.binaryop, identity))
     return monoid
Exemple #4
0
    def new_semiring(cls, monoid, op):
        from .semiring import Semiring

        semiring = core_ffi.new("GrB_Semiring[1]")
        _check(
            lib.GrB_Semiring_new(semiring, monoid.get_monoid(),
                                 op.get_binaryop(core_ffi.NULL)))
        return Semiring("PLUS", "TIMES", cls.__name__, semiring[0], udt=cls)
Exemple #5
0
    def new_monoid(cls, op, identity):
        from .monoid import Monoid

        monoid = core_ffi.new("GrB_Monoid[1]")
        if cls.base_name == "UDT":
            i = cls.ffi.new(cls.ptr)
            i[0] = identity
            identity = i
        _check(cls.Monoid_new(monoid, op.binaryop, identity))
        return Monoid("PLUS", cls.__name__, monoid[0], udt=cls)
Exemple #6
0
    def __new__(meta, type_name, bases, attrs):
        if attrs.get('base', False):
            cls = super().__new__(meta, type_name, bases, attrs)
            return cls
        if 'members' in attrs:
            m = attrs['members']
            cls_ffi = FFI()
            cls_ffi.cdef(build_udt_def(type_name, m))
            t = core_ffi.new('GrB_Type*')
            _check(lib.GrB_Type_new(t, cls_ffi.sizeof(type_name)))
            cffi_support.map_type(cls_ffi.typeof(type_name),
                                  use_record_dtype=True)
            attrs['ffi'] = cls_ffi
            attrs['gb_type'] = t[0]
            attrs['C'] = type_name
            attrs['member_def'] = list(map(methodcaller('split'), m))
            attrs['base_name'] = 'UDT'
        else:
            attrs['ffi'] = core_ffi
            gb_type_name = type_name

        cls = super().__new__(meta, type_name, bases, attrs)
        meta._gb_type_map[cls.gb_type] = cls
        cls.ptr = cls.C + '*'
        cls.zero = getattr(cls, 'zero', core_ffi.NULL)
        cls.one = getattr(cls, 'one', core_ffi.NULL)
        get = partial(getattr, lib)
        cls.base_name = base_name = getattr(cls, 'base_name', cls.__name__)
        cls.Monoid_new = get('GrB_Monoid_new_{}'.format(base_name))
        cls.Matrix_setElement = get(
            'GrB_Matrix_setElement_{}'.format(base_name))
        cls.Matrix_extractElement = get(
            'GrB_Matrix_extractElement_{}'.format(base_name))
        cls.Matrix_extractTuples = get(
            'GrB_Matrix_extractTuples_{}'.format(base_name))
        cls.Matrix_assignScalar = get('GrB_Matrix_assign_{}'.format(base_name))
        cls.Vector_setElement = get(
            'GrB_Vector_setElement_{}'.format(base_name))
        cls.Vector_extractElement = get(
            'GrB_Vector_extractElement_{}'.format(base_name))
        cls.Vector_extractTuples = get(
            'GrB_Vector_extractTuples_{}'.format(base_name))
        cls.Vector_assignScalar = get('GrB_Vector_assign_{}'.format(base_name))
        cls.Scalar_setElement = get(
            'GxB_Scalar_setElement_{}'.format(base_name))
        cls.Scalar_extractElement = get(
            'GxB_Scalar_extractElement_{}'.format(base_name))
        return cls
Exemple #7
0
 def new_semiring(cls, monoid, op):
     from .semiring import Semiring
     semiring = ffi.new('GrB_Semiring[1]')
     _check(lib.GrB_Semiring_new(semiring, monoid[0], op.binaryop))
     return Semiring('add', 'mult', cls.__name__, semiring[0])
Exemple #8
0
 def new_monoid(cls, op, identity):
     monoid = ffi.new('GrB_Monoid[1]')
     _check(lib.GrB_Monoid_new_UDT(monoid, op.binaryop, identity))
     return monoid
Exemple #9
0
    def __new__(meta, type_name, bases, attrs):
        if attrs.get("base", False):
            cls = super().__new__(meta, type_name, bases, attrs)
            return cls
        if "members" in attrs:
            m = attrs["members"]
            cls_ffi = FFI()
            cls_ffi.cdef(build_udt_def(type_name, m))
            t = core_ffi.new("GrB_Type*")
            _check(lib.GrB_Type_new(t, cls_ffi.sizeof(type_name)))
            cffi_support.map_type(cls_ffi.typeof(type_name),
                                  use_record_dtype=True)
            attrs["ffi"] = cls_ffi
            attrs["gb_type"] = t[0]
            attrs["C"] = type_name
            attrs["member_def"] = list(map(methodcaller("split"), m))
            attrs["base_name"] = "UDT"
        else:
            attrs["ffi"] = core_ffi
            gb_type_name = type_name

        cls = super().__new__(meta, type_name, bases, attrs)
        meta._gb_type_map[cls.gb_type] = cls
        meta._type_gb_map[cls] = cls.gb_type
        meta._gb_name_type_map[type_name] = cls
        meta._gb_name_type_map[cls.C] = cls

        cls.ptr = cls.C + "*"
        cls.zero = getattr(cls, "zero", core_ffi.NULL)
        cls.one = getattr(cls, "one", core_ffi.NULL)
        get = partial(getattr, lib)
        cls.base_name = base_name = getattr(cls, "base_name", cls.__name__)
        cls.prefix = prefix = getattr(cls, "prefix", "GrB")
        cls.Monoid_new = get("{}_Monoid_new_{}".format(prefix, base_name))
        cls.Matrix_setElement = get("{}_Matrix_setElement_{}".format(
            prefix, base_name))
        cls.Matrix_extractElement = get("{}_Matrix_extractElement_{}".format(
            prefix, base_name))
        cls.Matrix_extractTuples = get("{}_Matrix_extractTuples_{}".format(
            prefix, base_name))
        cls.Matrix_assignScalar = get("{}_Matrix_assign_{}".format(
            prefix, base_name))
        cls.Matrix_apply_BinaryOp1st = get(
            "{}_Matrix_apply_BinaryOp1st_{}".format(prefix, base_name))
        cls.Matrix_apply_BinaryOp2nd = get(
            "{}_Matrix_apply_BinaryOp2nd_{}".format(prefix, base_name))
        cls.Vector_setElement = get("{}_Vector_setElement_{}".format(
            prefix, base_name))
        cls.Vector_extractElement = get("{}_Vector_extractElement_{}".format(
            prefix, base_name))
        cls.Vector_extractTuples = get("{}_Vector_extractTuples_{}".format(
            prefix, base_name))
        cls.Vector_assignScalar = get("{}_Vector_assign_{}".format(
            prefix, base_name))
        cls.Vector_apply_BinaryOp1st = get(
            "{}_Vector_apply_BinaryOp1st_{}".format(prefix, base_name))
        cls.Vector_apply_BinaryOp2nd = get(
            "{}_Vector_apply_BinaryOp2nd_{}".format(prefix, base_name))
        cls.Scalar_setElement = get(
            "GxB_Scalar_setElement_{}".format(base_name))
        cls.Scalar_extractElement = get(
            "GxB_Scalar_extractElement_{}".format(base_name))
        return cls