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

时间:2015-01-15 21:54:58

标签: c++ arrays mpi broadcast

我很难让MPI_Bcast功能起作用。我想向所有进程广播二维数组。似乎广播消息不起作用,因为从属进程正在读取的fisrt消息是使它们存在其功能的消息 - 当DIETAG使用时te函数退出。

有人能说出使用广播功能将二维数组传递给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();
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

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

不要将MPI_Bcast视为向一堆其他进程发送大量消息的单个进程。相反,将其视为一组协同工作,以便在MPI_Bcast结束时,所有进程都具有相同的数据。这要求他们都拨打MPI_Bcast而不是一个流程广播,而所有其他人都拨打MPI_Send

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