I have difficulties making MPI_Bcast
function to work. I want to broadcast two dimensional array to all processes. It seems that the broadcast message is not working, because the fisrt message the slave processes are reading is the one that makes them exist their functions - when DIETAG
is use te function is exit.
Could anyone tell what is the right way to pass two dimension array to slace processes using broadcast function?
#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, ¤t_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, ¤t_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, ¤t_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();
}
}
}