c++ - 如何使用广播将二维数组传递给所有进程?

标签 c++ arrays mpi broadcast

我在使 MPI_Bcast 功能正常工作时遇到困难。我想将二维数组广播到所有进程。看来广播消息不起作用,因为从属进程正在读取的第一个消息是使它们存在其功能的消息 - 当使用DIETAG时,该功能将退出。

谁能告诉我使用广播函数将二维数组传递给 slace 进程的正确方法是什么?

#include <stdio.h>
#include <iostream>
#include <string>
#include <time.h>
#include <mpi.h>

using namespace std;

/* Global constants */

    #define MASTER 0
    #define WORKTAG 1
    #define DIETAG 2
    #define MATRIX_SIZE 100

/* Local functions */

    static void master(void);
    static void slave(void);
    static void initialize_matrix(int (*matrix)[MATRIX_SIZE]);

/* Function executed when program  is started */

    int main(int argc, char **argv)
    {
        // Declaring/Initizing local variables
        int current_rank;

        // Initialize MPI 
        MPI_Init(&argc, &argv);

        // Finding out current procces identity in the default communicator
        MPI_Comm_rank(MPI_COMM_WORLD, &current_rank);

        if (current_rank == MASTER) {
            master();
        } else {
            slave();
        }

        // Shut down MPI
        MPI_Finalize();

        return 0;
    }

/* Function executed by "master" process */

    static void master(void)
    {
        // Declaring variables
        int matrix_one[MATRIX_SIZE][MATRIX_SIZE];
        int processes_count, current_process_rank;

        // Initializing variables   
        initialize_matrix(matrix_one);
        MPI_Comm_size(MPI_COMM_WORLD, &processes_count);
        MPI_Comm_rank(MPI_COMM_WORLD, &current_process_rank);

        // this is currently not working
        MPI_Bcast(&matrix_one, MATRIX_SIZE * MATRIX_SIZE, MPI_INT, current_process_rank, MPI_COMM_WORLD);

        // Tell all the slaves to exit by sending an empty message with the DIETAG
        for (current_process_rank = 1; current_process_rank < processes_count; current_process_rank++) {
            MPI_Send(0, 0, MPI_INT, current_process_rank, DIETAG, MPI_COMM_WORLD);
        }

    }

/* Function executed by "slave" processes_count */

    static void slave(void) {

        MPI_Status status;
        int current_process_rank;
        int matrix_one[MATRIX_SIZE][MATRIX_SIZE];

        MPI_Comm_rank(MPI_COMM_WORLD, &current_process_rank);

        //received

        while(1) {

            MPI_Recv(&matrix_one, MATRIX_SIZE * MATRIX_SIZE, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

            // Check the tag of the received message
            if (status.MPI_TAG == DIETAG) {
                return;
            }

            cout<<"value"<<matrix_one[0][0];
        }
    }

/* Function for populating matrix with random numbers */

    static void initialize_matrix(int (*matrix)[MATRIX_SIZE])
    {
        int row, col;
        for (row = 0; row < MATRIX_SIZE; row++)
        {
            for (col = 0; col < MATRIX_SIZE; col++)
            {
                matrix[row][col] = rand();
            }
        }
    }

最佳答案

这是人们刚开始使用 MPI 时常犯的错误。集体操作应由 MPI 通信器中的所有进程调用。它们仅与相同类型的其他调用匹配。

不要将MPI_Bcast视为向一堆其他进程发送一堆消息的单个进程。相反,请将其视为一组一起工作的进程,以便当 MPI_Bcast 结束时,所有进程都具有相同的数据。这要求它们全部调用 MPI_Bcast,而不是一个进程广播而所有其他进程调用 MPI_Send

这里有一个关于如何使用简单集体函数的很好的教程: http://mpitutorial.com/mpi-broadcast-and-collective-communication/

关于c++ - 如何使用广播将二维数组传递给所有进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973700/

相关文章:

c++ - Visual Studio 2015 无法创建和编辑 c++ 项目

c++ - 表示任意枚举类型范围内的均匀分布

php - 如何允许数组中重复的mysql行id?

arrays - 在数组中查找与数组具有相同均值的对

c - 使用具有自定义操作功能的 MPI reduceAll

c - 处理 void 数据类型的指针算法

.net - 使用 .NET 和 OpenCV 进行线程化?

c++ - 使用循环填充结构

php - 比较 mysql 和 php 中的两个数组

c++ - 在 MPI_Reduce 中传递和插入 vector