You are here: Home Resources Computers Historical Computers at PDC Lenngren example2.f

example2.f

Example file number 2 (MPI) for running programs.

— Plain Text, 1 kB (1262 bytes)

File contents

    PROGRAM example2
      IMPLICIT NONE
      INCLUDE 'mpif.h'

      INTEGER, PARAMETER :: maxn = 14000
      INTEGER :: i, mymaxn, n
      INTEGER :: nproc, myid, mpi_err
      REAL (8) :: a, b, c
      CHARACTER(MPI_MAX_PROCESSOR_NAME):: hostname
      INTEGER :: nhostchars

      CALL mpi_init(mpi_err)
      CALL mpi_comm_rank(mpi_comm_world,myid,mpi_err)
      CALL mpi_comm_size(mpi_comm_world,nproc,mpi_err)
      CALL mpi_get_processor_name(hostname,nhostchars,mpi_err)

      mymaxn = maxn/nproc
      IF (myid==0) THEN
        mymaxn = maxn - (nproc-1)*mymaxn
      END IF

      a = 0
      DO i = 1, mymaxn
        call random_number(b)
        call random_number(c)
        a = a + b + c
      END DO

      PRINT *, 'Host number:', myid, ' (',hostname(1:nhostchars),')', &
        '  Number of iterations:', mymaxn, &
        '  Result: ', a

      CALL mpi_reduce(a,b,1,mpi_double_precision,mpi_sum,0,mpi_comm_world, &
        mpi_err)
      CALL mpi_reduce(mymaxn,n,1,mpi_integer,mpi_sum,0,mpi_comm_world,mpi_err)

      IF (myid==0) THEN
        PRINT *,('-',i=1,32)
        PRINT *, 'Host number:', myid, '  Total number of iterations:', n, &
          '  Result: ', b
      END IF

      CALL mpi_finalize(mpi_err)
    END PROGRAM example2