saving within SPMD or parfor

14 ビュー (過去 30 日間)
Mohammad Abouali
Mohammad Abouali 2015 年 5 月 7 日
コメント済み: Thomas Koelen 2015 年 5 月 8 日
Why matlab does not allow to save something to a file when within spmd or parfor?
let's say I have:
parfor i=1:n or spmd(nWorker)
VarA=some calculations
save(filename(i), 'VarA');
end
Matlab gives me an error "SAVE can not be called in an SPMD block. (the same for parfor except it says within parfor)
Does anyone knows why is that?
The funny thing is that if I create a new function like
function savetofile(data,fullfilename)
save(fullfilename,'data');
end
and then use this function, everything works fine. Well, of course, this is not recognized by matlab to flag an error and stop running the code. But I am trying to show that it means that the operation is perfectly natural and ok.
I have so many files, that some operation needs to be done, which each file could be processed completely independently. The output needs to be stored also in separate files. So, I want multiple worker, each opening one file at a time, and then storing their results once they are done, and then moving to next available file to process. But MATLAB somehow thinks it is not allowed to use save command within spmd and parfor and I need to trick it by kinda changing the function name. Why is that matlab thinks save shouldn't be used?
edit: writetable works just fine. It seems somehow it is save command that is not sitting well.

採用された回答

Edric Ellis
Edric Ellis 2015 年 5 月 8 日
The problem with the save command is that it needs to inspect the contents of the workspace in which it is running. That's not allowed inside parfor and spmd. There's more on this constraint in the documentation. As you note, the constraint is about the body of the block itself, not a fundamental constraint on workers calling save or load, and that's because it's an analysis constraint.
  1 件のコメント
Mohammad Abouali
Mohammad Abouali 2015 年 5 月 8 日
Thank you.

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

その他の回答 (1 件)

Thomas Koelen
Thomas Koelen 2015 年 5 月 8 日
編集済み: Thomas Koelen 2015 年 5 月 8 日
Transparency is violated by the SAVE command because in general MATLAB cannot determine which variables from the workspace will be saved to a file.
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.
  2 件のコメント
Mohammad Abouali
Mohammad Abouali 2015 年 5 月 8 日
編集済み: Mohammad Abouali 2015 年 5 月 8 日
Dankjewel
Thomas Koelen
Thomas Koelen 2015 年 5 月 8 日
Geen probleem ;)

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

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by