I am working on a small simulation that runs on my 8-core workstation. Modeling involves modeling the interaction between a large number of independent nodes. During one phase, I need to perform a series of simple atomic operations for each node in parallel. I am using Parallel.ForEach from System.Threading.Tasks to simultaneously apply the operation to each node in the list of all nodes.
This works well for the 100-500 nodes that I used for testing. The load was balanced very well when all the cores are constantly in use. Unfortunately, when I try to run a simulation with a basic dataset (5000+ nodes), everything goes wrong. All 8 cores in most cases are idle, up to 100% every few seconds, and then return to 1% use. A few minutes after that, an OutOfMemoryException is thrown and the program crashes.
I'm not quite sure what is wrong, but I remain suspicious that my current code generates many threads, which would be optimal for the task. I think that the ideal method for the model would be to determine the number of available kernels N, split the list of nodes into N segments, then generate N threads, providing each thread with a separate section of the list.
I would like to ask if this is really a good solution to the problem, are there better ones and how should this be implemented in C #? Any tips or comments are welcome.
EDIT: sample code on request
Parallel.ForEach(listOfNodes, tempNode => { tempNode.foo(); } ); <snip> void foo() { foreach(myType bar in listOfmyType) { if (bar.isActive) setNodeActive(); } }
source share