Parallelize[MapIndexed[…,Association[…]]] returns broken result

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











up vote
1
down vote

favorite












Bug introduced in 10.0.2 or earlier and persisting through 11.3





Confirmed: CASE:4041910




Consider the following example:



MapIndexed[f[##, $KernelID]&, <|a -> 1, b -> 2, c -> 3|>]
(* <|a -> f[1, Key[a], 0], b -> f[2, Key[b], 0], c -> f[3, Key[c], 0]|> *)


Now lets try to Parallelize this:



Parallelize@MapIndexed[f[##, $KernelID]&, <|a -> 1, b -> 2, c -> 3|>]
(* ---- Output in Mathematica 11.2 - 11.3 ---- *)
(* Association[f[a -> 1, 1, 2], f[b -> 2, 2, 2], f[c -> 3, 3, 1]] *)

(* ---- Output in Mathematica 10 - 11.1.1 ---- *)
(* Transpose::nmtx: The first two levels of < cannot be transposed. >> *)
(* Transpose[f[<|a -> 1, b -> 2, c -> 3|>, 1, 2, 3, 4]] *)


Clearly, this is completely broken - it doesn't simply switch to sequential evaluation, the output is actually useless.



How can one work around this issue until it gets fixed?










share|improve this question



























    up vote
    1
    down vote

    favorite












    Bug introduced in 10.0.2 or earlier and persisting through 11.3





    Confirmed: CASE:4041910




    Consider the following example:



    MapIndexed[f[##, $KernelID]&, <|a -> 1, b -> 2, c -> 3|>]
    (* <|a -> f[1, Key[a], 0], b -> f[2, Key[b], 0], c -> f[3, Key[c], 0]|> *)


    Now lets try to Parallelize this:



    Parallelize@MapIndexed[f[##, $KernelID]&, <|a -> 1, b -> 2, c -> 3|>]
    (* ---- Output in Mathematica 11.2 - 11.3 ---- *)
    (* Association[f[a -> 1, 1, 2], f[b -> 2, 2, 2], f[c -> 3, 3, 1]] *)

    (* ---- Output in Mathematica 10 - 11.1.1 ---- *)
    (* Transpose::nmtx: The first two levels of < cannot be transposed. >> *)
    (* Transpose[f[<|a -> 1, b -> 2, c -> 3|>, 1, 2, 3, 4]] *)


    Clearly, this is completely broken - it doesn't simply switch to sequential evaluation, the output is actually useless.



    How can one work around this issue until it gets fixed?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Bug introduced in 10.0.2 or earlier and persisting through 11.3





      Confirmed: CASE:4041910




      Consider the following example:



      MapIndexed[f[##, $KernelID]&, <|a -> 1, b -> 2, c -> 3|>]
      (* <|a -> f[1, Key[a], 0], b -> f[2, Key[b], 0], c -> f[3, Key[c], 0]|> *)


      Now lets try to Parallelize this:



      Parallelize@MapIndexed[f[##, $KernelID]&, <|a -> 1, b -> 2, c -> 3|>]
      (* ---- Output in Mathematica 11.2 - 11.3 ---- *)
      (* Association[f[a -> 1, 1, 2], f[b -> 2, 2, 2], f[c -> 3, 3, 1]] *)

      (* ---- Output in Mathematica 10 - 11.1.1 ---- *)
      (* Transpose::nmtx: The first two levels of < cannot be transposed. >> *)
      (* Transpose[f[<|a -> 1, b -> 2, c -> 3|>, 1, 2, 3, 4]] *)


      Clearly, this is completely broken - it doesn't simply switch to sequential evaluation, the output is actually useless.



      How can one work around this issue until it gets fixed?










      share|improve this question















      Bug introduced in 10.0.2 or earlier and persisting through 11.3





      Confirmed: CASE:4041910




      Consider the following example:



      MapIndexed[f[##, $KernelID]&, <|a -> 1, b -> 2, c -> 3|>]
      (* <|a -> f[1, Key[a], 0], b -> f[2, Key[b], 0], c -> f[3, Key[c], 0]|> *)


      Now lets try to Parallelize this:



      Parallelize@MapIndexed[f[##, $KernelID]&, <|a -> 1, b -> 2, c -> 3|>]
      (* ---- Output in Mathematica 11.2 - 11.3 ---- *)
      (* Association[f[a -> 1, 1, 2], f[b -> 2, 2, 2], f[c -> 3, 3, 1]] *)

      (* ---- Output in Mathematica 10 - 11.1.1 ---- *)
      (* Transpose::nmtx: The first two levels of < cannot be transposed. >> *)
      (* Transpose[f[<|a -> 1, b -> 2, c -> 3|>, 1, 2, 3, 4]] *)


      Clearly, this is completely broken - it doesn't simply switch to sequential evaluation, the output is actually useless.



      How can one work around this issue until it gets fixed?







      bugs parallelization associations map






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      Szabolcs

      155k13420911




      155k13420911










      asked 2 hours ago









      Lukas Lang

      5,2931525




      5,2931525




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          Workaround



          Wolfram support suggested the following workaround until the issue is resolved:



          Parallelize@Map[First, MapIndexed[Hold[f[##, $KernelID]]&, <|a -> 1, b -> 2, c -> 3|>]]
          (* <|a -> f[1, Key[a], 2], b -> f[2, Key[b], 2], c -> f[3, Key[c], 1]|> *)


          which is working. This is because the expressions to be evaluated are built sequentially (but wrapped in Hold). The evaluation (by means of First/ReleaseHold) is then done using ParallelMap[…]/Parallelize[Map[…]]



          Fix



          While the above is a nice workaround, it's still something that has to be remembered. The following code fixes the issue globally.



          Mathematica 11



          Begin["Parallel`Evaluate`Private`"];
          Unprotect@MapIndexed;
          HoldPattern[tryCombine[MapIndexed[f_, str_Association], opts___]] ^:=
          ParallelCombine[MapIndexed[f], str, opts]
          Protect@MapIndexed;
          End;


          To verify:



          Parallelize@MapIndexed[f[##, $KernelID] &, <|a -> 1, b -> 2, c -> 3|>]
          (* <|a -> f[1, Key[a], 4], b -> f[2, Key[b], 3], c -> f[3, Key[c], 2]|> *)


          This works by adding a special case for the problematic case (Parallelize[MapIndexed[…,<|…|>]]) to the code that handles the parallelization internally. Since ParallelCombine supports associations and associations can be Joined, the actual implementation is trivial (since we don't have to add the position specification manually).



          Mathematica 10 (works also for 11)



          Begin["Parallel`Evaluate`Private`"];
          Unprotect@MapIndexed;
          tryCombine[MapIndexed[f_, str_Association], opts___] ^:=
          ParallelCombine[MapIndexed[f, <|#|>] &, Normal@str, opts]
          Protect@MapIndexed;
          End;


          The only difference to the above fix is that ParallelCombine doesn't seem to support associations in 10, so we have to convert it to a list first. Of course this will also work for Mathematica 11+, but it feels like an unnecessary step to covert to a list first.






          share|improve this answer




















            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: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fmathematica.stackexchange.com%2fquestions%2f185259%2fparallelizemapindexed-association-returns-broken-result%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
            3
            down vote













            Workaround



            Wolfram support suggested the following workaround until the issue is resolved:



            Parallelize@Map[First, MapIndexed[Hold[f[##, $KernelID]]&, <|a -> 1, b -> 2, c -> 3|>]]
            (* <|a -> f[1, Key[a], 2], b -> f[2, Key[b], 2], c -> f[3, Key[c], 1]|> *)


            which is working. This is because the expressions to be evaluated are built sequentially (but wrapped in Hold). The evaluation (by means of First/ReleaseHold) is then done using ParallelMap[…]/Parallelize[Map[…]]



            Fix



            While the above is a nice workaround, it's still something that has to be remembered. The following code fixes the issue globally.



            Mathematica 11



            Begin["Parallel`Evaluate`Private`"];
            Unprotect@MapIndexed;
            HoldPattern[tryCombine[MapIndexed[f_, str_Association], opts___]] ^:=
            ParallelCombine[MapIndexed[f], str, opts]
            Protect@MapIndexed;
            End;


            To verify:



            Parallelize@MapIndexed[f[##, $KernelID] &, <|a -> 1, b -> 2, c -> 3|>]
            (* <|a -> f[1, Key[a], 4], b -> f[2, Key[b], 3], c -> f[3, Key[c], 2]|> *)


            This works by adding a special case for the problematic case (Parallelize[MapIndexed[…,<|…|>]]) to the code that handles the parallelization internally. Since ParallelCombine supports associations and associations can be Joined, the actual implementation is trivial (since we don't have to add the position specification manually).



            Mathematica 10 (works also for 11)



            Begin["Parallel`Evaluate`Private`"];
            Unprotect@MapIndexed;
            tryCombine[MapIndexed[f_, str_Association], opts___] ^:=
            ParallelCombine[MapIndexed[f, <|#|>] &, Normal@str, opts]
            Protect@MapIndexed;
            End;


            The only difference to the above fix is that ParallelCombine doesn't seem to support associations in 10, so we have to convert it to a list first. Of course this will also work for Mathematica 11+, but it feels like an unnecessary step to covert to a list first.






            share|improve this answer
























              up vote
              3
              down vote













              Workaround



              Wolfram support suggested the following workaround until the issue is resolved:



              Parallelize@Map[First, MapIndexed[Hold[f[##, $KernelID]]&, <|a -> 1, b -> 2, c -> 3|>]]
              (* <|a -> f[1, Key[a], 2], b -> f[2, Key[b], 2], c -> f[3, Key[c], 1]|> *)


              which is working. This is because the expressions to be evaluated are built sequentially (but wrapped in Hold). The evaluation (by means of First/ReleaseHold) is then done using ParallelMap[…]/Parallelize[Map[…]]



              Fix



              While the above is a nice workaround, it's still something that has to be remembered. The following code fixes the issue globally.



              Mathematica 11



              Begin["Parallel`Evaluate`Private`"];
              Unprotect@MapIndexed;
              HoldPattern[tryCombine[MapIndexed[f_, str_Association], opts___]] ^:=
              ParallelCombine[MapIndexed[f], str, opts]
              Protect@MapIndexed;
              End;


              To verify:



              Parallelize@MapIndexed[f[##, $KernelID] &, <|a -> 1, b -> 2, c -> 3|>]
              (* <|a -> f[1, Key[a], 4], b -> f[2, Key[b], 3], c -> f[3, Key[c], 2]|> *)


              This works by adding a special case for the problematic case (Parallelize[MapIndexed[…,<|…|>]]) to the code that handles the parallelization internally. Since ParallelCombine supports associations and associations can be Joined, the actual implementation is trivial (since we don't have to add the position specification manually).



              Mathematica 10 (works also for 11)



              Begin["Parallel`Evaluate`Private`"];
              Unprotect@MapIndexed;
              tryCombine[MapIndexed[f_, str_Association], opts___] ^:=
              ParallelCombine[MapIndexed[f, <|#|>] &, Normal@str, opts]
              Protect@MapIndexed;
              End;


              The only difference to the above fix is that ParallelCombine doesn't seem to support associations in 10, so we have to convert it to a list first. Of course this will also work for Mathematica 11+, but it feels like an unnecessary step to covert to a list first.






              share|improve this answer






















                up vote
                3
                down vote










                up vote
                3
                down vote









                Workaround



                Wolfram support suggested the following workaround until the issue is resolved:



                Parallelize@Map[First, MapIndexed[Hold[f[##, $KernelID]]&, <|a -> 1, b -> 2, c -> 3|>]]
                (* <|a -> f[1, Key[a], 2], b -> f[2, Key[b], 2], c -> f[3, Key[c], 1]|> *)


                which is working. This is because the expressions to be evaluated are built sequentially (but wrapped in Hold). The evaluation (by means of First/ReleaseHold) is then done using ParallelMap[…]/Parallelize[Map[…]]



                Fix



                While the above is a nice workaround, it's still something that has to be remembered. The following code fixes the issue globally.



                Mathematica 11



                Begin["Parallel`Evaluate`Private`"];
                Unprotect@MapIndexed;
                HoldPattern[tryCombine[MapIndexed[f_, str_Association], opts___]] ^:=
                ParallelCombine[MapIndexed[f], str, opts]
                Protect@MapIndexed;
                End;


                To verify:



                Parallelize@MapIndexed[f[##, $KernelID] &, <|a -> 1, b -> 2, c -> 3|>]
                (* <|a -> f[1, Key[a], 4], b -> f[2, Key[b], 3], c -> f[3, Key[c], 2]|> *)


                This works by adding a special case for the problematic case (Parallelize[MapIndexed[…,<|…|>]]) to the code that handles the parallelization internally. Since ParallelCombine supports associations and associations can be Joined, the actual implementation is trivial (since we don't have to add the position specification manually).



                Mathematica 10 (works also for 11)



                Begin["Parallel`Evaluate`Private`"];
                Unprotect@MapIndexed;
                tryCombine[MapIndexed[f_, str_Association], opts___] ^:=
                ParallelCombine[MapIndexed[f, <|#|>] &, Normal@str, opts]
                Protect@MapIndexed;
                End;


                The only difference to the above fix is that ParallelCombine doesn't seem to support associations in 10, so we have to convert it to a list first. Of course this will also work for Mathematica 11+, but it feels like an unnecessary step to covert to a list first.






                share|improve this answer












                Workaround



                Wolfram support suggested the following workaround until the issue is resolved:



                Parallelize@Map[First, MapIndexed[Hold[f[##, $KernelID]]&, <|a -> 1, b -> 2, c -> 3|>]]
                (* <|a -> f[1, Key[a], 2], b -> f[2, Key[b], 2], c -> f[3, Key[c], 1]|> *)


                which is working. This is because the expressions to be evaluated are built sequentially (but wrapped in Hold). The evaluation (by means of First/ReleaseHold) is then done using ParallelMap[…]/Parallelize[Map[…]]



                Fix



                While the above is a nice workaround, it's still something that has to be remembered. The following code fixes the issue globally.



                Mathematica 11



                Begin["Parallel`Evaluate`Private`"];
                Unprotect@MapIndexed;
                HoldPattern[tryCombine[MapIndexed[f_, str_Association], opts___]] ^:=
                ParallelCombine[MapIndexed[f], str, opts]
                Protect@MapIndexed;
                End;


                To verify:



                Parallelize@MapIndexed[f[##, $KernelID] &, <|a -> 1, b -> 2, c -> 3|>]
                (* <|a -> f[1, Key[a], 4], b -> f[2, Key[b], 3], c -> f[3, Key[c], 2]|> *)


                This works by adding a special case for the problematic case (Parallelize[MapIndexed[…,<|…|>]]) to the code that handles the parallelization internally. Since ParallelCombine supports associations and associations can be Joined, the actual implementation is trivial (since we don't have to add the position specification manually).



                Mathematica 10 (works also for 11)



                Begin["Parallel`Evaluate`Private`"];
                Unprotect@MapIndexed;
                tryCombine[MapIndexed[f_, str_Association], opts___] ^:=
                ParallelCombine[MapIndexed[f, <|#|>] &, Normal@str, opts]
                Protect@MapIndexed;
                End;


                The only difference to the above fix is that ParallelCombine doesn't seem to support associations in 10, so we have to convert it to a list first. Of course this will also work for Mathematica 11+, but it feels like an unnecessary step to covert to a list first.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Lukas Lang

                5,2931525




                5,2931525



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185259%2fparallelizemapindexed-association-returns-broken-result%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