How do I use save with a parfor loop using Parallel Computing Toolbox?

191 ビュー (過去 30 日間)
I would like to save some variables to .mat files from inside a parfor loop. However I get an error:
ERROR: ??? Error using ==> parallel_function at 598
Error in ==> parallel_function>make_general_channel/channel_general at 894
Transparency violation error.
See Parallel Computing Toolbox documentation about Transparency.

採用された回答

MathWorks Support Team
MathWorks Support Team 2024 年 7 月 3 日
編集済み: MathWorks Support Team 2024 年 7 月 3 日
Transparency is violated by the save command because in general MATLAB cannot determine which variables from the workspace will be saved to a file.
Pre-R2024a
The solution is to move the call to save to a separate function and to call that function from inside the parfor loop. Pass any variables that are to be saved as arguments to the function. That way MATLAB can determine which ones will be saved and transparency is not violated.
For example:
Save the following as "parsave.m":
function parsave(fname, x,y)
save(fname, 'x', 'y')
end
Then run it with:
parfor ii = 1:4
x = rand(10,10);
y = ones(1,3);
parsave(sprintf('output%d.mat', ii), x, y);
end
R2024a Onwards
MATLAB now supports calling save within a parfor loop when the -fromstruct option is used. 
The following documentation example demonstrates how to collect your variables into a struct and then save inside the parfor loop.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 4 月 30 日
編集済み: MathWorks Support Team 2021 年 4 月 19 日
Notice Alex Shaw's discussion, below

サインインしてコメントする。

その他の回答 (1 件)

Daniel
Daniel 2016 年 3 月 14 日
A nice alternative to implement your own save function is using the built-in matfile function:
parfor ii = 1:4
m=matfile(sprintf('output%d.mat', ii),'writable',true)
x = rand(10,10);
y = ones(1,3);
m.x=x;
m.y=y;
end
Here the parser has no issue understanding which variables are used.

カテゴリ

Help Center および File ExchangeParallel Computing Toolbox についてさらに検索

タグ

製品


リリース

R2010a

Community Treasure Hunt

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

Start Hunting!

Translated by