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
Following on from the various comments, if you set a cluster object's
NumThreadsproperty, 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'sNumThreadsproperty.Putting this together, we get:
On my machine, the relevant timings are: