DiSMEC++
fast_sparse_row_iter.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_FAST_SPARSE_ITER_H
7 #define DISMEC_FAST_SPARSE_ITER_H
8 
9 #include "matrix_types.h"
10 
11 namespace dismec
12 {
20 {
21 public:
22  using Scalar = SparseFeatures::Scalar;
23  using Index = SparseFeatures::Index;
24  using StorageIndex = SparseFeatures::StorageIndex;
26  : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(row)
27  {
28  m_id = mat.outerIndexPtr()[row];
29  m_end = mat.outerIndexPtr()[row+1];
30  }
31 
32  inline FastSparseRowIter& operator++() { m_id++; return *this; }
33 
34  [[nodiscard]] inline const Scalar& value() const { return m_values[m_id]; }
35  inline Scalar& valueRef() { return const_cast<Scalar&>(m_values[m_id]); }
36 
37  [[nodiscard]] inline int index() const { return m_indices[m_id]; }
38  [[nodiscard]] inline int outer() const { return m_outer; }
39  [[nodiscard]] inline int row() const { return m_outer; }
40  [[nodiscard]] inline int col() const { return index(); }
41 
42  inline explicit operator bool() const { return (m_id < m_end); }
43 
44 protected:
45  const Scalar* m_values;
47  const Index m_outer;
50 };
51 
52 
53 
54 template<typename Scalar, int Options, typename StorageIndex, typename OtherDerived>
55 auto fast_dot(const Eigen::SparseMatrix<Scalar, Options, StorageIndex>& first, int row, const Eigen::MatrixBase<OtherDerived>& other) -> Scalar {
56  auto values = first.valuePtr();
57  auto indices = first.innerIndexPtr();
58  auto id = first.outerIndexPtr()[row];
59  auto end = first.outerIndexPtr()[row + 1];
60 
61  Scalar a(0);
62  Scalar b(0);
63  Scalar c(0);
64  Scalar d(0);
65  for (; id < end - 4; id += 4) {
66  a += values[id] * other.coeff(indices[id]);
67  b += values[id + 1] * other.coeff(indices[id + 1]);
68  c += values[id + 2] * other.coeff(indices[id + 2]);
69  d += values[id + 3] * other.coeff(indices[id + 3]);
70  }
71  Scalar res = (a + b) + (c + d);
72  while (id != end) {
73  res += values[id] * other.coeff(indices[id]);
74  ++id;
75  }
76  return res;
77 }}
78 
79 
80 #endif //DISMEC_FAST_SPARSE_ITER_H
This is an almost verbatim copy of the SparseFeatures::InnerIterator provided by Eigen.
FastSparseRowIter & operator++()
SparseFeatures::Scalar Scalar
const StorageIndex * m_indices
FastSparseRowIter(const SparseFeatures &mat, Index row)
const Scalar & value() const
SparseFeatures::StorageIndex StorageIndex
SparseFeatures::Index Index
Main namespace in which all types, classes, and functions are defined.
Definition: app.h:15
auto fast_dot(const Eigen::SparseMatrix< Scalar, Options, StorageIndex > &first, int row, const Eigen::MatrixBase< OtherDerived > &other) -> Scalar
types::SparseRowMajor< real_t > SparseFeatures
Sparse Feature Matrix in Row Major format.
Definition: matrix_types.h:50