tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
transpose.hpp
Go to the documentation of this file.
1#ifndef TATAMI_TRANSPOSE_HPP
2#define TATAMI_TRANSPOSE_HPP
3
4#include <algorithm>
5
11namespace tatami {
12
37template<typename Input_, typename Output_>
38void transpose(const Input_* input, size_t nrow, size_t ncol, size_t input_stride, Output_* output, size_t output_stride) {
39 if ((nrow == 1 && output_stride == 1) || (ncol == 1 && input_stride == 1)) {
40 std::copy_n(input, nrow * ncol, output);
41 return;
42 }
43
44 // Using a blockwise strategy to perform the transposition,
45 // in order to be more input-friendly.
46 constexpr size_t block = 16;
47 size_t col_start = 0;
48 while (col_start < ncol) {
49 size_t col_end = col_start + std::min(block, ncol - col_start);
50
51 size_t row_start = 0;
52 while (row_start < nrow) {
53 size_t row_end = row_start + std::min(block, nrow - row_start);
54 for (size_t c = col_start; c < col_end; ++c) {
55 for (size_t r = row_start; r < row_end; ++r) {
57 }
58 }
59
61 }
63 }
64}
65
85template<typename Input_, typename Output_>
86void transpose(const Input_* input, size_t nrow, size_t ncol, Output_* output) {
87 transpose(input, nrow, ncol, ncol, output, nrow);
88}
89
90// COMMENT:
91// I tried really hard to make an in-place version, but it's too frigging complicated for non-square matrices.
92// It can be done, but I can't see a way to do it efficiently as you end up hopping all over the matrix (a la in-place reordering).
93// There doesn't seem to be any opportunity to do it in blocks for cache-friendliness;
94// the displaced values from the original matrix don't form a corrresponding block in the transposed matrix.
95// Perhaps this is a skill issue but at least Eigen agrees with my assessment, as they just make a new copy when the matrix is not square.
96// (See https://gitlab.com/libeigen/eigen/-/blob/master/Eigen/src/Core/Transpose.h#L293 for the relevant code.)
97// I'm not going to optimize the square case because tatami rarely, if ever, deals in situations with square matrices.
98
99}
100
101#endif
Flexible representations for matrix data.
Definition Extractor.hpp:15
void transpose(const Input_ *input, size_t nrow, size_t ncol, size_t input_stride, Output_ *output, size_t output_stride)
Definition transpose.hpp:38
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35