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