Is it possible to parallelize a code with this structure?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












Is it possible to parallelize a code with this structure?



n=0;
Do[If[condition, n++],100]
n


ParallelDo always returns n=0.







share|improve this question
























    up vote
    2
    down vote

    favorite












    Is it possible to parallelize a code with this structure?



    n=0;
    Do[If[condition, n++],100]
    n


    ParallelDo always returns n=0.







    share|improve this question






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Is it possible to parallelize a code with this structure?



      n=0;
      Do[If[condition, n++],100]
      n


      ParallelDo always returns n=0.







      share|improve this question












      Is it possible to parallelize a code with this structure?



      n=0;
      Do[If[condition, n++],100]
      n


      ParallelDo always returns n=0.









      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 5 at 8:32









      mattiav27

      2,00011428




      2,00011428




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n.



          SetSharedVariable will let you pretend that n is shared between the parallel kernels, but this is not what really happens. n will instead be always evaluated in on the main kernel. This means that every time n is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition takes a very long time to evaluate).



          What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine does this quite directly, but it can be implemented with other parallel constructs as well.



          To give a specific example, the following code counts how many times the condition RandomReal < 0.3 is satisfied out of 10000 trials.



          Total@ParallelTable[
          If[RandomReal < 0.3, 1, 0],
          10000
          ]





          share|improve this answer






















          • Is something like this correct? ParallelCombine[Do[If[condition, n++],100]]
            – mattiav27
            Sep 5 at 10:06










          • @mattiav27 I'm sorry to have to ask this, but have you looked at the ParallelCombine documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
            – Szabolcs
            Sep 5 at 10:09










          • @mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the n variable the way you were trying to. In this comment you are trying to use it the same way.
            – Szabolcs
            Sep 5 at 10:11











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181256%2fis-it-possible-to-parallelize-a-code-with-this-structure%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n.



          SetSharedVariable will let you pretend that n is shared between the parallel kernels, but this is not what really happens. n will instead be always evaluated in on the main kernel. This means that every time n is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition takes a very long time to evaluate).



          What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine does this quite directly, but it can be implemented with other parallel constructs as well.



          To give a specific example, the following code counts how many times the condition RandomReal < 0.3 is satisfied out of 10000 trials.



          Total@ParallelTable[
          If[RandomReal < 0.3, 1, 0],
          10000
          ]





          share|improve this answer






















          • Is something like this correct? ParallelCombine[Do[If[condition, n++],100]]
            – mattiav27
            Sep 5 at 10:06










          • @mattiav27 I'm sorry to have to ask this, but have you looked at the ParallelCombine documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
            – Szabolcs
            Sep 5 at 10:09










          • @mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the n variable the way you were trying to. In this comment you are trying to use it the same way.
            – Szabolcs
            Sep 5 at 10:11















          up vote
          6
          down vote



          accepted










          Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n.



          SetSharedVariable will let you pretend that n is shared between the parallel kernels, but this is not what really happens. n will instead be always evaluated in on the main kernel. This means that every time n is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition takes a very long time to evaluate).



          What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine does this quite directly, but it can be implemented with other parallel constructs as well.



          To give a specific example, the following code counts how many times the condition RandomReal < 0.3 is satisfied out of 10000 trials.



          Total@ParallelTable[
          If[RandomReal < 0.3, 1, 0],
          10000
          ]





          share|improve this answer






















          • Is something like this correct? ParallelCombine[Do[If[condition, n++],100]]
            – mattiav27
            Sep 5 at 10:06










          • @mattiav27 I'm sorry to have to ask this, but have you looked at the ParallelCombine documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
            – Szabolcs
            Sep 5 at 10:09










          • @mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the n variable the way you were trying to. In this comment you are trying to use it the same way.
            – Szabolcs
            Sep 5 at 10:11













          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n.



          SetSharedVariable will let you pretend that n is shared between the parallel kernels, but this is not what really happens. n will instead be always evaluated in on the main kernel. This means that every time n is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition takes a very long time to evaluate).



          What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine does this quite directly, but it can be implemented with other parallel constructs as well.



          To give a specific example, the following code counts how many times the condition RandomReal < 0.3 is satisfied out of 10000 trials.



          Total@ParallelTable[
          If[RandomReal < 0.3, 1, 0],
          10000
          ]





          share|improve this answer














          Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n.



          SetSharedVariable will let you pretend that n is shared between the parallel kernels, but this is not what really happens. n will instead be always evaluated in on the main kernel. This means that every time n is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition takes a very long time to evaluate).



          What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine does this quite directly, but it can be implemented with other parallel constructs as well.



          To give a specific example, the following code counts how many times the condition RandomReal < 0.3 is satisfied out of 10000 trials.



          Total@ParallelTable[
          If[RandomReal < 0.3, 1, 0],
          10000
          ]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 5 at 10:03

























          answered Sep 5 at 8:56









          Szabolcs

          152k13415896




          152k13415896











          • Is something like this correct? ParallelCombine[Do[If[condition, n++],100]]
            – mattiav27
            Sep 5 at 10:06










          • @mattiav27 I'm sorry to have to ask this, but have you looked at the ParallelCombine documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
            – Szabolcs
            Sep 5 at 10:09










          • @mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the n variable the way you were trying to. In this comment you are trying to use it the same way.
            – Szabolcs
            Sep 5 at 10:11

















          • Is something like this correct? ParallelCombine[Do[If[condition, n++],100]]
            – mattiav27
            Sep 5 at 10:06










          • @mattiav27 I'm sorry to have to ask this, but have you looked at the ParallelCombine documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
            – Szabolcs
            Sep 5 at 10:09










          • @mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the n variable the way you were trying to. In this comment you are trying to use it the same way.
            – Szabolcs
            Sep 5 at 10:11
















          Is something like this correct? ParallelCombine[Do[If[condition, n++],100]]
          – mattiav27
          Sep 5 at 10:06




          Is something like this correct? ParallelCombine[Do[If[condition, n++],100]]
          – mattiav27
          Sep 5 at 10:06












          @mattiav27 I'm sorry to have to ask this, but have you looked at the ParallelCombine documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
          – Szabolcs
          Sep 5 at 10:09




          @mattiav27 I'm sorry to have to ask this, but have you looked at the ParallelCombine documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
          – Szabolcs
          Sep 5 at 10:09












          @mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the n variable the way you were trying to. In this comment you are trying to use it the same way.
          – Szabolcs
          Sep 5 at 10:11





          @mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the n variable the way you were trying to. In this comment you are trying to use it the same way.
          – Szabolcs
          Sep 5 at 10:11


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181256%2fis-it-possible-to-parallelize-a-code-with-this-structure%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery