Biomedical Engineering Reference
In-Depth Information
12: void master(char *filename) {
13:
14: FILE *fp;
15: float Data[NVOLS][NVOXELS];
16: int i, mpi_size, voxel_pair[2];
17: float correlation, sum;
18: MPI_Status stat;
19:
20: fp = fopen(filename, "rb");
21: i = fread(Data, sizeof(float), NVOLS * NVOXELS, fp);
22: fclose(fp);
23: fp = fopen("Output.txt", "w");
24:
25: MPI_Bcast(Data, NVOLS*NVOXELS, MPI_FLOAT, 0,
26: MPI_COMM_WORLD);
27: MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
28: int count = mpi_size - 1;
29:
30: while (count > 0) {
31: MPI_Recv(voxel_pair, 2, MPI_INT, MPI_ANY_SOURCE,
32: PAIR_TAG, MPI_COMM_WORLD, &stat);
33: if (voxel_pair[0] >= 0) {
34: MPI_Recv(&correlation, 1, MPI_REAL,
35: stat.MPI_SOURCE, CORRELATION_TAG,
36: MPI_COMM_WORLD, &stat);
37: fprintf(fp, "%d %d %f\n", voxel_pair[0],
38: voxel_pair[1], correlation);
39: }
40: else count--;
41: }
42: fclose(fp);
43: sum=0;
44: MPI_Allreduce(MPI_IN_PLACE, &sum, 1, MPI_REAL,
45: MPI_SUM, MPI_COMM_WORLD);
46: printf("Sum absolute correlation is %f\n", sum);
47: }
Figure 5.16 Function master of the program correlations.c.
Thelastargumentof MPI Bcast is the communicator, which is set to the default
communicator ( MPI COMM WORLD ) in this program.
The semantics of MPI Bcast require that all the processes (that belong to
the communicator) call this function. Any program that does not ensure this is
an incorrect MPI program and its behavior is undefined by the MPI standard. In
reality, such a program may crash, give errors, deadlock, or give incorrect results.
Unlike MPI Send or MPI Recv , which are point-to-point communication
functions, MPI Bcast is called a collective communication function. It is defined
on a group of MPI processes as defined by the communicator. The MPI semantics
require that all the MPI processes in the group make call to this function. The
Search WWH ::




Custom Search