File: memory-view.cpp

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (311 lines) | stat: -rw-r--r-- 9,906 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include "memory-view.hpp"

#include <arrow-glib/arrow-glib.hpp>
#include <rbgobject.h>

#include <ruby/version.h>

#if RUBY_API_VERSION_MAJOR >= 3
#  define HAVE_MEMORY_VIEW
#  define private memory_view_private
#  include <ruby/memory_view.h>
#  undef private
#endif

#include <sstream>

namespace red_arrow {
  namespace memory_view {
#ifdef HAVE_MEMORY_VIEW
    // This is workaround for the following rb_memory_view_t problems
    // in C++:
    //
    //   * Can't use "private" as member name
    //   * Can't assign a value to "rb_memory_view_t::private"
    //
    // This has compatible layout with rb_memory_view_t.
    struct memory_view {
      VALUE obj;
      void *data;
      ssize_t byte_size;
      bool readonly;
      const char *format;
      ssize_t item_size;
      struct {
        const rb_memory_view_item_component_t *components;
        size_t length;
      } item_desc;
      ssize_t ndim;
      const ssize_t *shape;
      const ssize_t *strides;
      const ssize_t *sub_offsets;
      void *private_data;
    };

    struct PrivateData {
      std::string format;
    };

    class PrimitiveArrayGetter : public arrow::ArrayVisitor {
    public:
      explicit PrimitiveArrayGetter(memory_view *view)
        : view_(view) {
      }

      arrow::Status Visit(const arrow::BooleanArray& array) override {
        fill(static_cast<const arrow::Array&>(array));
        // Memory view doesn't support bit stream. We use one byte
        // for 8 elements. Users can't calculate the number of
        // elements from memory view but it's limitation of memory view.
#ifdef ARROW_LITTLE_ENDIAN
        view_->format = "b8";
#else
        view_->format = "B8";
#endif
        view_->item_size = 1;
        view_->byte_size = (array.length() + 7) / 8;
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Int8Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "c";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Int16Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "s";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Int32Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "l";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Int64Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "q";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::UInt8Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "C";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::UInt16Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "S";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::UInt32Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "L";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::UInt64Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "Q";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::FloatArray& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "f";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::DoubleArray& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "d";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::FixedSizeBinaryArray& array) override {
        fill(static_cast<const arrow::Array&>(array));
        auto priv = static_cast<PrivateData *>(view_->private_data);
        const auto type =
          std::static_pointer_cast<const arrow::FixedSizeBinaryType>(
            array.type());
        std::ostringstream output;
        output << "C" << type->byte_width();
        priv->format = output.str();
        view_->format = priv->format.c_str();
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Date32Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "l";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Date64Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "q";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Time32Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "l";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Time64Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "q";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::TimestampArray& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "q";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Decimal128Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "q2";
        return arrow::Status::OK();
      }

      arrow::Status Visit(const arrow::Decimal256Array& array) override {
        fill(static_cast<const arrow::Array&>(array));
        view_->format = "q4";
        return arrow::Status::OK();
      }

      private:
      void fill(const arrow::Array& array) {
        const auto array_data = array.data();
        const auto data = array_data->GetValuesSafe<uint8_t>(1);
        view_->data = const_cast<void *>(reinterpret_cast<const void *>(data));
        const auto type =
          std::static_pointer_cast<const arrow::FixedWidthType>(array.type());
        view_->item_size = type->bit_width() / 8;
        view_->byte_size = view_->item_size * array.length();
      }

      memory_view *view_;
    };

    bool primitive_array_get(VALUE obj, rb_memory_view_t *view, int flags) {
      if (flags != RUBY_MEMORY_VIEW_SIMPLE) {
        return false;
      }
      auto view_ = reinterpret_cast<memory_view *>(view);
      view_->obj = obj;
      view_->private_data = new PrivateData();
      auto array = GARROW_ARRAY(RVAL2GOBJ(obj));
      auto arrow_array = garrow_array_get_raw(array);
      PrimitiveArrayGetter getter(view_);
      auto status = arrow_array->Accept(&getter);
      if (!status.ok()) {
        return false;
      }
      view_->readonly = true;
      view_->ndim = 1;
      view_->shape = NULL;
      view_->strides = NULL;
      view_->sub_offsets = NULL;
      return true;
    }

    bool primitive_array_release(VALUE obj, rb_memory_view_t *view) {
      auto view_ = reinterpret_cast<memory_view *>(view);
      delete static_cast<PrivateData *>(view_->private_data);
      return true;
    }

    bool primitive_array_available_p(VALUE obj) {
      return true;
    }

    rb_memory_view_entry_t primitive_array_entry = {
      primitive_array_get,
      primitive_array_release,
      primitive_array_available_p,
    };

    bool buffer_get(VALUE obj, rb_memory_view_t *view, int flags) {
      if (flags != RUBY_MEMORY_VIEW_SIMPLE) {
        return false;
      }
      auto view_ = reinterpret_cast<memory_view *>(view);
      view_->obj = obj;
      auto buffer = GARROW_BUFFER(RVAL2GOBJ(obj));
      auto arrow_buffer = garrow_buffer_get_raw(buffer);
      view_->data =
        const_cast<void *>(reinterpret_cast<const void *>(arrow_buffer->data()));
      // Memory view doesn't support bit stream. We use one byte
      // for 8 elements. Users can't calculate the number of
      // elements from memory view but it's limitation of memory view.
#ifdef ARROW_LITTLE_ENDIAN
      view_->format = "b8";
#else
      view_->format = "B8";
#endif
      view_->item_size = 1;
      view_->byte_size = arrow_buffer->size();
      view_->readonly = true;
      view_->ndim = 1;
      view_->shape = NULL;
      view_->strides = NULL;
      view_->sub_offsets = NULL;
      return true;
    }

    bool buffer_release(VALUE obj, rb_memory_view_t *view) {
      return true;
    }

    bool buffer_available_p(VALUE obj) {
      return true;
    }

    rb_memory_view_entry_t buffer_entry = {
      buffer_get,
      buffer_release,
      buffer_available_p,
    };
#endif

    void init(VALUE mArrow) {
#ifdef HAVE_MEMORY_VIEW
      auto cPrimitiveArray =
        rb_const_get_at(mArrow, rb_intern("PrimitiveArray"));
      rb_memory_view_register(cPrimitiveArray,
                              &(red_arrow::memory_view::primitive_array_entry));

      auto cBuffer = rb_const_get_at(mArrow, rb_intern("Buffer"));
      rb_memory_view_register(cBuffer, &(red_arrow::memory_view::buffer_entry));
#endif
    }
  }
}