0

我一直在尝试使用参考来提高速度。下面的(不工作的)简单示例可能不会看到任何改进。但是,我认为通过不将数据复制到单独的(成本)函数中,应该为一个不平凡的例子节省一些时间。

至于现在,我在 R 项目中将示例作为三个 c++ 文件:

标题

#ifndef ExampleInternal_H
#define ExampleInternal_H

namespace ExampleInternal{

#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include <Rcpp.h>

void myfuncA(arma::rowvec &vec_in, arma::colvec& data){

  vec_in.at(1) = vec_in.at(0)*arma::accu(data);

}

struct PARALLEL_WORKER : RcppParallel::Worker{

  arma::mat &input_output;
  const arma::colvec &data_in;

  PARALLEL_WORKER(arma::mat &input_output, const arma::colvec &data_in);
  void operator()(std::size_t begin, std::size_t end);
};

}

#endif

功能

#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include "ExampleInternal.h"
using namespace ExampleInternal;

// [[Rcpp::export]]
arma::mat Parallelfunc(int Len_in, const arma::colvec data_in){

  arma::mat input(Len_in, 2, arma::fill::zeros);
  for(unsigned int i = 0; i < Len_in; i ++){
    input.at(i, 0) =i;
    }

  ExampleInternal::PARALLEL_WORKER worker(input, data_in);
  parallelFor(0, Len_in, worker);
  return input;
}

并行工作者

#include <RcppArmadillo.h>
#include <RcppParallel.h>
#include "ExampleInternal.h"

using namespace RcppParallel;
using namespace ExampleInternal;

namespace ExampleInternal{

PARALLEL_WORKER::PARALLEL_WORKER(arma::mat &input_output, const arma::colvec &data_in) : input_output(input_output), data_in(data_in) {}
  void PARALLEL_WORKER::operator()(std::size_t begin, std::size_t end){

    for(unsigned int k = begin; k < end; k ++){

      ExampleInternal::myfuncA(input_output.row(k), data_in);

    }

  }

}
4

0 回答 0