Discussion:
[Rcpp-devel] starter
f***@mailbox.org
2017-05-31 10:22:33 UTC
Permalink
Hi all,

I am very new to Rcpp and I wrote a function which I now want to parallelize.
The function is working fine, and is much faster than in R, however, it really is slow for bigger datasets.

Since the function is running I don’t need to explain what it does, however, it is has
3 nested for loops. It loops through columns and within each column if loops through the rows and in a third loop produces pair comparisons


So the parallelisation should parallelize the column loop.

I found the RcppParallel package and for the beginning wanted to run on of the example to understand the
workflow first. However, I already have issues running the code below:

following is sourced with: Rcpp::sourceCpp("src/par_example.cpp")
However, I get the error:

par_example.cpp:6:10: fatal error: 'RcppParallel.h' file not found
#include <RcppParallel.h>
^
1 error generated.
make: *** [par_example.o] Error 1

I would much appreciate if someone could give me a start with this!

Cheers,
Franz



#include <Rcpp.h>

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

// [[Rcpp::export]]
NumericMatrix matrixSqrt(NumericMatrix orig) {
// allocate the matrix we will return
NumericMatrix mat(orig.nrow(), orig.ncol());
// transform it
std::transform(orig.begin(), orig.end(), mat.begin(), ::sqrt);
// return the new matrix
return mat;
}


using namespace RcppParallel;
struct SquareRoot : public Worker {
const RMatrix<double> input;
// source matrix RMatrix<double> output; // destination matrix
// initialize with source and destination
SquareRoot(const NumericMatrix input, NumericMatrix output)
: input(input), output(output) {}
// take the square root of the range of elements requested
void operator()(std::size_t begin, std::size_t end) { std::transform(input.begin() + begin,
input.begin() + end,
output.begin() + begin,
::sqrt);
}
};

// [[Rcpp::export]]
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
// allocate the output matrix
NumericMatrix output(x.nrow(), x.ncol());
// SquareRoot functor (pass input and output matrixes)
SquareRoot squareRoot(x, output);
// call parallelFor to do the work
parallelFor(0, x.length(), squareRoot);
// return the output matrix
return output; }
Romain Francois
2017-05-31 10:26:47 UTC
Permalink
You can add this somewhere on top of your cpp file

// [[Rcpp::depends(RcppParallel)]]

Romain
Post by f***@mailbox.org
Hi all,
I am very new to Rcpp and I wrote a function which I now want to parallelize.
The function is working fine, and is much faster than in R, however, it really is slow for bigger datasets.
Since the function is running I don’t need to explain what it does, however, it is has
3 nested for loops. It loops through columns and within each column if loops through the rows and in a third loop produces pair comparisons

So the parallelisation should parallelize the column loop.
I found the RcppParallel package and for the beginning wanted to run on of the example to understand the
following is sourced with: Rcpp::sourceCpp("src/par_example.cpp")
par_example.cpp:6:10: fatal error: 'RcppParallel.h' file not found
#include <RcppParallel.h>
^
1 error generated.
make: *** [par_example.o] Error 1
I would much appreciate if someone could give me a start with this!
Cheers,
Franz
#include <Rcpp.h>
using namespace Rcpp;
#include <cmath>
#include <algorithm>
#include <RcppParallel.h>
// [[Rcpp::export]]
NumericMatrix matrixSqrt(NumericMatrix orig) {
// allocate the matrix we will return
NumericMatrix mat(orig.nrow(), orig.ncol());
// transform it
std::transform(orig.begin(), orig.end(), mat.begin(), ::sqrt);
// return the new matrix
return mat;
}
using namespace RcppParallel;
struct SquareRoot : public Worker {
const RMatrix<double> input;
// source matrix RMatrix<double> output; // destination matrix
// initialize with source and destination
SquareRoot(const NumericMatrix input, NumericMatrix output)
: input(input), output(output) {}
// take the square root of the range of elements requested
void operator()(std::size_t begin, std::size_t end) { std::transform(input.begin() + begin,
input.begin() + end,
output.begin() + begin,
::sqrt);
}
};
// [[Rcpp::export]]
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
// allocate the output matrix
NumericMatrix output(x.nrow(), x.ncol());
// SquareRoot functor (pass input and output matrixes)
SquareRoot squareRoot(x, output);
// call parallelFor to do the work
parallelFor(0, x.length(), squareRoot);
// return the output matrix
return output; }
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
f***@mailbox.org
2017-05-31 10:42:23 UTC
Permalink
Thanks Romain,

this error is fine now, however, now I run into new

