Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use a parallel.foreach. However, I noticed that when I set a count to see the iteration, I noticed that the amount of times that it iterates through is less than the actual amount of iterations.

Here is my code:

Parallel.ForEach(dt.AsEnumerable,
       Sub(f)
           countI += 1
       End Sub)


count of dt = 154563
count of parallel.foreach = 154510

can someone please help me with this. I need to iterate through all of them through parallel foreach.

What I have tried:

Tried to change it to a parallel.for but this made the count 1/3 instead which is worse.
Posted
Updated 28-Dec-19 19:40pm

The problem is that when you try to parallelise "basic code" you have to be carefull about what you are doing.
C#
x += 1
is "syntactic sugar", it's actually this:
C#
x = x + 1
Which in "machine terms" is a short sequence of discrete operations:
1) Load "x" into a register.
2) Add one to the register.
3) Load the register into "x".
When you parallelise that sequence, you get multiple tasks running it as close to simultaneously as possible, and because you don't have 154,563 separate cores in your processor that means that they don't all execute the same steps at the same time. So if you have one core that has just loaded "x" into a register, and a second core that is just about to load a new value into it, then the first task is going to overwrite the second tasks revised value with it's new one - but since they both loaded the same value the new value ends up incremented once only!

This gets complicated, and there are ways to avoid it, but they effectively "force" all the parallel task to execute sequentially because the whole of the method you wrote has to be treated as a single discrete operation and that loses any advantage of parallel operation! (In fact, it'll actually slow the whole thing down considerably, as each thread needs it's own memory space, stack, thread controller, and will require a significant amount of task switching work by the operating system.
 
Share this answer
 
Comments
Member 11856456 28-Dec-19 22:25pm    
What would you recommend for someone who wants to implement their code across multiple cores to decrease the time of their processing?
OriginalGriff 29-Dec-19 2:09am    
Unless your code is of a "significant size" - and that code is far from that - then don't try to parallelise or multithread it, you will add a larger overhead than you will save, and unless you have enough cores that there will be minimal "waiting" you will not experience significant improvement, and may well slow things down.
Certainly, trying to use 154,563 separate threads to do anything is doomed to fail!

Parallelism and multithreading aren't "magic bullets" which will solve problems automatically: they need very carefully planning and forethought before implementing them.

In this case? Just forget about it - it's not a task that is suitable for parallel operation!
As Griff pointed out parallel.foreach can get very problematic.
An alternative which works well (in my experience) is using Tasks, which is also recommended by Microsoft, see example here: TaskFactory.StartNew Method (System.Threading.Tasks) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900