The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: parfor: keep outputs with same output filename
Replies: 12   Last Post: Aug 1, 2013 2:35 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 18,038
Registered: 12/7/04
Re: parfor: keep outputs with same output filename
Posted: Jul 23, 2012 10:09 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Mike" <> wrote in message
> Hi, dpb
> thank you for your info.
> To be more clear, I simply my question with follows:
> 1) I create a fortran file with following:
> program main
> integer:: i
> open(10,file='inputpar.dat',status='old')
> read(10,*) i
> write(30,*) i
> write(*,*) 'fortran main :', i
> end
> I compile it within the project and have Console2.exe in
> 'D:\temp\Console2\Console2\Debug'.
> 2) in matlab, I have following:
> fid2=fopen('test.dat','a'); % this file should be run parallel'
> matlabpool open local 3
> parfor i=1:100

The workers have no idea that fid2 is a file identifier; they didn't open
the file so they don't have access to it.

> fid1=fopen('inputpar.dat','w'); % be read by Console2.exe
> fprintf(fid1,'%2d\n',i);
> fclose(fid1);
> dos('Console2.exe<inputpar.dat');
> out=importdata('fort.30')
> fprintf(fid2,'%2d\n',out);
> end
> matlabpool close
> s=load('test.dat')
> Then I run this cell (step 2), I have the results:
> Starting matlabpool using the 'local' configuration ... connected to 3
> labs.
> fortran main : 64
> fortran main : 64
> fortran main : 64
> ??? Error using ==> parallel_function at 598
> Error in ==> parallel_function>make_general_channel/channel_general at 894
> Invalid file identifier. Use fopen to generate a valid file identifier.

See above.

Note that the iterations of a PARFOR loop are NOT repeat NOT guaranteed to
be executed in any particular order. Therefore if you were to do this, at
best your output would be in arbitrary order in the file; at worst each
worker may "scribble" over the results another worker wrote to the file at
the same time.

I would either assemble all your data in a _variable_ rather than a file
inside the PARFOR loop [different workers can write to different elements of
a variable simultaneously without a problem] or have each worker write its
output to a separate file and combine them all together _after_ the PARFOR
loop is finished.

Steve Lord
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.