spmd and persistent variables

조회 수: 2 (최근 30일)
Nicolas
Nicolas 2015년 8월 13일
답변: Harsha Medikonda 2015년 8월 17일
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