How to send a variable to the worker workspace?

2 views (last 30 days)
An example code is show below:
classdef myclass
properties
end
methods
function obj = myclass
obj.variable1 = dsm.signal.Bias(3);
theClass = class(obj)
assignin('base','theClass',theClass)
end
end
% When loading the class and run by using parsim(), the system can't find
% theClass in the worker's workspace.
% My question is: how to send "theClass" into the parallel worker's
% workspace?
  1 Comment
Rui Zhang
Rui Zhang on 27 Aug 2024
When the parallel runs with one worker, the system can find "theClass" in the worker's workspace and there is no error in the simulation.
When the paralle runs with 2 workers, the system can't find "theClass" in the worker;s workspace when simulation starts. An error came.

Sign in to comment.

Answers (1)

Jacob Mathew
Jacob Mathew on 29 Aug 2024
Hey Rui,
To send the “myclass” class that you defined, to the worker’s workspace, you can use the “addAttachedFiles” function. The documentation for the function can be found at the following link:
Here is an example of using the function with a pool of 3 workers. The “myclass” is the same as you have provided, but with “variable1” added as property of the class. We then create the pool, then add the class file to it and then every worker creates an object of the class:
classdef myclass
properties
variable1
end
methods
function obj = myclass
obj.variable1 = 3; % Your property value here
theClass = class(obj);
assignin('base','theClass',theClass)
end
end
end
% Assuming the class definition is in a file named myclass.m
% Clear and stop any previous pools. Then create a pool of 3 workers
% clear all
% delete(gcp('nocreate'))
pool = parpool(3);
% attach the file to be workers
addAttachedFiles(pool,{'myclass.m'})
% Use the spmd block to execute code on each worker
spmd
% Create an instance of myclass on each worker
obj = myclass;
% Display the variable1 value in the object
disp(['Worker ', num2str(spmdIndex), ' myclass objects value of variable1: ', num2str(obj.variable1)]);
end
We get the output as follows:
>>
% Starting parallel pool (parpool) using the 'Processes' profile
% connected to parallel pool with 3 workers.
%
% Worker 1:
% Worker 1 myclass objects value of variable1: 3
%
% Worker 2:
% Worker 2 myclass objects value of variable1: 3
%
% Worker 3:
% Worker 3 myclass objects value of variable1: 3
However, if you don’t want to send the class file to the workers and instead want to copy the object between the worker’s worspace, then we can use “parallel.pool.Constant” method. The documentation for the method can be found in the link below:
Using this, you can share copies of the object itself to the worker’s workspace. The following is a code snippet that does this:
% Assuming the class definition is in a file named myclass.m
% Clear and stop any previous pools. Then create a pool of 3 workers
% clear
% delete(gcp('nocreate'))
pool = parpool(3);
% creating an object of the class
theClass = myclass;
% storing the object
distributedObj = parallel.pool.Constant(theClass);
% Use the spmd block to execute code on each worker
spmd
% getting the object
obj = distributedObj.Value;
% Displaying the value of variable1 in object
disp(['Worker ', num2str(spmdIndex), ' accessing myClass variable1: ', num2str(obj.variable1)]);
end
We get the output as follow:
>>
% Starting parallel pool (parpool) using the 'Processes' profile
% Connected to parallel pool with 3 workers.
%
% Worker 1:
% Worker 1 accessing myClass variable1: 3
%
% Worker 2:
% Worker 2 accessing myClass variable1: 3
%
% Worker 3:
% Worker 3 accessing myClass variable1: 3

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!