<commit_before>#pragma once

namespace spark
{
    template<typename T> struct Kernel;

    template<typename T>
    struct device_buffer1d
    {
        device_buffer1d()
        : _count(0)
        , _buffer(nullptr)
        { }

        device_buffer1d(size_t count, const T* data)
        : _count(count)
        , _buffer(spark_create_buffer(size(), data, SPARK_THROW_ON_ERROR()),
            [](spark_buffer_t* buffer)
            {
                spark_destroy_buffer(buffer, SPARK_THROW_ON_ERROR());
            })
        { }

        // zero fill
        device_buffer1d(size_t count) : device_buffer1d(count, nullptr) {}
        template<size_t N>
        device_buffer1d(const T (&arr)[N]) : device_buffer1d(N, arr) {}

        void write(size_t offset, size_t count, const T* data)
        {
            spark_write_buffer(_buffer.get(), sizeof(T) * offset, sizeof(T) * count, data, SPARK_THROW_ON_ERROR());
        }

        void write(size_t count, const T* data)
        {
            this->write(0, count, data);
        }

        void write(const T* data)
        {
            this->write(0, _count, data);
        }

        void zero(size_t offset, size_t count)
        {
            write(offset, count, nullptr);
        }

        void zero(size_t count)
        {
            zero(0, count);
        }

        void zero()
        {
            zero(0, this->_size);
        }

        void read(size_t offset, size_t count, T* dest)
        {
            spark_read_buffer(_buffer.get(), sizeof(T) * offset, sizeof(T) * count, dest, SPARK_THROW_ON_ERROR());
        }

        void read(size_t count, T* dest) const
        {
            read(0, count, dest);
        }

        void read(T* dest) const
        {
            read(0, this->_size, dest);
        }

        int32_t count() const { return _count;}
        size_t size() const { return count() * sizeof(T); }

    private:
        template<typename F>
        friend struct Kernel;

        const size_t _count;
        std::shared_ptr<spark_buffer_t> _buffer;
    };

    template<typename T>
    struct device_buffer2d
    {
        device_buffer2d(size_t width, size_t height, const T* data)
        : _width(width)
        , _height(height)
        , _buffer(spark_create_buffer(sizeof(T) * this->count(), data, SPARK_THROW_ON_ERROR()),
            [](spark_buffer_t* buffer)
            {
                spark_destroy_buffer(buffer, SPARK_THROW_ON_ERROR());
            })
        { }

        device_buffer2d(size_t width, size_t height) : device_buffer2d(width, height, nullptr) {}
        template<size_t M, size_t N><commit_msg>fixed device_buffer' read() methods<commit_after>#pragma once

namespace spark
{
    template<typename T> struct Kernel;

    template<typename T>
    struct device_buffer1d
    {
        device_buffer1d()
        : _count(0)
        , _buffer(nullptr)
        { }

        device_buffer1d(size_t count, const T* data)
        : _count(count)
        , _buffer(spark_create_buffer(size(), data, SPARK_THROW_ON_ERROR()),
            [](spark_buffer_t* buffer)
            {
                spark_destroy_buffer(buffer, SPARK_THROW_ON_ERROR());
            })
        { }

        // zero fill
        device_buffer1d(size_t count) : device_buffer1d(count, nullptr) {}
        template<size_t N>
        device_buffer1d(const T (&arr)[N]) : device_buffer1d(N, arr) {}

        void write(size_t offset, size_t count, const T* data)
        {
            spark_write_buffer(_buffer.get(), sizeof(T) * offset, sizeof(T) * count, data, SPARK_THROW_ON_ERROR());
        }

        void write(size_t count, const T* data)
        {
            this->write(0, count, data);
        }

        void write(const T* data)
        {
            this->write(0, _count, data);
        }

        void zero(size_t offset, size_t count)
        {
            write(offset, count, nullptr);
        }

        void zero(size_t count)
        {
            zero(0, count);
        }

        void zero()
        {
            zero(0, this->size());
        }

        void read(size_t offset, size_t count, T* dest) const
        {
            spark_read_buffer(_buffer.get(), sizeof(T) * offset, sizeof(T) * count, dest, SPARK_THROW_ON_ERROR());
        }

        void read(size_t count, T* dest) const
        {
            read(0, count, dest);
        }

        void read(T* dest) const
        {
            read(0, this->size(), dest);
        }

        size_t count() const { return _count;}
        size_t size() const { return count() * sizeof(T); }

    private:
        template<typename F>
        friend struct Kernel;

        const size_t _count;
        std::shared_ptr<spark_buffer_t> _buffer;
    };

    template<typename T>
    struct device_buffer2d
    {
        device_buffer2d(size_t width, size_t height, const T* data)
        : _width(width)
        , _height(height)
        , _buffer(spark_create_buffer(sizeof(T) * this->count(), data, SPARK_THROW_ON_ERROR()),
            [](spark_buffer_t* buffer)
            {
                spark_destroy_buffer(buffer, SPARK_THROW_ON_ERROR());
            })
        { }

        device_buffer2d(size_t width, size_t height) : device_buffer2d(width, height, nullptr) {}
        template<size_t M, size_t N>
{'fraction_non_alphanumeric': 0.1116569053466843, 'fraction_numerical': 0.007179293406385793, 'mean_word_length': 1.8756110809342748, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1069949', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1638, 'n_words': 349}