[R] Multi-threads in R

R. Michael Weylandt michael.weylandt at gmail.com
Mon Jun 18 06:14:06 CEST 2012


I would argue (somewhat emphatically) that the parallel facilities you
are looking at are absolutely not for `for` loops. `for` loops are a
control structure native to imperative programming and, as such, are
inherently stateful. This provides many advantages, but easy
parallelization is absolutely not one of them. Rather consider R as a
functional language and the appropriate "control structure" is the
Map(), most famously of MapReduce, but also inherent in R's *apply()
family of functions. The careful reader will note that here is where
parallelization is exposed. By being state-free, there's no inherent
sequentiality and, consequently, no barriers to parallel computing.

For a somewhat concrete example consider what it would take to do a
state-dependent simulation (like a gibbs sampler) in parallel: it is,
to my mind, quite non-trivial. Compare this to the question of doing
column sums of a matrix in parallel -- effectively trivial. The
parallelization afforded by R is of that second sort.

A red herring in this discussion is the foreach() formalism, but a
moment's thought suggests to me that foreach() is much closer to Map()
than to for().

Best,
Michael

On Sun, Jun 17, 2012 at 1:23 PM, Gary Dong <pdxgary163 at gmail.com> wrote:
> Thanks for all replied.
>
> I read the introduction of R parallel. Is it for loops only?
>
> Gary
>
>
> On Sun, Jun 17, 2012 at 10:04 AM, R. Michael Weylandt
> <michael.weylandt at gmail.com> <michael.weylandt at gmail.com> wrote:
>>
>> Take a look at the parallel package which ships with all current versions
>> of R.
>>
>> Michael
>>
>> On Jun 17, 2012, at 11:39 AM, Gary Dong <pdxgary163 at gmail.com> wrote:
>>
>> > Dear R users,
>> >
>> > I'm wonder if there is a easy way to make R use multi-CPUs on my
>> > computer.
>> > My computer has four CPUs but R uses only one. Thanks.
>> >
>> > Gary
>> >
>> >    [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list