Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
ApplyWeights.cpp File Reference
#include "moab/Remapping/TempestOnlineMap.hpp"
#include <algorithm>
#include <utility>
#include <vector>
+ Include dependency graph for ApplyWeights.cpp:

Go to the source code of this file.

Classes

struct  KahanSum
 

Functions

double pairwiseSum (const std::set< double > &sorted)
 
double pairwiseKahanSum (const std::set< double > &sorted)
 
void deterministicSparseMatVecMul (const typename moab::TempestOnlineMap::WeightMatrix &A, const typename moab::TempestOnlineMap::WeightColVector &x, typename moab::TempestOnlineMap::WeightRowVector &result)
 
void deterministicSparseMatVecMulKahan (const typename moab::TempestOnlineMap::WeightMatrix &A, const typename moab::TempestOnlineMap::WeightColVector &x, typename moab::TempestOnlineMap::WeightRowVector &result)
 
void deterministicSparseMatVecMulClean (const typename moab::TempestOnlineMap::WeightMatrix &A, const typename moab::TempestOnlineMap::WeightColVector &x, typename moab::TempestOnlineMap::WeightRowVector &result)
 
void deterministicSparseMatVecMulNative (const typename moab::TempestOnlineMap::WeightMatrix &A, const typename moab::TempestOnlineMap::WeightColVector &x, typename moab::TempestOnlineMap::WeightRowVector &result)
 
void deterministicSparseMatTransposeVecMul (const typename moab::TempestOnlineMap::WeightMatrix &A, const typename moab::TempestOnlineMap::WeightRowVector &x, typename moab::TempestOnlineMap::WeightColVector &result)
 
void deterministicSparseMatTransposeVecMulClean (const typename moab::TempestOnlineMap::WeightMatrix &A, const typename moab::TempestOnlineMap::WeightRowVector &x, typename moab::TempestOnlineMap::WeightColVector &result)
 
void deterministicSparseMatTransposeVecMulNative (const typename moab::TempestOnlineMap::WeightMatrix &A, const typename moab::TempestOnlineMap::WeightRowVector &x, typename moab::TempestOnlineMap::WeightColVector &result)
 

Function Documentation

◆ deterministicSparseMatTransposeVecMul()

void deterministicSparseMatTransposeVecMul ( const typename moab::TempestOnlineMap::WeightMatrix &  A,
const typename moab::TempestOnlineMap::WeightRowVector &  x,
typename moab::TempestOnlineMap::WeightColVector &  result 
)
inline

Definition at line 152 of file ApplyWeights.cpp.

155 {
156  result.setZero(); // Ensure no uninitialized memory issues
157 
158  // Temporary storage for pairwise summation
159  std::vector< std::set< double > > accumulators( A.cols() );
160 
161  // Iterate over A row-wise, but accumulate into result as if computing A^T * x
162  for( int row = 0; row < A.outerSize(); ++row )
163  {
164  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
165  {
166  accumulators[it.col()].insert( it.value() * x( row ) );
167  }
168  }
169 
170  // Compute final sum using pairwise summation for each entry
171  for( int col = 0; col < A.cols(); ++col )
172  {
173  // result( col ) = pairwiseSum( accumulators[col] );
174  result( col ) = pairwiseKahanSum( accumulators[col] );
175  }
176 }

References pairwiseKahanSum().

◆ deterministicSparseMatTransposeVecMulClean()

void deterministicSparseMatTransposeVecMulClean ( const typename moab::TempestOnlineMap::WeightMatrix &  A,
const typename moab::TempestOnlineMap::WeightRowVector &  x,
typename moab::TempestOnlineMap::WeightColVector &  result 
)
inline

Definition at line 179 of file ApplyWeights.cpp.

182 {
183  result.setZero(); // Ensure no uninitialized memory issues
184 
185  // Iterate over A row-wise, but accumulate into result as if computing A^T * x
186  for( int row = 0; row < A.outerSize(); ++row )
187  {
188  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
189  {
190  const double product = it.value() * x( row ); // Compute product
191  result( it.col() ) += product; // Accumulate contributions to the corresponding row in A^T
192  }
193  }
194 }

References product().

Referenced by moab::TempestOnlineMap::ApplyWeights().

◆ deterministicSparseMatTransposeVecMulNative()

void deterministicSparseMatTransposeVecMulNative ( const typename moab::TempestOnlineMap::WeightMatrix &  A,
const typename moab::TempestOnlineMap::WeightRowVector &  x,
typename moab::TempestOnlineMap::WeightColVector &  result 
)
inline

Definition at line 197 of file ApplyWeights.cpp.

200 {
201  result = A.adjoint() * x; // Perform the adjoint.matrix-vector multiplication using Eigen3
202 }

◆ deterministicSparseMatVecMul()

