Matlab中的并行处理 [英] Parallel processing in Matlab

查看:816
本文介绍了Matlab中的并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个功能:生成数据和处理数据.数据处理非常耗时,因此我想在并行线程中对其进行处理.但是我有一些问题.首先,这是我的程序:

I created two functions: generating data and processing data. Data processing is time-consuming, so I want to process them in a parallel thread. But I have some problems with them. At first, here is 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

返回的日志:

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.

  1. Values which it is returning by generate_data are the same for both thread. I should be different. Threads should process different data, rather than same data twice. I cannot generate the entire data set at once and use getLocalPart.

变量结果不是双精度的1x10矩阵,而是1x2的1x2矩阵 复合材料.我读了有关(co)分布式数组的信息,但没有帮助 我.我应该怎么做才能收到1x10的双精度矩阵?

Variable result isn't a 1x10 matrix of doubles, but 1x2 matrix of composites. I read about (co)distributed array but it didn't help me. What I should do to receive the 1x10 matrix of doubles?

完成后,我对CPU1的处理应该处理CPU2的数据 处理自己的数据?通常我不知道该怎么做.

What I should do to CPU1 processes CPU2's data, when finishes to process own data? Generally I don't have any idea how to do this.

是否可以删除实验1"和实验2"?他们搞砸了 我的日志:)

It is possible to remove "Lab 1:" and "Lab 2:"? They are messing my log :)

考虑到上述情况,日志(对于较大的数据集)应如下所示:

Taking into consideration above, log (for the larger data set) should look this way:

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.

推荐答案

为什么不使用简单得多的parfor?目前,您正在每个工作程序上运行循环,我假设您想并行运行循环的迭代.

Why don't you use the much simpler parfor? At the moment you're running the loop on each workers, and I assume that you wanted to run the iterations of the loop in parallel.

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

这篇关于Matlab中的并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