<commit_before>module Heikou
  class Buffer
    include FFI::OpenCL

    attr_reader :mem, :host_ptr, :size, :context

    def initialize(cl, context, options, type, size = 1)
      @cl = cl
      @context = context
      @type = type
      @size = size

      @host_ptr = FFI::MemoryPointer.new type, size

      mark_clean

      create(options)
    end

    def finalize
      reference_count.times { release }
    end

    def retain
      clRetainMemObject(@mem)
    end

    def release
      clReleaseMemObject(@mem)
    end

    def mark_clean
      @dirty = false
    end

    def mark_dirty
      @dirty = true
    end

    def dirty?
      @dirty
    end

    def reference_count
      ref_count = FFI::MemoryPointer.new FFI::OpenCL.find_type(:cl_uint)
      clGetMemObjectInfo(@mem, CL_MEM_REFERENCE_COUNT, ref_count.size, ref_count, nil)
      ref_count.read_uint
    end

    def []=(index, value)
      @host_ptr[index].send("write_#{@type}", value)
    end

    def [](index)
      @host_ptr[index].send("read_#{@type}")
    end

    def read
      @cl.default_queue.read_buffer(self)
    end

    def to_a
      read if dirty?
      @host_ptr.send("read_array_of_#{@type}", @size)
    end

    private

    def create(options)<commit_msg>Mark buffer clean after reading it from the device<commit_after>module Heikou
  class Buffer
    include FFI::OpenCL

    attr_reader :mem, :host_ptr, :size, :context

    def initialize(cl, context, options, type, size = 1)
      @cl = cl
      @context = context
      @type = type
      @size = size

      @host_ptr = FFI::MemoryPointer.new type, size

      mark_clean

      create(options)
    end

    def finalize
      reference_count.times { release }
    end

    def retain
      clRetainMemObject(@mem)
    end

    def release
      clReleaseMemObject(@mem)
    end

    def mark_clean
      @dirty = false
    end

    def mark_dirty
      @dirty = true
    end

    def dirty?
      @dirty
    end

    def reference_count
      ref_count = FFI::MemoryPointer.new FFI::OpenCL.find_type(:cl_uint)
      clGetMemObjectInfo(@mem, CL_MEM_REFERENCE_COUNT, ref_count.size, ref_count, nil)
      ref_count.read_uint
    end

    def []=(index, value)
      @host_ptr[index].send("write_#{@type}", value)
    end

    def [](index)
      @host_ptr[index].send("read_#{@type}")
    end

    def read
      @cl.default_queue.read_buffer(self)
      mark_clean
    end

    def to_a
      read if dirty?
      @host_ptr.send("read_array_of_#{@type}", @size)
    end

    private

    def create(options)
{'fraction_non_alphanumeric': 0.0967238689547582, 'fraction_numerical': 0.00078003120124805, 'mean_word_length': 2.33984375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1960411', 'n_tokens_mistral': 874, 'n_tokens_neox': 842, 'n_words': 202}