MCPcopy Create free account
hub / github.com/apache/arrow / Convert

Method Convert

cpp/src/arrow/tensor/csx_converter.cc:51–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

49 : axis_(axis), tensor_(tensor), index_value_type_(index_value_type), pool_(pool) {}
50
51 Status Convert() {
52 RETURN_NOT_OK(::arrow::internal::CheckSparseIndexMaximumValue(index_value_type_,
53 tensor_.shape()));
54
55 const int index_elsize = index_value_type_->byte_width();
56 const int value_elsize = tensor_.type()->byte_width();
57
58 const int64_t ndim = tensor_.ndim();
59 if (ndim > 2) {
60 return Status::Invalid("Invalid tensor dimension");
61 }
62
63 const int major_axis = static_cast<int>(axis_);
64 const int64_t n_major = tensor_.shape()[major_axis];
65 const int64_t n_minor = tensor_.shape()[1 - major_axis];
66 ARROW_ASSIGN_OR_RAISE(int64_t nonzero_count, tensor_.CountNonZero());
67
68 std::shared_ptr<Buffer> indptr_buffer;
69 std::shared_ptr<Buffer> indices_buffer;
70
71 ARROW_ASSIGN_OR_RAISE(auto values_buffer,
72 AllocateBuffer(value_elsize * nonzero_count, pool_));
73 auto* values = values_buffer->mutable_data();
74
75 const auto* tensor_data = tensor_.raw_data();
76
77 if (ndim <= 1) {
78 return Status::NotImplemented("TODO for ndim <= 1");
79 } else {
80 ARROW_ASSIGN_OR_RAISE(indptr_buffer,
81 AllocateBuffer(index_elsize * (n_major + 1), pool_));
82 auto* indptr = indptr_buffer->mutable_data();
83
84 ARROW_ASSIGN_OR_RAISE(indices_buffer,
85 AllocateBuffer(index_elsize * nonzero_count, pool_));
86 auto* indices = indices_buffer->mutable_data();
87
88 std::vector<int64_t> coords(2);
89 int64_t k = 0;
90 std::fill_n(indptr, index_elsize, 0);
91 indptr += index_elsize;
92 for (int64_t i = 0; i < n_major; ++i) {
93 for (int64_t j = 0; j < n_minor; ++j) {
94 if (axis_ == SparseMatrixCompressedAxis::ROW) {
95 coords = {i, j};
96 } else {
97 coords = {j, i};
98 }
99 const int64_t offset = tensor_.CalculateValueOffset(coords);
100 if (std::any_of(tensor_data + offset, tensor_data + offset + value_elsize,
101 IsNonZero)) {
102 std::copy_n(tensor_data + offset, value_elsize, values);
103 values += value_elsize;
104
105 AssignIndex(indices, j, index_elsize);
106 indices += index_elsize;
107
108 k++;

Callers 1

Calls 11

raw_dataMethod · 0.80
InvalidFunction · 0.50
NotImplementedFunction · 0.50
ARROW_ASSIGN_OR_RAISEFunction · 0.50
AllocateBufferFunction · 0.50
OKFunction · 0.50
shapeMethod · 0.45
byte_widthMethod · 0.45
typeMethod · 0.45
mutable_dataMethod · 0.45

Tested by

no test coverage detected