So.... I"m trying to use MPI_Bcast because it seems like the easiest way to take a multidimensional array (the full program has a 3-d one all filled out) and make it available to every process. The code below, however, does not work... 'grid' holds nothing but 0s when accessed from a child process. (Example: "Proc 1 has found 0, Proc 0 has found 600 ").

Anyone know how to properly do this?

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

using namespace std;

int grid[10][10][1];

main (int argc, char* argv[]) {
	int procID, nproc;
	MPI_Status status;
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &procID);
	MPI_Comm_size(MPI_COMM_WORLD, &nproc);
	int root = 0;

	if(procID == root) {
		grid[0][0][0] = 600;
		printf("Proc %i has found %i \n", procID, grid[0][0][0]);
		MPI_Bcast(&grid, 200, MPI_INT, root, MPI_COMM_WORLD);
	}
	else {
		printf("Proc %i has found %i \n", procID, grid[0][0][0]);
	}
	MPI_Finalize();
}

The code below, however, does not work... 'grid' holds nothing but 0s when accessed from a child process. (Example: "Proc 1 has found 0, Proc 0 has found 600 ").

But that's exactly what your programmed:

if(procID == root) {
		grid[0][0][0] = 600;
// only grid[0][0][0] has a value and it's '600'
// But only if the process-id = root
		printf("Proc %i has found %i \n", procID, grid[0][0][0]);
		MPI_Bcast(&grid, 200, MPI_INT, root, MPI_COMM_WORLD);
	}
	else {
// grid[][][] has not yet received any values, so all elements are
// undefined.... But if you're lucky they'll be 0
		printf("Proc %i has found %i \n", procID, grid[0][0][0]);
	}

Hmmm. I'd expected that after the broadcast the new processes would possess the values for grid[][][] that they'd been sent.

This article has been dead for over six months. Start a new discussion instead.