From 12d6f6a5681ba7f3fb46ecbd9715ab6169ffbf25 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Fri, 10 Oct 2014 00:26:49 +0200 Subject: [PATCH] Added ocaml qp_run --- data/Makefile | 9 ++-- data/README.rst | 1 + ocaml/Qpackage.ml | 26 +++++++++++- ocaml/qp_run.ml | 92 ++++++++++++++++++++++++++++++++++++++++ scripts/follow_output.py | 5 ++- src/Makefile | 8 ++++ 6 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 ocaml/qp_run.ml diff --git a/data/Makefile b/data/Makefile index 8a14eb56..ad31ecec 100644 --- a/data/Makefile +++ b/data/Makefile @@ -1,6 +1,9 @@ -all: inputs +all: inputs executables -FORCE: +.PHONY: inputs executables -inputs: FORCE +inputs: cd inputs ; $(MAKE) all_ezfio + +executables: + $(MAKE) -C $(QPACKAGE_ROOT)/src executables && mv $(QPACKAGE_ROOT)/src/executables . diff --git a/data/README.rst b/data/README.rst index 4b78b926..b116de6e 100644 --- a/data/README.rst +++ b/data/README.rst @@ -8,6 +8,7 @@ These include: * Input data for the test suites * Atomic basis sets +* List of built executables *Important*: EZFIO files are large and should not be tracked by git. diff --git a/ocaml/Qpackage.ml b/ocaml/Qpackage.ml index 9726deb6..96c85646 100644 --- a/ocaml/Qpackage.ml +++ b/ocaml/Qpackage.ml @@ -1,5 +1,6 @@ open Core.Std;; open Qptypes;; +open Qputils;; (** Variables related to the quantum package installation *) @@ -11,7 +12,7 @@ Please source the quantum_package.rc file." ;; let bit_kind_size = lazy ( - let filename = root^"/src/Bitmask/bitmasks_module.f90" in + let filename = root / "src/Bitmask/bitmasks_module.f90" in if not (Sys.file_exists_exn filename) then raise (Failure ("File "^filename^" not found")); @@ -39,3 +40,26 @@ let bit_kind_size = lazy ( in get_data lines ) ;; + +let executables = lazy ( + let filename = root / "data/executables" + and func in_channel = + In_channel.input_lines in_channel + |> List.map ~f:(fun x -> + let e = String.split ~on:' ' x + |> List.map ~f:String.strip + |> List.filter ~f:(fun x -> x <> "") + in + match e with + | [a;b] -> (a,String.substr_replace_all ~pattern:"$QPACKAGE_ROOT" ~with_:root b) + | _ -> ("","") + ) + in + In_channel.with_file filename ~f:func + |> List.sort ~cmp:(fun (x,_) (y,_) -> + if x < y then -1 + else if x > y then 1 + else 0) +) + + diff --git a/ocaml/qp_run.ml b/ocaml/qp_run.ml new file mode 100644 index 00000000..75620b2c --- /dev/null +++ b/ocaml/qp_run.ml @@ -0,0 +1,92 @@ +open Core.Std;; +open Qputils;; + +let print_list () = + Lazy.force Qpackage.executables + |> List.iter ~f:(fun (x,_) -> Printf.printf " * %s\n" x) +;; + +let run exe ezfio_file = + + let time_start = Time.now() in + + if (not (Sys.file_exists_exn ezfio_file)) then + failwith ("EZFIO directory "^ezfio_file^" not found"); + + let executables = Lazy.force Qpackage.executables in + if (not (List.exists ~f:(fun (x,_) -> x = exe) executables)) then + failwith ("Executable "^exe^" not found"); + + Printf.printf "===============\nQuantum Package\n===============\n\n"; + Printf.printf "Date : %s\n\n%!" (Time.to_string time_start); + + let output_dir = ezfio_file / "output" in + if (Sys.file_exists_exn output_dir) then + begin + Sys.ls_dir output_dir + |> List.iter ~f:(fun x -> Sys.remove (output_dir / x)); + Unix.rmdir output_dir + end; + + let fifo_name = ezfio_file / ".fifo" in + if (Sys.file_exists_exn fifo_name) then + Sys.remove fifo_name; + Unix.mkfifo ~perm:0o664 fifo_name; + let script = Printf.sprintf "%s/scripts/follow_output.py %s & + echo $! > %s &" Qpackage.root ezfio_file fifo_name in + ignore (Sys.command script); + + let pid = + In_channel.with_file fifo_name ~f:(fun in_channel -> + In_channel.input_all in_channel |> String.strip ) + |> Int.of_string + |> Pid.of_int + in + Sys.remove fifo_name; + + let exe = + match (List.find ~f:(fun (x,_) -> x = exe) executables) with + | None -> assert false + | Some (_,x) -> x + in + match (Sys.command (exe^" "^ezfio_file)) with + | 0 -> () + | i -> Printf.printf "Program exited with code %d.\n%!" i; + ; + + Signal.send_exn (Signal.of_system_int 2) (`Pid pid); + + + + (* Run the executable in the foreground + * ==================================== *) + + let duration = Time.diff (Time.now()) time_start + |> Core.Span.to_string in + Printf.printf "Wall time : %s\n\n" duration; +;; + +let spec = + let open Command.Spec in + empty + +> anon ("exectuable" %: string) + +> anon ("ezfio_file" %: string) +;; + +let () = + Command.basic + ~summary: "Quantum Package command" + ~readme:( fun () -> " +Executes a Quantum Package binary file among these:\ni\n" +^ (Lazy.force Qpackage.executables + |> List.map ~f:(fun (x,_) -> Printf.sprintf " * %s" x ) + |> String.concat ~sep:"\n" + ) + ) + spec + (fun exe ezfio_file () -> + run exe ezfio_file + ) + |> Command.run +;; + diff --git a/scripts/follow_output.py b/scripts/follow_output.py index cc3f8ca4..06fc4336 100755 --- a/scripts/follow_output.py +++ b/scripts/follow_output.py @@ -54,7 +54,10 @@ def main(): # Handle signals import signal def handler(signum,frame): - F.running = False + if F.running: + F.running = False + else: + sys.exit(0) for i in [2, 15]: try: diff --git a/src/Makefile b/src/Makefile index 5143e300..ccaba79b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,6 +5,8 @@ OBJ= include $(QPACKAGE_ROOT)/src/Makefile.common +.PHONY: executables + all: all_modules all_clean: @@ -15,3 +17,9 @@ all_modules: $(NEEDED_MODULES) $(NEEDED_MODULES): FORCE @cd $@ ; unset NEEDED_MODULES INCLUDE_DIRS ; make +executables: + rm -f executables ; \ + for EXE in $$(find $(QPACKAGE_ROOT)/src -type f -executable | grep -e "$(QPACKAGE_ROOT)/src/[^/]*/[^/]*$$" ) ; \ + do printf "%-30s %s\n" $$(basename $$EXE) $$EXE | sed "s|$(QPACKAGE_ROOT)|\$$QPACKAGE_ROOT|g" >> executables ;\ + done +