This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Using cnl::fixed_point with Eigen

Tags: None
(comma "," separated)
metafor
Registered Member
Posts
1
Karma
0

Using cnl::fixed_point with Eigen

Fri Oct 16, 2020 8:36 am
I'm trying to make Eigen::Matrix operations work with a fixed point format that will handle the precision/rounding of multiply/divide and other opts associated with matrix operations. I settled on cnl::fixed_point as it's already included in most G++ distros.

I read on the Eigen site that I need NumTraits as well as math functions defined in order for Eigen to support custom types. I have this code:
Code: Select all
namespace Eigen {
 
template<> struct NumTraits<cnl::fixed_point<int, -16>> : NumTraits<int>
{   
  typedef cnl::fixed_point<int, -16> Real;
  typedef cnl::fixed_point<int, -16> NonInteger;
  typedef cnl::fixed_point<int, -16> Nested;
   
  enum {                           
    IsComplex = 0,                 
    IsInteger = 0,                 
    IsSigned = 1,                 
    RequireInitialization = 1,     
    ReadCost = 1,                 
    AddCost = 3,                   
    MulCost = 3                   
  };
};
 
}
                                                                                                                                                                                 
namespace cnl {
 
inline const cnl::fixed_point<int, -16>& conj(const cnl::fixed_point<int, -16>& x)  { return x; }
inline const cnl::fixed_point<int, -16>& real(const cnl::fixed_point<int, -16>& x)  { return x; }
inline cnl::fixed_point<int, -16> imag(const cnl::fixed_point<int, -16>&)    { return 0; }
// inline cnl::fixed_point<int, -16> abs(const cnl::fixed_point<int, -16>&  x)  { return (cnl::to_rep<int, -16>(x) < 0) ? -x : x; }
inline cnl::fixed_point<int, -16> abs2(const cnl::fixed_point<int, -16>& x)  { return x*x; }
inline cnl::fixed_point<int, -16> max(const cnl::fixed_point<int, -16>& x, const cnl::fixed_point<int, -16>& y) { return (x < y) ? y : x; }
 
}


I then try to do a Matrix multiply of a vector:
Code: Select all
template <typename DATA_T, int F, int K, int V>
void my_func(
    Eigen::Matrix<DATA_T, 1, V>& my_prod,
    const Eigen::Matrix<DATA_T, (F * K), V>& my_mat,
    const Eigen::Matrix<DATA_T, 1, (F * K)>& my_vec) {
  my_prod = my_vec * my_mat;
}


I get the following error though:
Code: Select all
/usr/local/include/cnl/_impl/scaled/convert_operator.h: In instantiation of ‘struct cnl::convert_operator<cnl::power<-16, 2>, cnl::_impl::native_tag, int, Eigen::Produ[142/1995]
Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 1, 16384, 1, 1, 16384>, Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 16384, 128, 0, 16384, 128>, 0>, void>’
:                                                                                                                                                                               
/usr/local/include/cnl/_impl/operators/tagged.h:30:63:   required by substitution of ‘template<class DestTag, class SrcTag, class Dest, class Src> constexpr decltype (cnl::conve
rt_operator<DestTag, SrcTag, Dest, Src>{}(src)) cnl::convert(const Src&) [with DestTag = cnl::power<-16, 2>; SrcTag = cnl::_impl::native_tag; Dest = int; Src = Eigen::Product<Ei
gen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 1, 16384, 1, 1, 16384>, Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 16384, 128, 0, 16384, 128>, 0>]’
/usr/local/include/cnl/_impl/number/definition.h:62:60:   required from ‘constexpr cnl::_impl::number<Rep, Tag>::number(const S&) [with S = Eigen::Product<Eigen::Matrix<cnl::sca
led_integer<int, cnl::power<-16, 2> >, 1, 16384, 1, 1, 16384>, Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 16384, 128, 0, 16384, 128>, 0>; typename std::enable_
if<(! cnl::_impl::is_number<Number>::value), int>::type Dummy = 0; Rep = int; Tag = cnl::power<-16, 2>]’                                                                         
/usr/local/include/cnl/_impl/scaled_integer/type.h:79:26:   required from ‘constexpr cnl::scaled_integer<Rep, Scale>::scaled_integer(const Number&) [with Number = Eigen::Product
<Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 1, 16384, 1, 1, 16384>, Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 16384, 128, 0, 16384, 128>, 0>
; Rep = int; Scale = cnl::power<-16, 2>]’                                                                                                                                       
/usr/local/include/eigen3/Eigen/src/Core/util/Meta.h:176:29:   required from ‘struct Eigen::internal::is_convertible_impl<Eigen::Product<Eigen::Matrix<cnl::scaled_integer<int, c
nl::power<-16, 2> >, 1, 16384, 1, 1, 16384>, Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 16384, 128, 0, 16384, 128>, 0>, cnl::scaled_integer<int, cnl::power<-16
, 2> > >’                                                                                                                                                                       
/usr/local/include/eigen3/Eigen/src/Core/util/Meta.h:185:8:   required from ‘struct Eigen::internal::is_convertible<Eigen::Product<Eigen::Matrix<cnl::scaled_integer<int, cnl::po
wer<-16, 2> >, 1, 16384, 1, 1, 16384>, Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 16384, 128, 0, 16384, 128>, 0>, cnl::scaled_integer<int, cnl::power<-16, 2> >
 >’                                                                                                                                                                             
/usr/local/include/eigen3/Eigen/src/Core/PlainObjectBase.h:870:67:   [ skipping 5 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]                         
/usr/local/include/eigen3/Eigen/src/Core/Matrix.h:225:24:   required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& Eigen::Matrix<_Scalar, _Rows, _Col
s, _Options, _MaxRows, _MaxCols>::operator=(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::Product<Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2>
>, 1, 16384, 1, 1, 16384>, Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 16384, 128, 0, 16384, 128>, 0>; _Scalar = cnl::scaled_integer<int, cnl::power<-16, 2> >;
int _Rows = 1; int _Cols = 128; int _Options = 1; int _MaxRows = 1; int _MaxCols = 128]’                                                                                         
[REDACTED]
/usr/local/include/cnl/_impl/scaled/convert_operator.h:73:12: error: invalid use of incomplete type ‘struct cnl::convert_operator<cnl::power<-16, 2>, cnl::power<>, int, Eigen::P
roduct<Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 1, 16384, 1, 1, 16384>, Eigen::Matrix<cnl::scaled_integer<int, cnl::power<-16, 2> >, 16384, 128, 0, 16384, 12
8>, 0>, void>’
   73 |     struct convert_operator<
      |            ^~~~~~~~~~~~~~~~~
   74 |             power<DestExponent, DestRadix>,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   75 |             _impl::native_tag,
      |             ~~~~~~~~~~~~~~~~~~
   76 |             Result, Input>
      |             ~~~~~~~~~~~~~~


I can only imagine this means I failed to implement one of the required operators that Matrix multiplication requires? But I'm having a hard time figuring out what that operator is from the error message.

Can someone with better eyes and understanding of this help?


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]