Julia @distributed: subsequent code run before all workers finish










1















I have been headbutting on a wall for a few days around this code:



using Distributed
using SharedArrays

# Dimension size
M=10;
N=100;

z_ijw = zeros(Float64,M,N,M)
z_ijw_tmp = SharedArrayFloat64(M*M*N)
i2s = CartesianIndices(z_ijw)

@distributed for iall=1:(M*M*N)
# get index
i=i2s[iall][1]
j=i2s[iall][2]
w=i2s[iall][3]
# Assign function value
z_ijw_tmp[iall]=sqrt(i+j+w) # Any random function would do
end

# Print the last element of the array
println(z_ijw_tmp[end])
println(z_ijw_tmp[end])
println(z_ijw_tmp[end])


The first printed out number is always 0, the second number is either 0 or 10.95... (sqrt of 120, which is correct). The 3rd is either 0 or 10.95 (if the 2nd is 0)



So it appears that the print code (@mainthread?) is allowed to run before all the workers finish. Is there anyway for the print code to run properly the first time (without a wait command)



Without multiple println, I thought it was a problem with scope and spend a few days reading about it @.@










share|improve this question




























    1















    I have been headbutting on a wall for a few days around this code:



    using Distributed
    using SharedArrays

    # Dimension size
    M=10;
    N=100;

    z_ijw = zeros(Float64,M,N,M)
    z_ijw_tmp = SharedArrayFloat64(M*M*N)
    i2s = CartesianIndices(z_ijw)

    @distributed for iall=1:(M*M*N)
    # get index
    i=i2s[iall][1]
    j=i2s[iall][2]
    w=i2s[iall][3]
    # Assign function value
    z_ijw_tmp[iall]=sqrt(i+j+w) # Any random function would do
    end

    # Print the last element of the array
    println(z_ijw_tmp[end])
    println(z_ijw_tmp[end])
    println(z_ijw_tmp[end])


    The first printed out number is always 0, the second number is either 0 or 10.95... (sqrt of 120, which is correct). The 3rd is either 0 or 10.95 (if the 2nd is 0)



    So it appears that the print code (@mainthread?) is allowed to run before all the workers finish. Is there anyway for the print code to run properly the first time (without a wait command)



    Without multiple println, I thought it was a problem with scope and spend a few days reading about it @.@










    share|improve this question


























      1












      1








      1








      I have been headbutting on a wall for a few days around this code:



      using Distributed
      using SharedArrays

      # Dimension size
      M=10;
      N=100;

      z_ijw = zeros(Float64,M,N,M)
      z_ijw_tmp = SharedArrayFloat64(M*M*N)
      i2s = CartesianIndices(z_ijw)

      @distributed for iall=1:(M*M*N)
      # get index
      i=i2s[iall][1]
      j=i2s[iall][2]
      w=i2s[iall][3]
      # Assign function value
      z_ijw_tmp[iall]=sqrt(i+j+w) # Any random function would do
      end

      # Print the last element of the array
      println(z_ijw_tmp[end])
      println(z_ijw_tmp[end])
      println(z_ijw_tmp[end])


      The first printed out number is always 0, the second number is either 0 or 10.95... (sqrt of 120, which is correct). The 3rd is either 0 or 10.95 (if the 2nd is 0)



      So it appears that the print code (@mainthread?) is allowed to run before all the workers finish. Is there anyway for the print code to run properly the first time (without a wait command)



      Without multiple println, I thought it was a problem with scope and spend a few days reading about it @.@










      share|improve this question
















      I have been headbutting on a wall for a few days around this code:



      using Distributed
      using SharedArrays

      # Dimension size
      M=10;
      N=100;

      z_ijw = zeros(Float64,M,N,M)
      z_ijw_tmp = SharedArrayFloat64(M*M*N)
      i2s = CartesianIndices(z_ijw)

      @distributed for iall=1:(M*M*N)
      # get index
      i=i2s[iall][1]
      j=i2s[iall][2]
      w=i2s[iall][3]
      # Assign function value
      z_ijw_tmp[iall]=sqrt(i+j+w) # Any random function would do
      end

      # Print the last element of the array
      println(z_ijw_tmp[end])
      println(z_ijw_tmp[end])
      println(z_ijw_tmp[end])


      The first printed out number is always 0, the second number is either 0 or 10.95... (sqrt of 120, which is correct). The 3rd is either 0 or 10.95 (if the 2nd is 0)



      So it appears that the print code (@mainthread?) is allowed to run before all the workers finish. Is there anyway for the print code to run properly the first time (without a wait command)



      Without multiple println, I thought it was a problem with scope and spend a few days reading about it @.@







      asynchronous julia-lang distributed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:44







      Huy Tran

















      asked Nov 12 '18 at 14:08









      Huy TranHuy Tran

      112




      112






















          1 Answer
          1






          active

          oldest

          votes


















          2














          @distributed with a reducer function, i.e. @distributed (+), will be synced, whereas @distributed without a reducer function will be started asynchronously.



          Putting a @sync in front of your @distributed should make the code behave the way you want it to.



          This is also noted in the documentation here:




          Note that without a reducer function, @distributed executes asynchronously, i.e. it spawns independent tasks on all available workers and returns immediately without waiting for completion. To wait for completion, prefix the call with @sync







          share|improve this answer























          • Thank you!! I am surprised I this is not common problem for everyone else. All examples are to be applied on REPL (step by step), so the problem only emerges when you run it all in a script.

            – Huy Tran
            Nov 12 '18 at 17:06












          • I don't think it's a problem at all. It's well defined and documented behavior.

            – crstnbr
            Nov 12 '18 at 18:56










          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%2f53263892%2fjulia-distributed-subsequent-code-run-before-all-workers-finish%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









          2














          @distributed with a reducer function, i.e. @distributed (+), will be synced, whereas @distributed without a reducer function will be started asynchronously.



          Putting a @sync in front of your @distributed should make the code behave the way you want it to.



          This is also noted in the documentation here:




          Note that without a reducer function, @distributed executes asynchronously, i.e. it spawns independent tasks on all available workers and returns immediately without waiting for completion. To wait for completion, prefix the call with @sync







          share|improve this answer























          • Thank you!! I am surprised I this is not common problem for everyone else. All examples are to be applied on REPL (step by step), so the problem only emerges when you run it all in a script.

            – Huy Tran
            Nov 12 '18 at 17:06












          • I don't think it's a problem at all. It's well defined and documented behavior.

            – crstnbr
            Nov 12 '18 at 18:56















          2














          @distributed with a reducer function, i.e. @distributed (+), will be synced, whereas @distributed without a reducer function will be started asynchronously.



          Putting a @sync in front of your @distributed should make the code behave the way you want it to.



          This is also noted in the documentation here:




          Note that without a reducer function, @distributed executes asynchronously, i.e. it spawns independent tasks on all available workers and returns immediately without waiting for completion. To wait for completion, prefix the call with @sync







          share|improve this answer























          • Thank you!! I am surprised I this is not common problem for everyone else. All examples are to be applied on REPL (step by step), so the problem only emerges when you run it all in a script.

            – Huy Tran
            Nov 12 '18 at 17:06












          • I don't think it's a problem at all. It's well defined and documented behavior.

            – crstnbr
            Nov 12 '18 at 18:56













          2












          2








          2







          @distributed with a reducer function, i.e. @distributed (+), will be synced, whereas @distributed without a reducer function will be started asynchronously.



          Putting a @sync in front of your @distributed should make the code behave the way you want it to.



          This is also noted in the documentation here:




          Note that without a reducer function, @distributed executes asynchronously, i.e. it spawns independent tasks on all available workers and returns immediately without waiting for completion. To wait for completion, prefix the call with @sync







          share|improve this answer













          @distributed with a reducer function, i.e. @distributed (+), will be synced, whereas @distributed without a reducer function will be started asynchronously.



          Putting a @sync in front of your @distributed should make the code behave the way you want it to.



          This is also noted in the documentation here:




          Note that without a reducer function, @distributed executes asynchronously, i.e. it spawns independent tasks on all available workers and returns immediately without waiting for completion. To wait for completion, prefix the call with @sync








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 15:43









          crstnbrcrstnbr

          3,81411022




          3,81411022












          • Thank you!! I am surprised I this is not common problem for everyone else. All examples are to be applied on REPL (step by step), so the problem only emerges when you run it all in a script.

            – Huy Tran
            Nov 12 '18 at 17:06












          • I don't think it's a problem at all. It's well defined and documented behavior.

            – crstnbr
            Nov 12 '18 at 18:56

















          • Thank you!! I am surprised I this is not common problem for everyone else. All examples are to be applied on REPL (step by step), so the problem only emerges when you run it all in a script.

            – Huy Tran
            Nov 12 '18 at 17:06












          • I don't think it's a problem at all. It's well defined and documented behavior.

            – crstnbr
            Nov 12 '18 at 18:56
















          Thank you!! I am surprised I this is not common problem for everyone else. All examples are to be applied on REPL (step by step), so the problem only emerges when you run it all in a script.

          – Huy Tran
          Nov 12 '18 at 17:06






          Thank you!! I am surprised I this is not common problem for everyone else. All examples are to be applied on REPL (step by step), so the problem only emerges when you run it all in a script.

          – Huy Tran
          Nov 12 '18 at 17:06














          I don't think it's a problem at all. It's well defined and documented behavior.

          – crstnbr
          Nov 12 '18 at 18:56





          I don't think it's a problem at all. It's well defined and documented behavior.

          – crstnbr
          Nov 12 '18 at 18:56

















          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%2f53263892%2fjulia-distributed-subsequent-code-run-before-all-workers-finish%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo