Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-MPI User's Guide > Appendix A Example applications

compute_pi_spawn.f

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

This example computes pi by integrating f(x) = 4/(1 + x**2) using MPI_Spawn. It starts with one process and spawns a new world that does the computation along with the original process. Each newly spawned process receives the # of intervals used, calculates the areas of its rectangles, and synchronizes for a global summation. The original process 0 prints the result and the time it took.

  program mainprog
  include 'mpif.h'
  double precision PI25DT
  parameter(PI25DT = 3.141592653589793238462643d0)
  double precision mypi, pi, h, sum, x, f, a
  integer n, myid, numprocs, i, ierr
  integer parenticomm, spawnicomm, mergedcomm, high
C
C Function to integrate
C
  f(a) = 4.d0 / (1.d0 + a*a)
\
  call MPI_INIT(ierr)
  call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)
  call MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr)
  call MPI_COMM_GET_PARENT(parenticomm, ierr)
  if (parenticomm .eq. MPI_COMM_NULL) then
       print *, "Original Process ", myid, " of ", numprocs,
  + " is alive"
      call MPI_COMM_SPAWN("./compute_pi_spawn", MPI_ARGV_NULL, 3,
  + MPI_INFO_NULL, 0, MPI_COMM_WORLD, spawnicomm,
  + MPI_ERRCODES_IGNORE, ierr)
      call MPI_INTERCOMM_MERGE(spawnicomm, 0, mergedcomm, ierr)
      call MPI_COMM_FREE(spawnicomm, ierr)
  else
       print *, "Spawned Process ", myid, " of ", numprocs,
  + " is alive"
      call MPI_INTERCOMM_MERGE(parenticomm, 1, mergedcomm, ierr)
      call MPI_COMM_FREE(parenticomm, ierr)
  endif
  call MPI_COMM_RANK(mergedcomm, myid, ierr)
  call MPI_COMM_SIZE(mergedcomm, numprocs, ierr)
  print *, "Process ", myid, " of ", numprocs,
  + " in merged comm is alive"

  sizetype = 1
  sumtype = 2
  if (myid .eq. 0) then
         n = 100
  endif
  call MPI_BCAST(n, 1, MPI_INTEGER, 0, mergedcomm, ierr)
C
C Calculate the interval size.
C
  h = 1.0d0 / n
  sum = 0.0d0
  do 20 i = myid + 1, n, numprocs
      x = h * (dble(i) - 0.5d0)
      sum = sum + f(x)
20 continue
  mypi = h * sum
C
C Collect all the partial sums.
C
  call MPI_REDUCE(mypi, pi, 1, MPI_DOUBLE_PRECISION,
  + MPI_SUM, 0, mergedcomm, ierr)
C
C Process 0 prints the result.
C
  if (myid .eq. 0) then
        write(6, 97) pi, abs(pi - PI25DT)
97 format(' pi is approximately: ', F18.16,
  + ' Error is: ', F18.16)
  endif
  call MPI_COMM_FREE(mergedcomm, ierr)
  call MPI_FINALIZE(ierr)
  stop
  end

compute_pi_spawn.f output

The output from running the compute_pi_spawn executable is shown below. The application was run with -np1 and with the -spawn option.

Original Process  0 of  1 is alive
Spawned Process 0 of 3 is alive
Spawned Process 2 of 3 is alive
Spawned Process 1 of 3 is alive
Process 0 of 4 in merged comm is alive
Process 2 of 4 in merged comm is alive
Process 3 of 4 in merged comm is alive
Process 1 of 4 in merged comm is alive
pi is approximately: 3.1416009869231254
Error is: 0.0000083333333323
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1979-2007 Hewlett-Packard Development Company, L.P.