Error in saving MAT file within a parfor!!!

Hi
I am trying to use a parfor to speed up my loops. Within the loop I have to save('*.mat') and then use it in a function. When I use simple for loop, it works. When it goes through parfor, it returns the following error:
'Unable to write file *.mat: Invalid argument'
It is interesting that it works well on my laptop but not on PC!!! I have an academic license of MATLAB2013a. Besides, when the number of worker is between 1-4 it again works. But, in case of 5-8 workers it does not. It seems there is a problem with parfor.
Could you let me know what to do?!!!

Answers (1)

That error message indicates that you are literally trying to use the filename "*.mat" as the destination file name. You need to use a real file name.
Please show your code for creating the file name and for doing the save.

4 Comments

Walter
My code is as follows:
function1
% calculate some data and save them as SimulinkParameters
save('C:\SimulinkParameters.mat');
end
Then I am going to use SimulinkParameters.mat in another function to run a simulink model.
function2
List = whos('-file','C:\SimulinkParameters.mat');
Model = load_system('C:\mymodel.mdl');
hws = get_param(Model,'modelworkspace');
for i = 1:length(List)
hws.assignin(List(i,1).name,eval(List(i,1).name));
end
sim('C:\mymodel.mdl');
end
This process has to be done using parfor for various input data. Could you let me know if I am doing it right? Why does it work well when the number of workers is 2 or 3, but it does not work in case of 4 or more workers?
Is function1 being called once for every parfor iteration, or just once before the parfor is started?
If it is once for every iteration, then you should be saving to a different file for every worker (or every iteration) to prevent the possibility of simultaneous saves to the file interfering with each other.
This is the correct structure:
parfor i =1:n
data(i) = function(i);
function1(data(i)); % calculates some variables and save them
% as SimulinkParameters.mat in current
%directory
function2; % Calls SimulinkParameters.mat and runs a Model
end
Based on this code, it seems function1 is being called for every parfor iteration. right?
Yes. In a situation like that, where you are using the same filename for each worker, and in the same directory, then there is the possibility that two different workers are going to be trying to save() the file at the same time. As you are using MS Windows, the one that starts second is likely to fail because the file is locked for writing.
At the moment, I do not see that you are using the values stored in the .mat file, other than to build the list of variables to work on, which is something that could just be passed from function1 to function2.
I do see that you eval(List(i,1).name) which is eval() of the name of a variable stored in the .mat file. However, that eval() is not going to load the value from the .mat file: it is going to get the value from the current workspace if it is there and otherwise is going to look for the name as the name of a function to execute. Could I ask you to explain where you expect that the eval() will fetch the variable from?

Sign in to comment.

Categories

Products

Asked:

on 13 Aug 2015

Commented:

on 13 Aug 2015

Community Treasure Hunt

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

Start Hunting!