C++ 获取单位值在boost ODEINT中的作用
在以下代码中:C++ 获取单位值在boost ODEINT中的作用,c++,templates,c++11,boost,odeint,C++,Templates,C++11,Boost,Odeint,在以下代码中: 您忘记复制其中一个专业: 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>
您忘记复制其中一个专业:
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
非常感谢你。现在,我更明白目的了。但这还不完全清楚。我从中了解到,如果没有CUDA加速器,那么取参数的
值?但是如果有CUDA ACC,就不要这样做?但为什么呢?@torbani,我不能说。我对CUDA一无所知,也不知道为什么在这种情况下该函数没有实现。我只能说它存在的一般原因。@torbani CUDA编译器因一看到任何Boost头就破译而臭名昭著;我猜有人试图站在Boost一边防守,我不理解Cuda的问题。如果有问题,你能详细解释一下吗。Barry是对的,我们只是介绍了它,以确保与Boost.Units的兼容性。运行时不应受到此模板的影响。优化器应该完全消除它。
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);
}
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;
};
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
int x = 7;
quantity<length> L = 2.0*meters;
get_unit_value(x); // 7
get_unit_value(L); // 2.0