I am experimenting on optimizing some mathematical operations using C#.net within a package called Grasshopper (part of Rhino3D). The operation is quite simple but the list on which it has to be performed is big and may get much bigger.
I am using Parallel.ForEach and lists in my C# script and the number of final results I get is lower than what is expected. This is most probably due to the fact that list.add is not thread safe (or not thread safe within the software I'm building it on top of).
private void RunScript(double z, int x, List<double> y, ref object A)
{
List<double> temp = new List<double>();
double r;
System.Threading.Tasks.Parallel.ForEach(y, numb =>
{
r = Math.Pow((numb * x), z);
temp.Add(r);
});
A = temp;
Please help me figure out a simple and efficient way of running this simple math operation over several hundreds of values using CPU multithreading (or if you have suggestions about GPU CUDA).
I hope that the obscure and specific software does not bother you because as far as I know it performs identically to normal C#.Net/Python/VB.Net.
List
, you could tryConcurrentBag
, which is thread-safe: MSDN – Reisman