Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two questions about paralel.for
I got to explain them by code

C#
points[x,y];  // a 2D int array
maxX=100;maxY=250;
paralel.for (0,maxX,x =>{
//.. do things to update points(x,y)
   for(int y=0,y<maxY,y++)
   {
     if (points [x,y]==0) {points[x,y]=10; // this would run quick }
                                           // but calculation can be complexer too
    }
}


In above a paralel.for is accessing an array that lives outside the loop.
I it is 100% sure that the paralel.for loop will do all points in a some random order, ea never will the same point be in use allready. However since the variable was existing before the paralel.for i wonder if it is problematic, maybe causing threads waiting on eachother. I had read something about atomic writes, but not sure if that is a problem here. So i wonder if above code should be written differently ea a beter way to include variables that lives outside its scope.


Another question i have about paralel.for

in above code after the paralel.for a 'normal' for loop for Y happens.
Although i'm still a bit troubled about passing trough variables (ea atomic and treath locks).. I think with proper code it could have nested paralel.for
But, in that it would create 100x 250 "tasks" to do. Is dat fine for a computer or isnt that a good idea. I saw some implementation where people used the number of cores. But the inner calculation in those loops might not require an equal time, and in that case using the available core numbers, could left one CPU calculate a lot longer then the others and cause slow code. So i wonder whats best ?
Posted
Comments
E.F. Nijboer 21-Oct-15 7:18am    
Why not try to run your code first?
Philippe Mori 21-Oct-15 12:56pm    
It is up to you to ensure that each branch are independant... In this case, the loop should run faster because of parallel loop.

In that case, you would have 100 "tasks" and each core will process one at a time until all of them are processed.

If calculation are complex and uses x and y that are not the one of the current item, you will have to analyse dependencies and ensure that the code is adequate.

Essentially, you can assumes that each task are executed independantly and should not depends on the result of other tasks.

1 solution

Use AsParallel instead of Parallel.For because it is much less safe. The result array is not thread safe. Using AsParallel you can aggregate the result safely.

https://msdn.microsoft.com/en-us/library/dd460697%28v=vs.110%29.aspx[^]

Good luck!
 
Share this answer
 
Comments
Philippe Mori 21-Oct-15 12:48pm    
Since the array is already allocated and computation is only based on the current point, I don't see what could be the problem...
E.F. Nijboer 21-Oct-15 16:42pm    
Well, in this case we might assume there won't be a problem. Using the Parallel.For is threading relying on side-effects for writing back values in a shared object. Instead of focussing on the actual task, it must also calculate/determine indexes to where it can write results. Also, if a more complex example would calculate a faulty index, it could overwrite or be overwritten and it would be much harder to find where it went wrong. It is very easy to complicate parallel code this way. Why not split it into separate pieces operating on their own data and returning their results when done? Just keeping the implementation clean without more effort. Anyone having to read your code later will thank you because they don't have to inspect the code on what it is doing and if it does/doesn't violate thread safety. It is much harder to understand for people other than the one who wrote it. So you're right not seeing any problems for now, but because it violates basic threading rules they will be coming soon... and others will have a hard time NOT seeing NO problems.
PGT 22-Oct-15 2:38am    
Well the math never uses the same point[] data in another threat, that i am sure.
Its only that i wonder since its an array if each threat would lock the whole array to write its result too, or only lock the specific array element.
E.F. Nijboer 22-Oct-15 6:26am    
In this case the same point[] data but there is no guarantee. The code can easily change in such a way that they will be overwritten. There is simply no protection. When using AsParallel in the way it is intended, each thread is working with its own data and producing its own resultset. Never any worries and everybody understands what is happening. Relying on side effects is mostly bad, especialy when it is not needed. By the way, side effect means that you have a method that doesn't produce a result by returning it, but updating something it has access to (more like a global variable). Anyone having access to this global variable can update it whenever they like. It easily becomes harder to juggle the possible points where this global thing can be updated. Just something to keep in mind.
PGT 22-Oct-15 10:56am    
Well i use this in a vertical filter on 3d depth data for Robotic control.
points[] only exist in the filter area of the code, not globally outside

Its inside an x row calculations and its not allowed to be on x-n or x+n, strictly only the current x. As to compare x,y against x-n,y or x+n, y strict horizontal filters are used.

The whole routine would never be called to fast, of which i can be sure since the robot that i control simply cant move that fast. A next robotic task comes after 2400ms. Cuyrrently I am fast enough (my math takes 120ms maximum). My math wouldnt work that fast without parallelism, I depend a lot on parallel code; and preferably i'd love to make it even faster. Because then i could make the behaviour of the robot smarter with some extra math.
There for i wonder this.

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