c++ - Rcpp 并行 : RMatrix and RVector arithmetic operations

标签 c++ parallel-processing rcpp

我正在尝试使用 RcppArmadillo 并行化一个双 for 循环,但我在处理 RMatrixRVector 可用的算术运算时遇到了问题。我看了标题 file在 github 上可用,但我在那里没有看到任何东西,所以我想我找错地方了。这是我的工作人员,我评论了我试图在两个 RMatrix 对象之间进行算术运算的地方。

#include <RcppParallel.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <Rmath.h>
#include <RcppArmadillo.h>
using namespace RcppParallel;


struct ClosestMean : public Worker {

  // Input data and means matrix
  const RMatrix<double> input_data;
  const RMatrix<double> means;

  // Output labels
  RVector<int> predicted_labels;

  // constructor
  ClosestMean(const Rcpp::NumericMatrix input_data, const Rcpp::NumericMatrix means, Rcpp::IntegerVector predicted_labels)
    : input_data(input_data), means(means), predicted_labels(predicted_labels) {}

  // function call operator for the specified range (begin/end)
  void operator () (std::size_t begin, std::size_t end){
    for (unsigned int i = begin; i < end; i++){

      // Check for User Interrupts
      Rcpp::checkUserInterrupt();

      // Get the label corresponding to the cluster mean
      // for which the point is closest to
      RMatrix<double>::Row point = input_data.row(i);
      int label_min = -1;
      double dist;
      double min_dist = INFINITY;

      for (unsigned int j = 0; j < means.nrow(); j++){
        RMatrix<double>::Row mean = means.row(j);
        dist = sqrt(Rcpp::sum((mean - point)^2)); // This is where the operation is failing
        if (dist < min_dist){
          min_dist = dist;
          label_min = j;
        }
      }

      predicted_labels[i] = label_min;

    }
  }

};

感谢您的任何建议。

最佳答案

基本上,您不能像使用常规 Rcpp vector 那样减去两个 Row 对象(即,利用所谓的 Rcpp sugar )——只是没有为 RcppParallel 包装器。您必须自己编写迭代。

关于c++ - Rcpp 并行 : RMatrix and RVector arithmetic operations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34862702/

相关文章:

C++ 如何从类 cpp 文件创建 .h

c++ - 为什么我的 C++ 实现比 R 源代码慢?

c++ - 如何在 QPixmap 上应用深色 mask 层?

c++ - 使用尾随返回类型定义的函数的Doxygen行为

c# - 更快或并行地将数据加载到单个 Azure Blob 中

java - 在Java中并行迭代单个列表而不重复

r - 默认 NULL 参数 Rcpp

c++ - Rcpp:将两个 NumericVectors 相乘,同时对其中一个进行子集化

c++ - Friend 模板函数类内定义

sql - Oracle 并行查询等待统计信息不在 tkprof 的报告中