Do something only in firs step of for loop in parallel



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I would like to execute commands in first step of for loop and other list of commands in all steps of the same for loop. It is easy to do that in non-paralel loop:



for (i in 1:5) 
if (i == 1)
print("First step")

print("Same code")



but I would like to execute for loop in parallel, that is write foreach loop, which means i would not be equal to 1 for all nodes (processes). Non-paralell foreach loop is:



library(foreach)
foreach(i = 1:5) %do%
if (i == 1)
print("First step")

print("Same code")



How to execute print("First") in only first step for every node?










share|improve this question






















  • What about just performing the first step and only then parallelize?

    – nicola
    Nov 15 '18 at 13:10











  • I don't think that would work since I have to execute this code on Azure batch in the end, so everything should be inside one foreachloop.

    – Mislav
    Nov 15 '18 at 13:12











  • Hmm, not much clear to me. You can always make a loop consisting of just one step. I guess you should describe better your needs.

    – nicola
    Nov 15 '18 at 13:14












  • I need for loop of n steps, not one. But few lines of code should be executed in only first step, and other in all steps. Everything should be inside one for(each) loop and executed in parallel (which means that for some processes first i would be 2, 3 etc). More concretely, I need to set web driver using RSelenium in first step and than work with it in all other steps, but I think it is not important what is the code for first step?

    – Mislav
    Nov 15 '18 at 13:21

















1















I would like to execute commands in first step of for loop and other list of commands in all steps of the same for loop. It is easy to do that in non-paralel loop:



for (i in 1:5) 
if (i == 1)
print("First step")

print("Same code")



but I would like to execute for loop in parallel, that is write foreach loop, which means i would not be equal to 1 for all nodes (processes). Non-paralell foreach loop is:



library(foreach)
foreach(i = 1:5) %do%
if (i == 1)
print("First step")

print("Same code")



How to execute print("First") in only first step for every node?










share|improve this question






















  • What about just performing the first step and only then parallelize?

    – nicola
    Nov 15 '18 at 13:10











  • I don't think that would work since I have to execute this code on Azure batch in the end, so everything should be inside one foreachloop.

    – Mislav
    Nov 15 '18 at 13:12











  • Hmm, not much clear to me. You can always make a loop consisting of just one step. I guess you should describe better your needs.

    – nicola
    Nov 15 '18 at 13:14












  • I need for loop of n steps, not one. But few lines of code should be executed in only first step, and other in all steps. Everything should be inside one for(each) loop and executed in parallel (which means that for some processes first i would be 2, 3 etc). More concretely, I need to set web driver using RSelenium in first step and than work with it in all other steps, but I think it is not important what is the code for first step?

    – Mislav
    Nov 15 '18 at 13:21













1












1








1








I would like to execute commands in first step of for loop and other list of commands in all steps of the same for loop. It is easy to do that in non-paralel loop:



for (i in 1:5) 
if (i == 1)
print("First step")

print("Same code")



but I would like to execute for loop in parallel, that is write foreach loop, which means i would not be equal to 1 for all nodes (processes). Non-paralell foreach loop is:



library(foreach)
foreach(i = 1:5) %do%
if (i == 1)
print("First step")

print("Same code")



How to execute print("First") in only first step for every node?










share|improve this question














I would like to execute commands in first step of for loop and other list of commands in all steps of the same for loop. It is easy to do that in non-paralel loop:



for (i in 1:5) 
if (i == 1)
print("First step")

print("Same code")



but I would like to execute for loop in parallel, that is write foreach loop, which means i would not be equal to 1 for all nodes (processes). Non-paralell foreach loop is:



library(foreach)
foreach(i = 1:5) %do%
if (i == 1)
print("First step")

print("Same code")



How to execute print("First") in only first step for every node?







r for-loop foreach parallel-processing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 13:03









MislavMislav

489319




