Easy Matlabpool Configuration

Dive in without caring how deep it is.

Reading time: about 1 minute (298 words).

The new parpool option for Matlab 2014b is depreciating matlabpool, merging the ideas of parallelisation locally and remotely in to one. Now, the same code can be easily sent off to a large, remote cluster or ran over a few more cores on your own machine without much hassle. Another useful feature is the fact that idle pools will close down after inactivity, allowing you to be lazy with your implementations, while at the same time giving the possibility of efficiency if you require the pool again inadvertently.

    function pool(num)
    p = gcp('nocreate'); % If no pool, do not create new one.
    if isempty(p)
        %poolsize = 0;
        parpool(num);
    else
        poolsize = p.NumWorkers;
        if poolsize ~= num
            delete(gcp);
            parpool(num);
        end
    end
    end

This function asks for number of pools you wish to be created. If a pool of that size is currently active, nothing is required of the function and your code will continue. Otherwise, the old pool is shut down and a new one is generated. Personally, I'd like a future release to enable user access to set the read only attribute p.NumWorkers, but I guess we all can't get everything we want in life.