Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: function makes 4 recursive calls. I have a 4-core processor. Can I parallelize?

I have a 4-core processor and have a recursive Matlab function which makes four recursive calls:

function J = do_stuff(I)

if some_condition(I)
    J = blah_blah(I);
else
    [I1,I2,I3,I4] = split4(I);

    J1 = do_stuff(I1);
    J2 = do_stuff(I2);
    J3 = do_stuff(I3);
    J4 = do_stuff(I4);

    J = join4(J1,J2,J3,J4);
end

Is there a way for me to assign do_stuff(I1) to core 1, do_stuff(I2) to core 2, and so on up to core 4?

like image 693
SamH Avatar asked Jan 23 '23 04:01

SamH


2 Answers

There is no way to do this in basic Matlab, but the Parallel Computing Toolbox provides this (and other) functionality. You would create an array [I1,I2,I3,I4], then use parallel map to map do_suff across that array.

like image 135
Barry Wark Avatar answered Apr 07 '23 11:04

Barry Wark


There is no way to assign a computation directly to a processor in MATLAB. There are a few different ways to make use of more processors, though.

First, there are a number of operations that are implicitly mulitthreaded in MATLAB. If blah_blah() relies heavily on linear algebra or element-wise computations like sin or log, the work will generally get spread over your 4 cores by the operating system. That functionality just comes with MATLAB.

The Parallel Computing Toolbox allows more access to explicit multiprocessing. Typically parfor is used to operate over different independent data segments. There are also other tools that allow you to write one piece of code that operates over different pieces of data assigned to different computational workers.

like image 23
Todd Avatar answered Apr 07 '23 12:04

Todd