Hello World

De Wiki de Calcul Québec
Aller à : Navigation, rechercher
Cette page est une traduction de la page Hello World et la traduction est complétée à 100 % et à jour.

Autres langues :anglais 100% • ‎français 100%

This very simple program contains the minimum needed to use the MPI parallel environment. It could serve as a template to start your trials with MPI programming.

In Fortran

File : hello.f
!--------------------------------------------------------
!  MPI_Init:
!     Inititialisation of the MPI Environment. At this time the 
!     executing program receives its unique process identification 
!     number (taskid)
!
!   MPI_Finalize:
!    Clean termination of the MPI environment. Must be called before
!    the end of the program (before an explicit stop for example), 
!    otherwise The MPI environment might not be able to exit 
!    cleanly. 
!   
!   MPI_Comm_Size:
!    Returns the number of processes available during the whole 
!    duration of the execution.
!
!   MPI_Comm_Rank:
!    Returns the unique identification number of the process (taskid).
!
!   Program output:
!    In this example, each process prints out the total number of
!    processes and its taskid, and of course "Hello World!".
!    You shall notice that since no process has any priority over the
!    other processes, the print out is not done in order.
!   
! Author: Carol Gauthier
!         Centre de Calcul scientifique
!         Universite de Sherbrooke
!
! Last revision: August 2004
!--------------------------------------------------------
Program HelloWorld
 
  use mpi
 
  integer ntasks,taskid,Error
 
  Call MPI_Init( Error )
 
  Call MPI_Comm_Size( MPI_COMM_WORLD,ntasks,Error )
  Call MPI_Comm_Rank( MPI_COMM_WORLD,taskid,Error )
 
  write(6,*)'ntasks = ',ntasks,'taskid = ',taskid,'   : Hello World!'
 
  Call MPI_Finalize( Error )
 
End Program


In C

File : hello.c
/* --------------------------------------------------------
   MPI_Init:
   Initialisation of the MPI Environment. At this time the 
   executing program receives its unique process identification 
   number (taskid)
 
   MPI_Finalize:
   Clean termination of the MPI environment. Must be called before
   the end of the program (before an explicit exit for example), 
   otherwise The MPI environment might not be able to exit 
   cleanly. 
 
   MPI_Comm_Size:
   Returns the number of processes available during the whole 
   duration of the execution.
 
   MPI_Comm_Rank:
   Returns the unique identification number of the process (taskid).
 
   Program output: 
   In this example, each process prints out the total number of
   processes and its taskid, and of course "Hello World!".
   You will notice that since no process has any priority over the
   other processes, the print out is not done in order.
 
 Author: Carol Gauthier
         Centre de Calcul scientifique
         Universite de Sherbrooke
 
 Last revision: August 2004
-------------------------------------------------------- */
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include "math.h"
#include "mpi.h"
 
int main(int argc,char** argv)
{
   int          taskid, ntasks;
   int          ierr,i,j,itask;
   int	        buffsize;
 
   MPI_Init(&argc, &argv);
 
   MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
   MPI_Comm_size(MPI_COMM_WORLD,&ntasks);
 
   printf("ntasks= %d : taskid= %d  : Hello World!\n",ntasks,taskid);
 
   MPI_Finalize(); 
}


Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Ressources de Calcul Québec
Outils
Partager