Guest User

Untitled

a guest
Jan 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.45 KB | None | 0 0
  1. #!/usr/bin/env guile-qm
  2. !#
  3.  
  4. ;;
  5. ;; This is artificial intelligence guessing temp dir:
  6. ;;
  7. (define (guess-temp-dir)
  8.   (or (getenv "TTFSTMP") ; returns the first that is set ...
  9.     (getenv "SCRATCH")
  10.     (getenv "OPT_TMP")
  11.     (getenv "TEMP")
  12.     (string-append "/scratch/" (getenv "USER")))) ; if none is set use this
  13.  
  14. ;;
  15. ;; This emulates the behaviour of the "runpg" bash script
  16. ;; that tells PG to put output into o.name/ for an input i.name:
  17. ;;
  18. (define (guess-output-dir input)
  19.   (or
  20.     (and (string=? "input" input) ".") ; for input named input use CWD
  21.     (and (string-prefix? "i." input)
  22.       (string-append "o." (string-drop input 2)))
  23.     (string-append "o." input))) ; by default use o.$input for output
  24.  
  25. ;;
  26. ;; Now that we are responsible for creating directories ourselves
  27. ;; we need to coordinate the work between process within and between
  28. ;; SMP hosts:
  29. ;;
  30. (define (maybe-mkdir! world dirname)
  31.   (let ((dirname (comm-bcast world 0 dirname))) ; prefer the value at rank 0
  32.     (let loop ((rank 0))
  33.       (if (< rank (comm-size world))
  34.         (begin
  35.           (if (= rank (comm-rank world)) ; my turn to act ...
  36.             (if (not (file-exists? dirname))
  37.               (mkdir dirname)))
  38.           (comm-barrier world) ; others wait here, till I finish with mkdir ...
  39.           (loop (+ rank 1)))))))
  40.  
  41. ;;
  42. ;; Set TTFSTMP to something different from $PWD to avoid
  43. ;; polluting working directory with temporary files
  44. ;; before running PG:
  45. ;;
  46. (define (run world input)
  47.   (let
  48.     ((temp-dir (comm-bcast world 0 (guess-temp-dir))) ; prefer the value at rank 0
  49.     (output-dir (comm-bcast world 0 (guess-output-dir input))))
  50.       (begin
  51.         (setenv "TTFSINPUT" input)
  52.         (setenv "TTFSOUTPUTDIR" output-dir)
  53.         (setenv "TTFSTMP" temp-dir)
  54.         (maybe-mkdir! world temp-dir) ; it case it isnt, create it, on all workes
  55.         (maybe-mkdir! world output-dir)
  56.         (qm-run world)))) ; this invokes the program
  57.  
  58. ;;
  59. ;; Intialize MPI, get the world communicator:
  60. ;;
  61. (define world (qm-init))
  62.  
  63. ;;
  64. ;; Actually run the program for all inputs in the command line:
  65. ;;
  66. (let loop ((inputs (cdr (command-line)))) ; first argument is the program name
  67.   (if (not (null? inputs))
  68.     (begin
  69.       (run world (car inputs)) ; this invokes the program
  70.       (loop (cdr inputs)))))
  71.  
  72. ;;
  73. ;; No more communication after that:
  74. ;;
  75. (qm-finalize world)
  76.  
  77. ;; Default options for vim:sw=2:expandtab:smarttab:autoindent:syntax=scheme
Add Comment
Please, Sign In to add comment