|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <assert.h> |
| 4 | +#include <binsparse/matrix.h> |
| 5 | + |
| 6 | +bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix, |
| 7 | + bsp_matrix_format_t format) { |
| 8 | + // Throw an error if matrix already in desired format. |
| 9 | + if (matrix.format == format) { |
| 10 | + assert(false); |
| 11 | + } |
| 12 | + |
| 13 | + if (format == BSP_COOR) { |
| 14 | + // *Convert to COO* from another format. |
| 15 | + if (matrix.format == BSP_CSR) { |
| 16 | + // Convert CSR -> COOR |
| 17 | + bsp_matrix_t result = bsp_construct_default_matrix_t(); |
| 18 | + |
| 19 | + result.format = BSP_COOR; |
| 20 | + |
| 21 | + // Inherit NNZ, nrows, ncols, ISO-ness, and structure directly from |
| 22 | + // original matrix. |
| 23 | + result.nnz = matrix.nnz; |
| 24 | + result.nrows = matrix.nrows; |
| 25 | + result.ncols = matrix.ncols; |
| 26 | + result.is_iso = matrix.is_iso; |
| 27 | + result.structure = matrix.structure; |
| 28 | + |
| 29 | + size_t max_dim = |
| 30 | + (matrix.nrows > matrix.ncols) ? matrix.nrows : matrix.ncols; |
| 31 | + |
| 32 | + bsp_type_t index_type = bsp_pick_integer_type(max_dim); |
| 33 | + |
| 34 | + result.values = bsp_copy_construct_array_t(matrix.values); |
| 35 | + |
| 36 | + // There is a corner case with tall and skinny matrices where we need a |
| 37 | + // higher width for rowind. In order to keep rowind/colind the same type, |
| 38 | + // we might upcast. |
| 39 | + |
| 40 | + if (index_type == matrix.indices_0.type) { |
| 41 | + result.indices_1 = bsp_copy_construct_array_t(matrix.indices_0); |
| 42 | + } else { |
| 43 | + result.indices_1 = bsp_construct_array_t(matrix.nnz, index_type); |
| 44 | + for (size_t i = 0; i < matrix.nnz; i++) { |
| 45 | + bsp_array_awrite(result.indices_1, i, matrix.indices_0, i); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + result.indices_0 = bsp_construct_array_t(matrix.nnz, index_type); |
| 50 | + |
| 51 | + for (size_t i = 0; i < matrix.nrows; i++) { |
| 52 | + size_t row_begin, row_end; |
| 53 | + bsp_array_read(matrix.pointers_to_1, i, row_begin); |
| 54 | + bsp_array_read(matrix.pointers_to_1, i + 1, row_end); |
| 55 | + for (size_t j_ptr = row_begin; j_ptr < row_end; j_ptr++) { |
| 56 | + bsp_array_write(result.indices_0, j_ptr, i); |
| 57 | + } |
| 58 | + } |
| 59 | + return result; |
| 60 | + } else { |
| 61 | + assert(false); |
| 62 | + } |
| 63 | + } else { |
| 64 | + // Convert to any another format. |
| 65 | + |
| 66 | + // Currently only support COOR -> X. |
| 67 | + // If matrix is not COOR, convert to COOR. |
| 68 | + if (matrix.format != BSP_COOR) { |
| 69 | + bsp_matrix_t intermediate = bsp_convert_matrix(matrix, BSP_COOR); |
| 70 | + bsp_matrix_t result = bsp_convert_matrix(intermediate, format); |
| 71 | + bsp_destroy_matrix_t(intermediate); |
| 72 | + return result; |
| 73 | + } else { |
| 74 | + if (format == BSP_CSR) { |
| 75 | + // Convert COOR -> CSR |
| 76 | + |
| 77 | + bsp_matrix_t result = bsp_construct_default_matrix_t(); |
| 78 | + |
| 79 | + result.format = BSP_CSR; |
| 80 | + |
| 81 | + result.nrows = matrix.nrows; |
| 82 | + result.ncols = matrix.ncols; |
| 83 | + result.nnz = matrix.nnz; |
| 84 | + result.is_iso = matrix.is_iso; |
| 85 | + result.structure = matrix.structure; |
| 86 | + |
| 87 | + // TODO: consider whether to produce files with varying integer types |
| 88 | + // for row indices, column indices, and offsets. |
| 89 | + |
| 90 | + size_t max_dim = |
| 91 | + (matrix.nrows > matrix.ncols) ? matrix.nrows : matrix.ncols; |
| 92 | + |
| 93 | + size_t max_value = |
| 94 | + (max_dim > matrix.values.size) ? max_dim : matrix.values.size; |
| 95 | + |
| 96 | + bsp_type_t value_type = matrix.values.type; |
| 97 | + bsp_type_t index_type = bsp_pick_integer_type(max_value); |
| 98 | + |
| 99 | + // Since COOR is sorted by rows and then by columns, values and column |
| 100 | + // indices can be copied exactly. Values' type will not change, but |
| 101 | + // column indices might, thus the extra branch. |
| 102 | + |
| 103 | + result.values = bsp_copy_construct_array_t(matrix.values); |
| 104 | + |
| 105 | + if (index_type == matrix.indices_1.type) { |
| 106 | + result.indices_0 = bsp_copy_construct_array_t(matrix.indices_1); |
| 107 | + } else { |
| 108 | + result.indices_0 = bsp_construct_array_t(matrix.nnz, index_type); |
| 109 | + |
| 110 | + for (size_t i = 0; i < matrix.nnz; i++) { |
| 111 | + bsp_array_awrite(result.indices_0, i, matrix.indices_1, i); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + result.pointers_to_1 = |
| 116 | + bsp_construct_array_t(matrix.nrows + 1, index_type); |
| 117 | + |
| 118 | + bsp_array_t rowptr = result.pointers_to_1; |
| 119 | + |
| 120 | + bsp_array_write(rowptr, 0, 0); |
| 121 | + |
| 122 | + size_t r = 0; |
| 123 | + size_t c = 0; |
| 124 | + for (size_t c = 0; c < matrix.nnz; c++) { |
| 125 | + size_t j; |
| 126 | + bsp_array_read(matrix.indices_0, c, j); |
| 127 | + |
| 128 | + while (r < j) { |
| 129 | + assert(r + 1 <= matrix.nrows); |
| 130 | + |
| 131 | + bsp_array_write(rowptr, r + 1, c); |
| 132 | + r++; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + for (; r < matrix.nrows; r++) { |
| 137 | + bsp_array_write(rowptr, r + 1, matrix.nnz); |
| 138 | + } |
| 139 | + |
| 140 | + return result; |
| 141 | + } else { |
| 142 | + assert(false); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments