在 matlab 中对子矩阵使用 parfor

发布于 2024-12-14 08:21:03 字数 488 浏览 1 评论 0 原文

我有一段代码,将图像矩阵 img 分成小块并并行处理它们。但是Matlab说不能使用parfor循环,因为outC{i,j}的方式是索引的。我该如何解决这个问题?

子矩阵的大小不同。如果 img=[4x7],那么

C=[3x3 3x3 3x1;
   1x3 1x3 1x1]

顺便说一句,我不确定这里使用元胞数组是否是一个好主意。如果没有,请随时就如何划分 img 提出建议。

C=mat2cell(img, rowSplit, colSplit);
[rowc,colc]=size(C);
outC=cell(rowc,colc);
parfor i=1:rowc
    for j=1:colc
       outC{i,j}=doWork(C{i,j}); 
    end
end

I have a piece of code that divides a image matrix img into small piece and work on them parallel. But Matlab says parfor loop cannot be used, because the way outC{i,j} is indexed. How do I fix this?

The sub matrices are of different size. If img=[4x7], then

C=[3x3 3x3 3x1;
   1x3 1x3 1x1]

On a side note, I'm not sure if using cell array is a good idea here. If not, feel free to give suggestion on how to divide up the img as well.

C=mat2cell(img, rowSplit, colSplit);
[rowc,colc]=size(C);
outC=cell(rowc,colc);
parfor i=1:rowc
    for j=1:colc
       outC{i,j}=doWork(C{i,j}); 
    end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情丝乱 2024-12-21 08:21:03

您可以对输入和输出使用线性索引。

首先,我假装输入您的形状,以及一个简单的 doWork 函数:

>> C= {rand(3) rand(3) rand(3,1); rand(1,3) rand(1,3) rand(1)};
>> C
C = 
    [3x3 double]    [3x3 double]    [3x1 double]
    [1x3 double]    [1x3 double]    [    0.3922]
>> doWork = @(x)2*x;

然后使用线性索引:

>> outC=cell(size(C));
>> parfor ci=1:numel(C)
     outC{ci} = doWork(C{ci});
   end

快速检查它是否有效:

>> outC{2,1}./C{2,1}
ans =
     2     2     2

You can use linear indexing for both the input and output.

First I make a pretend input of your shape, and a simple doWork function:

>> C= {rand(3) rand(3) rand(3,1); rand(1,3) rand(1,3) rand(1)};
>> C
C = 
    [3x3 double]    [3x3 double]    [3x1 double]
    [1x3 double]    [1x3 double]    [    0.3922]
>> doWork = @(x)2*x;

Then use linear indexing:

>> outC=cell(size(C));
>> parfor ci=1:numel(C)
     outC{ci} = doWork(C{ci});
   end

A quick check that it's worked:

>> outC{2,1}./C{2,1}
ans =
     2     2     2
开始看清了 2024-12-21 08:21:03

尽管这里有很多答案将有助于使用 parfor 块来完成您需要的操作,但我认为更好的解决方案可能是使用 spmd 块代替。

不要将图像分割成元胞数组中的较小部分,而是考虑将其转换为 分布式数组,可能分布在与您当前正在做的相同的部分。在 spmd 块中,您可以执行 doWork 函数,它将应用于该工作线程上存在的(共同)分布式数组的任何部分。最后,您可以汇总结果并将其收集返回给客户端。

我发现 spmdparfor 更难掌握,但一旦你掌握了它的想法,它就会非常强大;我认为这个例子可以非常方便地以这种形式表达。

Although there are many answers here that will help in using a parfor block to do what you need, I would think that an even better solution might be to use an spmd block instead.

Rather than splitting your image up into smaller portions within a cell array, consider instead converting it into a distributed array, perhaps distributing in the same portions as you're currently doing. Within the spmd block, you can execute your doWork function, and it will be applied to whatever portion of the (co-)distributed array is present on that worker. Finally you can assemble the results and gather them back to the client.

I find spmd more complex to grasp than parfor, but it's very powerful once you've got the idea of it; and I would think this example might be very conveniently expressed in that form.

陌上青苔 2024-12-21 08:21:03

只需构建一个输出向量,然后使用reshape将其变成一个矩阵。

Just build a vector of outputs, and use reshape afterwards to make it into a matrix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文