User Tools

Site Tools


Site Tools

hello.f90
program hello_world
	use mpi
	implicit none
	integer ::  rank, nb_mpi_processes, ierror, hostname_len
	character (len=MPI_MAX_PROCESSOR_NAME) :: hostname
 
	!To enhance code readability, we let MPI call or MPI native variables in capital letters in Fortran
	call MPI_INIT(ierror) ! Init MPI (init MPI_COMM_WORLD communicator, set rank to each process, etc)
 
	call MPI_COMM_SIZE(MPI_COMM_WORLD, nb_mpi_processes, ierror) ! Ask the number of MPI processes running
 
	call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierror) ! Ask the rank of the current process
 
	call MPI_GET_PROCESSOR_NAME(hostname,hostname_len,ierror) ! Ask the name of the host the process is running on
 
	print*, 'Hello world ! I am process',rank,'on',nb_mpi_processes,'processes. I am running on',hostname ! Say hello
 
	call MPI_FINALIZE(ierror) ! Close MPI
 
end program hello_world
hello.c
#include <stddef.h>
#include <mpi.h>
 
int main(int argc, char** argv) 
{
	MPI_Init(NULL, NULL); // Init MPI (init MPI_COMM_WORLD communicator, set rank to each process, etc)
 
	int nb_mpi_processes;
	MPI_Comm_size(MPI_COMM_WORLD, &nb_mpi_processes); // Ask the number of MPI processes running
 
	int rank;
	MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Ask the rank of the current process
 
	char hostname[256];
	int hostname_len;
	MPI_Get_processor_name(hostname, &hostname_len); // Ask the name of the host the process is running on
 
	printf("Hello world I am process %d on %d processes. I am running on %s\n",rank, nb_mpi_processes, hostname);
 
	MPI_Finalize(); // Close MPI
 
	return 0;
}