Skip to content

malloc_buffer

MallocBuffer

Bases: Buffer

A buffer that uses buffers managed by malloc to store data.

Source code in src/finch/codegen/buffers/malloc_buffer.py
class MallocBuffer(Buffer):
    """
    A buffer that uses buffers managed by malloc to store data.
    """

    def __init__(self, length: int, dtype: FType, data=None):
        """
        Constructor for the MallocBuffer class.

        length (int): the length of the malloc array.
        dtype (FType): the type that the buffer will be based on.
        data (optional): a list of data to initialize the buffer with.
        """
        self._dtype = dtype
        self._c_dtype = c_type(self._dtype)
        self.buffer = ctypes.pointer(CMallocBufferStruct())

        MallocBufferBackend.library(self.ftype).init(
            self.buffer,
            serialize_to_c(self.length_type, ctypes.sizeof(self._c_dtype)),
            serialize_to_c(self.length_type, length),
        )
        if data is None:
            return
        if len(data) > length:
            raise IndexError

        for idx, elt in enumerate(data):
            self.castbuffer[idx] = serialize_to_c(self._dtype, elt)

    def __del__(self):
        """
        Frees the mallocbuffer stored inside.
        """
        if hasattr(self, "buffer"):
            MallocBufferBackend.library(self.ftype).free(self.buffer)

    @property
    def castbuffer(self):
        return ctypes.cast(self.buffer.contents.data, ctypes.POINTER(self._c_dtype))

    @property
    def ftype(self):
        """
        Returns the ftype of the buffer, which is a MallocBufferFType.
        """
        return MallocBufferFType(self._dtype)

    # TODO should be property
    def length(self):
        return np.intp(self.buffer.contents.length)

    def load(self, idx):
        value = self.castbuffer[idx]
        return construct_from_c(self.ftype.element_type, value)

    def store(self, idx, val):
        val = serialize_to_c(self.ftype.element_type, val)
        self.castbuffer[idx] = val

    def to_numpy(self, *, copy: bool | None = None):
        # Native resize/free can invalidate views, so exported arrays own their data.
        if copy is False:
            raise ValueError(
                "Unable to avoid copy while creating an array as requested."
            )
        return np.fromiter(
            (self.load(i) for i in range(self.length())),
            dtype=np_dtype(self.element_type),
            count=self.length(),
        )

    def resize(self, new_len):
        # we effectively have to run an entire serialization cycle ourselves.
        old_length = serialize_to_c(self.ftype.length_type, self.length())
        new_len = serialize_to_c(self.ftype.length_type, new_len)
        newptr = MallocBufferBackend.library(self.ftype).resize(
            self.castbuffer, old_length, new_len
        )
        self.buffer.contents.data = ctypes.cast(newptr, ctypes.c_void_p)
        self.buffer.contents.length = new_len

    def __str__(self):
        array = self.castbuffer[: self.length()]
        return f"malloc_buf({array})"

ftype property

Returns the ftype of the buffer, which is a MallocBufferFType.

__del__()

Frees the mallocbuffer stored inside.

Source code in src/finch/codegen/buffers/malloc_buffer.py
def __del__(self):
    """
    Frees the mallocbuffer stored inside.
    """
    if hasattr(self, "buffer"):
        MallocBufferBackend.library(self.ftype).free(self.buffer)

__init__(length, dtype, data=None)

Constructor for the MallocBuffer class.

length (int): the length of the malloc array. dtype (FType): the type that the buffer will be based on. data (optional): a list of data to initialize the buffer with.

Source code in src/finch/codegen/buffers/malloc_buffer.py
def __init__(self, length: int, dtype: FType, data=None):
    """
    Constructor for the MallocBuffer class.

    length (int): the length of the malloc array.
    dtype (FType): the type that the buffer will be based on.
    data (optional): a list of data to initialize the buffer with.
    """
    self._dtype = dtype
    self._c_dtype = c_type(self._dtype)
    self.buffer = ctypes.pointer(CMallocBufferStruct())

    MallocBufferBackend.library(self.ftype).init(
        self.buffer,
        serialize_to_c(self.length_type, ctypes.sizeof(self._c_dtype)),
        serialize_to_c(self.length_type, length),
    )
    if data is None:
        return
    if len(data) > length:
        raise IndexError

    for idx, elt in enumerate(data):
        self.castbuffer[idx] = serialize_to_c(self._dtype, elt)

MallocBufferBackend

