olamundo
olamundo

Reputation: 24991

parfor not printing

I am running a small script with a parfor loop. The script starts with the followin line:

parfor i=1:length(vX)
    fprintf('%d/%d\n',i,length(X));
    ...

So apparently I should see printouts right away. When I run it with a pool of two workers, opened by matlabpool(2), I see no printout. When I close the workers, keeping the parfor loop, I see printouts but only when I hit ctrl-c. When I change the parfor into a regular for I see the output. Note that I never saw the loop run to completion as it is quite long, but the printout is the second line the script and should happen immediately, unless there is some buffer-flushing issue I am not aware of in matlab. What is happening??

Upvotes: 4

Views: 2745

Answers (2)

Prithviraj Prabhu
Prithviraj Prabhu

Reputation: 93

I had the same problem and tried for months to find a solution. But in the end I just resorted to using

disp(['text ',num2str(variable));

Upvotes: 0

slayton
slayton

Reputation: 20309

I suspect that the delay is due to the overhead of setting up a parfor loop.

In my experience it can take several, up to 15, seconds just to initialize the loop. This is particularly true when the code in my a loop uses variables with large memory footprints, as the variables must be copied to the workers.

The copying of data between workers is done over the network and a basic network activity monitor should reveal this activity. I find it useful to determine how much of my parfor execution time is getting eaten up initializing the workers.

I use fprintf all the time in my parfor loops; however, I try to be creative about the generated output as the loops iterations are not sequentially.

If all you are looking for is a progress indicator check out: http://www.mathworks.com/matlabcentral/fileexchange/32101-progress-monitor-progress-bar-that-works-with-parfor

Upvotes: 3

Related Questions