r - How to use the doSMP and the foreach packages correctly? -


I am trying to provide a parallel backend for that foreach package.

Can you point out what I do wrong? Actually, the use of foreclosure in such a way increases the computing time ...

  DoSMP Register to experiment with the document ------ Library (doSMP) w & lt; - Startwalkers (4) Register DOSMP (W) # -------------------------------------- - ----------- # ------ A simple task ------ SIM & LT; - Function (A, B) {Return (10 * A + B)} AVAILABLE; - 1: 200 Beverages & lt; - 1: 400 # ----------------------------- # ------ Innocent Method ------ Pityime & Lt; - System. Time (for 1 mat: (matrix (NA, NAV = length), NOCL = length (BVC)) (for i: 1: length) (for 1: length (bcc) } {Mat [i, j] & lt; - sim (evek [ii], bvc [j])}}}) [3] fell away 0.36 # ----- --------- -------------- # ------ Foreign Use ------ Pityime & lt; - System.time ({matte 2 <% Foreach (b = bvec, .combine = "cbind")%:% foreach (a = aviation, .combine = "c")% dopar% {sim (a, b) }}}) [3] ptime 86.98 # -------------------------   

edited

This is a very similar and it has been transferred from statistics. Stackexchange

I do not like the doSMP package individually because it often crashes my R. It is called Revolution Has been developed for build, and somehow fails to run smooth on my machine. For example, your code is upside down, just crashes my R. After that, it seems weird that trying to use a parallel function within a loop function has more logic to parallel the loop. In the communication involved in nested parallel computing, there has been a dramatic increase in the time of computation. You do not receive anything, because your SIM function is incredibly fast. In fact, maintaining the serial of internal loop makes more sense, because at that time, calculation time is calculated on one core Due to the overhead becomes larger.

Using an application for a depiction and looping using a snowman package is also very simple, rather than a loop, because there is a lot to win with the vector (see below). #same avec, bvec, sim ("avi", "avi", "aviasi", (avesse, function) "

  library (snow) sfInit (parallel = t, cpu = 2) BVC "," SIM ") system. Time ({out & lt; - sfSapply (avi, function (i) {# this is a parallel sapply (bvc, function (j) {# is not it one, there is no point in doing so, so SIM (i, j) Apart from the dimension names due to the structure 0.17   

, both matrices are similar:

  & gt; All.equal (out1, out2) [1] "Attributes: "   

This would be the correct R method to do this:

  system.time (out3 & lt; - external (avi * 10, bcc, "+") [3] passed 0.01   

Fast, and creates a similar (though transposed) matrix:

  & gt; All.equal (out1, t (out3)) [1] TRUE   

(As a reference, your double-to-loop 0.73 time has passed on my system ...)

Comments