Path: news.mathworks.com!not-for-mail
From: Edric M Ellis <eellis@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: parfor: fopen Issues
Date: Wed, 24 Jun 2009 13:15:46 +0100
Organization: The Mathworks, Ltd.
Lines: 54
Message-ID: <ytw1vp9omj1.fsf@uk-eellis-deb4-64.mathworks.co.uk>
References: <h1t155$jbg$1@fred.mathworks.com>
NNTP-Posting-Host: uk-eellis-deb4-64.mathworks.co.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: fred.mathworks.com 1245845746 10852 172.16.27.112 (24 Jun 2009 12:15:46 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 24 Jun 2009 12:15:46 +0000 (UTC)
X-Face: $Ahg}Iylezql"r1WV1Me5&)ng"a4v%D>==KMs-elCfj"o}$bh-VOt7lVXgLWsC?9mZ`mINT
 G6PDvca;nrgs$lfcr0l1ew'N]>nXKl}m|Zpg>,6*gLp~-N0N2*+b.iwv=u>@R$L4SEG&NYUU;lSR@u
 IHphdAy
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)
Cancel-Lock: sha1:P5OpHWg57895jnCpAr5+92+xmug=
Xref: news.mathworks.com comp.soft-sys.matlab:550202


"Mr. CFD" <s2108860@student.rmit.edu.au> writes:

> I have a simulation which runs over &#8216;n&#8217; successive iterations
> using the parfor command. At each iteration, a series of scripts are generated
> using the fopen command; these contain user-defined commands for an external
> program. MATLAB then runs the script file and saves the results. The files are
> named according to the index of the parfor command (e.g parfor ii = 1:20). I
> am using the MATLABPOOL with a local scheduler.
>
> At each iteration the contents of the files are updated i.e. files are never
> deleted the naming index remains according to the state of the parfor command
> and the contents (user-defined script commands) are updated.
>
> On most occasions the simulations run successfully with no issues. However, on
> some rare occasions an error is reported relating the failure of fopen as:
>
> ??? Error using ==> parallel_function at 587
> Error in ==> Main_Flight at 70
> Invalid file identifier.  Use fopen to generate a valid file identifier.
>
> Error in ==> Optz_v2 at 203
>     matlabpool local 4 
>
> The code where MATLAB is pointing to is as:
> script = [mesh_prefix, int2str(ii),'.bat'];
> [fidd] = fopen(script, 'wt');
> fprintf(fidd, '%s','Cscript.exe //NoLogo Airfoil_',num2str(ii),'.vbs | xfoil.exe');
> fclose all

That seems to me as thought it should work. The main risk is that some other
process has the file open for writing - on Windows, you cannot have two
processes writing to the same file simultaneously, and any second call to fopen
will fail. A couple of suggestions:

1. Replace your fopen line with something like this:

[fidd, message] = fopen( script, 'wt' );
if fidd == -1
  error( 'Couldn''t write to %s: %s', script, message );
end

2. Replace your fclose line with something like this:

st = fclose( fidd );
if st ~= 0
  error( 'Error code from close: %d, file: %s', st, script );
end

Hopefully that will help pinpoint which files are failing to open or close, and
possibly even why.

Cheers,

Edric.