AlenkaF commented on code in PR #38472:
URL: https://github.com/apache/arrow/pull/38472#discussion_r1416893060


##########
cpp/src/arrow/c/dlpack.cc:
##########
@@ -0,0 +1,151 @@
+// 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 "arrow/c/dlpack.h"
+
+#include "arrow/array/array_base.h"
+#include "arrow/c/dlpack_abi.h"
+#include "arrow/device.h"
+#include "arrow/type.h"
+
+namespace arrow {
+
+namespace dlpack {
+
+Status getDLDataType(const std::shared_ptr<DataType>& type, DLDataType* out) {
+  DLDataType dtype;
+  dtype.lanes = 1;
+  dtype.bits = type->bit_width();
+  switch (type->id()) {
+    case Type::INT8:
+    case Type::INT16:
+    case Type::INT32:
+    case Type::INT64:
+      dtype.code = DLDataTypeCode::kDLInt;
+      *out = dtype;
+      return Status::OK();
+    case Type::UINT8:
+    case Type::UINT16:
+    case Type::UINT32:
+    case Type::UINT64:
+      dtype.code = DLDataTypeCode::kDLUInt;
+      *out = dtype;
+      return Status::OK();
+    case Type::HALF_FLOAT:
+    case Type::FLOAT:
+    case Type::DOUBLE:
+      dtype.code = DLDataTypeCode::kDLFloat;
+      *out = dtype;
+      return Status::OK();
+    case Type::BOOL:
+      // DLPack supports byte-packed boolean values
+      return Status::TypeError("Bit-packed boolean data type not supported by 
DLPack.");
+    default:
+      return Status::TypeError(
+          "Can only use __dlpack__ on primitive arrays without NullType and 
Decimal "
+          "types.");
+  }
+}
+
+struct DLMTensorCtx {
+  std::shared_ptr<ArrayData> ref;
+  std::vector<int64_t> shape;
+  DLManagedTensor tensor;
+};
+
+static void deleter(DLManagedTensor* arg) {
+  delete static_cast<DLMTensorCtx*>(arg->manager_ctx);
+}
+
+Status ExportArray(const std::shared_ptr<Array>& arr, DLManagedTensor** out) {
+  if (arr->null_count() > 0) {
+    return Status::TypeError("Can only use __dlpack__ on arrays with no 
nulls.");
+  }
+
+  // Define the DLDataType struct
+  // Supported data types: int, uint, float
+  DLDataType arr_type;
+  RETURN_NOT_OK(getDLDataType(arr->type(), &arr_type));
+
+  // Create DLMTensorCtx struct with the reference to
+  // the data of the array
+  std::shared_ptr<ArrayData> array_ref = arr->data();
+  DLMTensorCtx* DLMTensor = new DLMTensorCtx;

Review Comment:
   Any pointers how to fix a segfault with `.release()` call on the ctx? 😬 
   
   ```cpp
   * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS 
(code=1, address=0x40)
       frame #0: 0x00000001085d0694 
libarrow.1500.dylib`arrow::dlpack::ExportArray(arr=std::__1::shared_ptr<arrow::Array>::element_type
 @ 0x000000011de17d88 strong=1 weak=1) at dlpack.cc:125:27
      122    ctx->tensor.dl_tensor.byte_offset = 0;
      123 
      124    // return dlm_tensor;
   -> 125    ctx->tensor.manager_ctx = ctx.release();
      126    ctx->tensor.deleter = [](struct DLManagedTensor *self) {
      127      delete reinterpret_cast<ManagerCtx*>(self->manager_ctx);
      128    };
   ```



##########
cpp/src/arrow/c/dlpack.cc:
##########
@@ -0,0 +1,151 @@
+// 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 "arrow/c/dlpack.h"
+
+#include "arrow/array/array_base.h"
+#include "arrow/c/dlpack_abi.h"
+#include "arrow/device.h"
+#include "arrow/type.h"
+
+namespace arrow {
+
+namespace dlpack {
+
+Status getDLDataType(const std::shared_ptr<DataType>& type, DLDataType* out) {
+  DLDataType dtype;
+  dtype.lanes = 1;
+  dtype.bits = type->bit_width();
+  switch (type->id()) {
+    case Type::INT8:
+    case Type::INT16:
+    case Type::INT32:
+    case Type::INT64:
+      dtype.code = DLDataTypeCode::kDLInt;
+      *out = dtype;
+      return Status::OK();
+    case Type::UINT8:
+    case Type::UINT16:
+    case Type::UINT32:
+    case Type::UINT64:
+      dtype.code = DLDataTypeCode::kDLUInt;
+      *out = dtype;
+      return Status::OK();
+    case Type::HALF_FLOAT:
+    case Type::FLOAT:
+    case Type::DOUBLE:
+      dtype.code = DLDataTypeCode::kDLFloat;
+      *out = dtype;
+      return Status::OK();
+    case Type::BOOL:
+      // DLPack supports byte-packed boolean values
+      return Status::TypeError("Bit-packed boolean data type not supported by 
DLPack.");
+    default:
+      return Status::TypeError(
+          "Can only use __dlpack__ on primitive arrays without NullType and 
Decimal "
+          "types.");
+  }
+}
+
+struct DLMTensorCtx {
+  std::shared_ptr<ArrayData> ref;
+  std::vector<int64_t> shape;
+  DLManagedTensor tensor;
+};
+
+static void deleter(DLManagedTensor* arg) {
+  delete static_cast<DLMTensorCtx*>(arg->manager_ctx);
+}
+
+Status ExportArray(const std::shared_ptr<Array>& arr, DLManagedTensor** out) {
+  if (arr->null_count() > 0) {
+    return Status::TypeError("Can only use __dlpack__ on arrays with no 
nulls.");
+  }
+
+  // Define the DLDataType struct
+  // Supported data types: int, uint, float
+  DLDataType arr_type;
+  RETURN_NOT_OK(getDLDataType(arr->type(), &arr_type));
+
+  // Create DLMTensorCtx struct with the reference to
+  // the data of the array
+  std::shared_ptr<ArrayData> array_ref = arr->data();
+  DLMTensorCtx* DLMTensor = new DLMTensorCtx;

Review Comment:
   Any pointers how to fix a segfault with `.release()` call on the ctx? 😬 
   
   ```cpp
   * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS 
(code=1, address=0x40)
       frame #0: 0x00000001085d0694 
libarrow.1500.dylib`arrow::dlpack::ExportArray(arr=std::__1::shared_ptr<arrow::Array>::element_type
 @ 0x000000011de17d88 strong=1 weak=1) at dlpack.cc:125:27
      122    ctx->tensor.dl_tensor.byte_offset = 0;
      123 
      124    // return dlm_tensor;
   -> 125    ctx->tensor.manager_ctx = ctx.release();
      126    ctx->tensor.deleter = [](struct DLManagedTensor *self) {
      127      delete reinterpret_cast<ManagerCtx*>(self->manager_ctx);
      128    };
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to