|
| 1 | +#include "binsparse/types.h" |
| 2 | +#include "taco/format.h" |
| 3 | +#include <binsparse/tensor.h> |
| 4 | +#include <binsparse/write_tensor.h> |
| 5 | +#include <taco.h> |
| 6 | + |
| 7 | +static inline bsp_type_t getTacoDataType(taco::Datatype type) { |
| 8 | + if (type == taco::UInt8) |
| 9 | + return BSP_UINT8; |
| 10 | + else if (type == taco::UInt16) |
| 11 | + return BSP_UINT16; |
| 12 | + else if (type == taco::UInt32) |
| 13 | + return BSP_UINT32; |
| 14 | + else if (type == taco::UInt64) |
| 15 | + return BSP_UINT64; |
| 16 | + else if (type == taco::Int8) |
| 17 | + return BSP_INT8; |
| 18 | + else if (type == taco::Int16) |
| 19 | + return BSP_INT16; |
| 20 | + else if (type == taco::Int32) |
| 21 | + return BSP_INT32; |
| 22 | + else if (type == taco::Int64) |
| 23 | + return BSP_INT64; |
| 24 | + else if (type == taco::Float32) |
| 25 | + return BSP_FLOAT32; |
| 26 | + else if (type == taco::Float64) |
| 27 | + return BSP_FLOAT64; |
| 28 | + else if (type == taco::Int8) |
| 29 | + return BSP_BINT8; |
| 30 | + else if (type == taco::Complex64) |
| 31 | + return BSP_COMPLEX_FLOAT32; |
| 32 | + else if (type == taco::Complex128) |
| 33 | + return BSP_COMPLEX_FLOAT64; |
| 34 | + else { |
| 35 | + taco_uerror << "Unsupported type supplied to taco converter"; |
| 36 | + return BSP_INVALID_TYPE; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +static bsp_array_t makeBspIndexArray(taco::Array arr) { |
| 41 | + taco_uassert(arr.getType() == taco::Int32); |
| 42 | + bsp_array_t res = bsp_construct_array_t(arr.getSize(), BSP_INT32); |
| 43 | + memcpy(res.data, arr.getData(), taco::Int32.getNumBytes() * arr.getSize()); |
| 44 | + return res; |
| 45 | +} |
| 46 | + |
| 47 | +bsp_tensor_t makeBspTensor(taco::TensorBase tacoTensor) { |
| 48 | + bsp_tensor_t res = bsp_construct_default_tensor_t(); |
| 49 | + auto storage = tacoTensor.getStorage(); |
| 50 | + auto index = storage.getIndex(); |
| 51 | + |
| 52 | + // copy over the transposes. |
| 53 | + auto modeOrdering = storage.getFormat().getModeOrdering(); |
| 54 | + res.transpose = (size_t*) malloc(sizeof(size_t) * modeOrdering.size()); |
| 55 | + for (int i = 0; i < modeOrdering.size(); i++) { |
| 56 | + res.transpose[i] = (size_t) modeOrdering[i]; |
| 57 | + } |
| 58 | + |
| 59 | + // copy over the dimensions. |
| 60 | + auto dims = storage.getDimensions(); |
| 61 | + res.rank = dims.size(); |
| 62 | + res.dims = (size_t*) malloc(sizeof(size_t) * dims.size()); |
| 63 | + for (int i = 0; i < dims.size(); i++) { |
| 64 | + res.dims[i] = dims[i]; |
| 65 | + } |
| 66 | + |
| 67 | + std::vector<taco::ModeFormat> formats = storage.getFormat().getModeFormats(); |
| 68 | + |
| 69 | + int dimsPtr = 0; |
| 70 | + res.level = (bsp_level_t*) malloc(sizeof(bsp_level_t)); |
| 71 | + bsp_level_t* curLevel = res.level; |
| 72 | + |
| 73 | + while (dimsPtr < dims.size()) { |
| 74 | + taco::ModeFormat format = formats[dimsPtr]; |
| 75 | + |
| 76 | + if (format.getName() == taco::Sparse.getName()) { |
| 77 | + int boundary = dimsPtr + 1; |
| 78 | + while (boundary < dims.size() && |
| 79 | + typeid(formats[boundary]) != typeid(taco::Singleton)) { |
| 80 | + boundary++; |
| 81 | + } |
| 82 | + curLevel->kind = BSP_TENSOR_SPARSE; |
| 83 | + |
| 84 | + bsp_sparse_t* data = (bsp_sparse_t*) malloc(sizeof(bsp_sparse_t)); |
| 85 | + curLevel->data = data; |
| 86 | + |
| 87 | + data->pointers_to = NULL; |
| 88 | + if (dimsPtr != 0) { |
| 89 | + data->pointers_to = (bsp_array_t*) malloc(sizeof(bsp_array_t)); |
| 90 | + *data->pointers_to = |
| 91 | + makeBspIndexArray(index.getModeIndex(dimsPtr).getIndexArray(0)); |
| 92 | + } |
| 93 | + |
| 94 | + data->rank = boundary - dimsPtr; |
| 95 | + data->indices = (bsp_array_t*) malloc(sizeof(bsp_array_t) * data->rank); |
| 96 | + for (int indicesIdx = 0; indicesIdx < data->rank; indicesIdx++) { |
| 97 | + data->indices[indicesIdx] = makeBspIndexArray( |
| 98 | + index.getModeIndex(dimsPtr + indicesIdx).getIndexArray(1)); |
| 99 | + } |
| 100 | + |
| 101 | + data->child = (bsp_level_t*) malloc(sizeof(bsp_level_t)); |
| 102 | + curLevel = data->child; |
| 103 | + |
| 104 | + dimsPtr = boundary; |
| 105 | + } else if (format.getName() == taco::Dense.getName()) { |
| 106 | + curLevel->kind = BSP_TENSOR_DENSE; |
| 107 | + bsp_dense_t* data = (bsp_dense_t*) malloc(sizeof(bsp_dense_t)); |
| 108 | + curLevel->data = data; |
| 109 | + |
| 110 | + data->rank = 1; |
| 111 | + data->child = (bsp_level_t*) malloc(sizeof(bsp_level_t)); |
| 112 | + curLevel = data->child; |
| 113 | + |
| 114 | + dimsPtr++; |
| 115 | + } else { |
| 116 | + taco_uerror << "This should be impossible; neither dense nor sparse"; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // code to deal with copying over the actual data. |
| 121 | + { |
| 122 | + curLevel->kind = BSP_TENSOR_ELEMENT; |
| 123 | + taco::Array values = storage.getValues(); |
| 124 | + res.nnz = values.getSize(); |
| 125 | + bsp_element_t* data = (bsp_element_t*) malloc(sizeof(bsp_element_t)); |
| 126 | + |
| 127 | + bsp_array_t valuesArray = |
| 128 | + bsp_construct_array_t(res.nnz, getTacoDataType(values.getType())); |
| 129 | + memcpy(valuesArray.data, values.getData(), |
| 130 | + values.getType().getNumBytes() * values.getSize()); |
| 131 | + |
| 132 | + bsp_array_t* arr = (bsp_array_t*) malloc(sizeof(bsp_array_t)); |
| 133 | + *arr = valuesArray; |
| 134 | + |
| 135 | + curLevel->data = arr; |
| 136 | + } |
| 137 | + return res; |
| 138 | +} |
0 commit comments