Repository: arrow Updated Branches: refs/heads/master 74f89cfbe -> 0dc6fe8f3
ARROW-846: [GLib] Add GArrowTensor, GArrowInt8Tensor and GArrowUInt8Tensor Author: Kouhei Sutou <k...@clear-code.com> Closes #560 from kou/glib-add-tensor and squashes the following commits: ed949d4 [Kouhei Sutou] [GLib] Support running tests on Ubuntu 14.04 39d40f0 [Kouhei Sutou] [GLib] Add GArrowTensor, GArrowInt8Tensor and GArrowUInt8Tensor Project: http://git-wip-us.apache.org/repos/asf/arrow/repo Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/0dc6fe8f Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/0dc6fe8f Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/0dc6fe8f Branch: refs/heads/master Commit: 0dc6fe8f33befaaa5fc8055b6c157ac1ccb09e6b Parents: 74f89cf Author: Kouhei Sutou <k...@clear-code.com> Authored: Wed Apr 19 17:17:38 2017 -0400 Committer: Wes McKinney <wes.mckin...@twosigma.com> Committed: Wed Apr 19 17:17:38 2017 -0400 ---------------------------------------------------------------------- c_glib/arrow-glib/Makefile.am | 8 + c_glib/arrow-glib/arrow-glib.h | 3 + c_glib/arrow-glib/arrow-glib.hpp | 2 + c_glib/arrow-glib/int8-tensor.cpp | 105 ++++++++ c_glib/arrow-glib/int8-tensor.h | 79 ++++++ c_glib/arrow-glib/numeric-tensor.hpp | 64 +++++ c_glib/arrow-glib/tensor.cpp | 390 ++++++++++++++++++++++++++++++ c_glib/arrow-glib/tensor.h | 77 ++++++ c_glib/arrow-glib/tensor.hpp | 27 +++ c_glib/arrow-glib/uint8-tensor.cpp | 105 ++++++++ c_glib/arrow-glib/uint8-tensor.h | 79 ++++++ c_glib/test/helper/omittable.rb | 28 +++ c_glib/test/run-test.rb | 1 + c_glib/test/test-int8-tensor.rb | 43 ++++ c_glib/test/test-tensor.rb | 100 ++++++++ c_glib/test/test-uint8-tensor.rb | 43 ++++ 16 files changed, 1154 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/Makefile.am ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/Makefile.am b/c_glib/arrow-glib/Makefile.am index 2e7a9a0..fbfe3a4 100644 --- a/c_glib/arrow-glib/Makefile.am +++ b/c_glib/arrow-glib/Makefile.am @@ -65,6 +65,7 @@ libarrow_glib_la_headers = \ int8-array.h \ int8-array-builder.h \ int8-data-type.h \ + int8-tensor.h \ int16-array.h \ int16-array-builder.h \ int16-data-type.h \ @@ -88,10 +89,12 @@ libarrow_glib_la_headers = \ struct-array-builder.h \ struct-data-type.h \ table.h \ + tensor.h \ type.h \ uint8-array.h \ uint8-array-builder.h \ uint8-data-type.h \ + uint8-tensor.h \ uint16-array.h \ uint16-array-builder.h \ uint16-data-type.h \ @@ -152,6 +155,7 @@ libarrow_glib_la_sources = \ int8-array.cpp \ int8-array-builder.cpp \ int8-data-type.cpp \ + int8-tensor.cpp \ int16-array.cpp \ int16-array-builder.cpp \ int16-data-type.cpp \ @@ -175,10 +179,12 @@ libarrow_glib_la_sources = \ struct-array-builder.cpp \ struct-data-type.cpp \ table.cpp \ + tensor.cpp \ type.cpp \ uint8-array.cpp \ uint8-array-builder.cpp \ uint8-data-type.cpp \ + uint8-tensor.cpp \ uint16-array.cpp \ uint16-array-builder.cpp \ uint16-data-type.cpp \ @@ -220,9 +226,11 @@ libarrow_glib_la_cpp_headers = \ data-type.hpp \ error.hpp \ field.hpp \ + numeric-tensor.hpp \ record-batch.hpp \ schema.hpp \ table.hpp \ + tensor.hpp \ type.hpp libarrow_glib_la_cpp_headers += \ http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/arrow-glib.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/arrow-glib.h b/c_glib/arrow-glib/arrow-glib.h index b15c56f..eec9e25 100644 --- a/c_glib/arrow-glib/arrow-glib.h +++ b/c_glib/arrow-glib/arrow-glib.h @@ -42,6 +42,7 @@ #include <arrow-glib/int8-array.h> #include <arrow-glib/int8-array-builder.h> #include <arrow-glib/int8-data-type.h> +#include <arrow-glib/int8-tensor.h> #include <arrow-glib/int16-array.h> #include <arrow-glib/int16-array-builder.h> #include <arrow-glib/int16-data-type.h> @@ -65,10 +66,12 @@ #include <arrow-glib/struct-array-builder.h> #include <arrow-glib/struct-data-type.h> #include <arrow-glib/table.h> +#include <arrow-glib/tensor.h> #include <arrow-glib/type.h> #include <arrow-glib/uint8-array.h> #include <arrow-glib/uint8-array-builder.h> #include <arrow-glib/uint8-data-type.h> +#include <arrow-glib/uint8-tensor.h> #include <arrow-glib/uint16-array.h> #include <arrow-glib/uint16-array-builder.h> #include <arrow-glib/uint16-data-type.h> http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/arrow-glib.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/arrow-glib.hpp b/c_glib/arrow-glib/arrow-glib.hpp index 3404d4d..d6ef370 100644 --- a/c_glib/arrow-glib/arrow-glib.hpp +++ b/c_glib/arrow-glib/arrow-glib.hpp @@ -31,9 +31,11 @@ #include <arrow-glib/error.hpp> #include <arrow-glib/field.hpp> #include <arrow-glib/float-array.hpp> +#include <arrow-glib/numeric-tensor.hpp> #include <arrow-glib/record-batch.h> #include <arrow-glib/schema.hpp> #include <arrow-glib/table.hpp> +#include <arrow-glib/tensor.hpp> #include <arrow-glib/type.hpp> #include <arrow-glib/file.hpp> http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/int8-tensor.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/int8-tensor.cpp b/c_glib/arrow-glib/int8-tensor.cpp new file mode 100644 index 0000000..06521a0 --- /dev/null +++ b/c_glib/arrow-glib/int8-tensor.cpp @@ -0,0 +1,105 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow-glib/buffer.hpp> +#include <arrow-glib/int8-tensor.h> +#include <arrow-glib/numeric-tensor.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: int8-tensor + * @short_description: 8-bit integer tensor class + * + * #GArrowInt8Tensor is a class for 8-bit integer tensor. It can store + * zero or more 8-bit integer data. + */ + +G_DEFINE_TYPE(GArrowInt8Tensor, \ + garrow_int8_tensor, \ + GARROW_TYPE_TENSOR) + +static void +garrow_int8_tensor_init(GArrowInt8Tensor *object) +{ +} + +static void +garrow_int8_tensor_class_init(GArrowInt8TensorClass *klass) +{ +} + +/** + * garrow_int8_tensor_new: + * @data: A #GArrowBuffer that contains tensor data. + * @shape: (array length=n_dimensions): A list of dimension sizes. + * @n_dimensions: The number of dimensions. + * @strides: (array length=n_strides) (nullable): A list of the number of + * bytes in each dimension. + * @n_strides: The number of strides. + * @dimention_names: (array length=n_dimention_names) (nullable): A list of + * dimension names. + * @n_dimention_names: The number of dimension names + * + * Returns: The newly created #GArrowInt8Tensor. + * + * Since: 0.3.0 + */ +GArrowInt8Tensor * +garrow_int8_tensor_new(GArrowBuffer *data, + gint64 *shape, + gsize n_dimensions, + gint64 *strides, + gsize n_strides, + gchar **dimension_names, + gsize n_dimension_names) +{ + auto tensor = + garrow::numeric_tensor_new<arrow::Int8Tensor>(data, + shape, + n_dimensions, + strides, + n_strides, + dimension_names, + n_dimension_names); + return GARROW_INT8_TENSOR(tensor); +} + +/** + * garrow_int8_tensor_get_raw_data: + * @tensor: A #GArrowInt8Tensor. + * @n_data: (out): The number of data. + * + * Returns: (array length=n_data): The raw data in the tensor. + * + * Since: 0.3.0 + */ +const gint8 * +garrow_int8_tensor_get_raw_data(GArrowInt8Tensor *tensor, + gint64 *n_data) +{ + return garrow::numeric_tensor_get_raw_data<arrow::Int8Tensor, int8_t>(GARROW_TENSOR(tensor), + n_data); +} + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/int8-tensor.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/int8-tensor.h b/c_glib/arrow-glib/int8-tensor.h new file mode 100644 index 0000000..76ed3c8 --- /dev/null +++ b/c_glib/arrow-glib/int8-tensor.h @@ -0,0 +1,79 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/tensor.h> + +G_BEGIN_DECLS + +#define GARROW_TYPE_INT8_TENSOR \ + (garrow_int8_tensor_get_type()) +#define GARROW_INT8_TENSOR(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_TYPE_INT8_TENSOR, \ + GArrowInt8Tensor)) +#define GARROW_INT8_TENSOR_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_TYPE_INT8_TENSOR, \ + GArrowInt8TensorClass)) +#define GARROW_IS_INT8_TENSOR(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_TYPE_INT8_TENSOR)) +#define GARROW_IS_INT8_TENSOR_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + GARROW_TYPE_INT8_TENSOR)) +#define GARROW_INT8_TENSOR_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_TYPE_INT8_TENSOR, \ + GArrowInt8TensorClass)) + +typedef struct _GArrowInt8Tensor GArrowInt8Tensor; +typedef struct _GArrowInt8TensorClass GArrowInt8TensorClass; + +/** + * GArrowInt8Tensor: + * + * It wraps `arrow::Int8Tensor`. + */ +struct _GArrowInt8Tensor +{ + /*< private >*/ + GArrowTensor parent_instance; +}; + +struct _GArrowInt8TensorClass +{ + GArrowTensorClass parent_class; +}; + +GType garrow_int8_tensor_get_type(void) G_GNUC_CONST; + +GArrowInt8Tensor *garrow_int8_tensor_new(GArrowBuffer *data, + gint64 *shape, + gsize n_dimensions, + gint64 *strides, + gsize n_strides, + gchar **dimention_names, + gsize n_dimention_names); + +const gint8 *garrow_int8_tensor_get_raw_data(GArrowInt8Tensor *tensor, + gint64 *n_data); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/numeric-tensor.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/numeric-tensor.hpp b/c_glib/arrow-glib/numeric-tensor.hpp new file mode 100644 index 0000000..07cea62 --- /dev/null +++ b/c_glib/arrow-glib/numeric-tensor.hpp @@ -0,0 +1,64 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/tensor.hpp> + +namespace garrow { + template <typename T> + GArrowTensor *numeric_tensor_new(GArrowBuffer *data, + gint64 *shape, + gsize n_dimensions, + gint64 *strides, + gsize n_strides, + gchar **dimention_names, + gsize n_dimention_names) { + auto arrow_data = garrow_buffer_get_raw(data); + std::vector<int64_t> arrow_shape; + for (gsize i = 0; i < n_dimensions; ++i) { + arrow_shape.push_back(shape[i]); + } + std::vector<int64_t> arrow_strides; + for (gsize i = 0; i < n_strides; ++i) { + arrow_strides.push_back(strides[i]); + } + std::vector<std::string> arrow_dimention_names; + for (gsize i = 0; i < n_dimention_names; ++i) { + arrow_dimention_names.push_back(dimention_names[i]); + } + auto arrow_numeric_tensor = + std::make_shared<T>(arrow_data, + arrow_shape, + arrow_strides, + arrow_dimention_names); + std::shared_ptr<arrow::Tensor> arrow_tensor = arrow_numeric_tensor; + auto tensor = garrow_tensor_new_raw(&arrow_tensor); + return tensor; + } + + template <typename T, typename value_type> + const value_type *numeric_tensor_get_raw_data(GArrowTensor *tensor, + gint64 *n_data) { + auto arrow_tensor = garrow_tensor_get_raw(tensor); + auto arrow_numeric_tensor = static_cast<const T *>(arrow_tensor.get()); + *n_data = arrow_numeric_tensor->size(); + return arrow_numeric_tensor->raw_data(); + } +} http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/tensor.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/tensor.cpp b/c_glib/arrow-glib/tensor.cpp new file mode 100644 index 0000000..cbc9d8e --- /dev/null +++ b/c_glib/arrow-glib/tensor.cpp @@ -0,0 +1,390 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow-glib/buffer.hpp> +#include <arrow-glib/data-type.hpp> +#include <arrow-glib/int8-tensor.h> +#include <arrow-glib/tensor.hpp> +#include <arrow-glib/type.hpp> +#include <arrow-glib/uint8-tensor.h> + +G_BEGIN_DECLS + +/** + * SECTION: tensor + * @short_description: Base class for all tensor classes + * + * #GArrowTensor is a base class for all tensor classes such as + * #GArrowInt8Tensor. + * #GArrowBooleanTensorBuilder to create a new tensor. + * + * Since: 0.3.0 + */ + +typedef struct GArrowTensorPrivate_ { + std::shared_ptr<arrow::Tensor> tensor; +} GArrowTensorPrivate; + +enum { + PROP_0, + PROP_TENSOR +}; + +G_DEFINE_TYPE_WITH_PRIVATE(GArrowTensor, garrow_tensor, G_TYPE_OBJECT) + +#define GARROW_TENSOR_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), GARROW_TYPE_TENSOR, GArrowTensorPrivate)) + +static void +garrow_tensor_finalize(GObject *object) +{ + auto priv = GARROW_TENSOR_GET_PRIVATE(object); + + priv->tensor = nullptr; + + G_OBJECT_CLASS(garrow_tensor_parent_class)->finalize(object); +} + +static void +garrow_tensor_set_property(GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + auto priv = GARROW_TENSOR_GET_PRIVATE(object); + + switch (prop_id) { + case PROP_TENSOR: + priv->tensor = + *static_cast<std::shared_ptr<arrow::Tensor> *>(g_value_get_pointer(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_tensor_get_property(GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +garrow_tensor_init(GArrowTensor *object) +{ +} + +static void +garrow_tensor_class_init(GArrowTensorClass *klass) +{ + GParamSpec *spec; + + auto gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->finalize = garrow_tensor_finalize; + gobject_class->set_property = garrow_tensor_set_property; + gobject_class->get_property = garrow_tensor_get_property; + + spec = g_param_spec_pointer("tensor", + "Tensor", + "The raw std::shared<arrow::Tensor> *", + static_cast<GParamFlags>(G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property(gobject_class, PROP_TENSOR, spec); +} + +/** + * garrow_tensor_get_value_data_type: + * @tensor: A #GArrowTensor. + * + * Returns: (transfer full): The data type of each value in the tensor. + * + * Since: 0.3.0 + */ +GArrowDataType * +garrow_tensor_get_value_data_type(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + auto arrow_data_type = arrow_tensor->type(); + return garrow_data_type_new_raw(&arrow_data_type); +} + +/** + * garrow_tensor_get_value_type: + * @tensor: A #GArrowTensor. + * + * Returns: The type of each value in the tensor. + * + * Since: 0.3.0 + */ +GArrowType +garrow_tensor_get_value_type(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + auto arrow_type = arrow_tensor->type_id(); + return garrow_type_from_raw(arrow_type); +} + +/** + * garrow_tensor_get_buffer: + * @tensor: A #GArrowTensor. + * + * Returns: (transfer full): The data of the tensor. + * + * Since: 0.3.0 + */ +GArrowBuffer * +garrow_tensor_get_buffer(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + auto arrow_buffer = arrow_tensor->data(); + return garrow_buffer_new_raw(&arrow_buffer); +} + +/** + * garrow_tensor_get_shape: + * @tensor: A #GArrowTensor. + * @n_dimensions: (out): The number of dimensions. + * + * Returns: (array length=n_dimensions): The shape of the tensor. + * It should be freed with g_free() when no longer needed. + * + * Since: 0.3.0 + */ +gint64 * +garrow_tensor_get_shape(GArrowTensor *tensor, gint *n_dimensions) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + auto arrow_shape = arrow_tensor->shape(); + auto n_dimensions_raw = arrow_shape.size(); + auto shape = + static_cast<gint64 *>(g_malloc_n(sizeof(gint64), n_dimensions_raw)); + for (gsize i = 0; i < n_dimensions_raw; ++i) { + shape[i] = arrow_shape[i]; + } + *n_dimensions = static_cast<gint>(n_dimensions_raw); + return shape; +} + +/** + * garrow_tensor_get_strides: + * @tensor: A #GArrowTensor. + * @n_strides: (out): The number of strides. + * + * Returns: (array length=n_strides): The strides of the tensor. + * It should be freed with g_free() when no longer needed. + * + * Since: 0.3.0 + */ +gint64 * +garrow_tensor_get_strides(GArrowTensor *tensor, gint *n_strides) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + auto arrow_strides = arrow_tensor->strides(); + auto n_strides_raw = arrow_strides.size(); + auto strides = + static_cast<gint64 *>(g_malloc_n(sizeof(gint64), n_strides_raw)); + for (gsize i = 0; i < n_strides_raw; ++i) { + strides[i] = arrow_strides[i]; + } + *n_strides = static_cast<gint>(n_strides_raw); + return strides; +} + +/** + * garrow_tensor_get_n_dimensions: + * @tensor: A #GArrowTensor. + * + * Returns: The number of dimensions of the tensor. + * + * Since: 0.3.0 + */ +gint +garrow_tensor_get_n_dimensions(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + return arrow_tensor->ndim(); +} + +/** + * garrow_tensor_get_dimension_name: + * @tensor: A #GArrowTensor. + * @i: The index of the target dimension. + * + * Returns: The i-th dimension name of the tensor. + * + * Since: 0.3.0 + */ +const gchar * +garrow_tensor_get_dimension_name(GArrowTensor *tensor, gint i) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + auto arrow_dimension_name = arrow_tensor->dim_name(i); + return arrow_dimension_name.c_str(); +} + +/** + * garrow_tensor_get_size: + * @tensor: A #GArrowTensor. + * + * Returns: The number of value cells in the tensor. + * + * Since: 0.3.0 + */ +gint64 +garrow_tensor_get_size(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + return arrow_tensor->size(); +} + +/** + * garrow_tensor_is_mutable: + * @tensor: A #GArrowTensor. + * + * Returns: %TRUE if the tensor is mutable, %FALSE otherwise. + * + * Since: 0.3.0 + */ +gboolean +garrow_tensor_is_mutable(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + return arrow_tensor->is_mutable(); +} + +/** + * garrow_tensor_is_contiguous: + * @tensor: A #GArrowTensor. + * + * Returns: %TRUE if the tensor is contiguous, %FALSE otherwise. + * + * Since: 0.3.0 + */ +gboolean +garrow_tensor_is_contiguous(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + return arrow_tensor->is_contiguous(); +} + +/** + * garrow_tensor_is_row_major: + * @tensor: A #GArrowTensor. + * + * Returns: %TRUE if the tensor is row major a.k.a. C order, + * %FALSE otherwise. + * + * Since: 0.3.0 + */ +gboolean +garrow_tensor_is_row_major(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + return arrow_tensor->is_row_major(); +} + +/** + * garrow_tensor_is_column_major: + * @tensor: A #GArrowTensor. + * + * Returns: %TRUE if the tensor is column major a.k.a. Fortran order, + * %FALSE otherwise. + * + * Since: 0.3.0 + */ +gboolean +garrow_tensor_is_column_major(GArrowTensor *tensor) +{ + auto arrow_tensor = garrow_tensor_get_raw(tensor); + return arrow_tensor->is_column_major(); +} + +G_END_DECLS + +GArrowTensor * +garrow_tensor_new_raw(std::shared_ptr<arrow::Tensor> *arrow_tensor) +{ + GType type; + GArrowTensor *tensor; + + switch ((*arrow_tensor)->type_id()) { + case arrow::Type::type::UINT8: + type = GARROW_TYPE_UINT8_TENSOR; + break; + case arrow::Type::type::INT8: + type = GARROW_TYPE_INT8_TENSOR; + break; +/* + case arrow::Type::type::UINT16: + type = GARROW_TYPE_UINT16_TENSOR; + break; + case arrow::Type::type::INT16: + type = GARROW_TYPE_INT16_TENSOR; + break; + case arrow::Type::type::UINT32: + type = GARROW_TYPE_UINT32_TENSOR; + break; + case arrow::Type::type::INT32: + type = GARROW_TYPE_INT32_TENSOR; + break; + case arrow::Type::type::UINT64: + type = GARROW_TYPE_UINT64_TENSOR; + break; + case arrow::Type::type::INT64: + type = GARROW_TYPE_INT64_TENSOR; + break; + case arrow::Type::type::HALF_FLOAT: + type = GARROW_TYPE_HALF_FLOAT_TENSOR; + break; + case arrow::Type::type::FLOAT: + type = GARROW_TYPE_FLOAT_TENSOR; + break; + case arrow::Type::type::DOUBLE: + type = GARROW_TYPE_DOUBLE_TENSOR; + break; +*/ + default: + type = GARROW_TYPE_TENSOR; + break; + } + tensor = GARROW_TENSOR(g_object_new(type, + "tensor", arrow_tensor, + NULL)); + return tensor; +} + +std::shared_ptr<arrow::Tensor> +garrow_tensor_get_raw(GArrowTensor *tensor) +{ + auto priv = GARROW_TENSOR_GET_PRIVATE(tensor); + return priv->tensor; +} http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/tensor.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/tensor.h b/c_glib/arrow-glib/tensor.h new file mode 100644 index 0000000..bedc803 --- /dev/null +++ b/c_glib/arrow-glib/tensor.h @@ -0,0 +1,77 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/buffer.h> +#include <arrow-glib/data-type.h> + +G_BEGIN_DECLS + +#define GARROW_TYPE_TENSOR \ + (garrow_tensor_get_type()) +#define GARROW_TENSOR(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), GARROW_TYPE_TENSOR, GArrowTensor)) +#define GARROW_TENSOR_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), GARROW_TYPE_TENSOR, GArrowTensorClass)) +#define GARROW_IS_TENSOR(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), GARROW_TYPE_TENSOR)) +#define GARROW_IS_TENSOR_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), GARROW_TYPE_TENSOR)) +#define GARROW_TENSOR_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), GARROW_TYPE_TENSOR, GArrowTensorClass)) + +typedef struct _GArrowTensor GArrowTensor; +typedef struct _GArrowTensorClass GArrowTensorClass; + +/** + * GArrowTensor: + * + * It wraps `arrow::Tensor`. + */ +struct _GArrowTensor +{ + /*< private >*/ + GObject parent_instance; +}; + +struct _GArrowTensorClass +{ + GObjectClass parent_class; +}; + +GType garrow_tensor_get_type (void) G_GNUC_CONST; + +GArrowDataType *garrow_tensor_get_value_data_type(GArrowTensor *tensor); +GArrowType garrow_tensor_get_value_type (GArrowTensor *tensor); +GArrowBuffer *garrow_tensor_get_buffer (GArrowTensor *tensor); +gint64 *garrow_tensor_get_shape (GArrowTensor *tensor, + gint *n_dimensions); +gint64 *garrow_tensor_get_strides (GArrowTensor *tensor, + gint *n_strides); +gint garrow_tensor_get_n_dimensions (GArrowTensor *tensor); +const gchar *garrow_tensor_get_dimension_name (GArrowTensor *tensor, + gint i); +gint64 garrow_tensor_get_size (GArrowTensor *tensor); +gboolean garrow_tensor_is_mutable (GArrowTensor *tensor); +gboolean garrow_tensor_is_contiguous (GArrowTensor *tensor); +gboolean garrow_tensor_is_row_major (GArrowTensor *tensor); +gboolean garrow_tensor_is_column_major (GArrowTensor *tensor); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/tensor.hpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/tensor.hpp b/c_glib/arrow-glib/tensor.hpp new file mode 100644 index 0000000..392aeee --- /dev/null +++ b/c_glib/arrow-glib/tensor.hpp @@ -0,0 +1,27 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow/api.h> + +#include <arrow-glib/tensor.h> + +GArrowTensor *garrow_tensor_new_raw(std::shared_ptr<arrow::Tensor> *arrow_tensor); +std::shared_ptr<arrow::Tensor> garrow_tensor_get_raw(GArrowTensor *tensor); http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/uint8-tensor.cpp ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/uint8-tensor.cpp b/c_glib/arrow-glib/uint8-tensor.cpp new file mode 100644 index 0000000..69f0f69 --- /dev/null +++ b/c_glib/arrow-glib/uint8-tensor.cpp @@ -0,0 +1,105 @@ +/* + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <arrow-glib/buffer.hpp> +#include <arrow-glib/uint8-tensor.h> +#include <arrow-glib/numeric-tensor.hpp> + +G_BEGIN_DECLS + +/** + * SECTION: uint8-tensor + * @short_description: 8-bit unsigned integer tensor class + * + * #GArrowUint8Tensor is a class for 8-bit unsigned integer tensor. It + * can store zero or more 8-bit integer data. + */ + +G_DEFINE_TYPE(GArrowUInt8Tensor, \ + garrow_uint8_tensor, \ + GARROW_TYPE_TENSOR) + +static void +garrow_uint8_tensor_init(GArrowUInt8Tensor *object) +{ +} + +static void +garrow_uint8_tensor_class_init(GArrowUInt8TensorClass *klass) +{ +} + +/** + * garrow_uint8_tensor_new: + * @data: A #GArrowBuffer that contains tensor data. + * @shape: (array length=n_dimensions): A list of dimension sizes. + * @n_dimensions: The number of dimensions. + * @strides: (array length=n_strides) (nullable): A list of the number of + * bytes in each dimension. + * @n_strides: The number of strides. + * @dimention_names: (array length=n_dimention_names) (nullable): A list of + * dimension names. + * @n_dimention_names: The number of dimension names + * + * Returns: The newly created #GArrowUInt8Tensor. + * + * Since: 0.3.0 + */ +GArrowUInt8Tensor * +garrow_uint8_tensor_new(GArrowBuffer *data, + gint64 *shape, + gsize n_dimensions, + gint64 *strides, + gsize n_strides, + gchar **dimension_names, + gsize n_dimension_names) +{ + auto tensor = + garrow::numeric_tensor_new<arrow::UInt8Tensor>(data, + shape, + n_dimensions, + strides, + n_strides, + dimension_names, + n_dimension_names); + return GARROW_UINT8_TENSOR(tensor); +} + +/** + * garrow_uint8_tensor_get_raw_data: + * @tensor: A #GArrowUInt8Tensor. + * @n_data: (out): The number of data. + * + * Returns: (array length=n_data): The raw data in the tensor. + * + * Since: 0.3.0 + */ +const guint8 * +garrow_uint8_tensor_get_raw_data(GArrowUInt8Tensor *tensor, + gint64 *n_data) +{ + return garrow::numeric_tensor_get_raw_data<arrow::UInt8Tensor, uint8_t>(GARROW_TENSOR(tensor), + n_data); +} + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/arrow-glib/uint8-tensor.h ---------------------------------------------------------------------- diff --git a/c_glib/arrow-glib/uint8-tensor.h b/c_glib/arrow-glib/uint8-tensor.h new file mode 100644 index 0000000..248c507 --- /dev/null +++ b/c_glib/arrow-glib/uint8-tensor.h @@ -0,0 +1,79 @@ +/* + * 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. + */ + +#pragma once + +#include <arrow-glib/tensor.h> + +G_BEGIN_DECLS + +#define GARROW_TYPE_UINT8_TENSOR \ + (garrow_uint8_tensor_get_type()) +#define GARROW_UINT8_TENSOR(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + GARROW_TYPE_UINT8_TENSOR, \ + GArrowUInt8Tensor)) +#define GARROW_UINT8_TENSOR_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + GARROW_TYPE_UINT8_TENSOR, \ + GArrowUInt8TensorClass)) +#define GARROW_IS_UINT8_TENSOR(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + GARROW_TYPE_UINT8_TENSOR)) +#define GARROW_IS_UINT8_TENSOR_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + GARROW_TYPE_UINT8_TENSOR)) +#define GARROW_UINT8_TENSOR_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + GARROW_TYPE_UINT8_TENSOR, \ + GArrowUInt8TensorClass)) + +typedef struct _GArrowUInt8Tensor GArrowUInt8Tensor; +typedef struct _GArrowUInt8TensorClass GArrowUInt8TensorClass; + +/** + * GArrowUInt8Tensor: + * + * It wraps `arrow::UInt8Tensor`. + */ +struct _GArrowUInt8Tensor +{ + /*< private >*/ + GArrowTensor parent_instance; +}; + +struct _GArrowUInt8TensorClass +{ + GArrowTensorClass parent_class; +}; + +GType garrow_uint8_tensor_get_type(void) G_GNUC_CONST; + +GArrowUInt8Tensor *garrow_uint8_tensor_new(GArrowBuffer *data, + gint64 *shape, + gsize n_dimensions, + gint64 *strides, + gsize n_strides, + gchar **dimention_names, + gsize n_dimention_names); + +const guint8 *garrow_uint8_tensor_get_raw_data(GArrowUInt8Tensor *tensor, + gint64 *n_data); + +G_END_DECLS http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/test/helper/omittable.rb ---------------------------------------------------------------------- diff --git a/c_glib/test/helper/omittable.rb b/c_glib/test/helper/omittable.rb new file mode 100644 index 0000000..a16ad32 --- /dev/null +++ b/c_glib/test/helper/omittable.rb @@ -0,0 +1,28 @@ +# 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. + +module Helper + module Omittable + def require_gi(major, minor, micro) + return if GLib.check_binding_version?(major, minor, micro) + message = + "Require gobject-introspection #{major}.#{minor}.#{micro} or later: " + + GLib::BINDING_VERSION.join(".") + omit(message) + end + end +end http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/test/run-test.rb ---------------------------------------------------------------------- diff --git a/c_glib/test/run-test.rb b/c_glib/test/run-test.rb index 53805ca..50f548f 100755 --- a/c_glib/test/run-test.rb +++ b/c_glib/test/run-test.rb @@ -35,5 +35,6 @@ Arrow = GI.load("Arrow") require "tempfile" require_relative "helper/buildable" +require_relative "helper/omittable" exit(Test::Unit::AutoRunner.run(true, test_dir.to_s)) http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/test/test-int8-tensor.rb ---------------------------------------------------------------------- diff --git a/c_glib/test/test-int8-tensor.rb b/c_glib/test/test-int8-tensor.rb new file mode 100644 index 0000000..a96a407 --- /dev/null +++ b/c_glib/test/test-int8-tensor.rb @@ -0,0 +1,43 @@ +# 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. + +class TestInt8Tensor < Test::Unit::TestCase + include Helper::Omittable + + def setup + @raw_data = [ + 1, 2, + 3, 4, + + 5, 6, + 7, 8, + + 9, 10, + 11, 12, + ] + data = Arrow::Buffer.new(@raw_data.pack("c*")) + shape = [3, 2, 2] + strides = [] + names = [] + @tensor = Arrow::Int8Tensor.new(data, shape, strides, names) + end + + def test_raw_data + require_gi(3, 1, 2) + assert_equal(@raw_data, @tensor.raw_data) + end +end http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/test/test-tensor.rb ---------------------------------------------------------------------- diff --git a/c_glib/test/test-tensor.rb b/c_glib/test/test-tensor.rb new file mode 100644 index 0000000..455b0d9 --- /dev/null +++ b/c_glib/test/test-tensor.rb @@ -0,0 +1,100 @@ +# 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. + +class TestTensor < Test::Unit::TestCase + include Helper::Omittable + + def setup + @raw_data = [ + 1, 2, + 3, 4, + + 5, 6, + 7, 8, + + 9, 10, + 11, 12, + ] + data = Arrow::Buffer.new(@raw_data.pack("c*")) + @shape = [3, 2, 2] + strides = [] + names = ["a", "b", "c"] + @tensor = Arrow::Int8Tensor.new(data, @shape, strides, names) + end + + def test_value_data_type + assert_equal(Arrow::Int8DataType, @tensor.value_data_type.class) + end + + def test_value_type + assert_equal(Arrow::Type::INT8, @tensor.value_type) + end + + def test_buffer + assert_equal(@raw_data, @tensor.buffer.data) + end + + def test_shape + require_gi(3, 1, 2) + assert_equal(@shape, @tensor.shape) + end + + def test_strides + require_gi(3, 1, 2) + assert_equal([4, 2, 1], @tensor.strides) + end + + def test_n_dimensions + assert_equal(@shape.size, @tensor.n_dimensions) + end + + def test_dimension_name + dimension_names = @tensor.n_dimensions.times.collect do |i| + @tensor.get_dimension_name(i) + end + assert_equal(["a", "b", "c"], + dimension_names) + end + + def test_size + assert_equal(@raw_data.size, @tensor.size) + end + + def test_mutable? + assert do + not @tensor.mutable? + end + end + + def test_contiguous? + assert do + @tensor.contiguous? + end + end + + def test_row_major? + assert do + @tensor.row_major? + end + end + + def test_column_major? + assert do + not @tensor.column_major? + end + end +end http://git-wip-us.apache.org/repos/asf/arrow/blob/0dc6fe8f/c_glib/test/test-uint8-tensor.rb ---------------------------------------------------------------------- diff --git a/c_glib/test/test-uint8-tensor.rb b/c_glib/test/test-uint8-tensor.rb new file mode 100644 index 0000000..0fe758b --- /dev/null +++ b/c_glib/test/test-uint8-tensor.rb @@ -0,0 +1,43 @@ +# 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. + +class TestUInt8Tensor < Test::Unit::TestCase + include Helper::Omittable + + def setup + @raw_data = [ + 1, 2, + 3, 4, + + 5, 6, + 7, 8, + + 9, 10, + 11, 12, + ] + data = Arrow::Buffer.new(@raw_data.pack("c*")) + shape = [3, 2, 2] + strides = [] + names = [] + @tensor = Arrow::UInt8Tensor.new(data, shape, strides, names) + end + + def test_raw_data + require_gi(3, 1, 2) + assert_equal(@raw_data, @tensor.raw_data) + end +end