Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parallel foreach loop - odd behavior

The code below simply creates a List> of random numbers and then calculates the cumulative sum of each list in a parallel foreach loop. Why do I get less than 'numLists' evaluations? Often around 9990. I'm guessing it has something to do with thread safety. What's an alternative method? (I'm a C# beginner so hopefully I'm using correct terms) Thanks.

using System;  
using System.Collections.Generic;  
using System.Threading.Tasks;  

namespace testParallelForeach
{
    class Program
    {
        static void Main(string[] args)
        {

            List<List<double>> bsData = new List<List<double>>();
            List<List<double>> cumsumDataP = new List<List<double>>();
            int numLists = 10000;
            int myLen = 400;
            Random rand = new Random();
            for (int i = 0; i < numLists; i++)
            {
                bsData.Add(new List<double>());
                for (int j = 0; j < myLen; j++)
                {
                    bsData[i].Add(rand.NextDouble());
                }
            }
            Parallel.ForEach(bsData, a => cumsumDataP.Add(CumulativeSumParallel(a)));
            Console.WriteLine("cumsumDataP.Count={0}", cumsumDataP.Count);
            Console.ReadKey();

        }

        public static List<double> CumulativeSumParallel(List<double> singleRetSeries)
        {
            int r = singleRetSeries.Count;
            List<double> cumsumList = new List<double>();

            cumsumList.Add(singleRetSeries[0]);
            for (int i = 1; i < r; i++)
            {
                cumsumList.Add(cumsumList[i - 1] + singleRetSeries[i]);
            }
            return cumsumList;
        }
    }
}
like image 908
John Avatar asked Feb 02 '23 22:02

John


1 Answers

List<T> is indeed not thread safe, so cumsupDataP.Add(...) is dropping data in unpredictable ways.

Replace that line with:

ConcurrentBag<List<double>> cumsumDataP = new ConcurrentBag<List<double>>();

and it will all work. Note that ConcurrentBag<T> is unordered, but that is fine because you have no way of predicting the order from the threads anyway ;p

like image 186
Marc Gravell Avatar answered Feb 05 '23 13:02

Marc Gravell