matlabdebuggingparallel-processingspmd

Skipping lines of code depending on available function(s)/toolboxes


Context

At work I built a GUI to perform image registration on several microscopy images. On the computer I'm using (i.e. at work) the Parallel Processing Toolbox is installed, so I can take advantage of spmd blocks to distribute the work on the computer's cores.

First I create a codistributed array to split my array into smaller chunks:

spmd

    DistributedCell = codistributed.cell(NumberFrames)
   ...
end

All is well until I run the code on my own laptop, on which the Parallel Processing Toolbox is not installed. Obviously I can't use spmd blocks and codistributed arrays so I wrote non-parallelized code to register the images, which works equally well but takes longer to execute.

The problem

The problem I'm having is that the code in spmd blocks needs to be commented when I'm using my home laptop, otherwise Matlab complains that:

MATLAB cannot determine whether "codistributed"
refers to a function or variable.

So even if I check whether I have the PP toolbox installed (or not) and use a condition to execute different commands depending on whether it is or not as follows:

%// Check for parallel toolbox installed
if ~isempty(ver('parallel'))

  NON-PARALLEL code...

else

  PARALLEL code...

   spmd 
      PROBLEM HERE                            
      DistributedCell = codistributed.cell(NumberFrames)
      ...
   end
end

Matlab won't evaluate the condition of the if statement and will throw the error I mentioned above. This is quite cumbersome since every time I use the code on my laptop I need to comment all the lines generating the error (there are many of them) and vice versa when I'm at work.

Question

Is it possible to avoid commenting the code manually and make Matlab don't bother about codistributed arrays when it is running on a computer on which the PP toolbox is not installed?

Maybe I missed something obvious in the debugging world but I can't get around it.

I'm running on Mac if that can be useful (R2015a at work and R2013a at home).

EDIT

To clarify the situation:

The error pops up before any line of code is executed at all. I guess Matlab parses the whole code before executing any of it, so the only way I can avoid it is to comment it. That does not happen with other functions (in this case the classic undefined function [...] appears) but I can avoid it with the if/else statement. It really is the codistributed term that Matlab does not like.


Solution

  • You need to use

    ver('distcomp')
    

    for your if condition, not ver('parallel'). Very confusing, I know.

    How can you find out what name you need for ver? The best way I know is if you look in the folder matlabroot\toolbox (where matlabroot is your installation folder), you'll see a number of folders, mostly one per toolbox. The name you need for ver is typically the same as the name of the toolbox folder - in this case, it's distcomp.

    Early versions of Parallel Computing Toolbox were named Distributed Computing Toolbox, hence the folder name, which has not been updated.

    Edit:

    You may also, or instead, like to test whether you have a license for PCT, rather than testing whether it's installed, which is what ver tests for.

    You can do this with license('test', 'Distrib_Computing_Toolbox').

    How do you know it's Distrib_Computing_Toolbox that you need? This time, you need to look in your license file (probably something.lic in the folder matlabroot\licenses, and scroll through the products until you find the name of the relevant toolbox. The product names in the license bear no relationship to the product folders used by ver.

    These confusing and differing product names drive me nuts, but there you go.