[英]Parallel Worker in namespace
本示例是該早期文章的后續示例。 我試圖將Parallel Worker移至其自己的cpp文件,並在頭文件中聲明它。
這兩個錯誤如下:1)變量類型'ExampleInternal :: PARALLEL_WORKER'是一個抽象類
在我的不可復制示例中:2)錯誤:Parallel_worker.cpp文件中“ ExampleInternal :: PARALLEL_WORKER {”行上的預期unqualified-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;
}
您需要正確地在結構的聲明和定義之間進行拆分。 頭文件中的聲明包含成員變量和方法簽名。
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屬性的上下文中才有意義。 順便說一句,由於您不提供測試數據,所以我只驗證了編譯,無法正確操作。
只是作為后續。 要將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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.