none
Parallel.ForEach loop with partition-local variables RRS feed

  • Question

  • see my below code. tell me why this loop variable will be required ? what will be stored in this loop variable ?

    static void Main(string[] args)
            {
                int[] nums = Enumerable.Range(1, 5).ToArray();
                long total = 0;
    
                // First type parameter is the type of the source elements
                // Second type parameter is the type of the thread-local variable (partition subtotal)
                Parallel.ForEach<int, long>(nums,new ParallelOptions { MaxDegreeOfParallelism = 1 }, // source collection
                                            () => 0, // method to initialize the local variable
                                            (j, loop, subtotal) => // method invoked by the loop on each iteration
                                            {
                                                subtotal += j; //modify local variable
                                                return subtotal; // value to be passed to next iteration
                                            },
                    // Method to be executed when each partition has completed.
                    // finalResult is the final value of subtotal for a particular partition.
                                            (finalResult) => 
                                                Interlocked.Add(ref total, finalResult)
                                            );
    
                Console.WriteLine("The total from Parallel.ForEach is {0:N0}", total);
            }

    thanks

    Monday, January 21, 2019 7:45 PM

Answers

  • Hi Studip_inn,

    Thank you for posting here.

    For your question, the loop variable is ParallelLoopState which used like loop statement. The loop variable is not used to store value like strings. It used to enable iterations of parallel loops to interact with other iterations.

    Please refer to the link below.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallelloopstate?view=netframework-4.7.2

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Saturday, January 26, 2019 5:52 PM
    Tuesday, January 22, 2019 5:27 AM
    Moderator

All replies

  • Did you check the documentation?  The "loop" variable is a ParallelLoopState structure.  It contains a few variables that allow you to manage the current state of the looping.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, January 21, 2019 10:27 PM
  • Hi Studip_inn,

    Thank you for posting here.

    For your question, the loop variable is ParallelLoopState which used like loop statement. The loop variable is not used to store value like strings. It used to enable iterations of parallel loops to interact with other iterations.

    Please refer to the link below.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallelloopstate?view=netframework-4.7.2

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Saturday, January 26, 2019 5:52 PM
    Tuesday, January 22, 2019 5:27 AM
    Moderator