Matlab中的并行处理

时间:2013-01-22 14:04:57

标签: matlab parallel-processing distributed

我创建了两个函数:生成数据和处理数据。数据处理非常耗时,因此我想在并行线程中处理它们。但是我遇到了一些问题。首先,这是我的计划:

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

并记录它返回的内容:

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.

有问题,我有:

  1. generate_data返回的值与之相同 两个线程。我应该是不同的。线程应该处理不同 数据,而不是两次相同的数据。我无法生成整个数据 立即设置并使用getLocalPart。

  2. 变量结果不是1x10的双精度矩阵,而是1x2矩阵的 复合材料。我读了(共)分布式阵列,但没有帮助 我。我应该怎样做才能获得1x10的双打矩阵?

  3. 完成后,我应该对CPU1处理CPU2的数据做些什么 处理自己的数据?一般来说,我不知道该怎么做。

  4. 可以删除“实验1:”和“实验2:”吗?他们搞乱了 我的日志:)

  5. 考虑到上述情况,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.
    

1 个答案:

答案 0 :(得分:10)

为什么不使用更简单的parfor?目前你在每个worker上运行循环,我假设你想并行运行循环的迭代。

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