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

wee*_*not 5 c++ boost thrust odeint

我正在使用boost的使用push的odeint扩展参数研究示例,我不知道如何将值向量传递给观察者的构造函数,以便可以从观察者的函子中访问这些值(只读) .

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

//// 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; // 
};
Run Code Online (Sandbox Code Playgroud)

我已尝试制作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
    }
  };
Run Code Online (Sandbox Code Playgroud)

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

ERROR MESSAGE /usr/local/cuda/bin/..//include/thrust/detail/function.h(104):错误:从设备函数调用主机函数("thrust :: device_vector> :: device_vector")("thrust :: detail :: device_function :: device_function")是不允许的

我究竟做错了什么?

hea*_*der 4

您可以将推力矢量传递给函子,但不能轻松地将其存储在这里。但是您可以存储此向量中的底层原始指针:

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] 
    }
};
Run Code Online (Sandbox Code Playgroud)

这几乎是罗伯特·克罗维拉的建议。