I have a big simulation that I would like to run on several cores, for this I foreach ()
I'm using the package.
I am parsing the loop 1000 times and within the loop I am using the counter as loop as a status vector. Exam:
reps & lt; For -1000 (in i reps) {a [i] & lt; If I do the same thing with foreach: library (foreach) cl < -mecluster (8) Register Dupperal (CL) LS & lt; -Fachch (IQ)% dopar% {rnorm (100)} I can not use the current counter I as the original loop, what is the way to use it is? I am also fine with keeping a counter, like every time I run one I start with i = i + 1 i = 0. As Roland suggests, you can type the "foreach" version like this:
reps & lt; - 1000 Ls & lt; - foreach (i = ICRD, reps, .combine = 'c')% dopar% {means (rnorm (100))}
variable i
It is not used, but if you want it in the future, then it is available.
Using "for" loops inside "foreach" loops can be very useful in achieving better performance, as it may be reduced by the number of iterations and workers working more in parallel Allow:
reps & lt; - 1000 ls & lt; - foreach (n = representative (rep / 8, 8), .combine = 'c')% dopar% {a & lt; - for numeric (n) (i seq_len (n)) {a [i] & lt; - Assignment to the a
inside the "foreach" loop is ok because it is a local variable when it is "rnorm (100))} a}
For "loop ends, returns a
, and finally all a
vector is added to the master with" C "function.
Note that this example is using the original "for" loop inside the "foreign" loop, it is more efficient because it is more efficient to send and collect tasks to the master and collect, There is no need to.
Actually, I will use "sapply" instead of "for", instead of "representative" loop and "movie" (from the "etter" package):
< Code> reps & lt; - 1000 ls & lt; % Dopar% {sapply (seq_len (n), function (i) mean (rnorm (100)) - foreach (n = idiv (reps, chunks = 8), with "combination", "dowie" = "c") }
What to worry about t is that if the number of iterations is not evenly divided by the number of workers.
Comments
Post a Comment