tatami_mult
Multiply tatami matrices
Loading...
Searching...
No Matches
column_to_column.hpp
Go to the documentation of this file.
1#ifndef TATAMI_MULT_DENSE_MATRIX_DENSE_COLUMN_COLUMN_TO_COLUMN_HPP
2#define TATAMI_MULT_DENSE_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/dense_matrix
22 * for an explanation of the choice of algorithm.
23 */
24
49
67template<typename LeftValue_, typename LeftIndex_, typename RightColumns_, class GetRightColumn_, typename Output_>
70 const RightColumns_ right_columns,
71 GetRightColumn_ get_right_column,
72 Output_* const output,
74) {
75 const auto left_NR = left.nrow();
76 const auto common_dim = left.ncol();
77
78 // Product must fit in a size_t in order for output to have been allocated correctly in the first place.
79 // Technically, right_columns could be larger than a size_t if left_NR == 0, but the product after wraparound would still be zero, so it's fine.
80 std::fill_n(output, sanisizer::product_unsafe<std::size_t>(left_NR, right_columns), 0);
81
82 const bool do_parallel = options.num_threads > 1;
83 std::optional<std::vector<std::optional<std::vector<Output_> > > > tmp_results;
84 if (do_parallel) {
85 tmp_results.emplace(sanisizer::cast<I<decltype(tmp_results->size())> >(options.num_threads - 1));
86 }
87
88 const auto num_used = tatami::parallelize([&](int t, LeftIndex_ start, LeftIndex_ length) -> void {
89 auto ext = tatami::consecutive_extractor<false>(left, false, start, length);
90
91 std::optional<std::vector<Output_> > tmp_output;
92 Output_* outptr;
93 if (!do_parallel || t == 0) {
94 outptr = output;
95 } else {
96 tmp_output.emplace(sanisizer::product<I<decltype(tmp_output->size())> >(left_NR, right_columns));
97 outptr = tmp_output->data();
98 }
99
100 if (options.primary_block_size == 1) {
102 for (LeftIndex_ cd = 0; cd < length; ++cd) {
103 const auto ptr = ext->fetch(buffer.data());
104 for (RightColumns_ rc = 0; rc < right_columns; ++rc) {
105 const Output_ mult = get_right_column(rc)[start + cd];
106 for (LeftIndex_ lr = 0; lr < left_NR; ++lr) {
107 outptr[sanisizer::nd_offset<std::size_t>(lr, left_NR, rc)] += mult * static_cast<Output_>(ptr[lr]);
108 }
109 }
110 }
111
112 } else {
113 std::vector<std::vector<LeftValue_> > left_buffers;
114 std::vector<const LeftValue_*> left_ptrs;
115 {
116 const LeftIndex_ max_block_cols = sanisizer::min(length, options.primary_block_size);
117 left_buffers.reserve(max_block_cols);
118 for (LeftIndex_ cd = 0; cd < max_block_cols; ++cd) {
119 left_buffers.emplace_back(tatami::cast_Index_to_container_size<std::vector<LeftValue_> >(left_NR));
120 }
121 sanisizer::resize(left_ptrs, max_block_cols);
122 }
123
124 LeftIndex_ cd = 0;
125 while (cd < length) {
126 const auto cd_num = sanisizer::min(options.primary_block_size, length - cd);
127 for (LeftIndex_ cd_counter = 0; cd_counter < cd_num; ++cd_counter) {
128 left_ptrs[cd_counter] = ext->fetch(left_buffers[cd_counter].data());
129 }
130
131 RightColumns_ rc = 0;
132 while (rc < right_columns) {
133 const RightColumns_ rc_end = rc + sanisizer::min(options.primary_block_size, right_columns - rc);
134 LeftIndex_ lr = 0;
135 while (lr < left_NR) {
136 const LeftIndex_ lr_end = lr + sanisizer::min(options.secondary_block_size, left_NR - lr);
137
138 for (LeftIndex_ cd_counter = 0; cd_counter < cd_num; ++cd_counter) {
139 const auto matcol = left_ptrs[cd_counter];
140 for (auto rc_copy = rc; rc_copy < rc_end; ++rc_copy) {
141 const Output_ mult = get_right_column(rc_copy)[start + cd + cd_counter];
142 for (auto lr_copy = lr; lr_copy < lr_end; ++lr_copy) {
143 outptr[sanisizer::nd_offset<std::size_t>(lr_copy, left_NR, rc_copy)] += mult * static_cast<Output_>(matcol[lr_copy]);
144 }
145 }
146 }
147
148 lr = lr_end;
149 }
150 rc = rc_end;
151 }
152 cd += cd_num;
153 }
154 }
155
156 if (do_parallel && t > 0) {
157 (*tmp_results)[t - 1] = std::move(tmp_output);
158 }
159 }, common_dim, options.num_threads);
160
161 if (do_parallel) {
162 for (int u = 1; u < num_used; ++u) {
163 const auto& tmp = *((*tmp_results)[u - 1]);
164 const auto N = tmp.size();
165 for (I<decltype(N)> x = 0; x < N; ++x) {
166 output[x] += tmp[x];
167 }
168 }
169 }
170}
171
192template<typename LeftValue_, typename LeftIndex_, typename RightValue_, typename RightIndex_, typename Output_>
196 Output_* const output,
198) {
199 const auto right_NC = right.ncol();
202 const auto common_dim = left.ncol();
203 populate_dense_buffers(false, right_NC, common_dim, right, right_buffers, right_ptrs, options.num_threads);
204
206 left,
207 right_NC,
208 [&](const RightIndex_ rc) -> const RightValue_* {
209 return right_ptrs[rc];
210 },
211 output,
212 options
213 );
214}
215
216}
217
218#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_dense_column_matrix_to_column_output(const tatami::Matrix< LeftValue_, LeftIndex_ > &left, const RightColumns_ right_columns, GetRightColumn_ get_right_column, Output_ *const output, const MultiplyDenseColumnWithDenseColumnMatrixToColumnOutputOptions &options)
Definition column_to_column.hpp:68
int parallelize(Function_ fun, const Index_ tasks, const int workers)
I< decltype(std::declval< Container_ >().size())> cast_Index_to_container_size(const Index_ x)
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_dense_column_matrix_to_column_output().
Definition column_to_column.hpp:28