Source code in src/finch/codegen/buffers/malloc_buffer.py
class MallocBufferBackend:
    _library: dict[Hashable, CMallocBufferLibrary] = {}

    @classmethod
    def gen_code(
        cls,
        ctx: CContext,
        ftype: MallocBufferFType,
        inline: bool = False,
    ) -> CMallocBufferMethods:
        ctx.add_header("#include <string.h>")
        ctx.add_header("#include <stdlib.h>")
        ctx.add_header("#include <stdio.h>")

        methods = CMallocBufferMethods(
            init=ctx.freshen("mallocbuffer_init"),
            free=ctx.freshen("mallocbuffer_free"),
            resize=ctx.freshen("mallocbuffer_resize"),
        )
        ctx.buffer_methods[ftype] = methods

        buffer_type = ctx.ctype_name(CMallocBufferStruct)
        elt_type = ctx.ctype_name(c_type(ftype.element_type))
        length_type = ctx.ctype_name(c_type(ftype.length_type))

        inline_s = "static inline " if inline else "FINCH_EXPORT "
        libcode = dedent(
            f"""
            {inline_s}{elt_type}*
            {methods.resize}(
                {elt_type}* data,
                {length_type} len_old,
                {length_type} len_new
            ) {{
                data = realloc(data, sizeof({elt_type}) * len_new);
                if (data == 0) {{
                    fprintf(stderr, "Malloc Failed!\\n");
                    exit(1);
                }}
                if (len_new > len_old) {{
                    memset(&data[len_old], 0, (len_new - len_old) * sizeof({elt_type}));
                }}
                return data;
            }}
            // methods below are not used by the kernel.
            {inline_s}void
            {methods.free}({buffer_type} *m) {{
                free(m->data);
                m->data = 0;
                m->length = 0;
            }}
            {inline_s}void
            {methods.init}(
                {buffer_type} *m,
                {length_type} datasize,
                {length_type} length
            ) {{
                m->length = length;
                m->data = malloc(length * datasize);
                if (m->data != 0)
                    memset(m->data, 0, length * datasize);
            }}
            """
        )
        ctx.add_header(libcode)
        return methods

    @classmethod
    def library(cls, ftype: MallocBufferFType) -> CMallocBufferLibrary:
        """
        Returns compiled library to operate on a buffer outside of
        the kernel.
        """
        if ftype in cls._library:
            return cls._library[ftype]
        ctx = CContext()
        methods = cls.gen_code(ctx, ftype)
        lib = load_shared_lib(ctx.emit_global())

        length_type = c_type(ftype.length_type)

        resize_func = getattr(lib, methods.resize)
        resize_func.argtypes = [
            ctypes.POINTER(c_type(ftype.element_type)),
            length_type,
            length_type,
        ]
        resize_func.restype = ctypes.POINTER(c_type(ftype.element_type))

        init_func = getattr(lib, methods.init)
        init_func.argtypes = [
            ctypes.POINTER(CMallocBufferStruct),
            length_type,
            length_type,
        ]
        init_func.restype = None

        free_func = getattr(lib, methods.free)
        free_func.argtypes = [
            ctypes.POINTER(CMallocBufferStruct),
        ]
        free_func.restype = None

        cls._library[ftype] = CMallocBufferLibrary(lib, methods)
        return cls._library[ftype]

library(ftype) classmethod

Returns compiled library to operate on a buffer outside of the kernel.

Source code in src/finch/codegen/buffers/malloc_buffer.py
@classmethod
def library(cls, ftype: MallocBufferFType) -> CMallocBufferLibrary:
    """
    Returns compiled library to operate on a buffer outside of
    the kernel.
    """
    if ftype in cls._library:
        return cls._library[ftype]
    ctx = CContext()
    methods = cls.gen_code(ctx, ftype)
    lib = load_shared_lib(ctx.emit_global())

    length_type = c_type(ftype.length_type)

    resize_func = getattr(lib, methods.resize)
    resize_func.argtypes = [
        ctypes.POINTER(c_type(ftype.element_type)),
        length_type,
        length_type,
    ]
    resize_func.restype = ctypes.POINTER(c_type(ftype.element_type))

    init_func = getattr(lib, methods.init)
    init_func.argtypes = [
        ctypes.POINTER(CMallocBufferStruct),
        length_type,
        length_type,
    ]
    init_func.restype = None

    free_func = getattr(lib, methods.free)
    free_func.argtypes = [
        ctypes.POINTER(CMallocBufferStruct),
    ]
    free_func.restype = None

    cls._library[ftype] = CMallocBufferLibrary(lib, methods)
    return cls._library[ftype]

MallocBufferFType

Bases: CBufferFType, CUnpackableFType

A ftype for buffers that uses libc-provided malloc functions. This is a concrete implementation of the BufferFType class.

This does not support the numba backend.

