Search code examples
matlabparallel-processingdistributed-computingspmd

MatLab Parallel computing toolbox: using more cores for same task


I have a laptop with 4 physical cores, and the MatLab parallel computing toolbox. I need to perform two independent task (really expensive, let's say to compute the largest eigenvalue of a dense,large, matrix).

So, I want to distribute the tasks to my core in the following way:

  • 2 cores on the first task
  • 2 cores on the second task

but I really can't understand/find how to set this in a MatLab code.

After searching a lot, I've seen I should use spmd, but I can't find in the documentation a proper example that allows me to use 2 cores for the same task.

Any minimal working example in MatLab would be really appreciated!

EDIT after Daniel's comment: After creating a parallel pool of 4, workers, I could do:

 spmd
     if labindex == 1 
        %first worker, do something             
     elseif labindex == 2
         %second worker, do sometihng
     end   
 end

EDIT(2)

I can set NumThreads=2, so each worker will do two tasks(right?). The question now is: do I have to create a parpool with 4 workers, so each worker does 2 threads? More explicitely:

parpool(4); %set NumThreads = 2 via Parallel computing toolbox %define matrix A1, A2 of size 1000x1000 parfor i=1:2 x(i) = max(abs(eigs(A(i)))); end

I would like now that the first two cores work on the x(1), while the other two on x(2)


LAST EDIT:

Using a parfor as written in the comments, I'd do:

c = parcluster('local');
A = {rand(2000), rand(2000),rand(2000), rand(2000),rand(2000), 
rand(2000),rand(2000),rand(2000)};
c.NumThreads = 2;
pool = parpool(c, 2); %2 workers
parfor i=1:8
   x(i) = max(abs(eig(A{i})));
end

Solution

  • Following on from the various comments, if you set a cluster object's NumThreads property, then each worker you launch will use that number of computational threads. You can do this through the Cluster Profile Manager, or programmatically.

    When you launch parpool, the number you specify is the number of worker processes you want to launch, and each worker will have a number of threads corresponding to the cluster object's NumThreads property.

    Putting this together, we get:

    % Use the local cluster
    c = parcluster('local');
    % Make 2 'A' matrices
    A = {rand(2000), rand(2000)};
    for numThreads = 1:2
        % Set up cluster NumThreads property for this iteration
        c.NumThreads = numThreads;
        % Build a pool with 2 worker processes
        pool = parpool(c, 2);
        tic
        spmd
            % Each worker operates on a separate element of A
            out = max(abs(eig(A{labindex})));
        end
        t = toc();
        fprintf('Time with NumThreads = %d: %.3f\n', numThreads, t);
        delete(pool);
    end
    

    On my machine, the relevant timings are:

    Time with NumThreads = 1: 4.693
    Time with NumThreads = 2: 3.636