【问题标题】:role of get_unit_value in boost ODEINTget_unit_value 在 boost ODEINT 中的作用
【发布时间】:2015-02-10 12:05:00
【问题描述】:

Boost library的以下代码中:

template<class T , class Enabler = void >
struct get_unit_value_impl
{
    static T value(const T &t)
    {
        return t;
    }
    typedef T result_type;
};

...

template<class T>
typename detail::get_unit_value_impl<T>::result_type get_unit_value(const T &t)
{
    return detail::get_unit_value_impl<T>::value(t);
}

get_unit_value 的角色我不清楚。它有什么作用?我们传递一些东西给它,它返回相同的值。为什么有人要把它包装成一个结构?除了减慢运行时间之外,它还有什么作用吗?

这段代码是从here调用的:

template< class Fac1 = double >
struct rel_error
{
    const Fac1 m_eps_abs , m_eps_rel , m_a_x , m_a_dxdt;

    rel_error( Fac1 eps_abs , Fac1 eps_rel , Fac1 a_x , Fac1 a_dxdt )
    : m_eps_abs( eps_abs ) , m_eps_rel( eps_rel ) , m_a_x( a_x ) , m_a_dxdt( a_dxdt ) { }


    template< class T1 , class T2 , class T3 >
    void operator()( T3 &t3 , const T1 &t1 , const T2 &t2 ) const
    {
        using std::abs;
        set_unit_value( t3 , abs( get_unit_value( t3 ) ) / ( m_eps_abs + m_eps_rel * ( m_a_x * abs( get_unit_value( t1 ) ) + m_a_dxdt * abs( get_unit_value( t2 ) ) ) ) );
    }

    typedef void result_type;
};

【问题讨论】:

  • 运行时不应受此模板的影响。优化器应该完全消除它。

标签: c++ templates c++11 boost odeint


【解决方案1】:

你忘了复制其中一个专精:

template<class T , class Enabler = void >
struct get_unit_value_impl
{
    static T value(const T &t)
    {
        return t;
    }
    typedef T result_type;
};

#ifndef __CUDACC__
template<class Unit , class T>
struct get_unit_value_impl< boost::units::quantity< Unit , T> >
{
    static T value( const boost::units::quantity< Unit , T> &t )
    {
        return t.value();
    }
    typedef T result_type;
};
#endif

get_unit_value() 的要点是接受一个值并返回它,或者一个boost::unit::quantity&lt;Unit, T&gt; 并从中返回值。这样,调用者就不必担心t 是否只是int 或更复杂的东西:

int x = 7;
quantity<length> L = 2.0*meters;

get_unit_value(x); // 7
get_unit_value(L); // 2.0

【讨论】:

  • 非常感谢。现在,我更好地理解了目的。仍然不完全清楚。我从中了解到的是,如果没有 CUDA 加速器,那么取 value 的论点?但是如果有 CUDA ACC 不这样做呢?但为什么呢?
  • @torbani 我不能说。我对 CUDA 以及为什么在这种情况下未实现该功能一无所知。我只能说它为什么普遍存在。
  • @torbani CUDA 编译器因在看到任何 Boost 头文件时就崩溃而臭名昭著;我猜有人试图在 Boost 这边进行防御。
  • 我不明白 Cuda 的问题。如果有问题,您能更详细地解释一下吗? Barry 是对的,我们只是引入它,以确保与 Boost.Units 的兼容性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2014-05-12
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多