Source code in src/finch/codegen/buffers/malloc_buffer.py
class MallocBufferFType(CBufferFType, CUnpackableFType):
    """
    A ftype for buffers that uses libc-provided malloc functions. This is a
    concrete implementation of the BufferFType class.

    This does not support the numba backend.
    """

    def __init__(self, element_type: FType):
        self._element_type = element_type

    def __eq__(self, other):
        if not isinstance(other, MallocBufferFType):
            return False
        return self._element_type == other._element_type

    def __str__(self):
        return f"malloc_buf_t({qual_str(self._element_type)})"

    def __repr__(self):
        return f"MallocBufferFType({qual_str(self._element_type)})"

    @property
    def length_type(self):
        """
        Returns the type used for the length of the buffer.
        """
        return ftypes.intp

    @property
    def element_type(self):
        """
        Returns the type of elements stored in the buffer. This will be a ctypes array.
        """
        return self._element_type

    def __hash__(self):
        return hash(("MallocBufferFType", self._element_type))

    def __call__(self, len: int = 0):
        return MallocBuffer(len, self._element_type)

    def c_type(self):
        return ctypes.POINTER(CMallocBufferStruct)

    def c_length(self, ctx: CContext, buf: CBufferFields):
        return buf.length

    def c_data(self, ctx: CContext, buf: CBufferFields):
        return buf.data

    def c_load(self, ctx: CContext, buf: CBufferFields, idx: AssemblyExpression):
        return f"({buf.data})[{ctx(idx)}]"

    def c_store(
        self,
        ctx: CContext,
        buf: CBufferFields,
        idx: AssemblyExpression,
        val: AssemblyExpression,
    ):
        ctx.exec(f"{ctx.feed}({buf.data})[{ctx(idx)}] = {ctx(val)};")

    def c_resize(self, ctx: CContext, buf: CBufferFields, new_len: AssemblyExpression):
        if self not in ctx.buffer_methods:
            raise Exception("A Mallocbuffer must be unpacked before being operated on!")

        methods: CMallocBufferMethods = ctx.buffer_methods[self]

        new_len = ctx(ctx.cache("len", new_len))
        data = buf.data
        length = buf.length

        ctx.exec(
            f"{ctx.feed}{data} = {methods.resize}({data}, {length}, {new_len});\n"
            f"{ctx.feed}{length} = {new_len};"
        )
        return

    def c_unpack(self, ctx: CContext, lhs, rhs):
        """
        Unpack the malloc buffer into C context.
        """
        data = ctx.freshen(lhs, "data")
        length = ctx.freshen(lhs, "length")
        t = ctx.ctype_name(c_type(self.element_type))
        ctx.add_header("#include <stddef.h>")

        if self not in ctx.buffer_methods:
            MallocBufferBackend.gen_code(ctx, self, inline=True)

        ctx.exec(
            f"{ctx.feed}{t}* {data} = ({t}*){ctx(rhs)}->data;\n"
            f"{ctx.feed}size_t {length} = {ctx(rhs)}->length;"
        )

        return CBufferFields(data, length, lhs)

    def c_repack(self, ctx, lhs: str, rhs: CBufferFields):
        """
        Repack the buffer from C context.
        """
        ctx.exec(
            f"{ctx.feed}{lhs}->data = (void*){rhs.data};\n"
            f"{ctx.feed}{lhs}->length = {rhs.length};"
        )
        return

    def serialize_to_c(self, obj: MallocBuffer):
        """
        Serialize the Malloc buffer to a C-compatible structure.
        This is trivial.
        """
        return obj.buffer

    def deserialize_from_c(self, obj, c_buffer):
        pass
        # this is handled by the resize callback

    def construct_from_c(self, c_buffer):
        """
        Construct a MallocBuffer from a C-compatible structure.
        """
        return c_buffer.contents

element_type property

Returns the type of elements stored in the buffer. This will be a ctypes array.

length_type property

Returns the type used for the length of the buffer.

c_repack(ctx, lhs, rhs)

Repack the buffer from C context.

Source code in src/finch/codegen/buffers/malloc_buffer.py
def c_repack(self, ctx, lhs: str, rhs: CBufferFields):
    """
    Repack the buffer from C context.
    """
    ctx.exec(
        f"{ctx.feed}{lhs}->data = (void*){rhs.data};\n"
        f"{ctx.feed}{lhs}->length = {rhs.length};"
    )
    return

c_unpack(ctx, lhs, rhs)

Unpack the malloc buffer into C context.

Source code in src/finch/codegen/buffers/malloc_buffer.py
def c_unpack(self, ctx: CContext, lhs, rhs):
    """
    Unpack the malloc buffer into C context.
    """
    data = ctx.freshen(lhs, "data")
    length = ctx.freshen(lhs, "length")
    t = ctx.ctype_name(c_type(self.element_type))
    ctx.add_header("#include <stddef.h>")

    if self not in ctx.buffer_methods:
        MallocBufferBackend.gen_code(ctx, self, inline=True)

    ctx.exec(
        f"{ctx.feed}{t}* {data} = ({t}*){ctx(rhs)}->data;\n"
        f"{ctx.feed}size_t {length} = {ctx(rhs)}->length;"
    )

    return CBufferFields(data, length, lhs)

construct_from_c(c_buffer)

Construct a MallocBuffer from a C-compatible structure.

Source code in src/finch/codegen/buffers/malloc_buffer.py
def construct_from_c(self, c_buffer):
    """
    Construct a MallocBuffer from a C-compatible structure.
    """
    return c_buffer.contents

serialize_to_c(obj)

Serialize the Malloc buffer to a C-compatible structure. This is trivial.

Source code in src/finch/codegen/buffers/malloc_buffer.py
def serialize_to_c(self, obj: MallocBuffer):
    """
    Serialize the Malloc buffer to a C-compatible structure.
    This is trivial.
    """
    return obj.buffer