gpt4 book ai didi

c++ - 如何将 vector 传递给基于推力的 odeint 观察器的构造函数,以便可以在仿函数中读取它

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:34:35 26 4
gpt4 key购买 nike

我正在扩展与 thrust 一起使用的 boost 的 odeint 的参数研究示例,我不知道如何将值 vector 传递给观察者的构造函数,以便可以从内部访问(只读)这些值观察者的仿函数。

以下是仅供观察者使用的代码。

//// 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; //
};

我已经尝试使 m_unchangingVector staticconst,但这是不正确的,因为它需要在观察者实例化时设置。

或者,也许最好的方法是将 unchangingVector 作为 thrust::make_zip_iterator( thrust::make_tuple(... 中的另一个参数传递,但我觉得这些项目将是索引状态变量的方式(这不是我想要的)。一个可能有帮助的答案是解释(T t)在仿函数声明中的含义,以及我如何将 unchangingVector 作为同一对象传递给每个正在评估运算符的线程。

我认为这可能只是选择正确的关键字变量描述符的问题,但我不知道要使用哪个,而且我不确定如何查找/弄清楚。

我在上面的代码中得到的错误是error: a nonstatic member reference must be relative to a specific object。当我尝试访问仿函数中的 m_unchangingVector 时抛出。


经过进一步的探索,我觉得我找到了完成这个任务的正确方法,但我仍然卡住了。

我已经为仿函数添加了一个构造函数。

  struct minPerturbFunctor
{

minPerturbFunctor( state_type unchangingVector, int len ) :
f_unchangingVector( len )
{
// copy from argument to local vector (probably unnecessary, but
// getting errors about calling host-functions from device/host
// so being paranoid about trying to make sure things are device-side
thrust::copy( f_unchangingVector.begin(),
f_unchangingVector.end(),
unchangingVector.begin());
f_len = len;
};

template< class T >
__host__ __device__
void operator()( T t ) const
{
// I can now access f_len here (progress!)
// But when I try to access any element via e.g.,
// f_unchangingVector[0] I get the error below
}
};

警告:从主机 设备调用主机函数("thrust::detail::vector_base >::operator []") > function("minimum_perturbation_observer::minPerturbFunctor::operator () > ") 是不允许的

错误信息/usr/local/cuda/bin/..//include/thrust/detail/function.h(104): 错误:调用host 函数("thrust::device_vector >::device_vector") 来自设备 函数("thrust::detail::device_function::device_function") 是不允许的

我做错了什么?

最佳答案

您可以将一个推力 vector 传递给仿函数,但您不能轻易地将它存储在这里。但是您可以从这个 vector 中存储底层原始指针:

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]
}
};

这几乎是 Robert Crovella 的建议。

关于c++ - 如何将 vector 传递给基于推力的 odeint 观察器的构造函数,以便可以在仿函数中读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com