Critical code section inside parfor
显示 更早的评论
With C++ one can define critical code section inside a parallel for.
Is there a similar construct that can be used with Matlab?
e.g. suppose that a shared variabe has to be infrequently updated, but must be shared.
With C/C++ we might use a typical construct such as this:
#pragma omp for
for (...) {
// Do something with elements indexed by loop control variable
// and use some shared resource in calculations
if (update_required) {
#pragma omp critical
// Critical block can only be entered by 1 thread at a time
{
// update shared resource
}
}
}
For instance, insert a new key-value pair into a shared hashtable inside the loop; a rare event, but nevertheless the update must be safely shared by all executing threads.
采纳的回答
更多回答(1 个)
Edric Ellis
2021-2-18
Further to Walter's response, the closest analogy in parfor is a reduction variable. These may appear to be updated by multiple loop iterations concurrently, but the parfor machinery (and language constraints) ensure that the result is computed deterministically with no race conditions. I.e. the following is supported, and completely deterministic.
list = [];
parfor i = 1:100
list = [list, i];
end
7 个评论
Shlomo Geva
2021-2-20
编辑:Shlomo Geva
2021-2-20
Shlomo Geva
2021-2-20
编辑:Shlomo Geva
2021-2-20
Walter Roberson
2021-2-20
编辑:Walter Roberson
2021-2-20
You should consider using SPMD instead of parfor. spmd can send data between threads, including query to find out if information is queued. It uses MPI2 underneath if I recall correctly. Maybe MPI3 these days, not sure.
There is evidence that it does not use OMP underneath: namely that MATLAB Compiler, which can handle Parallel Processing Toolbox, does not require an OMP compliant compiler, and has been supported on compilers that do not support omp.
Shlomo Geva
2021-2-20
Edric Ellis
2021-2-22
As Walter points out, the spmd communication methods labSend and labReceive etc. are MPI based. These are two-sided communication primitives - this means that both parties in any communication need to cooperate to get messages delivered. This can be challenging for what sounds like a less structured communication pattern (i.e. the updates can be required at unpredictable points in your algorithm). Normally, in spmd, the general pattern is each worker performs a bunch of work purely locally, then all workers together collaborate using communication.
Walter Roberson
2021-2-22
A valid spmd pattern is
otherlabs = setdiff(1:numlabs,labIndex);
while true
while labProbe
newkv = labRecieve;
update local hash using newkv{1} key and newkv{2} value
end
compute the next thing
if the computation required a new hash key
labSend({newkey, newvalue}, otherlabs)
end
end
This would have to be enhanced to handle shutting down labs gracefully when end of calculating is reached. For example instead of sending a cell a lab could send its own lab index with a tag marking a shut down. The labSend would first labProbe checking for that tag and if it finds it, labRecieve the tag and remove the received lab number from the list otherlabs before going ahead with the labSend to update the others.
Edric Ellis
2021-2-22
I would not recommend that pattern, as it can lead to deadlock. labSend does not necessarily complete until the corresponding labReceive has started. Therefore, you can easily end up with all workers stuck trying to call labSend simultaneously (e.g. if they all wish to send on the first iteration). In practice, small messages get sent immediately by labSend using underlying buffering, but it is definitely not a good idea to rely on that. The "safe" version of that pattern unfortunately loses efficiency because it requires a synchronisation point after each "compute the next thing". Something like
spmd
while ~done
compute the next thing
% all-to-all communication to disseminate values
if the computation required a new hash key
toCommunicate = {newkey, newvalue};
else
toCommunicate = {};
end
allNewKeysAndValues = gcat(toCommunicate);
updateCache(allNewKeysAndValues);
done = % some other collective decision
end
end
类别
在 帮助中心 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!