;; This lisp program spawns the "slave1" program from slave1.c

(proclaim '(optimize (safety 2)))
(defvar nproc 0)
(defvar mytid 0)
(defvar numt 0)
(defvar n 0)
(defvar nhost 0)
(defvar narch 0)
(defvar who 0)
(defvar tids (make-array '(32)))
(defvar data nil)
(defvar msgtype 0)
(defvar cnt 0)
(defvar SLAVENAME "slave1")
(defvar result 0)
(defun master1()
     (setq mytid (pvm-mytid))
     (cond ( (not (equal (pvm-parent) 'PvmNoParent))
             (print "How many slaves to start 1-32?") (terpri)
             (let ((in (read)))
	     (setq nproc in)))
           (t 
	     (pvm-config 'nhost 'narch 'hostp)
	     (setq nproc (car nhost))
	     (if (> nproc 32) (setq nproc 32))))
     (setq numt (pvm-spawn SLAVENAME "" 0 "" nproc 'tids))
     (cond ( (< numt nproc)
	     (print "Trouble spawning slaves, Aborting. Error Codes are:") (terpri)
	     (print tids)(terpri) 
	     (loop
		(if (equal (car tids) nil) (return (pvm-exit)))
		(pvm-kill (car tids))
		(setq tids (cdr tids)))
           ))
     (setq n 100)
     (pvm-initsend 0)
     (pvm-pkint  nproc 1 1)
     (pvm-pkint  tids nproc 1)
     (pvm-pkint  n 1 1)
     (pvm-pkfloat (make-array '(100) :initial-element (float 1.0 0.1s0) :element-type 'short-float)  n 1)
     (pvm-mcast  tids nproc 0)
     (setq msgtype 5)
     (setq cnt 0)
     (loop 
	 (if (equal cnt nproc) (return (pvm-exit)))
	 (pvm-recv -1 msgtype)
	 (pvm-upkint  'who 1 1)
	 (pvm-upkfloat 'result 1 1)
	 (print "From")
	 (print who)
	 (print "Result=")
	 (print result)(terpri)
	 (setq cnt (+ cnt 1))))

