Высокопроизводительные вычисления на кластерах. Беликов Д.А - 48 стр.

UptoLike

48
#include <stdio.h>
#include <mpi.h>
main(int argc, char **argv)
{
int rank, size, tag, rc, i;
MPI_Status status;
char message[20];
rc = MPI_Init(&argc, &argv);
rc = MPI_Comm_size(MPI_COMM_WORLD, &size);
rc = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
tag = 8;
if (rank == 0) {
strcpy(message, "Hello, world!");
for (i=1; i<size; i++)
rc = MPI_Send(message, 13, MPI_CHAR, i, tag,
MPI_COMM_WORLD);
}
else
rc = MPI_Recv(message, 13, MPI_CHAR, 0, tag,
MPI_COMM_WORLD, &status);
printf( "process %d : %s\n", rank, message);
rc = MPI_Finalize();
}
Fortran-версия программы ‘Hello World’:
program hello
include 'mpif.h'
integer rank, size, ierror, tag, status(MPI_STATUS_SIZE)
character(13) message
call MPI_INIT(ierror)
call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierror)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror)
tag = 8
if (rank .eq. 0) then
message = 'Hello, world!'
do i=1, size-1
call MPI_SEND(message, 13, MPI_CHARACTER, i, tag,