MPI发送struct和recive数组

时间:2016-03-11 09:52:39

标签: c arrays struct mpi

我的程序中存在通信问题。程序发送到进程结构并一起计数搜索功能。我无法正确地从搜索函数接收反馈,该函数返回一个数组。

我的结构:

struct sets{
    int array[R];
};

一些结构初始化

                tab[0].array[0] = 1;
                tab[0].array[1] = 2;
                tab[0].array[2] = 3;

发送信息:

    obl = calloc(q, sizeof(int));
    MPI_Scatter(tab, q, MPI_INT, obl, q, MPI_INT, 0, MPI_COMM_WORLD);
    counter = search(tab, setX);

并且发送并发送:

if(rank == 0)
    {
        for(i=1; i < p; i++)
        {
            //edit
            MPI_Recv(counter+i, 1, MPI_INT, i, 1, MPI_COMM_WORLD, &status);
        }

        int k;

        for(k=0; k<N; k++)
        {

            if(*(counter+k) == 5)
            {
                printf("Set number %d is the same \n", k);
            }
            if(*(counter+k) == 1)
            {
                printf("Set number %d have at least one identical \n", k);
            }
            if(*(counter+k) == 2)
            {
                printf("No elements in set number %d \n", k);
            }
        }
    }
    else
    {
        MPI_Send(counter, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
    }

和搜索功能:

int * search(struct sets *tab, int *setX)
{
    int result[N];
    int i; //pass=0

    for(i=0; i<N; i++)
    {
        result[i]=0;
    }

    for(i=0; i<N; i++)
    {
        int j, k;
        int counter=0;

        for(j=0; j<R; j++)
        {
            for(k=0; k<R; k++)
            {
                if(tab[i].array[j] == setX[k])
                {
                    counter++;
                }
            }

        //printf("Conuter: %d   -   i: %d \n", counter,i);

            if(counter == R)
            {
                result[i]=5;
                //the same sets
            }
            else if(counter > 1 && counter < R)
            {
                result[i]=1;
                //
            }
            else if(counter == 0)
            {
                result[i]=2;
                //0
            }
        }
    printf("counter: %d\n", counter);
    }

    return result;
}

顺序运行它运行正常,但是当我在4个处理器上运行此代码时结果很糟糕。我认为问题出在MPI函数中的发送和修复数据中。

完整代码:http://pastebin.com/5c8Jn3sa

编辑:

Resuts:

./ a.out - 顺序

counter: 10
counter: 9
counter: 0
counter: 10
counter: 6
counter: 6
Set number 0 is the same
Set number 1 have at least one identical
No elements in set number 2
Set number 3 is the same
Set number 4 have at least one identical
Set number 5 have at least one identical

mpiecex -np 2 ./a.out - 2进程

counter: 100
counter: 100
counter: 100
counter: 100
counter: 100
counter: 100
counter: 10
counter: 9
counter: 0
counter: 10
counter: 6
counter: 6
No elements in set number 0
Set number 1 have at least one identical
Set number 2 have at least one identical

mpiexec结果不好

0 个答案:

没有答案