1

我需要对图像执行某些琐碎的操作(在这种情况下为负数)。有人告诉我,我需要将图像递归地划分为相等的矩形,并使用 MPI 并行处理它们。

我在我的顺序图像处理类中编写了一个用于执行此操作的函数:

void recursive_negative_helper(int x, int y, int WIDTH, int HEIGHT)
{
    /* recurse until dimensions become odd, to avoid corruption of image
      (equal sized rectangles only) */
    if( (WIDTH&1) || (HEIGHT&1) )
    {
        uint8_t color[BPP];
        for(int i=y ; i<y+HEIGHT ; i++)
        {
            for(int j=x ; j<x+WIDTH ; j++)
            {
                    get_pixel(color,i,j);
                    for(int k=0 ; k<BPP ; k++)
                        color[k] = 255-color[k];
                    set_pixel(color,i,j);
            }
        }
        return;
    }
    recursive_negative_helper(x        , y         , WIDTH/2, HEIGHT/2);
    recursive_negative_helper(x+WIDTH/2, y         , WIDTH/2, HEIGHT/2);
    recursive_negative_helper(x        , y+HEIGHT/2, WIDTH/2, HEIGHT/2);
    recursive_negative_helper(x+WIDTH/2, y+HEIGHT/2, WIDTH/2, HEIGHT/2);
}

void recursive_negative()
{
    recursive_negative_helper(0,0,width,height);
}

我是第一次学习 MPI,并且很难弄清楚如何并行执行此操作。我想一种方法是分成 4 份并将每个部分分配给一个过程。这对我的应用程序来说已经足够了!但是我怎样才能为任意数量的进程做到这一点?

任何人都可以帮忙吗?另外,如果可能的话,请发布一些 MPI 伪代码,以便我可以更清楚地看到它!

4

1 回答 1

1

对于您的情况,我想建议使用 OPENMP ( http://openmp.org/wp/ ) 进行并行处理。易于配置和编译以优化简单循环。代码更改也是微不足道的。但我不确定递归处理,所以要小心,或者只使用非递归实现。一些教程和指南:http ://bisqwit.iki.fi/story/howto/openmp/

于 2013-01-27T05:03:38.280 回答