Advertisement
Guest User

Untitled

a guest
May 8th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.41 KB | None | 0 0
  1. ;;;
  2. ;;; This is  called on  all workers. But  only rank-0 should  read the
  3. ;;; FIFO.  The reader will block for measurable time (minutes).  Other
  4. ;;; workers should not  be too eagerly waiting for  the result because
  5. ;;; of agressive polling used  by MPI_Bcast().  Otherwise everyone but
  6. ;;; rank-0 burns 100% CPU.
  7. ;;;
  8. (define (read-fifo-v2 fifo)
  9.   (let ((flag #f)
  10.         (data #f)
  11.         (mutex (make-mutex)))
  12.     ;; This thread (created on  rank-0) blocks on reading a FIFO, when
  13.     ;; done sets two globals secured by the mutex:
  14.     (let ((helper (if (zero? (comm-rank))
  15.                       (make-thread
  16.                        (lambda ()
  17.                          (let ((value (normalize (with-input-from-file fifo read))))
  18.                            (with-mutex mutex
  19.                                        (set! data value)
  20.                                        (set! flag #t)))))
  21.                       #f)))
  22.       ;; Every worker checks the value of the flag on rank-0. If the
  23.       ;; data has not yet arrived, then sleep for some time and
  24.       ;; repeat. Access to flag needs to be secured by a mutex on
  25.       ;; rank-0. FIXME: other workers dont need a mutex, actually.
  26.       (let loop ()
  27.         (unless (comm-bcast 0 (with-mutex mutex flag))
  28.                 (usleep 100000)         ; FIXME: literal 0.1 sec
  29.                 (loop)))
  30.       (when helper (join-thread helper))
  31.       (comm-bcast 0 data))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement