如何使用 MPI 散布和阵列收集

New*_*ner 2 c parallel-processing mpi

我是 MPI 的新手,并使用 C 语言编写了以下程序。我想设置我的数组,而不是使用指针,如下所示。我的第一个数组元素读取正确,之后,它不会读取数组元素。你能告诉我这是否不是使用分散和收集的正确方法
以下是我得到的结果:

$ mpicc test.c -o test
$ mpirun -np 4 test
1. Processor 0 has data 0 1 2 3
2. Processor 0 has data 0
3. Processor 0 doubling the data, now has 5
2. Processor 1 has data 32767
3. Processor 1 doubling the data, now has 5
2. Processor 2 has data -437713961
3. Processor 2 doubling the data, now has 5
2. Processor 3 has data 60
3. Processor 3 doubling the data, now has 5
4. Processor 0 has data: 5 1 2 3
Run Code Online (Sandbox Code Playgroud)

正确的结果应该是:

$ mpicc test.c -o test
$ mpirun -np 4 test
1. Processor 0 has data 0 1 2 3
2. Processor 0 has data 0
3. Processor 0 doubling the data, now has 5
2. Processor 1 has data 1
3. Processor 1 doubling the data, now has 5
2. Processor 2 has data 2
3. Processor 2 doubling the data, now has 5
2. Processor 3 has data 3
3. Processor 3 doubling the data, now has 5
4. Processor 0 has data: 5 5 5 5
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。以下代码使用 4 个处理器运行:

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

int main(int argc, char **argv) {
    int size, rank;

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

    int globaldata[4]; /*wants to declare array this way*/
    int localdata[4]; /*without using pointers*/

    int i;
    if (rank == 0) {

        for (i = 0; i < size; i++)
            globaldata[i] = i;

        printf("1. Processor %d has data: ", rank);
        for (i = 0; i < size; i++)
            printf("%d ", globaldata[i]);
        printf("\n");
    }

    MPI_Scatter(globaldata, 1, MPI_INT, &localdata, 1, MPI_INT, 0, MPI_COMM_WORLD);

    printf("2. Processor %d has data %d\n", rank, localdata[rank]);
    localdata[rank]= 5;
    printf("3. Processor %d now has %d\n", rank, localdata[rank]);

    MPI_Gather(&localdata, 1, MPI_INT, globaldata, 1, MPI_INT, 0, MPI_COMM_WORLD);

    if (rank == 0) {
        printf("4. Processor %d has data: ", rank);
        for (i = 0; i < size; i++)
            printf("%d ", globaldata[i]);
        printf("\n");
    }


    MPI_Finalize();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Mak*_*isH 5

你的设置和你的分散原则上没问题。您的问题在于打印,因为您在这里误解了分散/聚集的细节。

当分散 4 元素数组时,每个进程只获得一个元素(如您使用 的第 2 和第 5 个参数定义的MPI_Scatter call())。此元素存储在本地数组的 0 索引中。它实际上是一个标量。

通常,您可能会分散非常大的数组,并且每个进程可能仍然需要处理一个大的本地数组。在这些情况下,正确计算全局指数局部指数至关重要。

假设以下玩具问题:您想将数组 [1 2 3 4 5 6] 分散到两个进程。Proc0 应该有 [1 2 3] 部分,而 Proc1 应该有 [4 5 6] 部分。在这种情况下,全局数组的大小为 6,本地数组的大小为 3。 Proc0 获取全局元素 0, 1, 2 并将它们分配给它的本地 0, 1, 2。 Proc1 获取全局元素 3, 4 , 5 并将它们分配给其本地 0, 1, 2。

当您了解MPI_Scatterv时,您可能会更好地理解这个概念,它不为每个进程假设相同数量的本地元素。

此版本的代码似乎有效:

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

int main(int argc, char **argv) {
    int size, rank;

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

    int globaldata[4];/*wants to declare array this way*/
    int localdata;/*without using pointers*/

    int i;
    if (rank == 0) {

        for (i=0; i<size; i++)
            globaldata[i] = i;

        printf("1. Processor %d has data: ", rank);
        for (i=0; i<size; i++)
            printf("%d ", globaldata[i]);
        printf("\n");
    }

    MPI_Scatter(globaldata, 1, MPI_INT, &localdata, 1, MPI_INT, 0, MPI_COMM_WORLD);

    printf("2. Processor %d has data %d\n", rank, localdata);
    localdata= 5;
    printf("3. Processor %d now has %d\n", rank, localdata);

    MPI_Gather(&localdata, 1, MPI_INT, globaldata, 1, MPI_INT, 0, MPI_COMM_WORLD);

    if (rank == 0) {
        printf("4. Processor %d has data: ", rank);
        for (i=0; i<size; i++)
            printf("%d ", globaldata[i]);
        printf("\n");
    }


    MPI_Finalize();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

享受学习 MPI!:-)