tatami_mult
Multiply tatami matrices
Loading...
Searching...
No Matches
tatami_mult.hpp
Go to the documentation of this file.
1#ifndef TATAMI_MULT_HPP
2#define TATAMI_MULT_HPP
3
8
9#include <vector>
10
11#include "tatami/tatami.hpp"
12#include "sanisizer/sanisizer.hpp"
13
23namespace tatami_mult {
24
49
57inline void set_num_threads(MultiplyWithMatrixOptions& options, int num_threads) {
58 set_num_threads(options.dense_matrix, num_threads);
59 set_num_threads(options.sparse_matrix, num_threads);
60}
61
69inline void set_dense_primary_block_size(MultiplyWithMatrixOptions& options, int primary_block_size) {
70 set_dense_primary_block_size(options.dense_matrix, primary_block_size);
71}
72
81inline void set_dense_secondary_block_size(MultiplyWithMatrixOptions& options, int secondary_block_size) {
82 set_dense_secondary_block_size(options.dense_matrix, secondary_block_size);
83}
84
92inline void set_sparse_block_size(MultiplyWithMatrixOptions& options, int block_size) {
93 set_sparse_block_size(options.dense_matrix, block_size);
94 set_sparse_block_size(options.sparse_matrix, block_size);
95}
96
120template<typename LeftValue_, typename LeftIndex_, typename RightValue_, typename RightIndex_, typename Output_>
124 Output_* const output,
125 const bool output_row_major,
126 const MultiplyWithMatrixOptions& options
127) {
128 if (options.larger_left) {
129 if (sanisizer::is_less_than(left.nrow(), right.ncol())) {
130 auto tright = tatami::make_DelayedTranspose(tatami::wrap_shared_ptr(&right));
131 auto tleft = tatami::make_DelayedTranspose(tatami::wrap_shared_ptr(&left));
132 if (tleft->is_sparse()) {
133 multiply_with_sparse_matrix(*tright, *tleft, output, !output_row_major, options.sparse_matrix);
134 } else {
135 multiply_with_dense_matrix(*tright, *tleft, output, !output_row_major, options.dense_matrix);
136 }
137 return;
138 }
139 }
140
141 if (right.is_sparse()) {
142 multiply_with_sparse_matrix(left, right, output, output_row_major, options.sparse_matrix);
143 } else {
144 multiply_with_dense_matrix(left, right, output, output_row_major, options.dense_matrix);
145 }
146}
147
151// For back-compatibility only.
152struct Options {
153 int num_threads = 1;
154 bool prefer_larger = true;
155 bool column_major_output = true;
156};
157
158// For back-compatibility only.
159template<typename Value_, typename Index_, typename Right_, typename Output_>
160void multiply(const tatami::Matrix<Value_, Index_>& left, const Right_* right, Output_* output, const Options& opt) {
162 left,
163 right,
164 output,
165 [&](){
166 MultiplyWithSingleVectorOptions mopt;
167 set_num_threads(mopt, opt.num_threads);
168 return mopt;
169 }()
170 );
171}
172
173// For back-compatibility only.
174template<typename Left_, typename Value_, typename Index_, typename Output_>
175void multiply(const Left_* left, const tatami::Matrix<Value_, Index_>& right, Output_* output, const Options& opt) {
177 left,
178 right,
179 output,
180 [&](){
181 MultiplyWithSingleVectorOptions mopt;
182 set_num_threads(mopt, opt.num_threads);
183 return mopt;
184 }()
185 );
186}
187
188// For back-compatibility only.
189template<typename Value_, typename Index_, typename Right_, typename Output_>
190void multiply(const tatami::Matrix<Value_, Index_>& left, const std::vector<Right_*>& right, const std::vector<Output_*>& output, const Options& opt) {
192 left,
193 right,
194 output,
195 [&](){
196 MultiplyWithMultipleVectorsOptions mopt;
197 set_num_threads(mopt, opt.num_threads);
198 return mopt;
199 }()
200 );
201}
202
203// For back-compatibility only.
204template<typename Left_, typename Value_, typename Index_, typename Output_>
205void multiply(const std::vector<Left_*>& left, const tatami::Matrix<Value_, Index_>& right, const std::vector<Output_*>& output, const Options& opt) {
207 left,
208 right,
209 output,
210 [&](){
211 MultiplyWithMultipleVectorsOptions mopt;
212 set_num_threads(mopt, opt.num_threads);
213 return mopt;
214 }()
215 );
216}
217
218// For back-compatibility only.
219template<typename LeftValue_, typename LeftIndex_, typename RightValue_, typename RightIndex_, typename Output_>
220void multiply(const tatami::Matrix<LeftValue_, LeftIndex_>& left, const tatami::Matrix<RightValue_, RightIndex_>& right, Output_* const output, const Options& opt) {
222 left,
223 right,
224 output,
225 !opt.column_major_output,
226 [&](){
227 MultiplyWithMatrixOptions mopt;
228 set_num_threads(mopt, opt.num_threads);
229 mopt.larger_left = opt.prefer_larger;
230 return mopt;
231 }()
232 );
233}
238}
239
240#endif
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
virtual bool is_sparse() const=0
Any matrix LHS, dense matrix RHS.
Any matrix LHS, multiple vectors RHS.
Multiplication of tatami matrices.
Definition column_to_column.hpp:19
void multiply_with_multiple_vectors(const tatami::Matrix< Value_, Index_ > &left, const std::vector< Right_ * > &right, const std::vector< Output_ * > &output, const MultiplyWithMultipleVectorsOptions &options)
Definition dispatch.hpp:114
void set_dense_primary_block_size(MultiplyDenseColumnWithDenseMatrixOptions &options, int primary_block_size)
Definition dispatch.hpp:68
void multiply_with_matrix(const tatami::Matrix< LeftValue_, LeftIndex_ > &left, const tatami::Matrix< RightValue_, RightIndex_ > &right, Output_ *const output, const bool output_row_major, const MultiplyWithMatrixOptions &options)
Definition tatami_mult.hpp:121
void set_dense_secondary_block_size(MultiplyDenseColumnWithDenseMatrixOptions &options, int secondary_block_size)
Definition dispatch.hpp:83
void multiply_with_sparse_matrix(const tatami::Matrix< LeftValue_, LeftIndex_ > &left, const tatami::Matrix< RightValue_, RightIndex_ > &right, Output_ *const output, const bool output_row_major, const MultiplyWithSparseMatrixOptions &options)
Definition dispatch.hpp:97
void multiply_with_dense_matrix(const tatami::Matrix< LeftValue_, LeftIndex_ > &left, const tatami::Matrix< RightValue_, RightIndex_ > &right, Output_ *const output, const bool output_row_major, const MultiplyWithDenseMatrixOptions &options)
Definition dispatch.hpp:121
void multiply_with_single_vector(const tatami::Matrix< Value_, Index_ > &left, const Right_ *const right, Output_ *const output, const MultiplyWithSingleVectorOptions &options)
Definition dispatch.hpp:77
void set_sparse_block_size(MultiplyWithDenseMatrixOptions &options, int block_size)
Definition dispatch.hpp:87
void set_num_threads(MultiplyDenseColumnWithDenseMatrixOptions &options, int num_threads)
Definition dispatch.hpp:54
std::shared_ptr< const Matrix< Value_, Index_ > > wrap_shared_ptr(const Matrix< Value_, Index_ > *const ptr)
Any matrix LHS, single vector RHS.
Any matrix LHS, sparse matrix RHS.
Options for multiply_with_dense_matrix().
Definition dispatch.hpp:19
Options for multiply_with_matrix().
Definition tatami_mult.hpp:28
MultiplyWithSparseMatrixOptions sparse_matrix
Definition tatami_mult.hpp:37
MultiplyWithDenseMatrixOptions dense_matrix
Definition tatami_mult.hpp:32
bool larger_left
Definition tatami_mult.hpp:47
Options for multiply_with_sparse_matrix().
Definition dispatch.hpp:19