| 160 | namespace { |
| 161 | |
| 162 | Result<std::shared_ptr<Tensor>> MakeTensorFromSparseCSXMatrix( |
| 163 | SparseMatrixCompressedAxis axis, MemoryPool* pool, |
| 164 | const std::shared_ptr<Tensor>& indptr, const std::shared_ptr<Tensor>& indices, |
| 165 | const int64_t non_zero_length, const std::shared_ptr<DataType>& value_type, |
| 166 | const std::vector<int64_t>& shape, const int64_t tensor_size, const uint8_t* raw_data, |
| 167 | const std::vector<std::string>& dim_names) { |
| 168 | const auto* indptr_data = indptr->raw_data(); |
| 169 | const auto* indices_data = indices->raw_data(); |
| 170 | |
| 171 | const int indptr_elsize = indptr->type()->byte_width(); |
| 172 | const int indices_elsize = indices->type()->byte_width(); |
| 173 | |
| 174 | const auto& fw_value_type = checked_cast<const FixedWidthType&>(*value_type); |
| 175 | const int value_elsize = fw_value_type.byte_width(); |
| 176 | ARROW_ASSIGN_OR_RAISE(auto values_buffer, |
| 177 | AllocateBuffer(value_elsize * tensor_size, pool)); |
| 178 | auto values = values_buffer->mutable_data(); |
| 179 | std::fill_n(values, value_elsize * tensor_size, 0); |
| 180 | |
| 181 | std::vector<int64_t> strides; |
| 182 | RETURN_NOT_OK(ComputeRowMajorStrides(fw_value_type, shape, &strides)); |
| 183 | |
| 184 | const auto nc = shape[1]; |
| 185 | |
| 186 | int64_t offset = 0; |
| 187 | for (int64_t i = 0; i < indptr->size() - 1; ++i) { |
| 188 | const auto start = |
| 189 | SparseTensorConverterMixin::GetIndexValue(indptr_data, indptr_elsize); |
| 190 | const auto stop = SparseTensorConverterMixin::GetIndexValue( |
| 191 | indptr_data + indptr_elsize, indptr_elsize); |
| 192 | |
| 193 | for (int64_t j = start; j < stop; ++j) { |
| 194 | const auto index = SparseTensorConverterMixin::GetIndexValue( |
| 195 | indices_data + j * indices_elsize, indices_elsize); |
| 196 | switch (axis) { |
| 197 | case SparseMatrixCompressedAxis::ROW: |
| 198 | offset = (index + i * nc) * value_elsize; |
| 199 | break; |
| 200 | case SparseMatrixCompressedAxis::COLUMN: |
| 201 | offset = (i + index * nc) * value_elsize; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | std::copy_n(raw_data, value_elsize, values + offset); |
| 206 | raw_data += value_elsize; |
| 207 | } |
| 208 | |
| 209 | indptr_data += indptr_elsize; |
| 210 | } |
| 211 | |
| 212 | return std::make_shared<Tensor>(value_type, std::move(values_buffer), shape, strides, |
| 213 | dim_names); |
| 214 | } |
| 215 | |
| 216 | } // namespace |
| 217 |
no test coverage detected