0

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
andereBen
  • 155
  • 5
  • 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 May 11 '20 at 15:41
  • @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 May 11 '20 at 15:43
  • `spmd` is single program, multiple datasets. Its designed so each worker runs 1 same program but with a different inptut each. 2 workers collaborating will necesarily not have the same program, AFAIK. Maybe I am worng. – Ander Biguri May 11 '20 at 15:44
  • 1
    Ignoring the 2 cores in the first place, are you able to distribute your code on 2 workers using spmd? – Daniel May 11 '20 at 15:47
  • 1
    @Daniel Yes, I've edited my answer with what I can do – andereBen May 11 '20 at 15:50
  • @Daniel that said, I can't understand how this could help. I'm probably missing something here... – andereBen May 11 '20 at 15:58
  • Try setting the NumThreads property https://mathworks.com/help/parallel-computing/parallel.cluster.html Assuming the functions you are using can be run on multiple cores, matlab will use them. – Daniel May 11 '20 at 15:58
  • 1
    @Daniel I just edited my post with your suggestion. Do you think I'm missing something? – andereBen May 11 '20 at 16:31
  • Exactly what I would do. Then I would check in the task manager if it actually uses multiple cores. – Daniel May 11 '20 at 18:58

1 Answers1

2

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
Edric
  • 23,676
  • 2
  • 38
  • 40
  • Thanks! I have two questions. First of all, since I have 4 physical cores, and in your code you're using *2 workers*, then is it correct to say that each worker is "made by" 2 cores? – andereBen May 12 '20 at 08:05
  • Also, I'm wondering how to do in the following scenario: let's say I have 8 matrices (and again 4 cores). Since `labindex` assume just two values, in the `spmd` I will compute just two of the spectral radius. In this case there is no way to achieve the task using `spmd`, so I should use a `parfor`, right? – andereBen May 12 '20 at 08:11
  • Yes, when the cluster `NumThreads` is 2, then each worker process has 2 threads each. In the case where you have more matrices than cores, `parfor` is probably a better bet. (And you might well find that you're better off with `NumThreads` == 1 - worth testing) – Edric May 12 '20 at 08:55
  • 1
    Many thanks, I learned a lot. Just one last thing: I added an edit to my question regarding the computation with 8 matrices. I wrote the `parfor` loop, could you please confirm if that is right? I mean, in this way I am using 2 cores to compute every spectral radius of every matrix, right? – andereBen May 12 '20 at 09:47
  • Yes, that's right, 2 worker processes each with 2 computational threads. – Edric May 12 '20 at 16:36
  • It's almost everything clear. Just to be sure: with "thread" in this case you mean **the computation of a spectral radius**, right? So, for instance, at a generic iteration `i`, the first worker (made by 2 cores), uses these two threads to compute the spectral radius of a matrix. – andereBen May 13 '20 at 11:34
  • Yes, in my example, the `eig` computation uses 2 threads behind the scenes. In MATLAB, this is called "implicit multithreading" - where a single MATLAB operation transparently uses multiple threads without you having to do anything in your code. https://www.mathworks.com/discovery/matlab-multicore.html – Edric May 14 '20 at 14:25