2

On my laptop, I have MatLab and also the Parallel Toolbox, and I want to perform the following task (I have 12 physical cores): compute the largest eigenvalue of 6 different (large) matrices using a parfor loop

I have two possibilities:

  • 2 cores work for the first matrix (A1), 2 cores for the second matrix (A2),..., 2 cores for the sixth matrix (A6).

  • 6 cores for the first matrix (A1), 6 cores for the second matrix (A2). Then I have another iteration of the loop and use 6 cores for the third (A3) and six for (A4), and with another iteration I can compute the spectral radius also for the last two matrices.


Let's focus on the first approach: with the parallel toolbox, how can I impose that "the first two cores work on the first matrix A1" and then "the third and fourth work on A2", and so on?

Here's what I thought: I would specify (in the toolbox), NumWorkers = 6 and NumThreads = 2, as done in the following image:

and then I would write the following parfor:

%initialize six large random matrices A(1),...,A(6)
x = zeros(6,1);
parfor i=1:6
    x(i) = max(abs(eig(A(i))));
end

I would like that now, for i=1, my first two cores work on the the computation of x(1), and so on. Is it the right way?

andereBen
  • 155
  • 5
  • I think that the size of your `A` matrices determines which approach is better; You need to distribute the matrices to the workers in either case. The question is whether all six, including the `eig()` operation, fit into memory simultaneously or not. Also, if your matrices aren't that big, six cores might not reach full operating power before the operation is finished, thus making it faster to use two cores per matrix. – Adriaan May 11 '20 at 07:56
  • @Adriaan A is a 2000x2000 matrix. You're right! Indeed, I want to do simulations to discover this thing! :) My question is, precisely, if what I wrote in the toolbox does the job I am thinking. Do you think it is? – andereBen May 11 '20 at 08:01
  • [This question of mine](https://stackoverflow.com/q/32095552/5211833) and answers thereon might give some insight on this issue. – Adriaan May 11 '20 at 08:25
  • @Adriaan From what I've seen, I should use `spmd` , because I could say "for each matrix, use 2 cores", right? – andereBen May 11 '20 at 09:04
  • I have almost no experience with `spmd`, sorry. I just chuck everything into a `parfor` loop usually. – Adriaan May 11 '20 at 09:04
  • No worries, but regarding my question: is it possible to do (**just with parfor**) the thing I wrote on my question? – andereBen May 11 '20 at 09:29
  • I think that the hyperthreading is automatically used when possible. – Mendi Barel May 11 '20 at 12:39
  • @MendiBarel No, that's incorrect. MATLAB doesn't use hyperthreading. See e.g. [this answer](https://stackoverflow.com/a/18892638/5211833) by the author of the parallel toolbox. **multi**threading is used on al *physical* cores, **hyper**threading, i.e. using *logical* cores, isn't used. Besides, this question isn't about whether hyper/multithreading is used, rather how to select and distribute data to workers. – Adriaan May 12 '20 at 09:06
  • @Adriaan Thanks! I have one last question: in Matlab, since threads can run concurrently only if they are on physical cores, what happens if I use more threads for a worker? Let me clarify: let's say I have two physical cores and so I use 2 workers, and I run 2 threads for each worker (via PTB). In each worker, the two threads will not run concurrently, right? They have to "wait" for each other, right? – andereBen May 13 '20 at 17:27
  • @andereBen what you say makes sense to me, but I'm no hardware or parallel computing specialist, sorry. It sounds like you should be able to test this by trying to nest a `spmd` block inside another `spmd`, and letting them write to file or terminal with a timestamp. – Adriaan May 14 '20 at 07:50

0 Answers0