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_ROW_COLUMN_TO_COLUMN_HPP
2#define TATAMI_MULT_SPARSE_MATRIX_DENSE_ROW_COLUMN_TO_COLUMN_HPP
3
4#include <cstddef>
5#include <vector>
6
7#include "tatami/tatami.hpp"
8#include "sanisizer/sanisizer.hpp"
9
10#include "../utils.hpp"
11#include "../../utils.hpp"
12#include "../../sparse_dot_product.hpp"
13
19namespace tatami_mult {
20
21/* See https://github.com/tatami-inc/test-multiplication/tree/master/dense_row/sparse_matrix
22 * for an explanation of the choice of algorithm.
23 */
24
41
63template<std::size_t accumulators_ = 4, typename LeftValue_, typename LeftIndex_, typename RightValue_, typename RightIndex_, typename Output_>
67 Output_* const output,
69) {
70 const auto left_NR = left.nrow();
71 const auto common_dim = left.ncol();
72 const auto right_NC = right.ncol();
73
77 populate_sparse_buffers(false, right_NC, common_dim, right, right_vbuffers, right_ibuffers, right_ranges, options.num_threads);
78
79 // If there are any empty RHS columns, we only iterate over the non-empty ones in the loop for each LHS row.
80 auto right_non_empty = filter_non_empty_sparse(
81 right_ranges,
82 [&](const RightIndex_ rc) -> void {
83 std::fill_n(output + sanisizer::product_unsafe<std::size_t>(left_NR, rc), left_NR, 0);
84 }
85 );
86
87 if (options.block_size == 1) {
88 tatami::parallelize([&](int, LeftIndex_ start, LeftIndex_ length) -> void {
89 auto ext = tatami::consecutive_extractor<false>(left, true, start, length);
91
92 for (LeftIndex_ lr = 0; lr < length; ++lr) {
93 const auto lptr = ext->fetch(dbuffer.data());
94
95 auto loop_body = [&](RightIndex_ rc) -> void {
96 const auto rrange = right_ranges[rc];
97 output[sanisizer::nd_offset<std::size_t>(start + lr, left_NR, rc)] = sparse_dot_product<accumulators_>(
98 rrange.number, // Implicit cast to size_t is safe, as per the tatami contract.
99 rrange.value,
100 rrange.index,
101 lptr,
102 static_cast<Output_>(0)
103 );
104 };
105
106 if (right_non_empty.has_value()) {
107 for (const auto rc : *right_non_empty) {
108 loop_body(rc);
109 }
110 } else {
111 for (RightIndex_ rc = 0; rc < right_NC; ++rc) {
112 loop_body(rc);
113 }
114 }
115 }
116 }, left_NR, options.num_threads);
117 return;
118 }
119
120 tatami::parallelize([&](int, LeftIndex_ start, LeftIndex_ length) -> void {
121 auto ext = tatami::consecutive_extractor<false>(left, true, start, length);
122
123 const LeftIndex_ max_block_rows = sanisizer::min(length, options.block_size);
124 std::vector<std::vector<LeftValue_> > lbuffers;
125 lbuffers.reserve(max_block_rows);
126 for (LeftIndex_ b = 0; b < max_block_rows; ++b) {
127 lbuffers.emplace_back(tatami::cast_Index_to_container_size<std::vector<LeftValue_> >(common_dim));
128 }
130
131 LeftIndex_ lr = 0;
132 while (lr < length) {
133 const LeftIndex_ lr_num = sanisizer::min(options.block_size, length - lr);
134 for (LeftIndex_ lr_counter = 0; lr_counter < lr_num; ++lr_counter) {
135 lptrs[lr_counter] = ext->fetch(lbuffers[lr_counter].data());
136 }
137
138 // Deliberately iterating over the (non-empty) sparse RHS columns in the outer loop and the dense LHS rows in the inner loop.
139 // This aims to keep the entirety of the dense LHS block in cache across multiple RHS columns, provided common_dim is small.
140 // If we did it the other way around, it would just be the same as the block_size == 1 case, but with more looping overhead.
141 auto loop_body = [&](RightIndex_ rc) -> void {
142 const auto rrange = right_ranges[rc];
143 for (LeftIndex_ lr_counter = 0; lr_counter < lr_num; ++lr_counter) {
144 output[sanisizer::nd_offset<std::size_t>(start + lr + lr_counter, left_NR, rc)] = sparse_dot_product<accumulators_>(
145 rrange.number, // Implicit cast of range.number to size_t is safe, as per the tatami contract.
146 rrange.value,
147 rrange.index,
148 lptrs[lr_counter],
149 static_cast<Output_>(0)
150 );
151 }
152 };
153
154 if (right_non_empty.has_value()) {
155 for (const auto rc : *right_non_empty) {
156 loop_body(rc);
157 }
158 } else {
159 for (RightIndex_ rc = 0; rc < right_NC; ++rc) {
160 loop_body(rc);
161 }
162 }
163
164 lr += lr_num;
165 }
166 }, left_NR, options.num_threads);
167}
168
169}
170
171#endif
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
Multiplication of tatami matrices.
Definition column_to_column.hpp:19
void multiply_dense_row_with_sparse_column_matrix_to_column_output(const tatami::Matrix< LeftValue_, LeftIndex_ > &left, const tatami::Matrix< RightValue_, RightIndex_ > &right, Output_ *const output, const MultiplyDenseRowWithSparseColumnMatrixToColumnOutputOptions &options)
Definition column_to_column.hpp:64
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_row_with_sparse_column_matrix_to_column_output().
Definition column_to_column.hpp:28