void deterministicSparseMatVecMul ( const typename moab::TempestOnlineMap::WeightMatrix &  A,
const typename moab::TempestOnlineMap::WeightColVector &  x,
typename moab::TempestOnlineMap::WeightRowVector &  result 
)
inline

Definition at line 73 of file ApplyWeights.cpp.

76 {
77  constexpr bool useKahanSum = false;
78  constexpr bool usePairwiseSum = false;
79 
80  result.setZero(); // Ensure no uninitialized memory issues
81 
82  // Iterate row-wise to enforce a fixed summation order
83  for( int row = 0; row < A.outerSize(); ++row )
84  {
85  std::set< double > accumulators;
86  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
87  {
88  // accumulators contains the sorted values of the product: A(row, col) * x(col)
89  accumulators.insert( it.value() * x( it.col() ) );
90  }
91  if( usePairwiseSum ) result( row ) = pairwiseSum( accumulators );
92  if( useKahanSum ) result( row ) = pairwiseKahanSum( accumulators );
93 
94  if( !usePairwiseSum && !useKahanSum )
95  {
96  double sum = 0.0;
97  for( double val : accumulators )
98  sum += val;
99  result( row ) = sum;
100  }
101  }
102 }

References pairwiseKahanSum(), pairwiseSum(), and moab::sum().

◆ deterministicSparseMatVecMulClean()

void deterministicSparseMatVecMulClean ( const typename moab::TempestOnlineMap::WeightMatrix &  A,
const typename moab::TempestOnlineMap::WeightColVector &  x,
typename moab::TempestOnlineMap::WeightRowVector &  result 
)
inline

Definition at line 127 of file ApplyWeights.cpp.

130 {
131  result.setZero(); // Ensure no uninitialized memory issues
132 
133  // Iterate row-wise to enforce a fixed summation order
134  for( int row = 0; row < A.outerSize(); ++row )
135  {
136  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
137  {
138  const double product = it.value() * x( it.col() ); // Compute product
139  result( it.row() ) += product;
140  }
141  }
142 }

References product().

Referenced by moab::TempestOnlineMap::ApplyWeights().

◆ deterministicSparseMatVecMulKahan()

void deterministicSparseMatVecMulKahan ( const typename moab::TempestOnlineMap::WeightMatrix &  A,
const typename moab::TempestOnlineMap::WeightColVector &  x,
typename moab::TempestOnlineMap::WeightRowVector &  result 
)
inline

Definition at line 106 of file ApplyWeights.cpp.

109 {
110  result.setZero(); // Ensure no uninitialized memory issues
111 
112  // Iterate row-wise to enforce a fixed summation order
113  for( int row = 0; row < A.outerSize(); ++row )
114  {
115  KahanSum kahan;
116  for( typename moab::TempestOnlineMap::WeightMatrix::InnerIterator it( A, row ); it; ++it )
117  {
118  double product = it.value() * x( it.col() ); // Compute product
119  kahan.add( product );
120  }
121 
122  result( row ) = kahan.result();
123  }
124 }

References KahanSum::add(), product(), and KahanSum::result().

◆ deterministicSparseMatVecMulNative()

void deterministicSparseMatVecMulNative ( const typename moab::TempestOnlineMap::WeightMatrix &  A,
const typename moab::TempestOnlineMap::WeightColVector &  x,
typename moab::TempestOnlineMap::WeightRowVector &  result 
)
inline

Definition at line 144 of file ApplyWeights.cpp.

147 {
148  result = A * x; // Perform the matrix-vector multiplication using Eigen3
149 }

◆ pairwiseKahanSum()

double pairwiseKahanSum ( const std::set< double > &  sorted)
inline

Definition at line 59 of file ApplyWeights.cpp.

60 {
61  if( sorted.empty() ) return 0.0;
62  if( sorted.size() == 1 ) return *sorted.begin();
63 
64  // Accumulate pairwise to minimize rounding error
65  // Apply pairwise summation with Kahan correction
66  KahanSum kahan;
67  for( double val : sorted )
68  kahan.add( val );
69  return kahan.result();
70 }

References KahanSum::add(), and KahanSum::result().

Referenced by deterministicSparseMatTransposeVecMul(), and deterministicSparseMatVecMul().

◆ pairwiseSum()

double pairwiseSum ( const std::set< double > &  sorted)
inline

Definition at line 46 of file ApplyWeights.cpp.

47 {
48  if( sorted.empty() ) return 0.0;
49  if( sorted.size() == 1 ) return *sorted.begin();
50 
51  // Accumulate pairwise to minimize rounding error
52  double sum = 0.0;
53  for( double val : sorted )
54  sum += val;
55  return sum;
56 }

References moab::sum().

Referenced by deterministicSparseMatVecMul().