
66 Cray T3E User’s Guide
CALL MPI_COMM_RANK(MPI_COMM_WORLD, id, rc)
data = id
CALL MPI_REDUCE(data, s, 1, MPI_INTEGER, &
MPI_SUM, 0, MPI_COMM_WORLD, rc)
CALL MPI_BCAST(s, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, rc)
WRITE(*,*) ’data:’, data, ’sum:’, s
CALL MPI_FINALIZE(rc)
END PROGRAM example
If this program is in the file collect.f90, it can be compiled and run
interactively as follows:
t3e% f90 -o collect.x collect.f90
t3e% mpprun -n 8 ./collect.x
data: 0 sum: 28
data: 4 sum: 28
data: 1 sum: 28
data: 5 sum: 28
data: 3 sum: 28
data: 7 sum: 28
data: 6 sum: 28
data: 2 sum: 28
t3e% mpprun -n 3 ./collect.x
data: 0 sum: 3
data: 1 sum: 3
data: 2 sum: 3
The program was first run on eight processors, and thereafter on three
processors. Chapter 8 discusses running batch jobs.
Here is a C language version of the same program:
#include <stdio.h>
#include <mpi.h>
main(int argc, char *argv[])
{
int ntasks, id, rc, data, s;
rc = MPI_Init(&argc, &argv);
if (rc != MPI_SUCCESS) {
printf("MPI initialization failed\n");
exit(1);
}
rc = MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
rc = MPI_Comm_rank(MPI_COMM_WORLD, &id);
data = id;
rc = MPI_Reduce(&data, &s, 1, MPI_INT, MPI_SUM, 0,
MPI_COMM_WORLD);
rc = MPI_Bcast(&s, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("data: %d sum: %d\n", data, s);
rc = MPI_Finalize();
exit(0);
}
If the program is in the file collect.c, it can be compiled as follows:
Komentarze do niniejszej Instrukcji