1

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

如果我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;

指针是个坏主意。其他选项会导致上述常见错误。

这里提出了一个非常相似的问题 。

有没有一种简单的方法来包装一个字符矩阵Rcpp::NumericMatrixRcppParallel::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 来提高工作的整体速度,并且至关重要的是,以尽可能接近操作系统独立的方式这样做。我的工作组由 Windows、OSX 和 Linux 用户组成。

4

1 回答 1

0

对于共病分类问题的一个非常快速的基于 Rcpp 的矩阵代数解决方案,请参阅我的包icd,特别是发送给 JSS 的关于该方法的 PDF 文章。

字符串处理永远不会很快,因为无论你做了多少更高级别的优化,你都会在分析时很快发现。

于 2018-12-16T21:38:54.530 回答