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



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

谁能说出使用广播函数将二维数组传递给 slac 进程的正确方法是什么?

#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/

相关内容

最新更新