Skip to content

Commit a50c8d8

Browse files
authored
Merge pull request #1173 from RcppCore/bugfix/remove_rngscope_from_examples
update old examples with respect to RNGScope (closes #1172)
2 parents 423a2ed + 1b8193e commit a50c8d8

File tree

4 files changed

+112
-100
lines changed

4 files changed

+112
-100
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2021-09-05 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/examples/Misc/piSugar.cpp (piSugar): Remove spurious call to
4+
RNGScope we do not need with Rcpp Attributes
5+
* inst/examples/RcppGibbs/RcppGibbs.R (Rgibbs): Comment on two uses
6+
of RNGScope as historical in pre-Attributes code
7+
* inst/examples/RcppGibbs/timeRNGs.R: Idem for one more
8+
19
2021-08-05 Dirk Eddelbuettel <[email protected]>
210

311
* inst/bib/Rcpp.bib: Use https for CRAN URLs

inst/examples/Misc/piSugar.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ using namespace Rcpp;
55

66
// [[Rcpp::export]]
77
double piSugar(const int N) {
8-
RNGScope scope; // ensure RNG gets set/reset
9-
NumericVector x = runif(N);
10-
NumericVector y = runif(N);
11-
NumericVector d = sqrt(x*x + y*y);
12-
return 4.0 * sum(d < 1.0) / N;
8+
NumericVector x = runif(N);
9+
NumericVector y = runif(N);
10+
NumericVector d = sqrt(x*x + y*y);
11+
return 4.0 * sum(d < 1.0) / N;
1312
}

inst/examples/RcppGibbs/RcppGibbs.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ gibbscode <- '
100100
int i,j;
101101
NumericMatrix mat(N, 2);
102102
103-
RNGScope scope; // Initialize Random number generator
103+
RNGScope scope; // Initialize Random number generator. Not needed when Attributes used.
104104
105105
// The rest of the code follows the R version
106106
double x=0, y=0;
@@ -174,10 +174,10 @@ NumericMatrix RcppGibbs(int N, int thn){
174174
175175
// Setup storage matrix
176176
NumericMatrix mat(N, 2);
177-
177+
178178
// The rest of the code follows the R version
179179
double x = 0, y = 0;
180-
180+
181181
for (int i = 0; i < N; i++) {
182182
for (int j = 0; j < thn; j++) {
183183
x = R::rgamma(3.0,1.0/(y*y+4));
@@ -186,14 +186,14 @@ NumericMatrix RcppGibbs(int N, int thn){
186186
mat(i,0) = x;
187187
mat(i,1) = y;
188188
}
189-
189+
190190
return mat; // Return to R
191191
}')
192192

193193

194194
## Use of the sourceCpp() is preferred for users who wish to source external
195195
## files or specify their headers and Rcpp attributes within their code.
196-
## Code here is able to easily be extracted and placed into its own C++ file.
196+
## Code here is able to easily be extracted and placed into its own C++ file.
197197

198198
## Compile and Load
199199
sourceCpp(code="
@@ -219,7 +219,7 @@ NumericMatrix GSLGibbs(int N, int thin){
219219
mat(i,1) = y;
220220
}
221221
gsl_rng_free(r);
222-
222+
223223
return mat; // Return to R
224224
}")
225225

@@ -289,5 +289,3 @@ print(res)
289289

290290

291291
## And we are done
292-
293-

inst/examples/RcppGibbs/timeRNGs.R

Lines changed: 94 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,111 +3,36 @@ suppressMessages(library(Rcpp))
33
suppressMessages(library(inline))
44
suppressMessages(library(rbenchmark))
55

6-
## NOTE: This is the old way to compile Rcpp code inline.
7-
## The code here has left as a historical artifact and tribute to the old way.
8-
## Please use the code under the "new" inline compilation section.
9-
10-
rcppGamma_old <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
11-
NumericVector x(xs);
12-
int n = x.size();
13-
14-
// Initialize Random number generator
15-
RNGScope scope;
16-
17-
const double y = 1.234;
18-
for (int i=0; i<n; i++) {
19-
x[i] = ::Rf_rgamma(3.0, 1.0/(y*y+4));
20-
}
21-
22-
// Return to R
23-
return x;
24-
')
25-
26-
27-
gslGamma_old <- cxxfunction(signature(xs="numeric"), plugin="RcppGSL",
28-
include='#include <gsl/gsl_rng.h>
29-
#include <gsl/gsl_randist.h>',
30-
body='
31-
NumericVector x(xs);
32-
int n = x.size();
33-
34-
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
35-
const double y = 1.234;
36-
for (int i=0; i<n; i++) {
37-
x[i] = gsl_ran_gamma(r,3.0,1.0/(y*y+4));
38-
}
39-
gsl_rng_free(r);
40-
41-
// Return to R
42-
return x;
43-
')
44-
45-
46-
rcppNormal_old <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
47-
NumericVector x(xs);
48-
int n = x.size();
49-
50-
// Initialize Random number generator
51-
RNGScope scope;
52-
53-
const double y = 1.234;
54-
for (int i=0; i<n; i++) {
55-
x[i] = ::Rf_rnorm(1.0/(y+1),1.0/sqrt(2*y+2));
56-
}
57-
58-
// Return to R
59-
return x;
60-
')
61-
62-
63-
gslNormal_old <- cxxfunction(signature(xs="numeric"), plugin="RcppGSL",
64-
include='#include <gsl/gsl_rng.h>
65-
#include <gsl/gsl_randist.h>',
66-
body='
67-
NumericVector x(xs);
68-
int n = x.size();
69-
70-
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
71-
const double y = 1.234;
72-
for (int i=0; i<n; i++) {
73-
x[i] = 1.0/(y+1)+gsl_ran_gaussian(r,1.0/sqrt(2*y+2));
74-
}
75-
gsl_rng_free(r);
76-
77-
// Return to R
78-
return x;
79-
')
80-
81-
826
## NOTE: Within this section, the new way to compile Rcpp code inline has been
83-
## written. Please use the code next as a template for your own project.
7+
## written. Please use the code next as a template for your own project, and
8+
## do NOT use the old code below
849

8510
cppFunction('
8611
NumericVector rcppGamma(NumericVector x){
8712
int n = x.size();
88-
13+
8914
const double y = 1.234;
9015
for (int i=0; i<n; i++) {
9116
x[i] = R::rgamma(3.0, 1.0/(y*y+4));
9217
}
93-
18+
9419
// Return to R
9520
return x;
9621
}')
9722

98-
## This approach is a bit sloppy. Generally, you will want to use
23+
## This approach is a bit sloppy. Generally, you will want to use
9924
## sourceCpp() if there are additional includes that are required.
10025
cppFunction('
10126
NumericVector gslGamma(NumericVector x){
10227
int n = x.size();
103-
28+
10429
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
10530
const double y = 1.234;
10631
for (int i=0; i<n; i++) {
10732
x[i] = gsl_ran_gamma(r,3.0,1.0/(y*y+4));
10833
}
10934
gsl_rng_free(r);
110-
35+
11136
// Return to R
11237
return x;
11338
}', includes = '#include <gsl/gsl_rng.h>
@@ -118,18 +43,18 @@ NumericVector gslGamma(NumericVector x){
11843
cppFunction('
11944
NumericVector rcppNormal(NumericVector x){
12045
int n = x.size();
121-
46+
12247
const double y = 1.234;
12348
for (int i=0; i<n; i++) {
12449
x[i] = R::rnorm(1.0/(y+1),1.0/sqrt(2*y+2));
12550
}
126-
51+
12752
// Return to R
12853
return x;
12954
}')
13055

13156

132-
## Here we demonstrate the use of sourceCpp() to show the continuity
57+
## Here we demonstrate the use of sourceCpp() to show the continuity
13358
## of the code artifact.
13459

13560
sourceCpp(code = '
@@ -144,14 +69,14 @@ using namespace Rcpp;
14469
// [[Rcpp::export]]
14570
NumericVector gslNormal(NumericVector x){
14671
int n = x.size();
147-
72+
14873
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
14974
const double y = 1.234;
15075
for (int i=0; i<n; i++) {
15176
x[i] = 1.0/(y+1)+gsl_ran_gaussian(r,1.0/sqrt(2*y+2));
15277
}
15378
gsl_rng_free(r);
154-
79+
15580
// Return to R
15681
return x;
15782
}')
@@ -167,3 +92,85 @@ res <- benchmark(rcppGamma(x),
16792
print(res)
16893

16994

95+
96+
97+
98+
##
99+
##
100+
## Old code below. Do not use in new projects, it is here solely for comparison
101+
##
102+
##
103+
104+
105+
## NOTE: This is the old way to compile Rcpp code inline.
106+
## The code here has left as a historical artifact and tribute to the old way.
107+
## Please use the code under the "new" inline compilation section.
108+
109+
rcppGamma_old <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
110+
NumericVector x(xs);
111+
int n = x.size();
112+
113+
RNGScope scope; // Initialize Random number generator. Not needed when Attributes used.
114+
115+
const double y = 1.234;
116+
for (int i=0; i<n; i++) {
117+
x[i] = ::Rf_rgamma(3.0, 1.0/(y*y+4));
118+
}
119+
120+
// Return to R
121+
return x;
122+
')
123+
124+
125+
gslGamma_old <- cxxfunction(signature(xs="numeric"), plugin="RcppGSL",
126+
include='#include <gsl/gsl_rng.h>
127+
#include <gsl/gsl_randist.h>',
128+
body='
129+
NumericVector x(xs);
130+
int n = x.size();
131+
132+
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
133+
const double y = 1.234;
134+
for (int i=0; i<n; i++) {
135+
x[i] = gsl_ran_gamma(r,3.0,1.0/(y*y+4));
136+
}
137+
gsl_rng_free(r);
138+
139+
// Return to R
140+
return x;
141+
')
142+
143+
144+
rcppNormal_old <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
145+
NumericVector x(xs);
146+
int n = x.size();
147+
148+
RNGScope scope; // Initialize Random number generator. Not needed when Attributes used.
149+
150+
const double y = 1.234;
151+
for (int i=0; i<n; i++) {
152+
x[i] = ::Rf_rnorm(1.0/(y+1),1.0/sqrt(2*y+2));
153+
}
154+
155+
// Return to R
156+
return x;
157+
')
158+
159+
160+
gslNormal_old <- cxxfunction(signature(xs="numeric"), plugin="RcppGSL",
161+
include='#include <gsl/gsl_rng.h>
162+
#include <gsl/gsl_randist.h>',
163+
body='
164+
NumericVector x(xs);
165+
int n = x.size();
166+
167+
gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
168+
const double y = 1.234;
169+
for (int i=0; i<n; i++) {
170+
x[i] = 1.0/(y+1)+gsl_ran_gaussian(r,1.0/sqrt(2*y+2));
171+
}
172+
gsl_rng_free(r);
173+
174+
// Return to R
175+
return x;
176+
')

0 commit comments

Comments
 (0)