假设我有n个进程:
他们做一个计算,然后将结果发送到排名0。这就是我想要的:
等级0将等待所有等级的结果,然后将它们相加。
我该怎么做?另外,我想避免以下情况:
例如,4个过程P0,P1,P2,P3,

P1 -> P0
P2 -> P0
P3 -> P0

与此同时,P1已经完成了计算,因此P1->P0再次发生。
我希望在为下一个周期进行添加之前,P0只为一个周期中的3个进程进行添加。
有人能推荐一个MPI函数来做这个吗?我知道MPI廑U收集,但我不确定它是否被阻塞
我想到了这个:
#include <mpi.h>
#include <stdio.h>

int main()
{
int pross, rank,p_count = 0;
int count = 10;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&pross);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);

int * num = malloc((pross-1)*sizeof(int));

        if(rank !=0)
        {
            MPI_Send(&count,1,MPI_INT,0,1,MPI_COMM_WORLD);
        }
        else
        {
            MPI_Gather(&count, 1,MPI_INT,num, 1, MPI_INT, 0,MPI_COMM_WORLD);
            for(ii = 0; ii < pross-1;ii++ ){printf("\n NUM %d \n",num[ii]); p_count += num[ii]; }
}
MPI_Finalize();
}

我有个错误:
  *** Process received signal ***
  Signal: Segmentation fault (11)
  Signal code: Address not mapped (1)
  Failing at address: (nil)
  [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x11630)[0x7fb3e3bc3630]
  [ 1] /lib/x86_64-linux-gnu/libc.so.6(+0x90925)[0x7fb3e387b925]
  [ 2] /usr/lib/libopen-pal.so.13(+0x30177)[0x7fb3e3302177]
  [ 3] /usr/lib/libmpi.so.12(ompi_datatype_sndrcv+0x54c)[0x7fb3e3e1e3ec]
  [ 4] /usr/lib/openmpi/lib/openmpi/mca_coll_tuned.so(ompi_coll_tuned_gather_intra_basic_linear+0x143)[0x7fb3d53d9063]
  [ 5] /usr/lib/libmpi.so.12(PMPI_Gather+0x1ba)[0x7fb3e3e29a3a]
  [ 6]  sosuks(+0xe83)[0x55ee72119e83]
  [ 7] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7fb3e380b3f1]
  [ 8]  sosuks(+0xb5a)[0x55ee72119b5a]
  *** End of error message ***

另外,我试着:
#include <mpi.h>
#include <stdio.h>

int main()
{
int pross, rank,p_count = 0;
int count = 10;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&pross);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);

int * num = malloc((pross-1)*sizeof(int));

        if(rank !=0)
        {
            MPI_Send(&count,1,MPI_INT,0,1,MPI_COMM_WORLD);
        }
        else
        {
            MPI_Gather(&count, 1,MPI_INT,num, 1, MPI_INT, 0,MPI_COMM_WORLD);
            for(ii = 0; ii < pross-1;ii++ ){printf("\n NUM %d \n",num[ii]); p_count += num[ii]; }
}
MPI_Finalize();
}

我这里有个错误:
  *** Process received signal ***
  Signal: Segmentation fault (11)
  Signal code: Address not mapped (1)
  Failing at address: 0x560600000002
  [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x11630)[0x7fefc8c11630]
  [ 1] mdscisuks(+0xeac)[0x5606c1263eac]
  [ 2] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7fefc88593f1]
  [ 3] mdscisuks(+0xb4a)[0x5606c1263b4a]
  *** End of error message ***

对于第二次尝试,这里要注意的是send和recv是成功的,但是由于某种原因,root只能接收来自列组的2条消息。所看到的分段错误是由于num中只有两个元素,我不明白num为什么只接收两次。
我把代码叫做
mpiexec -n 6 ./sosuks

有人能告诉我一个更好/正确的方法来实现我的想法吗?
更新
除了下面的答案之外,我还发现了我在实现过程中的错误,我想在上面与大家分享一下:
#include <mpi.h>
#include <stdio.h>

int main()
{
int pross, rank,p_count = 0;
int count = 10;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&pross);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Status status;
int * num = malloc((pross-1)*sizeof(int));

        if(rank !=0)
        {
            MPI_Send(&count,1,MPI_INT,0,1,MPI_COMM_WORLD);
        }
        else
        {
        int var,lick = 0;
        for(lick = 1; lick < pross; u++)
        {
        int fetihs;
        MPI_Recv(&fetihs,1,MPI_INT,lick,1,MPI_COMM_WORLD,&status);

        var += fetihs;
        }
     // do things with var
}
MPI_Finalize();
}

最佳答案

在您的情况下,正如Sneftel指出的,您需要MPI_Reduce。另外,在循环完成之前不需要显式同步。

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    int pross, rank, p_count, count = 10;

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD, &pross);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int* num = malloc((pross-1)*sizeof(int));

    // master does not send data to itself.
    // only workers send data to master.

    for (int i=0; i<3; ++i)
    {
        // to prove that no further sync is needed.
        // you will get the same answer in each cycle.
        p_count = 0;

        if (rank == 0)
        {
            // this has not effect since master uses p_count for both
            // send and receive buffers due to MPI_IN_PLACE.
            count = 500;

            MPI_Reduce(MPI_IN_PLACE, &p_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
        }
        else
        {
            // for slave p_count is irrelevant.
            MPI_Reduce(&count, NULL, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
        }

        if (rank == 0)
        {
            printf("p_count = %i\n", p_count);
        }

        // slaves send their data to master before the cycle completes.
        // no need for explicit sync such as MPI_Barrier.
        // MPI_Barrier(MPI_COMM_WORLD); // no need.
    }

    MPI_Finalize();
}

在上面的代码中,从系统中的count在主系统中减少为p_count。注意MPI_IN_PLACE和两个MPI_Reduce调用。您可以通过简单地设置count = 0并在不MPI_Reduce的情况下按所有列调用MPI_IN_PLACE来获得相同的功能。
for (int i=0; i<3; ++i)
{
    p_count = 0;
    if (rank == 0) count = 0;

    MPI_Reduce(&count, &p_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
}

关于c - 如何获得MPI中的所有等级以发送一个数值到等级0,然后阻塞对所有等级进行接收?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43413433/

10-08 22:44
查看更多