如何将嵌套循环转换为parfor循环 [英] How to convert a nested loop into parfor loop

查看:279
本文介绍了如何将嵌套循环转换为parfor循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的MATLAB脚本。

This is my from my MATLAB script.

function [ Im ] = findBorders( I )


Im = false(size(I));

I = padarray(I, [1, 1], 1);
[h w] = size(Im);

bkgFound = false;
for row = 1 : h
    for col = 1 : w
        if I(row + 1, col + 1)

            bkgFound = false;
            for i = 0:2
                for j = 0:2
                    if ~I(row + i, col + j)
                        Im(row, col) = 1;
                        bkgFound = true;
                        break;
                    end;
                end;

                if bkgFound
                    break;
                end;
            end;
        end;
    end;
end;

end

所以,我需要将其转换为 parfor 循环,以运行到GPU中。

So, I need to convert it to parfor loop, to run into GPU.

我需要帮助。我读了一些文章,但不知道如何转换。

I need help. I read some articles, but have no idea about how to convert this.

推荐答案

在MATLAB中, parfor 不允许事物在GPU上运行。通过MATLAB与GPU交互的最佳方法是将数据转换为 gpuArray ,然后针对该数据针对GPU优化的所有操作都将在那里进行优化。

In MATLAB, parfor doesn't allow for things to run on the GPU. The best way to interface with the GPU through MATLAB is to convert your data to a gpuArray and then all operations performed on that data that are optimized for the GPU will be optimized there.

正如@Daniel所说,您发布的代码1)对于任何类型的并行处理都不理想,并且2)可能仅通过向量化才能加速。

As @Daniel stated, the code that you have posted 1) is not ideal for any sort of parallel processing and 2) could likely be sped up only through vectorization.

我不确定您要做什么,但似乎您正在寻找图像中被非背景包围的像素。为此,我通常将2D卷积与邻域内核一起使用,以找出一个像素有多少个给定值的邻居。

I'm not entirely sure what you're trying to do, but it seems like you're trying to find pixels within an image that are surrounded by "not-background". For this I would usually use 2D convolution with a neighborhood kernel to figure out how many neighbors of a given value a pixel has.

例如,以下代码定位任何像素这本身就是 false 并完全被 false 值包围(假设您输入的图像是逻辑

For example, the following code locates any pixel which is itself false and completely surrounded by false values (assuming your input image is a logical)

I = [...
    1 1 1 1 0;
    1 0 0 0 0;
    0 0 0 0 0;
    0 0 0 0 0;
    0 0 0 1 1;
    0 0 0 1 0;
];

surrounded_by_zeros = conv2(double(I), ones(3), 'same') == 0

surrounded_by_zeros =

    0 0 0 0 0
    0 0 0 0 0
    0 0 1 1 1
    1 1 0 0 0
    1 1 0 0 0
    1 1 0 0 0

我个人很喜欢这种解决方案,但是如果您拥有图像处理工具箱,也可以使用 imerode imdilate 基本上可以做同样的事情

I personally like this solution, but if you have the Image Processing Toolbox, you can also use imerode or imdilate to basically do the same thing.

surrounded_by_zeros = ~imdilate(I, ones(3));
surrounded_by_zeros = imerode(~I, ones(3));

如果出于某些原因您确实需要将此计算移至GPU(您不需要),您可以将其转换为 gpuArray 然后执行相同的操作,它将在后台使用GPU

If for some reason you really needed to move this calculation to the GPU (you don't), you could cast this as a gpuArray and then perform the same operation and it would use the GPU behind the scenes

I = gpuArray(I);
surrounded_by_zeros_on_gpu = conv2(double(I), ones(3), 'same') == 0;

请记住,这有复制 I 移到GPU上,对于足够大的图像,这可能会严重影响性能。

Keep in mind that this has the overhead of copying I over to the GPU which for large enough images can be a significant performance hit.

这篇关于如何将嵌套循环转换为parfor循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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