
      program option_pricing
      implicit none
      include '/home/R10E/gcheng/pvm3/include/fpvm3.h'
      
      integer info, nodes, msgtype
      integer mytid, tids(0:32)
      character*8 arch
      COMMON /PVMENV/ nodes, mytid, tids
      
      CHARACTER*80 PRGNAM_eu,PRGNAM_am
      PARAMETER ( PRGNAM_eu='node_eu', PRGNAM_am='node_am')
      
      real sz,k,r,t,tdiv,div,v(4),rate
      real psi,xmu,rho,cbscall,camc,ceur,cams
      integer repb,reps,reps1,i, j, times
      integer sec1,sec2,usec1,usec2
      
c     ------------ Starting up all the tasks ---------------------------

c     Enroll this program in PVM 
      call pvmfmytid( mytid )
      
c     Initiate nodes instances of node program 
      print *,'How many slave programs (1-32)?'
      read *, nodes
      
c     --- only suitable node number can be used
      if (nodes.ge.32) then
         nodes = 32
         reps1 = 5
      else if (nodes.ge.16) then
         nodes = 16
         reps1 = 4
      else if (nodes.ge.8) then
         nodes = 8
         reps1 = 3
      else if (nodes.ge.4) then
         nodes = 4
         reps1 = 2
      else if (nodes.ge.2) then
         nodes = 2
         reps1 = 1
      else
         nodes = 1
         reps1 = 0
      ENDIF           

c     --- initial primitive model parameters
      sz = 110
      repb = 100
      reps = 17
      psi = 1.0
      xmu = 0.0 
      rho = 0.234
      k = 100.00       
      t = .25          
      v(2) = 0.15                                                 
      v(3) = 0.15 
      v(4) = 0.15           
      div = 1.0
      tdiv = 0.125

c      goto 1234
c     If arch is set to '*' then ANY configured machine is acceptable
c     otherwise arch should be set to architecture type you wish to use.

c      arch = '*'
c      call pvmfspawn( PRGNAM_eu, 0, arch, nodes, tids, info )
      arch = 'ALPHA'
      call pvmfspawn( PRGNAM_eu, PVMARCH, arch, nodes, tids, info )
      if (info .lt. 0) then
         WRITE(6,*) 'Failed to load program'
         goto 999
      endif
      
c     --- stochastic volatility European Model
      print *,'stochastic volatility European Model'
      print *,'reps         Value      Time(usec.)'
      print *,'----------------------------------'
      times = 100
      do 1111 reps = 17, 17
         call timer(sec1,usec1)
         do 5000 i = 1, times
            call svcorp2(sz,k,rate,t,v(3),psi,xmu,rho,reps,reps1,ceur)
 5000    continue
         call timer(sec2,usec2)
         write(*,'(I3,9X,f9.4)') reps,ceur
         write(*,'(I3,9X,f9.4,5X,I8)') reps,ceur,
     &        ((sec2-sec1)*1000000+(usec2-usec1))/times
 1111 continue

c      goto 999
c       ---- kill the node processes
       do i = 0, nodes-1
          call pvmfkill(tids(i),info)
       enddo
       
      print *,'nodes = ', nodes

 1234 continue

c      arch = '*'
      arch = 'ALPHA'
      call pvmfspawn( PRGNAM_am, PVMARCH, arch, nodes, tids, info )
      if (info .lt. 0) then
         WRITE(6,*) 'Failed to load program'
         goto 999
      endif

c     --- stochastic volatility American Model
      print *,'stochastic volatility American Model'
      print *,'reps   reps1      Value  Time(usec.)'
      print *,'-----------------------------------'
      times = 100
      do 2222 reps = 17, 17
c         do 3333 j = 0, reps-1
         do 3333 j = 1, 1
            tdiv = t*j/float(reps)
            call timer(sec1,usec1)
            do 7000 i = 1, times
               call asvcorp2(sz,k,rate,t,v(4),tdiv,div,psi,
     &              xmu,rho,reps,reps1,cams) 
 7000       continue
         call timer(sec2,usec2)  
            write(*,'(I3,5X,I3,5X,f9.4)') reps,
     &           (INT(reps*tdiv/t)+1),cams
            write(*,'(I3,5X,I3,5X,f9.4,3X,I8)')
     &        reps,(INT(reps*tdiv/t)+1),cams,
     &        ((sec2-sec1)*1000000+(usec2-usec1))/times

 3333    continue
 2222 continue
      
 999  continue
c      ---- kill the node processes
      do i = 0, nodes-1
         call pvmfkill(tids(i),info)
      enddo
      
c     --------- End user program -------- 
      
c     program finished leave PVM before exiting 
      
      call pvmfexit(info) 
      
      stop
      end
