share data between workers while retrieving data from handle class within parpool
10 views (last 30 days)
Show older comments
Hey guys,
following problem.
I have a handle class which is linked to different handle class objects of its own class and of other classes.
So like i have 3 points and each points knows its position and to which points its is connected by an edge, called neighbour vertex in my case.
I now need to refine the edges connecting the points and I want to do this in parralel because there are many of these points and edges. At the same tome I have to relink the new edges and nes points to its neighbour points.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/315756/image.png)
So in this example A,B,C are the old points and D and E are the new points. Additionaly I have to change the edges AB to AD and create a new edge DB, same goes for BC to BE and creating EC.
When doing this in parralel there is the problem the when the points as handle classes are a broacast variable that changes from another worker do not affect them. When worker 1 changes the connection from BC to BE and another worker changes the connection from BA to BD, they dont know from each other. It would results in two different B's with 2 different sets af neighbour points on each worker.
So how is this done in Matlab, especially with handle classes, since I even have problems to link vertices to each other in parrallel (without creating new ones), since somehow the chages are not reflected after parfor correctly to the editor even though I used all of the advices here:
They advise direct indexing, but I dont know which way would be more direct than:
parfor i=1:length(edges)
v1=vertex(edges(i,1));
v2=vertex(edges(i,2));
v1.neighbour_vertex(end+1)=v2;
v2.neighbour_vertex(end+1)=v1;
end
So this code does not work in aprfor, I dont get an error but I just get vertices back with 0 neighbours...
Maybe somebody culd help me eith this or give me a hint
Many thanks in advance
Best regards
0 Comments
Accepted Answer
Edric Ellis
on 16 Jun 2020
To get data out of a parfor loop, you must have either a "sliced output" or a "reduction output". Your code example above has neither of these - v1 and v2 will be analysed as "temporary variables" because you assign a whole new value to them in each iteration of the loop.
I spotted your other question about handle classes in parfor. It's not clear to me which part of your algorithm is the time-consuming piece that you are hoping to speed up with parfor. But in general, you cannot modify instances of handle classes on the worker and have the changes show up back at the client (nor on other workers). So, in general, modifying a graph structure in this way is really not amenable to parallelism.
My general advice would be: use the MATLAB profiler on your code running in serial, and work out where the real time is being spent. See if you can arrange things so that this is divided up into independent pieces of work. That is the simplest way to being able to run stuff in parallel.
0 Comments
More Answers (0)
See Also
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!