【问题标题】:difficulty with MPI_Gather functionMPI_Gather 函数的困难
【发布时间】:2023-03-15 09:57:01
【问题描述】:

我在每个处理器(假设 3 个处理器)的本地数组(名为 lvotes)上都有一个值,每个处理器的第一个元素都存储一个值,即:

P0 : 4
P1 : 6
p2 : 7

现在,使用 MPI_Gather,我想将它们全部收集到 P0 中,所以它看起来像:

P0 : 4, 6, 7

我用这种方式收集:

MPI_Gather(lvotes, P, MPI_INT, lvotes, 1, MPI_INT, 0, MPI_COMM_WORLD);

但我遇到了问题。这是我第一次在 MPI 中编码。我可以使用任何建议。 谢谢

【问题讨论】:

    标签: mpi


    【解决方案1】:

    这是第一次使用聚集/分散集合的人的常见问题;在发送和接收计数中,您指定要发送到或从 each 进程接收的项目计数。因此,尽管您确实会获得(比如说)P 项目,但如果 P 是处理器的数量,那不是您为收集操作指定的;您指定您发送的计数为 1,接收的计数为 1(来自每个进程)。像这样:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <mpi.h>
    
    int main ( int argc, char **argv ) {
    
        int rank;
        int size;
    
        int lvotes;
        int *gvotes;
    
        MPI_Init ( &argc, &argv );
        MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
        MPI_Comm_size ( MPI_COMM_WORLD, &size );
    
        if (rank == 0)
            gvotes = malloc(size * sizeof(int) );
    
        /* everyone sets their first lvotes element */
        lvotes = rank+4;
    
        /* Gather to process 0 */
        MPI_Gather(&lvotes, 1, MPI_INT, /* send 1 int from lvotes.. */
                    gvotes, 1, MPI_INT, /* gather 1 int each process into lvotes */
                   0, MPI_COMM_WORLD); /* ... to root process 0 */
    
    
        printf("P%d: %d\n", rank, lvotes);
        if (rank == 0) {
            printf("P%d: Gathered ", rank);
            for (int i=0; i<size; i++)
                printf("%d ", gvotes[i]);
            printf("\n");
        }
    
        if (rank == 0)
            free(gvotes);
    
        MPI_Finalize();
    
        return 0;
    }
    

    跑步给予

    $ mpirun -np 3 ./gather
    P1: 5
    P2: 6
    P0: 4
    P0: Gathered 4 5 6 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      相关资源
      最近更新 更多