spmd and persistent variables

2 次查看(过去 30 天)
Nicolas
Nicolas 2015-8-13
Hi all,
I use 'spmd' to call a function 'fun1' in which some persistent variables are declared. The documentation states that 'The body of an spmd statement cannot contain global or persistent variable declarations.'.
1) But is-it robust to call a function that declares persistent variables inside an 'spmd' statement? I am not interested by the value of these persistent variables at the end of the process in the client.
2) In the future, the called function 'fun1' will be a mex-file. Is-there a difference?
Here is an example with no special meaning
spmd
M=fun1(labindex);
end
function y = fun1(x)
persistent y1
y1 = magic(x);
y = y1+10;
end
I think the answer will be the same for a 'parfor' loop.
Thank you.
Nicolas

回答(1 个)

Harsha Medikonda
Harsha Medikonda 2015-8-17
Hi Nicolas,
I understand that you wish to know
a)If persistent variables can be declared in a function that is called from an spmd block
b)If we are replacing the function with a mex file, can we still use the persistent variables in the same way?
For your first question: You can declare persistent variables in a function that is called by the spmd block. Please make sure that you are attaching the file 'fun1.m' using "addAttachedFiles" so that it is accessible by all the workers. For Example:
poolobj = gcp;
addAttachedFiles(poolobj,{'fun1.m'})
spmd
M=fun1(labindex);
end
For your second question: We require "mxArray" datatype for both input and output parameters for a mexFunction(which is the starting point for a mex file). By default, an mxArray is not persistent. To make an mxArray persistent through multiple invocations of the Mex-function, call the "mexMakeArrayPersistent" function.
Please refer to the following documentation links for more information
Harsha

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by