使用C ++和Rcpp中的times参数重现R rep

时间:2015-02-10 21:53:25

标签: r rcpp

我正在学习使用Rcpp。我想使用C ++复制R中的rep函数.Rcpp包含几个与R中rep对应的糖函数。(参见第3页底部:http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-quickref.pdf根据我对文档的理解,糖函数reprep_eachrep_len有两个参数 - 一个向量和一个整数。但是,我想做的是复制当我使用rep参数时,R中times的功能。在这种情况下,你可以提供两个向量.R中的一个简单示例:

x <- c(10, 5, 12)
y <- c(2, 6, 3)

rep(x, times = y)
[1] 10 10  5  5  5  5  5  5 12 12 12

因此rep times参数复制x的每个元素的次数与对应的y值一样多。据我了解,我无法看到任何使用Rcpp糖功能的方法。

我创建了以下适用的C ++函数:

// [[Rcpp::export]]
NumericVector reptest(NumericVector x, NumericVector y) {
    int n = y.size();
    NumericVector myvector(sum(y));
    int ind = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < y(i); ++j) {
            myvector(ind) = x[i];
            ind = ind + 1;
            }
        }
    return myvector;
}

x <- c(10, 5, 12)
y <- c(2, 6, 3)

reptest(x, y)
[1] 10 10  5  5  5  5  5  5 12 12 12

它比R中的rep慢一点。我想知道是否还有加速这个或者是否有人有更好的想法。据我了解,rep正在调用C代码,因此在rep上几乎不可能改进。我的目标是加速MCMC循环(使用rep函数),这需要花费大量时间在R中运行,因此任何加速都会很有用。 MCMC循环的其他部分是慢速部分,而不是rep,但我需要在循环中使用相同的功能。

3 个答案:

答案 0 :(得分:9)

加快速度的一种方法是使用std::fill而不是遍历每个要填充的元素:

#include <algorithm>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector reptest2(NumericVector x, NumericVector y) {
  int n = y.size();
  std::vector<double> myvector(sum(y));
  int ind=0;
  for (int i=0; i < n; ++i) {
    std::fill(myvector.begin()+ind, myvector.begin()+ind+y[i], x[i]);
    ind += y[i];
  }
  return Rcpp::wrap(myvector);
}

在一个更大的例子中,这似乎更接近rep

x <- rep(c(10, 5, 12), 10000)
y <- rep(c(20, 60, 30), 10000)
all.equal(reptest(x, y), reptest2(x, y), rep(x, times=y))
# [1] TRUE

library(microbenchmark)
microbenchmark(reptest(x, y), reptest2(x, y), rep(x, times=y))
# Unit: milliseconds
#               expr      min       lq      mean   median        uq      max neval
#      reptest(x, y) 9.072083 9.297573 11.469345 9.522182 13.015692 20.47905   100
#     reptest2(x, y) 5.097358 5.270827  7.367577 5.436549  8.961004 15.68812   100
#  rep(x, times = y) 1.457933 1.499051  2.884887 1.561408  1.949750 13.21706   100

答案 1 :(得分:9)

这是两个初始版本的快速重复。它还添加了rep.int()

#include <algorithm>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector reptest(NumericVector x, NumericVector y) {
    int n = y.size();
    NumericVector myvector(sum(y));
    int ind = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < y[i]; ++j) {
            myvector[ind] = x[i];
            ind = ind + 1;
            }
        }
    return myvector;
}

// [[Rcpp::export]]
NumericVector reptest2(NumericVector x, NumericVector y) {
    int n = y.size();
    NumericVector myvector(sum(y));
    int ind=0;
    for (int i=0; i < n; ++i) {
        int p = y[i];
        std::fill(myvector.begin()+ind, myvector.begin()+ind+p, x[i]);
        ind += p;
    }
    return myvector;
}


/*** R
x <- rep(c(10, 5, 12), 10000)
y <- rep(c(20, 60, 30), 10000)
all.equal(reptest(x, y), reptest2(x, y), rep(x, times=y))

library(microbenchmark)
microbenchmark(reptest(x, y), reptest2(x, y), rep(x, times=y), rep.int(x, y))
***/

有了这个,我们得到了一点距离,但R仍然获胜:

R> Rcpp::sourceCpp("/tmp/rep.cpp")

R> x <- rep(c(10, 5, 12), 10000)

R> y <- rep(c(20, 60, 30), 10000)

R> all.equal(reptest(x, y), reptest2(x, y), rep(x, times=y))
[1] TRUE

