命名空间中的并行工作器 [英] Parallel Worker in namespace

查看:127
本文介绍了命名空间中的并行工作器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This example is a follow up example from this earlier post. I am trying to move the Parallel Worker to its own cpp file and and declare it in the header file.

在公共工作人员内部调用"mypackage"函数

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

The two errors are as follows: 1) variable type 'ExampleInternal::PARALLEL_WORKER' is an abstract class

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

and in my non-reproducible example: 2) error: expected unqualified-id on the 'ExampleInternal::PARALLEL_WORKER{' line in the Parallel_worker.cpp file.

现在,代码如下所示:

ExampleInternal.h

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

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

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

推荐答案

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

You need to do the split between the declaration and the definition of your struct correctly. The declaration in the header file contains the member variables and method signatures.

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文件中定义方法:

In the cpp file you then define your methods:

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等).详细信息,请参见

I had to do a few more changes to get everything compiled without warnings (the function defined in the header should be inline etc.) Full details at https://github.com/rstub/stackoverflow/tree/master/55082456. Note that some of the changes make only sense in the context of Rcpp Attributes outside of a package. BTW, since you don't provide test data, I have only verified the compilation, not proper operation.

这篇关于命名空间中的并行工作器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