命名空间中的并行工作器

时间:2019-03-09 22:08:07

标签: r rcpp rcppparallel rcpparmadillo

此示例是此先前文章的后续示例。我试图将Parallel Worker移至其自己的cpp文件,并在头文件中声明它。

Calling 'mypackage' function within public worker

两个错误如下: 1)变量类型'ExampleInternal :: PARALLEL_WORKER'是抽象类

,在我的不可复制示例中: 2)错误:Parallel_worker.cpp文件中“ ExampleInternal :: PARALLEL_WORKER {”行上的预期不合格ID。

现在代码如下:

ExampleInternal.h

#ifndef ExampleInternal_H
#define ExampleInternal_H

namespace ExampleInternal{

#include <RcppArmadillo.h>
#include <RcppParallel.h>

double myfunc3(arma::vec vec_in){

  int Len = arma::size(vec_in)[0];
  return (vec_in[0] +vec_in[1])/Len;
}

struct PARALLEL_WORKER : RcppParallel::Worker{};

}


#endif

Parallel_Worker.cpp

#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include <random>
#include "ExampleInternal.h"

using namespace RcppParallel;
using namespace ExampleInternal;

namespace ExampleInternal{

ExampleInternal::PARALLEL_WORKER{

  const arma::vec &input;
  arma::vec &output;

  PARALLEL_WORKER(const arma::vec &input, arma::vec &output) : input(input), output(output) {}

  void operator()(std::size_t begin, std::size_t end){


    std::mt19937 engine(1);

    // Create a loop that runs through a selected section of the total Boot_reps
    for( int k = begin; k < end; k ++){
      engine.seed(k);
      arma::vec index = input;
      std::shuffle( index.begin(), index.end(), engine);

      output[k] = ExampleInternal::myfunc3(index);
    }
  }

};

} //Close Namespace

Parallel_func.cpp

#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include "ExampleInternal.h"
using namespace ExampleInternal;

// [[Rcpp::export]]
arma::vec Parallelfunc(int Len_in){

  arma::vec input = arma::regspace(0, 500);
  arma::vec output(Len_in);

  ExampleInternal::PARALLEL_WORKER parallel_woker(input, output);
  parallelFor( 0, Len_in, parallel_woker);
  return output;
}

2 个答案:

答案 0 :(得分:1)

您需要正确地在结构的声明和定义之间进行拆分。头文件中的声明包含成员变量和方法签名。

namespace ExampleInternal{

struct PARALLEL_WORKER : RcppParallel::Worker{
  const arma::vec &input;
  arma::vec &output;

  PARALLEL_WORKER(const arma::vec &input, arma::vec &output);
  void operator()(std::size_t begin, std::size_t end);

};
}

然后在cpp文件中定义方法:

namespace ExampleInternal{

  PARALLEL_WORKER::PARALLEL_WORKER(const arma::vec &input, arma::vec &output) : input(input), output(output) {}

  void PARALLEL_WORKER::operator()(std::size_t begin, std::size_t end){

    std::mt19937 engine(1);

    // Create a loop that runs through a selected section of the total Boot_reps
    for( std::size_t k = begin; k < end; k ++){
      engine.seed(k);
      arma::vec index = input;
      std::shuffle( index.begin(), index.end(), engine);

      output[k] = ExampleInternal::myfunc3(index);
    }
  }


} //Close Namespace

我必须做一些更改才能编译所有内容而没有警告(标头中定义的函数应为inline等)。完整的详细信息位于https://github.com/rstub/stackoverflow/tree/master/55082456。请注意,某些更改仅在包的Rcpp属性 outside 上下文中才有意义。 顺便说一句,由于您不提供测试数据,因此我只验证了编译,无法正确操作。

答案 1 :(得分:0)

只是跟进。要将myfunc3移至单独的.cpp文件,需要将RcppParallel标头包含到myfunc3.cpp文件中。我也不必添加“内联”。

myfunc3.cpp

#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include "ExampleInternal.h"

using namespace arma;

namespace ExampleInternal{

double myfunc3(arma::vec vec_in){

  int Len = arma::size(vec_in)[0];
  return (vec_in[0] +vec_in[1])/Len;
}

} // Close namespace

和ExampleInternal.h

#ifndef ExampleInternal_H
#define ExampleInternal_H

namespace ExampleInternal{

#include <RcppArmadillo.h>
#include <RcppParallel.h>

double myfunc3(arma::vec vec_in);

struct PARALLEL_WORKER : RcppParallel::Worker{
  const arma::vec &input;
  arma::vec &output;
  PARALLEL_WORKER( const arma::vec &input, arma::vec &output);
  void operator()(std::size_t begin, std::size_t end);
};

}

#endif