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;
}
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With