[R] Parallel processing question ...

Robinson, David G drobin at sandia.gov
Wed Feb 9 00:18:57 CET 2011


I am experimenting with parallel processing using foreach and seem to be
missing something fundamental. Cool stuff. I've gone through the list and
seen a couple of closely related issues, but nothing I've tried seems to
work.

I know that the results from foreach are combined, but what if there is more
than one variable within the loop?  Below is a snippet (non-functioning) of
code that I hope provides enough insight into what I am trying to do.  The
commented out lines are what I would be doing (successfully) if I wasn't
trying to implement the %dopar% . The goal is to do statistics on the
sequence of lambda vectors that were originally accumulated in the matrix
lambdas using cbind.

Thanks in advance for any suggestions,
Dave

---------------snip
update_N <- function(sets, indexes, lam) {
    n <- length(indexes)-1    # count of events
    N <- rep(0, K) # count of failures per node
    for (i in 1:n) {
        nodes <- as.numeric(sets[indexes[i]:(indexes[i+1]-1)])
        node <- resample(nodes, 1, prob=lam[nodes]/sum(lam[nodes]))
        N[node] = N[node] + 1
    }
    N
}

lambdas<- foreach(j=1:(2*burn_in), .combine=cbind) %dopar% {
    N <- update_N(min_sets, min_sets_indexes, lambda)
    lambda <- rgamma(K, shape=a+N, rate=bT)
    lambda
    if (j%%100==0) { print(j); print(lambda); print(N)}
#    if (j > burn_in) {
#        lambdas <- cbind(lambdas, lambda)
#    }
}

---------------snip 



More information about the R-help mailing list