0
votes

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
1
What you are saying is quite advanced parallel code. Making some cores collaborate while others don't is non trivial. You will likely need targeted messaging and signal passing. Perhaps you can do this with an elaborate parfor.Ander Biguri
@AnderBiguri afaik, this should be possible with spmd command. Do you have any idea about how to do that with a parfor? It would be great!andereBen
Ignoring the 2 cores in the first place, are you able to distribute your code on 2 workers using spmd?Daniel
@Daniel Yes, I've edited my answer with what I can doandereBen
@Daniel I just edited my post with your suggestion. Do you think I'm missing something?andereBen

1 Answers

2
votes

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