如何使用RcppParallel创建Rcpp :: CharacterMatrix的线程安全包装?

时间:2017-04-15 08:53:57

标签: c++ r rcpp rcppparallel

我有一个任务,我需要处理一个大矩阵(数百万行,数百列)的字符串。每行操作都是独立的。因此,我想利用一些并行计算来提高整个项目的速度。

如果我为数字矩阵构建myWorker,如下所示,我可以编译代码而不会出错

// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
#include <Rcpp.h>
#include <string.h>

struct myWorker : public RcppParallel::Worker
{
  // input
  const RcppParallel::RMatrix<double> input;
  int version;

  // output
  RcppParallel::RMatrix<double> outmat;

  // initialization
  myWorker(const Rcpp::NumericMatrix input, int version, Rcpp::NumericMatrix outmat) 
    : input(input), version(version), outmat(outmat) {}

  // the operator
  void operator()(std::size_t begin, std::size_t end) {
    // do stuff
  }
};

但是,当我设置输入矩阵和初始化时使用 Rcpp::CharacterMatrix我收到编译错误。

In instantiation of ‘RcppParallel::RMatrix<T>::RMatrix(const Source&) [with
Source = Rcpp::Matrix<16>; T = <typehere>]

R/x86_64-pc-linux-gnu-library/3.3/RcppParallel/include/RcppParallel/RMatrix.h:198:28:
error: cannot convert ‘Rcpp::Matrix<16>::iterator {aka
Rcpp::internal::Proxy_Iterator<Rcpp::internal::string_proxy<16> >}’ to
‘std::basic_string<char>*’ in initialization
         ncol_(source.ncol())

我尝试过使用myWorker(const Rcpp::NumericMatrix input

的组合
const RcppParallel::RMatrix<std::string> input;
const RcppParallel::RMatrix<char> input;
const RcppParallel::RMatrix<char*> input;
const RcppParallel::RMatrix<char**> input;
const RcppParallel::RMatrix<char32_t> input;

指针是一个坏主意。其他选项导致注意到常见错误 上方。

提出了一个非常相似的问题 here

是否有一种简单的方法来包装Rcpp::NumericMatrix RcppParallel::RMatrix用于使用字符矩阵的线程安全工作吗?

修改

有关任务的更多详细信息:

imput矩阵由ICD-9-CM或ICD-10-CM代码组成,需要 与用于确定分类的代码集进行比较。有数百万 行,数百列,以及大约十几个分类。

纯R中的一个小例子是:

classification_1 <-
  c("99680", "99688", "99689", "V421", "V422", "V426", "V5391", "4697", "5051",
    "5059", "5280", "5282", "4103", "0091", "0092", "0093")
classification_2 <-
  c("14", "15", "16", "17", "18", "19", "20", "23", "V4281", "V4282", "0010", "9925")

icd_codes <- 
  structure(c("5282", "3320", "4100", "0234", "V426", "3895", "3592", 
              "5651", "0397", "V5302", "5675", "0092", "V461", "4697", "5571", 
              "3776", "9964", "9702", "3583", "8607", "99661", "3767", "3129", 
              "3182", "5503", "5285", "4641", "6861", "3351", "2751", "76511", 
              "V446", "34581", "7472", "5190", "9723", "28801", "0010", "8103", 
              "4270", "9962", "4211", "4242", "34511", "3352", "0372", "76492", 
              "5675", "284", "4281", "3314", "0681", "3781", "0152", "3760", 
              "3763", "5597", "4399", "V5351", "8108", "3994", "4581", "V460", 
              "5533", "8137", "99663", "4210", "741", "5722", "8949", "76412", 
              "5569", "5674", "99667", "7707", "3753", "8606", "V553", "5051", 
              "2884", "5059", "7711", "8136", "5673", "7373", "2821", "5993", 
              "3776", "2822", "4274", "3789", "0371", "3591", "76523", "5722", 
              "V56", "V445", "2359", "4243", "99683"), .Dim = c(5L, 20L))

apply(icd_codes, 1,
      function(x) {
        c(class1 = as.integer(any(x %in% classification_1)),
          class2 = as.integer(any(x %in% classification_2)))
      }) 

icd_codes对象的每一行都可以并行计算。因为我有一个 工作单线程C ++版本的上述工作,我希望能够使用 RcppParallel提高了工作的整体速度,并且批判性地,这样做了 尽可能接近OS独立的方式。我正在与之合作的小组 由Windows,OSX和Linux用户组成。

1 个答案:

答案 0 :(得分:0)

有关共病分类问题的基于Rcpp的矩阵代数的快速求解方法,请参见我的软件包icd,尤其是在methodology上发送给JSS的PDF文章。

使用字符串处理永远不会很快,因为无论进行多少高级优化,您都可以在进行性能分析时迅速找到答案。