2

R で次のスクリプトを実行する場合:

library(doMC)
registerDoMC(cores=3)

# First foreach
# This runs in 3 threads
foreach(i=1:3) %dopar% sqrt(i)

# Second foreach 
# This add 3 threads to the previous ones (now inactive but still consuming memory), totalling 6 threads
foreach(i=1:3) %dopar% sqrt(i)

foreachスクリプト全体が常に 3 つのコアを使用して実行されるように、2 番目のスレッドを実行するときに最初のスレッドを再利用する方法を知りたいです。

4

1 に答える 1

1

doMC の開発者の 1 人の提案のおかげで、回避策を見つけることができました。別のライブラリを使用して、次のコードは私が探していたものを実行します。

library(doParallel)
cores=makeForkCluster(3)
registerDoParallel(cores)

# First foreach
# This runs in 3 threads
foreach(i=1:3) %dopar% sqrt(i)

# Second foreach 
# This reuses the previous 3 threads (total of 3 active threads)
foreach(i=1:3) %dopar% sqrt(i)
于 2016-10-22T23:33:19.250 に答える