Parallel Processing in Matlab

I created two functions: data creation and data processing. Data processing takes a lot of time, so I want to process it in a parallel thread. But I have some problems with them. First my program:

result = zeros(1, 10);

matlabpool open local 2
spmd
    for a = 1:5
        data = generate_data();
        display(sprintf('Received data on CPU%d: %d', labindex, data));
        result(end + 1) = process_data(data);
    end
    display(sprintf('All done on CPU%d', labindex));
end
matlabpool close

And the log of what he returned:

Starting matlabpool using the 'local' profile ... connected to 2 workers.
Lab 1: 
  Received data on CPU1: 100
Lab 2: 
  Received data on CPU2: 100
Lab 1: 
  Received data on CPU1: 101
  Received data on CPU1: 102
  Received data on CPU1: 103
  Received data on CPU1: 104
  All done on CPU1
Lab 2: 
  Received data on CPU2: 101
  Received data on CPU2: 102
  Received data on CPU2: 103
  Received data on CPU2: 104
  All done on CPU2
Sending a stop signal to all the workers ... stopped.

There is a problem that I have:

  • The values ​​returned by the generate_data function are the same for both threads. I have to be different. Themes should process different data, not the same data twice. I can’t generate all the data set right away and use getLocalPart.

  • The result of the variable is not a 1 × 10 doubles matrix, but a 1x2 matrix is ​​composites. I read about (co) distributed array, but that didn't help me. What should I do to get a 1x10 doubling matrix?

  • CPU1, CPU2, ? , , .

  • " 1:" " 2:"? :)

, log ( ) :

Starting matlabpool using the 'local' profile ... connected to 2 workers.
Received data on CPU1: 100
Received data on CPU2: 101
Received data on CPU1: 102
Received data on CPU1: 103
Received data on CPU1: 104
Received data on CPU1: 105
Received data on CPU2: 106
Received data on CPU1: 107
Received data on CPU1: 108
Received data on CPU2: 109
All done on CPU1
All done on CPU2
Sending a stop signal to all the workers ... stopped.
+5
1

parfor? , , .

nIter = 10;
result = zeros(1, nIter);

matlabpool open local 2

    parfor a = 1:nIter
        data = generate_data();
        fprintf('%s: processing set %i/%i\n',datestr(now),a,nIter)
        result(a) = process_data(data);
    end
end
matlabpool close
+10

All Articles