3

各プロセッサ(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でコーディングするのは初めてです。私はどんな提案でも使うことができました。ありがとう

4

1 に答える 1

8

これは、収集/分散コレクティブを初めて使用する人々に共通の問題です。送信カウントと受信カウントの両方で、各プロセスに送信または受信するアイテムの数を指定します。したがって、合計で (たとえば)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 
于 2012-12-12T15:32:27.230 に答える