I took the example code from this <http://dirk.eddelbuettel.com/papers/rcpp_rfinance_may2015.pdf> presentation of Dirk
Post by f***@mailbox.org
Rcpp::sourceCpp("src/par_example.cpp")
par_example.cpp:26:21: error: member initializer 'output' does not name a non-static data member or base class
: input(input), output(output) {}
^~~~~~~~~~~~~~
par_example.cpp:31:5: error: use of undeclared identifier 'output'
output.begin() + begin,
^
2 errors generated.
make: *** [par_example.o] Error 1


Hope this is as easily solved as the before error.

Cheers, Franz
Post by f***@mailbox.org
You can add this somewhere on top of your cpp file
// [[Rcpp::depends(RcppParallel)]]
Romain
Post by f***@mailbox.org
Hi all,
I am very new to Rcpp and I wrote a function which I now want to parallelize.
The function is working fine, and is much faster than in R, however, it really is slow for bigger datasets.
Since the function is running I don’t need to explain what it does, however, it is has
3 nested for loops. It loops through columns and within each column if loops through the rows and in a third loop produces pair comparisons

So the parallelisation should parallelize the column loop.
I found the RcppParallel package and for the beginning wanted to run on of the example to understand the
following is sourced with: Rcpp::sourceCpp("src/par_example.cpp")
par_example.cpp:6:10: fatal error: 'RcppParallel.h' file not found
#include <RcppParallel.h>
^
1 error generated.
make: *** [par_example.o] Error 1
I would much appreciate if someone could give me a start with this!
Cheers,
Franz
#include <Rcpp.h>
using namespace Rcpp;
#include <cmath>
#include <algorithm>
#include <RcppParallel.h>
// [[Rcpp::export]]
NumericMatrix matrixSqrt(NumericMatrix orig) {
// allocate the matrix we will return
NumericMatrix mat(orig.nrow(), orig.ncol());
// transform it
std::transform(orig.begin(), orig.end(), mat.begin(), ::sqrt);
// return the new matrix
return mat;
}
using namespace RcppParallel;
struct SquareRoot : public Worker {
const RMatrix<double> input;
// source matrix RMatrix<double> output; // destination matrix
// initialize with source and destination
SquareRoot(const NumericMatrix input, NumericMatrix output)
: input(input), output(output) {}
// take the square root of the range of elements requested
void operator()(std::size_t begin, std::size_t end) { std::transform(input.begin() + begin,
input.begin() + end,
output.begin() + begin,
::sqrt);
}
};
// [[Rcpp::export]]
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
// allocate the output matrix
NumericMatrix output(x.nrow(), x.ncol());
// SquareRoot functor (pass input and output matrixes)
SquareRoot squareRoot(x, output);
// call parallelFor to do the work
parallelFor(0, x.length(), squareRoot);
// return the output matrix
return output; }
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Romain Francois
2017-05-31 10:46:34 UTC
Permalink
This looks like a copy and paste mistake. I can definitely see this line on the slide 57:
RMatrix<double> output;

Anyway, the example is there:
http://gallery.rcpp.org/articles/parallel-matrix-transform/ <http://gallery.rcpp.org/articles/parallel-matrix-transform/>

Romain
Post by f***@mailbox.org
Thanks Romain,
this error is fine now, however, now I run into new

I took the example code from this <http://dirk.eddelbuettel.com/papers/rcpp_rfinance_may2015.pdf> presentation of Dirk
Post by f***@mailbox.org
Rcpp::sourceCpp("src/par_example.cpp")
par_example.cpp:26:21: error: member initializer 'output' does not name a non-static data member or base class
: input(input), output(output) {}
^~~~~~~~~~~~~~
par_example.cpp:31:5: error: use of undeclared identifier 'output'
output.begin() + begin,
^
2 errors generated.
make: *** [par_example.o] Error 1
Hope this is as easily solved as the before error.
Cheers, Franz
Post by f***@mailbox.org
You can add this somewhere on top of your cpp file
// [[Rcpp::depends(RcppParallel)]]
Romain
Post by f***@mailbox.org
Hi all,
I am very new to Rcpp and I wrote a function which I now want to parallelize.
The function is working fine, and is much faster than in R, however, it really is slow for bigger datasets.
Since the function is running I don’t need to explain what it does, however, it is has
3 nested for loops. It loops through columns and within each column if loops through the rows and in a third loop produces pair comparisons

