如何在R中使用多核与循环 [英] How to use multicore with loops in R

查看:1155
本文介绍了如何在R中使用多核与循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加快这个内置循环,我该怎么办? (M_P in 0:9)

 for(M_D in 0:(9- (9-M_P-M_D-M_A))中的(M_A in 0:(9-M_P-M_D))
{
) )($ M $ CD in(9-M_P-M_D-M_A-M_CC))
{


for(G_D in 0:9 )
($ 0 $($ G $ B $ 0 $($ G $ B $ 0 $($ G $ B $ 0 $ (9-G_D-G_A-G_CC)中的(G_CD)
($ 0 $ 9中的S_D)
{



{$ b (S_D在0:(9-S_D))
{
for(S_CC in 0:(9-S_D-S_A))
{
for(S_CD in (Q_D in 0:3)
{
for(Q_D in 0:3)
{


(9-S_D-S_A-S_CC) (3-Q_P))
{
for(Q_A in 0:(3-Q_P-Q_D))
{$ b (3-Q_P-Q_D-Q_A-Q_CC)中的Q_CD)($ Q $ B $(b) $ b

计算过程如何,我该怎么做?我只是一个初学者,但我听说有可能是东西或应用的东西或多核心,我有4cores,我使用Rstudio请帮助我。谢谢!
$ b

PS:告诉我你是否需要代码的其余部分,但这是一个简单的计算,使用*和+使用所有的M_P,M_D等。如果我离开它一夜之间有没有机会结束?我想添加几个循环,所以我将不得不这样做所有这3到10次:s

解决方案

变量 M_xx G_xx S_xx Q_xx 是独立的。此外,还有很多重合的值生成。我已经将这4个变量分割成单独的循环,然后生成了所有仅考虑唯一值的组合。查看代码:

  M < -  NULL 

(M_P in 0:9)for (9-M_P-M_D)中的(M_D在0:(9-M_P)中的M_D) M_D + M_A + M_CC))
{
M [长度(M)+1] -1.1 * M_P + 2.1 * M_D + 3.1 * M_A + 4.1 * M_CC + 4 * M_CD $ b $ (G_D in 0:(9-G_D))for(G_CC in 0:(9-G_D))中的(G_D在0:9中)b:b
$ b G< - NULL

(9-G_D-G_A-G_CC))中的(G_CD)(b)(b)中的G_D + G_A + G_D + G_A + (S_D in 0:9)for(S_A in 0:(9-bit))中的S_D为1.5 * G_CC + 3 * G_CD
}

S < - NULL

(9-S_D-S_A))中的S_CD)(S_D中的S_CD)对于(9-S_D-S_A-S_CC)中的S_CD) ;(Q_P in 0:3)中的Q_P值为5 * S_D + 4 * S_A + 3 * S_CC + 6 * S_CD
}

Q < - NULL

(3-Q_P-Q_D))中的(Q_D在0:(3-Q_P))中的(Q_D) Q_P-Q_D-Q_A-Q_CC))
{
Q [length(Q)+1] <-2 * Q_P + 3 * Q_D + 2.2 * Q_A + 3 * Q_CC + 4 * Q_CD $ (expand.grid(唯一的(M),唯一的(G),唯一的(S),唯一的(Q)),b $ b}

max 1,sum))


I need to speed up this built in loops how can I do please ?

for(M_P in 0:9)
{
for(M_D in 0:(9-M_P))
{
for(M_A in 0:(9-M_P-M_D))
{
  for(M_CC in 0:(9-M_P-M_D-M_A))
  {
    for(M_CD in (9-M_P-M_D-M_A-M_CC))
    {


      for(G_D in 0:9)
      {
        for(G_A in 0:(9-G_D))
        {
          for(G_CC in 0:(9-G_D-G_A))
          {
            for(G_CD in (9-G_D-G_A-G_CC))
            {


              for(S_D in 0:9)
              {
                for(S_A in 0:(9-S_D))
                {
                  for(S_CC in 0:(9-S_D-S_A))
                  {
                    for(S_CD in (9-S_D-S_A-S_CC))
                    {


                      for(Q_P in 0:3)
                      {
                        for(Q_D in 0:(3-Q_P))
                        {
                          for(Q_A in 0:(3-Q_P-Q_D))
                          {
                            for(Q_CC in 0:(3-Q_P-Q_D-Q_A))
                            {
                              for(Q_CD in (3-Q_P-Q_D-Q_A-Q_CC))
                              {

It's taking forever to compute how can I do please ? I'm only a beginner but I heard that there's maybe something with the apply thing or also with multi cores i have 4cores, i'm using Rstudio please help me. Thank mens !

PS : tell me if you need the rest of code but it's a simple calculation with * and + using all the M_P, M_D etc. If i leave it overnight does it have a chance to end ? I would like to add few loops in that so I would have to do all of this 3 or 10 more times :s

解决方案

The variables M_xx, G_xx, S_xx and Q_xx are independent. Also, there is a lot of coincident values generated. I've split these 4 variables in separate loops, then I've generated all combinations considering only unique values. See the code:

M <- NULL

for(M_P in 0:9) for(M_D in 0:(9-M_P)) for(M_A in 0:(9-M_P-M_D)) for(M_CC in 0:(9-M_P-M_D-M_A)) for(M_CD in (9-M_P-M_D-M_A-M_CC))
{
    M[length(M)+1] <- 1.1*M_P+2.1*M_D+3.1*M_A+4.1*M_CC+4*M_CD
}

G <- NULL

for(G_D in 0:9) for(G_A in 0:(9-G_D)) for(G_CC in 0:(9-G_D-G_A)) for(G_CD in (9-G_D-G_A-G_CC))
{
    G[length(G)+1] <- 2*G_D+5*G_A+1.5*G_CC+3*G_CD
}

S <- NULL

for(S_D in 0:9) for(S_A in 0:(9-S_D)) for(S_CC in 0:(9-S_D-S_A)) for(S_CD in (9-S_D-S_A-S_CC))
{
    S[length(S)+1] <- 5*S_D+4*S_A+3*S_CC+6*S_CD
}

Q <- NULL

for(Q_P in 0:3) for(Q_D in 0:(3-Q_P)) for(Q_A in 0:(3-Q_P-Q_D)) for(Q_CC in 0:(3-Q_P-Q_D-Q_A)) for(Q_CD in (3-Q_P-Q_D-Q_A-Q_CC))
{
    Q[length(Q)+1] <- 2*Q_P+3*Q_D+2.2*Q_A+3*Q_CC+4*Q_CD
}

max(apply(expand.grid(unique(M), unique(G), unique(S), unique(Q)), 1, sum))

这篇关于如何在R中使用多核与循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