tatami_mult
Multiply tatami matrices
Loading...
Searching...
No Matches
row_to_column.hpp
Go to the documentation of this file.
1#ifndef TATAMI_MULT_SPARSE_MATRIX_DENSE_COLUMN_ROW_TO_COLUMN_HPP
2#define TATAMI_MULT_SPARSE_MATRIX_DENSE_COLUMN_ROW_TO_COLUMN_HPP
3
4#include <cstddef>
5#include <vector>
6#include <optional>
7
8#include "tatami/tatami.hpp"
9#include "sanisizer/sanisizer.hpp"
10
11#include "../utils.hpp"
12#include "../../utils.hpp"
13
19namespace tatami_mult {
20
21/* See https://github.com/tatami-inc/test-multiplication/tree/master/dense_column/sparse_matrix
22 * for an explanation of the choice of algorithm.
23 */
24
35
54template<typename LeftValue_, typename LeftIndex_, typename RightValue_, typename RightIndex_, typename Output_>
58 Output_* const output,
60) {
61 const auto left_NR = left.nrow();
62 const auto common_dim = left.ncol();
63 const auto right_NC = right.ncol();
64
65 const bool do_parallel = options.num_threads > 1;
66 std::optional<std::vector<std::optional<std::vector<Output_> > > > tmp_results;
67 if (do_parallel) {
68 tmp_results.emplace(sanisizer::cast<I<decltype(tmp_results->size())> >(options.num_threads - 1));
69 }
70
71 std::fill_n(output, sanisizer::product_unsafe<std::size_t>(left_NR, right_NC), 0);
72
73 const int num_used = tatami::parallelize([&](int t, LeftIndex_ start, LeftIndex_ length) -> void {
74 auto left_ext = tatami::consecutive_extractor<false>(left, false, start, length);
75 auto right_ext = tatami::consecutive_extractor<true>(right, true, start, length);
76
77 std::optional<std::vector<Output_> > tmp_output;
78 Output_* outptr;
79 if (!do_parallel || t == 0) {
80 outptr = output;
81 } else {
82 tmp_output.emplace(sanisizer::product<I<decltype(tmp_output->size())> >(left_NR, right_NC));
83 outptr = tmp_output->data();
84 }
85
89
90 for (LeftIndex_ cd = 0; cd < length; ++cd) {
91 const auto lptr = left_ext->fetch(dbuffer.data());
92 const auto rrange = right_ext->fetch(vbuffer.data(), ibuffer.data());
93
94 // Do this after all fetch() calls, to ensure that they both remain in sync with iteration through common_dim.
95 if (rrange.number == 0) {
96 continue;
97 }
98
99 for (RightIndex_ x = 0; x < rrange.number; ++x) {
100 const Output_ mult = rrange.value[x];
101 const auto idx = rrange.index[x];
102 for (LeftIndex_ lr = 0; lr < left_NR; ++lr) {
103 outptr[sanisizer::nd_offset<std::size_t>(lr, left_NR, idx)] += mult * static_cast<Output_>(lptr[lr]);
104 }
105 }
106 }
107
108 if (do_parallel && t > 0) {
109 (*tmp_results)[t - 1] = std::move(tmp_output);
110 }
111 }, common_dim, options.num_threads);
112
113 if (do_parallel) {
114 for (int u = 1; u < num_used; ++u) {
115 const auto& tmp = *((*tmp_results)[u - 1]);
116 const auto N = tmp.size();
117 for (I<decltype(N)> x = 0; x < N; ++x) {
118 output[x] += tmp[x];
119 }
120 }
121 }
122}
123
124}
125
126#endif
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
Multiplication of tatami matrices.
Definition column_to_column.hpp:19
void multiply_dense_column_with_sparse_row_matrix_to_column_output(const tatami::Matrix< LeftValue_, LeftIndex_ > &left, const tatami::Matrix< RightValue_, RightIndex_ > &right, Output_ *const output, const MultiplyDenseColumnWithSparseRowMatrixToColumnOutputOptions &options)
Definition row_to_column.hpp:55
int parallelize(Function_ fun, const Index_ tasks, const int workers)
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
auto consecutive_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, const Index_ iter_start, const Index_ iter_length, Args_ &&... args)
Options for multiply_dense_column_with_sparse_row_matrix_to_column_output().
Definition row_to_column.hpp:28