489319












  • What about just performing the first step and only then parallelize?

    – nicola
    Nov 15 '18 at 13:10











  • I don't think that would work since I have to execute this code on Azure batch in the end, so everything should be inside one foreachloop.

    – Mislav
    Nov 15 '18 at 13:12











  • Hmm, not much clear to me. You can always make a loop consisting of just one step. I guess you should describe better your needs.

    – nicola
    Nov 15 '18 at 13:14












  • I need for loop of n steps, not one. But few lines of code should be executed in only first step, and other in all steps. Everything should be inside one for(each) loop and executed in parallel (which means that for some processes first i would be 2, 3 etc). More concretely, I need to set web driver using RSelenium in first step and than work with it in all other steps, but I think it is not important what is the code for first step?

    – Mislav
    Nov 15 '18 at 13:21

















  • What about just performing the first step and only then parallelize?

    – nicola
    Nov 15 '18 at 13:10











  • I don't think that would work since I have to execute this code on Azure batch in the end, so everything should be inside one foreachloop.

    – Mislav
    Nov 15 '18 at 13:12











  • Hmm, not much clear to me. You can always make a loop consisting of just one step. I guess you should describe better your needs.

    – nicola
    Nov 15 '18 at 13:14












  • I need for loop of n steps, not one. But few lines of code should be executed in only first step, and other in all steps. Everything should be inside one for(each) loop and executed in parallel (which means that for some processes first i would be 2, 3 etc). More concretely, I need to set web driver using RSelenium in first step and than work with it in all other steps, but I think it is not important what is the code for first step?

    – Mislav
    Nov 15 '18 at 13:21
















What about just performing the first step and only then parallelize?

– nicola
Nov 15 '18 at 13:10





What about just performing the first step and only then parallelize?

– nicola
Nov 15 '18 at 13:10













I don't think that would work since I have to execute this code on Azure batch in the end, so everything should be inside one foreachloop.

– Mislav
Nov 15 '18 at 13:12





I don't think that would work since I have to execute this code on Azure batch in the end, so everything should be inside one foreachloop.

– Mislav
Nov 15 '18 at 13:12













Hmm, not much clear to me. You can always make a loop consisting of just one step. I guess you should describe better your needs.

– nicola
Nov 15 '18 at 13:14






Hmm, not much clear to me. You can always make a loop consisting of just one step. I guess you should describe better your needs.

– nicola
Nov 15 '18 at 13:14














I need for loop of n steps, not one. But few lines of code should be executed in only first step, and other in all steps. Everything should be inside one for(each) loop and executed in parallel (which means that for some processes first i would be 2, 3 etc). More concretely, I need to set web driver using RSelenium in first step and than work with it in all other steps, but I think it is not important what is the code for first step?

– Mislav
Nov 15 '18 at 13:21





I need for loop of n steps, not one. But few lines of code should be executed in only first step, and other in all steps. Everything should be inside one for(each) loop and executed in parallel (which means that for some processes first i would be 2, 3 etc). More concretely, I need to set web driver using RSelenium in first step and than work with it in all other steps, but I think it is not important what is the code for first step?

– Mislav
Nov 15 '18 at 13:21












1 Answer
1






active

oldest

votes


















1














You can do:



library(doParallel)
registerDoParallel(cl <- makeCluster(3, outfile = ""))
do_print <- TRUE
foreach(i = 1:5) %dopar%
if (do_print)
print("First step")
do_print <- FALSE

print("Same code")

stopCluster(cl)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320114%2fdo-something-only-in-firs-step-of-for-loop-in-parallel%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can do:



    library(doParallel)
    registerDoParallel(cl <- makeCluster(3, outfile = ""))
    do_print <- TRUE
    foreach(i = 1:5) %dopar%
    if (do_print)
    print("First step")
    do_print <- FALSE

    print("Same code")

    stopCluster(cl)





    share|improve this answer



























      1














      You can do:



      library(doParallel)
      registerDoParallel(cl <- makeCluster(3, outfile = ""))
      do_print <- TRUE
      foreach(i = 1:5) %dopar%
      if (do_print)
      print("First step")
      do_print <- FALSE

      print("Same code")

      stopCluster(cl)





      share|improve this answer

























        1












        1








        1







        You can do:



        library(doParallel)
        registerDoParallel(cl <- makeCluster(3, outfile = ""))
        do_print <- TRUE
        foreach(i = 1:5) %dopar%
        if (do_print)
        print("First step")
        do_print <- FALSE

        print("Same code")

        stopCluster(cl)





        share|improve this answer













        You can do:



        library(doParallel)
        registerDoParallel(cl <- makeCluster(3, outfile = ""))
        do_print <- TRUE
        foreach(i = 1:5) %dopar%
        if (do_print)
        print("First step")
        do_print <- FALSE

        print("Same code")

        stopCluster(cl)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 13:16









        F. PrivéF. Privé

        7,24521145




        7,24521145





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320114%2fdo-something-only-in-firs-step-of-for-loop-in-parallel%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20