C++ 如何将向量传递给基于推力的odeint观察器的构造函数,以便在函子中读取向量
我从boost的odeint(与推力一起使用)扩展了参数研究示例,我不知道如何将值向量传递给观测者的构造函数,以便可以从观测者的函子中访问(只读)这些值 以下是仅供观察者使用的代码C++ 如何将向量传递给基于推力的odeint观察器的构造函数,以便在函子中读取向量,c++,boost,thrust,odeint,C++,Boost,Thrust,Odeint,我从boost的odeint(与推力一起使用)扩展了参数研究示例,我不知道如何将值向量传递给观测者的构造函数,以便可以从观测者的函子中访问(只读)这些值 以下是仅供观察者使用的代码 //// Observes the system, comparing the current state to //// values in unchangingVector struct minimum_perturbation_observer { struct minPerturbFunctor
//// Observes the system, comparing the current state to
//// values in unchangingVector
struct minimum_perturbation_observer {
struct minPerturbFunctor
{
template< class T >
__host__ __device__
void operator()( T t ) const
{
//// I would like to be able to read any member
//// of m_unchangingVector here.
}
};
// CONSTRUCTOR
minimum_perturbation_observer( size_t N, state_type unchangingVector, int len) :
m_N( N ),
m_output( N ),
m_unchangingVector( len ) // len is the correct length of unchangingVector
{
// all trials start with output = 0
thrust::fill( m_output.begin() , m_output.end() , 0.0 );
// copy unchangingVector to m_unchangingVector, the latter
// of which should be accessible from the functor operator()
// above.
thrust::copy( unchangingVector.begin(), unchangingVector.end(),
m_unchangingVector.begin());
}
template< class State >
void operator()(State x , value_type t )
{
thrust::for_each(
thrust::make_zip_iterator( thrust::make_tuple(
boost::begin( x ) + 0*m_N,
boost::begin( x ) + 1*m_N,
boost::begin( m_output )
)
),
thrust::make_zip_iterator( thrust::make_tuple(
boost::begin( x ) + 1*m_N,
boost::begin( x ) + 2*m_N,
boost::begin( m_output ) + m_N
)
) ,
minPerturbFunctor() );
}
// variables
size_t m_N; // number of trials (i.e. number of initial conditions)
state_type m_output; // of length N_ICS
state_type m_unchangingVector; //
};
警告:不允许从主机设备调用主机函数(“最小扰动观察者::最小扰动函子::运算符()>”)
错误消息
/usr/local/cuda/bin/.//include/推力/细节/函数.h(104):错误:不允许从设备函数(“推力::细节::设备功能::设备功能”)调用主机函数(“推力::设备向量>::设备向量”)
我做错了什么?可以将推力向量传递给函子,但不能轻松地将其存储在这里。但您可以存储此向量中的基础原始指针:
struct minPerturbFunctor
{
state_type::value_type* m_ptr;
size_t m_len;
minPerturbFunctor( state_type const& x )
: m_ptr( thrust::raw_pointer_cast(&x[0]) )
, m_len( x.size() )
{ }
template< class T >
__host__ __device__
void operator()( T t ) const
{
// now you can access m_ptr like m_ptr[i]
}
};
struct函数
{
状态类型::值类型*m\u ptr;
尺寸;
最小扰动函子(状态类型常量&x)
:m_ptr(推力::原始指针\u投射(&x[0]))
,m_len(x.size())
{ }
模板
__主机设备__
void运算符()(T)常量
{
//现在您可以像访问m_ptr[i]一样访问m_ptr
}
};
这几乎是罗伯特·克罗维拉的建议。我想我找到了一个例子,可以满足我的需要。我会看得更远,然后在这里发帖。你可以把一个初始化参数传递给你的函子。该参数可以是
.data()
为推力::设备\u向量返回的指针。然后,该指针可以在函子中使用,使用普通的c指针方法,访问函子中设备向量的任何元素
并将初始化元素作为m_unchangingVector.data()
,粗略地说。@RobertCrovella我可以撤回我的答案,这样您就可以给出正确的(希望是)接受的答案……我看不出您的答案有任何错误。我投了更高的票。我还没有真正尝试过,但我认为它正确地传达了这个想法。
struct minPerturbFunctor
{
state_type::value_type* m_ptr;
size_t m_len;
minPerturbFunctor( state_type const& x )
: m_ptr( thrust::raw_pointer_cast(&x[0]) )
, m_len( x.size() )
{ }
template< class T >
__host__ __device__
void operator()( T t ) const
{
// now you can access m_ptr like m_ptr[i]
}
};