Commit 015fa462 authored by Jussi Enkovaara's avatar Jussi Enkovaara
Browse files

Hello world exercise

parent 55ff56ee
Copyright (C) 2018 CSC - IT Center for Science Ltd.
Licensed under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Code is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
Copy of the GNU General Public License can be obtained from
<http://www.gnu.org/licenses/>.
## Parallel Hello World
a) Write a simple parallel program that prints out something
(e.g. “Hello world!”) from multiple processes. Include the MPI headers
(C), use the MPI module (Fortran), or import mpi4py (Python) and
call appropriate initialization and finalization routines.
b) Modify the program so that each process prints out also its rank
and so that the process with rank 0 prints out the total number of MPI
processes as well.
program hello
use mpi
implicit none
integer :: rc, myid, ntasks
call MPI_INIT(rc)
call MPI_COMM_SIZE(MPI_COMM_WORLD, ntasks, rc)
call MPI_COMM_RANK(MPI_COMM_WORLD, myid, rc)
if(myid == 0) then
write(*,*) 'In total there are ',ntasks, 'tasks'
endif
write(*,*) 'Hello from ',myid
call MPI_FINALIZE(rc)
end program hello
#include<stdio.h>
#include<stdlib.h>
#include<mpi.h>
int main(int argc, char *argv[])
{
int i, myid, ntasks;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
if (myid == 0) {
printf("In total there are %i tasks\n", ntasks);
}
printf("Hello from %i\n", myid);
MPI_Finalize();
return 0;
}
from __future__ import print_function
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
print("In total there are {0} tasks".format(size))
print("Hello from", rank)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment