      program ff_slave 
c ---------------------------------------------
c fitness function slave program
c ---------------------------------------------
      implicit none

      include '../include/fpvm3.h'

      integer info, mytid, mtid, msgtype, speed, length, i
      integer n, nhost, narch, dtid, hostid, trial, flag

      double precision userff, data(64), result

      character*40 hostname,machine,arch

c ---------------------------------------------
c enroll this program in PVM
c ---------------------------------------------
      call pvmfmytid( mytid )

c ---------------------------------------------
c get the master's task id
c ---------------------------------------------
      call pvmfparent( mtid )

c ---------------------------------------------
c receive data from master host
c ---------------------------------------------
      msgtype = 1 
      call pvmfrecv( mtid, msgtype, info ) 
      call pvmfunpack( INTEGER4, trial, 1, 1, info )
      call pvmfunpack( INTEGER4, flag, 1, 1, info )
      call pvmfunpack( INTEGER4, n, 1, 1, info )
      call pvmfunpack( REAL8, data, n, 1, info ) 

c ---------------------------------------------
c perform calculations with data
c ---------------------------------------------
      result = userff( n, data, flag ) 

c ---------------------------------------------
c send result to master host
c ---------------------------------------------
      call pvmftidtohost( mytid, hostid )
 100  call pvmfconfig( nhost, narch, dtid, hostname, arch, speed, info )
      if (dtid .ne. hostid) goto 100
      length = len(hostname)
      machine = hostname(1:length)

      call pvmfinitsend( PVMDEFAULT, info )
      call pvmfpack( INTEGER4, trial, 1, 1, info )
      call pvmfpack( REAL8, result, 1, 1, info )
      call pvmfpack( INTEGER4, length, 1, 1, info )
      call pvmfpack( STRING, machine, length, 1, info )
      msgtype  = 2 
      call pvmfsend( mtid, msgtype, info ) 

c ---------------------------------------------
c leave PVM before exiting
c ---------------------------------------------
      call pvmfexit(info) 

      stop
      end
c*********************************************************************