R> library(microbenchmark)

R> microbenchmark(reptest(x, y), reptest2(x, y), rep(x, times=y), rep.int(x, y))
Unit: milliseconds
              expr     min      lq    mean  median      uq       max neval
     reptest(x, y) 4.61604 4.74203 5.47543 4.78120 6.78039   7.01879   100
    reptest2(x, y) 3.14788 3.27507 5.25515 3.33166 5.24583 140.64080   100
 rep(x, times = y) 2.45876 2.56025 3.26857 2.60669 4.60116   6.76278   100
     rep.int(x, y) 2.42390 2.50241 3.38362 2.53987 4.56338   6.44241   100
R> 

答案 2 :(得分:4)

我们可以使用rep实现R base no_init性能:

// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
NumericVector reptest3(const NumericVector& x, const IntegerVector& times) {
    std::size_t n = times.size();
    if (n != 1 && n != x.size())
        stop("Invalid 'times' value");
    std::size_t n_out = std::accumulate(times.begin(), times.end(), 0);
    NumericVector res = no_init(n_out);
    auto begin = res.begin();
    for (std::size_t i = 0, ind = 0; i < n; ind += times[i], ++i) {
        auto start = begin + ind;
        auto end = start + times[i];
        std::fill(start, end, x[i]);
    }
    return res;
}

基准:

library(microbenchmark)
x <- rep(c(10, 5, 12), 10000)
y <- rep(c(20, 60, 30), 10000)
microbenchmark(
    reptest(x, y), reptest2(x, y), reptest3(x, y),
    rep(x, times = y), rep.int(x, y))
#> Unit: milliseconds
#>              expr       min        lq      mean    median        uq       max neval
#>     reptest(x, y) 13.209912 14.014886 15.129395 14.457418 15.123676 56.655527   100
#>    reptest2(x, y)  4.289786  4.653088  5.789094  5.105859  5.782284 46.679824   100
#>    reptest3(x, y)  1.812713  2.810637  3.860590  3.194529  3.809141 44.111422   100
#> rep(x, times = y)  2.510219  2.877324  3.576183  3.461315  3.927312  5.961317   100
#>     rep.int(x, y)  2.496481  2.901303  3.422384  3.318761  3.831794  5.283187   100

我们也可以使用RcppParallel

改进此代码
struct Sum : Worker {
    const RVector<int> input;
    int value;
    Sum(const IntegerVector& input) : input(input), value(0) {}
    Sum(const Sum& sum, Split) : input(sum.input), value(0) {}
    void operator()(std::size_t begin, std::size_t end) {
        value += std::accumulate(input.begin() + begin, input.begin() + end, 0);
    }
    void join(const Sum& rhs) {
        value += rhs.value;
    }
};

struct Fill: Worker {
    const RVector<double> input;
    const RVector<int> times;
    RVector<double> output;
    std::size_t ind;
    Fill(const NumericVector& input, const IntegerVector& times, NumericVector& output)
        : input(input), times(times), output(output), ind(0) {}
    void operator()(std::size_t begin, std::size_t end) {
        for (std::size_t i = begin; i < end; ind += times[i], ++i)
            std::fill(output.begin() + ind, output.begin() + ind + times[i], input[i]);
    }
};

// [[Rcpp::export]]
NumericVector reptest4(const NumericVector& x, const IntegerVector& times) {
    std::size_t n = times.size();
    if (n != 1 && n != x.size())
        stop("Invalid 'times' value");
    Sum s(times);
    parallelReduce(0, n, s);
    NumericVector res = no_init(s.value);
    Fill f(x, times, res);
    parallelFor(0, n, f);
    return res;
}

比较:

library(microbenchmark)
x <- rep(c(10, 5, 12), 10000)
y <- rep(c(20, 60, 30), 10000)
microbenchmark(
    reptest(x, y), reptest2(x, y), reptest3(x, y),
    rep(x, times = y), rep.int(x, y))
#> Unit: milliseconds
#>               expr      min       lq     mean   median       uq       max neval
#>     reptest3(x, y) 2.442446 3.410985 5.143627 3.893345 5.054285 57.871429   100
#>     reptest4(x, y) 1.211256 1.534428 1.979526 1.821398 2.170999  4.073395   100
#>  rep(x, times = y) 2.435122 3.173904 4.447954 3.795285 4.687695 54.000920   100
#>      rep.int(x, y) 2.444310 3.208522 4.026722 3.913618 4.798793  6.690333   100