|
| 1 | +#include <Rcpp.h> |
| 2 | +using namespace Rcpp; |
| 3 | + |
| 4 | +// [[Rcpp::depends(RcppParallel)]] |
| 5 | +#include <RcppParallel.h> |
| 6 | +using namespace RcppParallel; |
| 7 | + |
| 8 | +struct Sum : public Worker |
| 9 | +{ |
| 10 | + // source vector |
| 11 | + const RVector<int> input; |
| 12 | + |
| 13 | + // accumulated value |
| 14 | + int value; |
| 15 | + |
| 16 | + // constructors |
| 17 | + Sum(const IntegerVector input) : input(input), value(0) {} |
| 18 | + Sum(const Sum& sum, Split) : input(sum.input), value(0) {} |
| 19 | + |
| 20 | + // accumulate just the element of the range I've been asked to |
| 21 | + void operator()(std::size_t begin, std::size_t end) { |
| 22 | + value += std::accumulate(input.begin() + begin, input.begin() + end, 0); |
| 23 | + } |
| 24 | + |
| 25 | + // join my value with that of another Sum |
| 26 | + void join(const Sum& rhs) { |
| 27 | + value += rhs.value; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +// [[Rcpp::export]] |
| 32 | +double parallelVectorSum(IntegerVector x) { |
| 33 | + |
| 34 | + // declare the SumBody instance |
| 35 | + Sum sum(x); |
| 36 | + |
| 37 | + // call parallel_reduce to start the work |
| 38 | + parallelReduce(0, x.length(), sum); |
| 39 | + |
| 40 | + // return the computed sum |
| 41 | + return sum.value; |
| 42 | +} |
| 43 | + |
| 44 | +// [[Rcpp::export]] |
| 45 | +void testMalloc() { |
| 46 | + |
| 47 | +#if RCPP_PARALLEL_USE_TBB |
| 48 | + |
| 49 | + std::vector<int,tbb::tbb_allocator<int> > vec; |
| 50 | + vec.push_back(42); |
| 51 | + |
| 52 | + std::vector<int,tbb::scalable_allocator<int> > scalableVec; |
| 53 | + scalableVec.push_back(42); |
| 54 | + |
| 55 | + std::vector<int,tbb::cache_aligned_allocator<int> > cacheAlignedVec; |
| 56 | + cacheAlignedVec.push_back(42); |
| 57 | + |
| 58 | +#endif |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | + |
0 commit comments