tatami_mult
Multiply tatami matrices
Loading...
Searching...
No Matches
column_to_column.hpp
Go to the documentation of this file.
1#ifndef TATAMI_MULT_SPARSE_MATRIX_DENSE_COLUMN_COLUMN_TO_COLUMN_HPP
2#define TATAMI_MULT_SPARSE_MATRIX_DENSE_COLUMN_COLUMN_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
55template<typename LeftValue_, typename LeftIndex_, typename RightValue_, typename RightIndex_, typename Output_>
59 Output_* const output,
61) {
62 const auto left_NR = left.nrow();
63 const auto common_dim = left.ncol();
64 const auto right_NC = right.ncol();
65
67 conv_opt.two_pass = false;
68 conv_opt.num_threads = options.num_threads;
70
71 // If there are any empty RHS rows, we only iterate over the non-empty ones in the outer loop.
72 auto right_non_empty = filter_non_empty_sparse(
73 rhs_data.index,
74 [&](const RightIndex_) -> void {}
75 );
76
77 const bool do_parallel = options.num_threads > 1;
78 std::optional<std::vector<std::optional<std::vector<Output_> > > > tmp_results;
79 if (do_parallel) {
80 tmp_results.emplace(sanisizer::cast<I<decltype(tmp_results->size())> >(options.num_threads - 1));
81 }
82
83 std::fill_n(output, sanisizer::product_unsafe<std::size_t>(left_NR, right_NC), 0);
84
85 // If we have empty RHS rows, we completely skip the corresponding LHS columns.
86 // Otherwise doing the easier approach of just looping with a counter.
87 const LeftIndex_ cd_total = (right_non_empty.has_value() ? static_cast<LeftIndex_>(right_non_empty->size()) : common_dim);
88 const int num_used = tatami::parallelize([&](int t, LeftIndex_ start, LeftIndex_ length) -> void {
89 std::optional<std::vector<Output_> > tmp_output;
90 Output_* outptr;
91 if (!do_parallel || t == 0) {
92 outptr = output;
93 } else {
94 tmp_output.emplace(sanisizer::product<I<decltype(tmp_output->size())> >(left_NR, right_NC));
95 outptr = tmp_output->data();
96 }
97
99
100 auto task = [&](std::unique_ptr<tatami::OracularDenseExtractor<LeftValue_, LeftIndex_> >& ext, auto converter) -> void {
101 for (LeftIndex_ cd = 0; cd < length; ++cd) {
102 const auto lptr = ext->fetch(dbuffer.data());
103 const auto actual_cd = converter(cd);
104 const auto& right_values = rhs_data.value[actual_cd];
105 const auto& right_indices = rhs_data.index[actual_cd];
106 const RightIndex_ right_nnz = right_values.size();
107 for (RightIndex_ x = 0; x < right_nnz; ++x) {
108 const Output_ mult = right_values[x];
109 const auto idx = right_indices[x];
110 for (LeftIndex_ lr = 0; lr < left_NR; ++lr) {
111 outptr[sanisizer::nd_offset<std::size_t>(lr, left_NR, idx)] += mult * static_cast<Output_>(lptr[lr]);
112 }
113 }
114 }
115 };
116
117 if (right_non_empty.has_value()) {
118 auto ext = tatami::new_extractor<false, true>(left, false, std::make_shared<tatami::FixedViewOracle<LeftIndex_> >(right_non_empty->data() + start, length));
119 task(
120 ext,
121 [&](const LeftIndex_ cd) -> LeftIndex_ {
122 return (*right_non_empty)[start + cd];
123 }
124 );
125 } else {
126 auto ext = tatami::consecutive_extractor<false>(left, false, start, length);
127 task(
128 ext,
129 [&](const LeftIndex_ cd) -> LeftIndex_ {
130 return cd + start;
131 }
132 );
133 }
134
135 if (do_parallel && t > 0) {
136 (*tmp_results)[t - 1] = std::move(tmp_output);
137 }
138 }, cd_total, options.num_threads);
139
140 if (do_parallel) {
141 for (int u = 1; u < num_used; ++u) {
142 const auto& tmp = *((*tmp_results)[u - 1]);
143 const auto N = tmp.size();
144 for (I<decltype(N)> x = 0; x < N; ++x) {
145 output[x] += tmp[x];
146 }
147 }
148 }
149}
150
151}
152
153#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_column_matrix_to_column_output(const tatami::Matrix< LeftValue_, LeftIndex_ > &left, const tatami::Matrix< RightValue_, RightIndex_ > &right, Output_ *const output, const MultiplyDenseColumnWithSparseColumnMatrixToColumnOutputOptions &options)
Definition column_to_column.hpp:56
auto new_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, Args_ &&... args)
FragmentedSparseContents< StoredValue_, StoredIndex_ > retrieve_fragmented_sparse_contents(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row, const RetrieveFragmentedSparseContentsOptions &options)
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_column_matrix_to_column_output().
Definition column_to_column.hpp:28