RcppParallel:RMatrix和RVector算术运算

时间:2016-01-18 19:37:21

标签: c++ parallel-processing rcpp

我正在尝试使用RcppArmadillo并行化一个for for循环,但是我遇到了RMatrixRVector可用的算术运算问题。我查看了github上可用的标题file,我在那里看不到任何东西,所以我猜我在找错了地方。这是我的工作人员,我评论了我在两个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;

    }
  }

};

感谢您的任何建议。

1 个答案:

答案 0 :(得分:3)

基本上,你不能像使用常规Rcpp向量那样减去两个Row对象(即利用所谓的Rcpp sugar) - 它只是没有为{{ 1}}包装器。你必须自己编写迭代。