Skip to content

Commit 05ec750

Browse files
committed
regenerate articles with RVector
1 parent 61cfcd7 commit 05ec750

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

_posts/2014-06-29-parallel-vector-sum.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,18 @@ using namespace RcppParallel;
6969
struct Sum : public Worker
7070
{
7171
// source vector
72-
const double * input;
72+
const RVector<double> input;
7373

7474
// accumulated value
7575
double value;
7676

7777
// constructors
78-
Sum(const double* input) : input(input), value(0) {}
78+
Sum(const NumericVector input) : input(input), value(0) {}
7979
Sum(const Sum& sum, Split) : input(sum.input), value(0) {}
8080

8181
// accumulate just the element of the range I've been asked to
8282
void operator()(std::size_t begin, std::size_t end) {
83-
value += std::accumulate(input + begin, input + end, 0.0);
83+
value += std::accumulate(input.begin() + begin, input.begin() + end, 0.0);
8484
}
8585

8686
// join my value with that of another Sum
@@ -93,20 +93,22 @@ struct Sum : public Worker
9393
Note that `Sum` derives from the `RcppParallel::Worker` class. This is
9494
required for function objects passed to `parallelReduce`.
9595

96-
Note also that we use a raw `double *` for accessing the vector. This is
97-
because this code will execute on a background thread where it's not safe to
98-
call R or Rcpp APIs.
96+
Note also that we use the `RVector<double>` type for accessing the vector.
97+
This is because this code will execute on a background thread where it's not
98+
safe to call R or Rcpp APIs. The `RVector` class is included in the
99+
RcppParallel package and provides a lightweight, thread-safe wrapper around R
100+
vectors.
99101

100102
Now that we've defined the functor, implementing the parallel sum
101103
function is straightforward. Just initialize an instance of `Sum`
102-
with a pointer to the input data and call `parallelReduce`:
104+
with the input vector and call `parallelReduce`:
103105

104106
{% highlight cpp %}
105107
// [[Rcpp::export]]
106108
double parallelVectorSum(NumericVector x) {
107109

108-
// declare the SumBody instance that takes a pointer to the vector data
109-
Sum sum(x.begin());
110+
// declare the SumBody instance
111+
Sum sum(x);
110112

111113
// call parallel_reduce to start the work
112114
parallelReduce(0, x.length(), sum);

_posts/2014-07-15-parallel-inner-product.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,24 @@ using namespace RcppParallel;
4949
struct InnerProduct : public Worker
5050
{
5151
// source vectors
52-
const double* x;
53-
const double* y;
52+
const RVector<double> x;
53+
const RVector<double> y;
5454

5555
// product that I have accumulated
5656
double product;
5757

5858
// constructors
59-
InnerProduct(const double* x, const double* y) : x(x), y(y), product(0) {}
59+
InnerProduct(const NumericVector x, const NumericVector y)
60+
: x(x), y(y), product(0) {}
6061
InnerProduct(const InnerProduct& innerProduct, Split)
6162
: x(innerProduct.x), y(innerProduct.y), product(0) {}
6263

6364
// process just the elements of the range I've been asked to
6465
void operator()(std::size_t begin, std::size_t end) {
65-
product += std::inner_product(x + begin, x + end, y + begin, 0.0);
66+
product += std::inner_product(x.begin() + begin,
67+
x.begin() + end,
68+
y.begin() + begin,
69+
0.0);
6670
}
6771

6872
// join my value with that of another InnerProduct
@@ -75,20 +79,22 @@ struct InnerProduct : public Worker
7579
Note that `InnerProduct` derives from the `RcppParallel::Worker` class. This
7680
is required for function objects passed to `parallelReduce`.
7781

78-
Note also that we use use raw `double *` for accessing the vectors. This is
79-
because this code will execute on a background thread where it's not safe to
80-
call R or Rcpp APIs.
82+
Note also that we use the `RVector<double>` type for accessing the vector.
83+
This is because this code will execute on a background thread where it's not
84+
safe to call R or Rcpp APIs. The `RVector` class is included in the
85+
RcppParallel package and provides a lightweight, thread-safe wrapper around R
86+
vectors.
8187

8288
Now that we've defined the function object, implementing the parallel inner
8389
product function is straightforward. Just initialize an instance of
84-
`InnerProduct` with pointers to the input data and call `parallelReduce`:
90+
`InnerProduct` with the input vectors and call `parallelReduce`:
8591

8692
{% highlight cpp %}
8793
// [[Rcpp::export]]
8894
double parallelInnerProduct(NumericVector x, NumericVector y) {
8995

9096
// declare the InnerProduct instance that takes a pointer to the vector data
91-
InnerProduct innerProduct(x.begin(), y.begin());
97+
InnerProduct innerProduct(x, y);
9298

9399
// call paralleReduce to start the work
94100
parallelReduce(0, x.length(), innerProduct);

0 commit comments

Comments
 (0)