brown.2179
brown.2179

Reputation: 1880

Programmatically determine if executing inside parfor vs. for loop

When developing parfor loops it is typically best to start off in a serial loop to test with small data set and then add the "par" to scale to large sample set etc. When running serially it is great to get diagnostic information about each iteration of the for loop however when using the parfor loop for huge data set diagnostic information should be turned off.

So the question: is there any way to programmatically detect if executing in a for loop vs. a parfor loop? This way could so something like if ~isParfor; fprintf(...); end

Upvotes: 4

Views: 509

Answers (3)

furt furt
furt furt

Reputation: 291

This is great to use inside a generic function that works in series but not parallel, like plotting and text to speech. Your analysis function will gracefully remain inert when it can't run.

function b = isParfor()
    %returns 1 if in a parfor loop
    b = ~isempty(getCurrentTask());
end

Upvotes: -1

Sam Roberts
Sam Roberts

Reputation: 24127

You can use the undocumented command feature('isdmlworker') to determine if code is being run on a worker. It returns true if run on a worker and false otherwise. Note that although this command has been around for a long time and appears stable, it is in theory undocumented and is subject to change.

Alternatively you can use the documented usejava('desktop'), which returns true if you're running with a MATLAB desktop and false otherwise (which is the case for a worker). This is a pretty good approximation to being a worker, so long as you don't also sometimes run MATLAB itself without a desktop (i.e. start it up with the -nodesktop option).

Note also that if you don't start up a pool of workers, parfor runs locally just like a for loop, so both of these will return false - I guess this is likely to be what you want, but worth noting in any case.

Upvotes: 3

Benoit_11
Benoit_11

Reputation: 13945

Yes you can! Using getCurrentTask tells you whether the operations executing are being done on a worker or not. Check this code for a simple demonstration:

clc
clear all


A1 = zeros(10,10);
A2 = zeros(10,10);

Check1 = zeros(1,10);
Check2 = zeros(1,10);


parfor ix = 1:10
    Check1(ix) = ~isempty(getCurrentTask());
    myTemp = zeros(1,10);

    for jx = 1:10                
        myTemp(jx) = ix + jx;
    end
    A1(ix,:) = myTemp;
end
clear myTemp

for ix = 1:10

   Check2(ix) =  ~isempty(getCurrentTask());

    myTemp = zeros(1,10);


    for jx = 1:10
        myTemp(jx) = ix + jx;
    end
    A2(ix,:) = myTemp;
end

Check1
Check2

Outputs this:

Check1 =

     1     1     1     1     1     1     1     1     1     1

Check2 =

     0     0     0     0     0     0     0     0     0     0

Therefore it would be possible to check only the first entry of the 'Check' variable (i.e. at the first iteration in either case) and deduce whether you are (1) or not (0) in a parfor loop.

Upvotes: 2

Related Questions