Overview
Teaching: 10 min
Exercises: 15 minQuestions
Objectives
Below program is written in c.
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv) {
int rank, size;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("Hello, World, from task %d of %d\n",rank, size);
MPI_Finalize();
return 0;
}
Below program is written in fortran.
program helloworld
use mpi
implicit none
integer :: rank, comsize, ierr
call MPI_Init(ierr)
call MPI_Comm_size(MPI_COMM_WORLD, comsize, ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
print *,'Hello World, from task ', rank, 'of', comsize
call MPI_Finalize(ierr)
end program helloworld
MPI_Init()
MPI_Comm_size()
MPI_Comm_rank()
MPI_Ssend()
MPI_Recv()
MPI_Finalize()
C
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv) {
int rank, size;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("Hello, World, from task %d of %d\n",rank, size);
MPI_Finalize();
return 0;
}
Fortran
program helloworld
use mpi
implicit none
integer :: rank, comsize, ierr
call MPI_Init(ierr)
call MPI_Comm_size(MPI_COMM_WORLD, comsize, ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
print *,'Hello World, from task ', rank, 'of', comsize
call MPI_Finalize(ierr)
end program helloworld
Compile and run it
Pick a language and type in the code. You’ll learn more if you don’t cut and paste! Compile it with
mpicc
ormpif90
then run the result withmpirun
.Solution
$ cd mpi/mpi-intro $ mpif90 hello-world.f90 -o hello-world or $ mpicc hello-world.c -o hello-world $ mpirun -np 1 hello-world $ mpirun -np 2 hello-world $ mpirun -np 8 hello-world
Key Points