How to send a variable to the worker workspace?

2 次查看(过去 30 天)
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 个评论
Rui Zhang
Rui Zhang 2024-8-27
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.

请先登录,再进行评论。

回答(1 个)

Jacob Mathew
Jacob Mathew 2024-8-29
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

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by