<div dir="ltr">It very well may be thread-safe, but my thinking is that it could be dangerous to use a class where some methods are thread-safe and some are not (and there isn't necessarily documentation or a formal contract on what's safe and what's not). Your current use is likely fine, I'm just being cautious.<div>
<br></div><div>J.J.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jul 17, 2014 at 4:40 PM, Alessandro Mammana <span dir="ltr"><<a href="mailto:mammana@molgen.mpg.de" target="_blank">mammana@molgen.mpg.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sorry,  I read this post just by chance,<br>
did I understand correctly, the class Rcpp::Matrix are not thread safe?<br>
Is there any problem in writing code such as:<br>
<br>
mat(i,j) = something<br>
<br>
in a multithreaded environment (such as inside a "#pragma omp parallel<br>
for" loop)?<br>
My scripts are full of such loops. I have assumed that it was the same as doing:<br>
<br>
double* M = mat.begin();<br>
M[i + ncol*j] = something;<br>
<br>
This at least should be thread safe, right?<br>
<br>
Thx a lot!<br>
Ale<br>
<br>
<br>
<br>
On Tue, Jul 15, 2014 at 12:00 PM,<br>
<<a href="mailto:rcpp-devel-request@lists.r-forge.r-project.org">rcpp-devel-request@lists.r-forge.r-project.org</a>> wrote:<br>
> Send Rcpp-devel mailing list submissions to<br>
>         <a href="mailto:rcpp-devel@lists.r-forge.r-project.org">rcpp-devel@lists.r-forge.r-project.org</a><br>
><br>
> To subscribe or unsubscribe via the World Wide Web, visit<br>
>         <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
><br>
> or, via email, send a message with subject or body 'help' to<br>
>         <a href="mailto:rcpp-devel-request@lists.r-forge.r-project.org">rcpp-devel-request@lists.r-forge.r-project.org</a><br>
><br>
> You can reach the person managing the list at<br>
>         <a href="mailto:rcpp-devel-owner@lists.r-forge.r-project.org">rcpp-devel-owner@lists.r-forge.r-project.org</a><br>
><br>
> When replying, please edit your Subject line so it is more specific<br>
> than "Re: Contents of Rcpp-devel digest..."<br>
><br>
><br>
> Today's Topics:<br>
><br>
>    1. Re: parallel distance matrix calculation (JJ Allaire)<br>
>    2. Re: parallel distance matrix calculation (JJ Allaire)<br>
><br>
><br>
> ----------------------------------------------------------------------<br>
><br>
> Message: 1<br>
> Date: Mon, 14 Jul 2014 10:45:06 -0400<br>
> From: JJ Allaire <<a href="mailto:jj.allaire@gmail.com">jj.allaire@gmail.com</a>><br>
> To: Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>><br>
> Cc: "<a href="mailto:rcpp-devel@lists.r-forge.r-project.org">rcpp-devel@lists.r-forge.r-project.org</a>"<br>
>         <<a href="mailto:rcpp-devel@lists.r-forge.r-project.org">rcpp-devel@lists.r-forge.r-project.org</a>><br>
> Subject: Re: [Rcpp-devel] parallel distance matrix calculation<br>
> Message-ID:<br>
>         <CAKw=oCxkxeHkM4Q=aFMk+9SrQaM1e-Hao1Mh=<a href="mailto:qgKzn22HCOB5g@mail.gmail.com">qgKzn22HCOB5g@mail.gmail.com</a>><br>
> Content-Type: text/plain; charset="utf-8"<br>
><br>
> Here's a parallel version:<br>
><br>
> <a href="https://github.com/jjallaire/RcppParallel/blob/master/inst/examples/parallel-distance-matrix.cpp" target="_blank">https://github.com/jjallaire/RcppParallel/blob/master/inst/examples/parallel-distance-matrix.cpp</a><br>

><br>
> To make the code reasonable I introduced a new RMatrix<T> class in<br>
> RcppParallel that makes offsetting into rows and columns safe and<br>
> straightforward. This class has no connection on the R or Rcpp APIs so is<br>
> guaranteed to be thread-safe.<br>
><br>
> On a 4 core machine (8 with hyperthreading) I'm observing a 10x speedup.<br>
> The parallel related speedup is 4x. There is an additional 2.5x speedup<br>
> which appears to be related to the lower level access to the Matrix memory<br>
> done by RMatrix<T> (and perhaps some elimination of copying).<br>
><br>
><br>
><br>
><br>
> On Sun, Jul 13, 2014 at 7:27 AM, Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br>
><br>
>><br>
>> On 12 July 2014 at 12:37, JJ Allaire wrote:<br>
>> | If you could send the full source code to your example (including<br>
>> js_distance<br>
>> | and whatever R code you are using to test/exercise the functions) I'll<br>
>> see if I<br>
>> | can come up with the code you'd use to parallelize the outer loop.<br>
>> Depending on<br>
>> | how it turns out perhaps we can even convert this into another gallery<br>
>> article!<br>
>><br>
>> That's the spirit!  [ I had another quick look at the inner-product example<br>
>> which is short and nice, but there is just too little of a pickup there...<br>
>> ]<br>
>><br>
>> Dirk<br>
>><br>
>> --<br>
>> <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
>><br>
> -------------- next part --------------<br>
> An HTML attachment was scrubbed...<br>
> URL: <<a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140714/a4fcf62a/attachment-0001.html" target="_blank">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140714/a4fcf62a/attachment-0001.html</a>><br>

><br>
> ------------------------------<br>
><br>
> Message: 2<br>
> Date: Mon, 14 Jul 2014 12:48:25 -0400<br>
> From: JJ Allaire <<a href="mailto:jj.allaire@gmail.com">jj.allaire@gmail.com</a>><br>
> To: Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>><br>
> Cc: "<a href="mailto:rcpp-devel@lists.r-forge.r-project.org">rcpp-devel@lists.r-forge.r-project.org</a>"<br>
>         <<a href="mailto:rcpp-devel@lists.r-forge.r-project.org">rcpp-devel@lists.r-forge.r-project.org</a>><br>
> Subject: Re: [Rcpp-devel] parallel distance matrix calculation<br>
> Message-ID:<br>
>         <CAKw=<a href="mailto:oCyCdv98hTUHRUf%2BXLbKKFUMp3L64hJX4Ku6uoDjjarvSw@mail.gmail.com">oCyCdv98hTUHRUf+XLbKKFUMp3L64hJX4Ku6uoDjjarvSw@mail.gmail.com</a>><br>
> Content-Type: text/plain; charset="utf-8"<br>
><br>
>> On a 4 core machine (8 with hyperthreading) I'm observing a 10x speedup.<br>
>> The parallel related speedup is 4x. There is an additional 2.5x speedup<br>
>> which appears to be related to the lower level access to the Matrix memory<br>
>> done by RMatrix<T> (and perhaps some elimination of copying).<br>
>><br>
><br>
> It turns out that the additional slowdown in the serial version was due to<br>
> repeatedly calling Vector::length as a loop termination condition. I<br>
> re-wrote the serial version using iterators and now the speedup from<br>
> parallel is about 5x (more in line with expectations).<br>
> -------------- next part --------------<br>
> An HTML attachment was scrubbed...<br>
> URL: <<a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140714/39077e47/attachment-0001.html" target="_blank">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140714/39077e47/attachment-0001.html</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" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
><br>
> End of Rcpp-devel Digest, Vol 57, Issue 18<br>
> ******************************************<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Alessandro Mammana, PhD Student<br>
Max Planck Institute for Molecular Genetics<br>
Ihnestraße 63-73<br>
D-14195 Berlin, Germany<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" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></font></span></blockquote></div><br></div>