So the parallelisation should parallelize the column loop.
I found the RcppParallel package and for the beginning wanted to run on of the example to understand the
following is sourced with: Rcpp::sourceCpp("src/par_example.cpp")
par_example.cpp:6:10: fatal error: 'RcppParallel.h' file not found
#include <RcppParallel.h>
^
1 error generated.
make: *** [par_example.o] Error 1
I would much appreciate if someone could give me a start with this!
Cheers,
Franz
#include <Rcpp.h>
using namespace Rcpp;
#include <cmath>
#include <algorithm>
#include <RcppParallel.h>
// [[Rcpp::export]]
NumericMatrix matrixSqrt(NumericMatrix orig) {
// allocate the matrix we will return
NumericMatrix mat(orig.nrow(), orig.ncol());
// transform it
std::transform(orig.begin(), orig.end(), mat.begin(), ::sqrt);
// return the new matrix
return mat;
}
using namespace RcppParallel;
struct SquareRoot : public Worker {
const RMatrix<double> input;
// source matrix RMatrix<double> output; // destination matrix
// initialize with source and destination
SquareRoot(const NumericMatrix input, NumericMatrix output)
: input(input), output(output) {}
// take the square root of the range of elements requested
void operator()(std::size_t begin, std::size_t end) { std::transform(input.begin() + begin,
input.begin() + end,
output.begin() + begin,
::sqrt);
}
};
// [[Rcpp::export]]
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
// allocate the output matrix
NumericMatrix output(x.nrow(), x.ncol());
// SquareRoot functor (pass input and output matrixes)
SquareRoot squareRoot(x, output);
// call parallelFor to do the work
parallelFor(0, x.length(), squareRoot);
// return the output matrix
return output; }
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel <https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
f***@mailbox.org
2017-05-31 11:02:57 UTC
Permalink
Darn
 it thought it is a copy error but couldn't find the fault!!
Thanks a lot. Not it is working.
Hope I can now transfer the stuff to my 3-nest-loop..

Cheers,
Franz
Post by f***@mailbox.org
RMatrix<double> output;
http://gallery.rcpp.org/articles/parallel-matrix-transform/ <http://gallery.rcpp.org/articles/parallel-matrix-transform/>
Romain
Post by f***@mailbox.org
Thanks Romain,
this error is fine now, however, now I run into new

I took the example code from this <http://dirk.eddelbuettel.com/papers/rcpp_rfinance_may2015.pdf> presentation of Dirk
Post by f***@mailbox.org
Rcpp::sourceCpp("src/par_example.cpp")
par_example.cpp:26:21: error: member initializer 'output' does not name a non-static data member or base class
: input(input), output(output) {}
^~~~~~~~~~~~~~
par_example.cpp:31:5: error: use of undeclared identifier 'output'
output.begin() + begin,
^
2 errors generated.
make: *** [par_example.o] Error 1
Hope this is as easily solved as the before error.
Cheers, Franz
Post by f***@mailbox.org
You can add this somewhere on top of your cpp file
// [[Rcpp::depends(RcppParallel)]]
Romain
Post by f***@mailbox.org
Hi all,
I am very new to Rcpp and I wrote a function which I now want to parallelize.
The function is working fine, and is much faster than in R, however, it really is slow for bigger datasets.
Since the function is running I don’t need to explain what it does, however, it is has
3 nested for loops. It loops through columns and within each column if loops through the rows and in a third loop produces pair comparisons

So the parallelisation should parallelize the column loop.
I found the RcppParallel package and for the beginning wanted to run on of the example to understand the
following is sourced with: Rcpp::sourceCpp("src/par_example.cpp")
par_example.cpp:6:10: fatal error: 'RcppParallel.h' file not found
#include <RcppParallel.h>
^
1 error generated.
make: *** [par_example.o] Error 1
I would much appreciate if someone could give me a start with this!
Cheers,
Franz
#include <Rcpp.h>
using namespace Rcpp;
#include <cmath>
#include <algorithm>
#include <RcppParallel.h>
// [[Rcpp::export]]
NumericMatrix matrixSqrt(NumericMatrix orig) {
// allocate the matrix we will return
NumericMatrix mat(orig.nrow(), orig.ncol());
// transform it
std::transform(orig.begin(), orig.end(), mat.begin(), ::sqrt);
// return the new matrix
return mat;
}
using namespace RcppParallel;
struct SquareRoot : public Worker {
const RMatrix<double> input;
// source matrix RMatrix<double> output; // destination matrix
// initialize with source and destination
SquareRoot(const NumericMatrix input, NumericMatrix output)
: input(input), output(output) {}
// take the square root of the range of elements requested
void operator()(std::size_t begin, std::size_t end) { std::transform(input.begin() + begin,
input.begin() + end,
output.begin() + begin,
::sqrt);
}
};
// [[Rcpp::export]]
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
// allocate the output matrix
NumericMatrix output(x.nrow(), x.ncol());
// SquareRoot functor (pass input and output matrixes)
SquareRoot squareRoot(x, output);
// call parallelFor to do the work
parallelFor(0, x.length(), squareRoot);
// return the output matrix
return output; }
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel <https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
Continue reading on narkive:
Loading...