<div dir="ltr">I have tried this with no luck.</div><div class="gmail_extra"><br><div class="gmail_quote">2015-07-14 14:34 GMT+03:00 Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Or just use a std::vector<double> for sg. That should do it.<br>
<span class="im HOEnZb"><br>
Romain<br>
<br>
Envoyé de mon iPhone<br>
<br>
</span><div class="HOEnZb"><div class="h5">> Le 14 juil. 2015 à 13:21, JJ Allaire <<a href="mailto:jj.allaire@gmail.com">jj.allaire@gmail.com</a>> a écrit :<br>
><br>
> The examples in the RcppParallel documentation assume that access to<br>
> vectors and matrixes are *aligned* (i.e. fall into neat buckets<br>
> whereby reading and writing doesn't overlap between worker instances).<br>
> Your example appears to access arbitrary elements of sg (depending on<br>
> what's passed in gi) which probably creates overlapping reads/writes.<br>
> You should also study the documentation for join carefully.<br>
><br>
> There's nothing incorrect about RcppParallel's behavior here, rather<br>
> you need to think more carefully about the access patterns of your<br>
> data and how they might conflict. You may need to introduce locking to<br>
> overcome the conflicts, which in turn could kill the performance<br>
> benefit you gain from parallelism. No easy answers here :-\<br>
><br>
>> On Tue, Jul 14, 2015 at 7:15 AM, Danas Zuokas <<a href="mailto:danas.zuokas@gmail.com">danas.zuokas@gmail.com</a>> wrote:<br>
>> Yes it is the same question on SO and I did consider RHertel's comments.<br>
>> But this problem (sums by group id) is not parallelFor it is parallelReduce:<br>
>> I split vector, calculate sums and then aggregate those sums.<br>
>> Please correct me if I am wrong.<br>
>><br>
>> 2015-07-14 13:54 GMT+03:00 Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>>:<br>
>>><br>
>>><br>
>>> On 14 July 2015 at 09:25, Danas Zuokas wrote:<br>
>>> | I have written parallel implementation of sums in groups using<br>
>>> RcppParallel.<br>
>>><br>
>>> Isn't this the same question as<br>
>>><br>
>>> <a href="http://stackoverflow.com/questions/31318419/when-calling-same-rcpp-function-several-times-different-results-are-returned" rel="noreferrer" target="_blank">http://stackoverflow.com/questions/31318419/when-calling-same-rcpp-function-several-times-different-results-are-returned</a><br>
>>><br>
>>> You got some excellent comments there by SO user 'RHertel'. Did you<br>
>>> consider those?<br>
>>><br>
>>> Dirk<br>
>>><br>
>>> | // [[Rcpp::depends(RcppParallel)]]<br>
>>> | #include <Rcpp.h><br>
>>> | #include <RcppParallel.h><br>
>>> | using namespace Rcpp;<br>
>>> | using namespace RcppParallel;<br>
>>> |<br>
>>> | struct SumsG: public Worker<br>
>>> | {<br>
>>> |   const RVector<double> v;<br>
>>> |   const RVector<int> gi;<br>
>>> |<br>
>>> |   RVector<double> sg;<br>
>>> |<br>
>>> |   SumsG(const NumericVector v, const IntegerVector gi, NumericVector<br>
>>> sg): v(v), gi(gi), sg(sg) {}<br>
>>> |   SumsG(const SumsG& p, Split): v(p.v), gi(<a href="http://p.gi" rel="noreferrer" target="_blank">p.gi</a>), sg(<a href="http://p.sg" rel="noreferrer" target="_blank">p.sg</a>) {}<br>
>>> |<br>
>>> |   void operator()(std::size_t begin, std::size_t end) {<br>
>>> |     for (std::size_t i = begin; i < end; i++) {<br>
>>> |       sg[gi[i]] += v[i];<br>
>>> |     }<br>
>>> |   }<br>
>>> |<br>
>>> |   void join(const SumsG& p) {<br>
>>> |     for(std::size_t i = 0; i < sg.length(); i++) {<br>
>>> |       sg[i] += <a href="http://p.sg" rel="noreferrer" target="_blank">p.sg</a>[i];<br>
>>> |     }<br>
>>> |   }<br>
>>> | };<br>
>>> |<br>
>>> | // [[Rcpp::export]]<br>
>>> | List sumsingroups(NumericVector v, IntegerVector gi, int ni) {<br>
>>> |   NumericVector sg(ni);<br>
>>> |   SumsG p(v, gi, sg);<br>
>>> |   parallelReduce(0, v.length(), p);<br>
>>> |<br>
>>> |   return List::create(_["sg"] = <a href="http://p.sg" rel="noreferrer" target="_blank">p.sg</a>);<br>
>>> | }<br>
>>> |<br>
>>> | It compiles using Rcpp::sourceCpp. Now when I call it from R<br>
>>> sumsingroups(1:10,<br>
>>> | rep(0:1, each = 5), 2) several times I get the right answer (15 40) and<br>
>>> then<br>
>>> | something different (usually some multiplicative of the right answer).<br>
>>> Running<br>
>>> |<br>
>>> |<br>
>>> | res <- sumsingroups(1:10, rep(0:1, each = 5), 2)<br>
>>> | for(i in 1:1000) {<br>
>>> |     tmp <- sumsingroups(1:10, rep(0:1, each = 5), 2)<br>
>>> |     if(res[[1]][1] != tmp[[1]][1]) break<br>
>>> |     Sys.sleep(0.1)<br>
>>> | }<br>
>>> |<br>
>>> | breaks at random iteration returning<br>
>>> |<br>
>>> | $sg<br>
>>> | [1]  60 160<br>
>>> |<br>
>>> | or<br>
>>> |<br>
>>> | $sg<br>
>>> | [1] 30 80<br>
>>> |<br>
>>> | I am new to Rcpp and RcppParallel and do not know what could cause such<br>
>>> | behavior.<br>
>>> |<br>
>>> | Things that did not help:<br>
>>> |<br>
>>> |  1. Added for (std::size_t i = 0; i < sg.length(); i++) sg[i] = 0; to<br>
>>> both of<br>
>>> |     constructors.<br>
>>> |  2. Changed names so that they are different in Worker definition and in<br>
>>> |     function implementation.<br>
>>> |<br>
>>> | _______________________________________________<br>
>>> | Rcpp-devel mailing list<br>
>>> | <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
>>> | <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
>>><br>
>>> --<br>
>>> <a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
>><br>
>><br>
>><br>
>> _______________________________________________<br>
>> Rcpp-devel mailing list<br>
>> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
>> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
> _______________________________________________<br>
> Rcpp-devel mailing list<br>
> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</div></div></blockquote></div><br></div>