Hello World

In the example all processes of MPI_COMM_WORLD send a hello world message to rank 0, which displays all received messages in the console.

The source code is shown below:

!******************************************************************************
!    Hello World example:
!
!    In this example the rank 0 of MPI_COMM_WORLD waits for messages from the 
!    other ranks. When a message is received it is displayed (send to stdout).
!
!    Copyright 2003 (c) Critical Software SA
!    . http://www.criticalsoftware.com
!    . http://www.criticalsoftware.com/hpc
!    . csWMPI@criticalsoftware.com
!
!******************************************************************************

      include "mpi_init.f"

!MESSAGELENGTH The length of the messages that
!                      we are going to transmit 
!TAG           The tag to use for the transmissions
!NODENAMESIZE  The size of the node name 


!     Main program
      program main
      implicit none
      include 'mpif.h'

      integer nCommSize, nCommRank, nCounter, ierr
      integer MESSAGELENGTH, NODENAMESIZE, TAG
      parameter (MESSAGELENGTH = 128, NODENAMESIZE = 65, TAG = 100)
      character*(MESSAGELENGTH) pchMessage
      character*(NODENAMESIZE) pchNodeName
      integer   dwNodeNameSize 
      character*(10) strnCommRank
      integer nCommTemp, nCount

      dwNodeNameSize = NODENAMESIZE

!     Initialize csWMPI II:                                                   
      call MPI_INIT(ierr)

!     Determine what the world looks like and our own position in it:
      call MPI_COMM_SIZE(MPI_COMM_WORLD, nCommSize, ierr)
      call MPI_COMM_RANK(MPI_COMM_WORLD, nCommRank, ierr)
      print *, 'I am rank ', nCommRank,' of ',nCommSize,
     +         ' in MPI_COMM_WORLD'

!     If I am rank 0 I'll receive messages from the other ranks in
!     MPI_COMM_WORLD and send them to stdout:
      if (nCommRank .eq. 0) then
        do nCounter = 1,nCommSize-1
!          Receive a message from a process:
            call MPI_RECV(pchMessage, MESSAGELENGTH,MPI_CHARACTER,
     +               MPI_ANY_SOURCE,TAG,MPI_COMM_WORLD,
     +               MPI_STATUS_IGNORE, ierr);

!           Display the received message:
            print *,'I received the following message: ', pchMessage
        end do
      else !I am NOT rank 0
!     Get my node name
            call MPI_GET_PROCESSOR_NAME(pchNodeName,dwNodeNameSize,ierr)
            if (trim(pchNodeName).eq.'' )then
                  pchNodeName =  'Unknown'
          end if

!           Number to string
            nCommTemp = nCommRank
            do nCount = 0,9
                  if(nCommTemp .gt. 0) then
                        strnCommRank(nCount+1:nCount+1) = 
     +                        char(mod(nCommTemp,10)+ichar('0'))
                  else
                        strnCommRank(nCount+1:nCount+1) = ' '
                  end if
                  nCommTemp = nCommTemp/10
            end do            
!           Compose my message:
            pchMessage = 'Hello world from rank '//trim(strnCommRank)//
     +      ' at '//pchNodeName
!           Send it:
            call MPI_SEND(pchMessage, MESSAGELENGTH, MPI_CHARACTER, 0,
     +                    TAG,MPI_COMM_WORLD, ierr)
      end if

!     Pause rank 0 so that the output can be verified:
      if (nCommRank .eq. 0) then
        pause 'Press ENTER to exit...'
      end if

!     Finalize csWMPI II:
      call MPI_FINALIZE(ierr)
      stop
      end



© 2009 Critical Software SA. All trademarks and copyrights on this page are owned by their respective owners.
cscsWMPI II™, cscsWMPI™ and PatentMPI™ are trademarks of Critical Software SA. All Rights Reserved.