tatami_mult
Multiply tatami matrices
Loading...
Searching...
No Matches
dense_column.hpp
Go to the documentation of this file.
1#ifndef TATAMI_MULT_SINGLE_VECTOR_DENSE_COLUMN_HPP
2#define TATAMI_MULT_SINGLE_VECTOR_DENSE_COLUMN_HPP
3
4#include <cstddef>
5#include <vector>
6#include <optional>
7#include <algorithm>
8
9#include "tatami/tatami.hpp"
10#include "sanisizer/sanisizer.hpp"
11
12#include "../utils.hpp"
13
19namespace tatami_mult {
20
21/* See https://github.com/tatami-inc/test-multiplication/tree/master/dense_column/single_vector
22 * for an explanation of the choice of algorithm.
23 */
24
35
50template<typename LeftValue_, typename LeftIndex_, typename RightValue_, typename Output_>
53 const RightValue_* const right,
54 Output_* const output,
56) {
57 const auto NR = left.nrow();
58 const auto NC = left.ncol();
59
60 std::optional<std::vector<std::optional<std::vector<Output_> > > > tmp_results;
61 const bool do_parallel = options.num_threads > 1;
62 if (do_parallel) {
63 tmp_results.emplace(sanisizer::cast<I<decltype(tmp_results->size())> >(options.num_threads - 1));
64 }
65 std::fill_n(output, NR, 0);
66
67 const auto num_used = tatami::parallelize([&](int t, LeftIndex_ start, LeftIndex_ length) -> void {
68 auto ext = tatami::consecutive_extractor<false>(left, false, start, length);
70
71 Output_* optr;
72 std::optional<std::vector<Output_> > cur_output;
73 if (!do_parallel || t == 0) {
74 optr = output;
75 } else {
76 cur_output.emplace(tatami::cast_Index_to_container_size<std::vector<Output_> >(NR));
77 optr = cur_output->data();
78 }
79
80 for (LeftIndex_ c = 0; c < length; ++c) {
81 auto ptr = ext->fetch(buffer.data());
82 const Output_ mult = right[start + c];
83 for (LeftIndex_ r = 0; r < NR; ++r) {
84 optr[r] += mult * ptr[r];
85 }
86 }
87
88 if (do_parallel && t > 0) {
89 (*tmp_results)[t - 1] = std::move(cur_output);
90 }
91 }, NC, options.num_threads);
92
93 if (do_parallel) {
94 for (int u = 1; u < num_used; ++u) {
95 const auto& tmp = *((*tmp_results)[u - 1]);
96 for (LeftIndex_ r = 0; r < NR; ++r) {
97 output[r] += tmp[r];
98 }
99 }
100 }
101}
102
103}
104
105#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_single_vector(const tatami::Matrix< LeftValue_, LeftIndex_ > &left, const RightValue_ *const right, Output_ *const output, const MultiplyDenseColumnWithSingleVectorOptions &options)
Definition dense_column.hpp:51
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_single_vector().
Definition dense_column.hpp:28