views:

411

answers:

4

Hi, 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?

+6  A: 

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.

Barry Wark
+2  A: 

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.

Todd
A: 

Hi

As the other respondents suggest the Parallel Computing Toolbox provides the functionality you need. But before you go ahead and buy it and implement your function, give some thought to what you want to happen on the first level of recursive calls. As it stands, your pseudo-code will create 4 more calls for each of the original 4. That may or may not be what you want to happen.

Personally, I think I'd approach this not with parallel_map in mind, but the inbuilt job scheduler and createTask facility.

Regards

Mark

High Performance Mark
A: 

If you don't want to pay for the Parallel Computing Toolbox, you might want to take a look at this multicore package. Depending on what you're working on and how much data you have, this may work for you.

Just be careful of the settings you use and how you set up your data because it is very easy to lose any benefits of parallelization by the intermediate loads and saves the package makes.

Hope this helps.

Shaka