Shared memory in parfor genetic algorithm
2 views (last 30 days)
Show older comments
Alexander Kobyzhev
on 11 May 2022
Commented: Alexander Kobyzhev
on 12 May 2022
Hello, community Matlab!
I use gamultiobj with parallelization, since my optimization function takes a lot of time when performing. To accelerate this process, I want to caching repeated data in some array or map, which can be calculated in the optimization function. That is, the functions can come to the input that the data that does not make no sense that it makes no sense to re-consider. All workers must have access to this array, read and write down values.
The problem is that I do not know how to do it, because gamultiobj uses parfor inside himself, in which, as I understand it, not to transmit data between workers. I wanted to use global variables, but they cannot be used in parfor. I would have perfectly suited the implementation of the LabProbe / Labreceive / Labsend, which are unfortunately used in spmd.
Thanks, Alexander.
0 Comments
Accepted Answer
Edric Ellis
on 12 May 2022
If you're using R2022a or later, you could use ValueStore to allow workers to share values. The main requirement here is for you to come up with a way to convert the input arguments of your function into a "key" that can be used with the ValueStore. If that is straightforward, then you might be able to get things to work like this:
if isempty(gcp("nocreate")); parpool("local"); end
parfor ii = 1:10
val = cachingMagic(randi(3));
out(ii) = sum(val, "all");
end
% cachingMagic returns "magic(in)", with caching.
function out = cachingMagic(in)
arguments
in (1,1) double {mustBeInteger}
end
% Because the input is a simple scalar, we can generate a string key very
% easily.
cacheKey = sprintf("magic_%d", in);
% getCurrentValueStore returns empty on the client, so we should guard
% against that
vs = getCurrentValueStore();
isCached = ~isempty(vs) && vs.isKey(cacheKey);
if isCached
fprintf('Cache hit for %s at %s\n', cacheKey, string(datetime));
out = vs(cacheKey);
else
fprintf('Cache miss for %s at %s\n', cacheKey, string(datetime));
% Not in cache, must compute
out = magic(in);
% Introduce an arbitrary delay to simulate slow computation
pause(rand);
% If we have a ValueStore, cache the result
if ~isempty(vs)
vs(cacheKey) = out;
end
end
end
More Answers (1)
Walter Roberson
on 11 May 2022
You can use Parallel Data Queue to send results back from the worker to the controller, and another set to distribute results to the worker. It is a bit of a nuisance, and might not be efficient.
You could also do something like hash the arguments to get an index to use into a memory map. This might be a challenge to do efficiently.
See Also
Categories
Find more on Startup and Shutdown 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!