quantum_package/ocaml/qp_run.ml

169 lines
4.2 KiB
OCaml
Raw Permalink Normal View History

2017-08-18 18:28:33 +02:00
open Core
2016-02-19 00:20:28 +01:00
open Qputils
(* Environment variables :
QP_PREFIX=gdb : to run gdb (or valgrind, or whatever)
QP_TASK_DEBUG=1 : debug task server
*)
2014-10-10 00:26:49 +02:00
let print_list () =
Lazy.force Qpackage.executables
|> List.iter ~f:(fun (x,_) -> Printf.printf " * %s\n" x)
2016-02-19 00:20:28 +01:00
let () =
Random.self_init ()
2017-11-27 16:31:00 +01:00
let run slave exe ezfio_file =
2016-02-19 00:20:28 +01:00
(** Check availability of the ports *)
let port_number =
let zmq_context =
2018-05-09 11:31:32 +02:00
Zmq.Context.create ()
2016-02-19 00:20:28 +01:00
in
let dummy_socket =
2018-05-09 11:31:32 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.rep
2016-02-19 00:20:28 +01:00
in
let rec try_new_port port_number =
try
2016-10-12 11:26:21 +02:00
List.iter [ 0;1;2;3;4;5;6;7;8;9 ] ~f:(fun i ->
2016-02-19 00:20:28 +01:00
let address =
2017-11-27 17:18:14 +01:00
Printf.sprintf "tcp://%s:%d" (Lazy.force TaskServer.ip_address) (port_number+i)
2016-02-19 00:20:28 +01:00
in
2018-05-09 11:31:32 +02:00
Zmq.Socket.bind dummy_socket address;
Zmq.Socket.unbind dummy_socket address;
2016-02-19 00:20:28 +01:00
);
port_number
with
2016-03-04 12:11:38 +01:00
| Unix.Unix_error _ -> try_new_port (port_number+100)
2016-02-19 00:20:28 +01:00
in
let result =
try_new_port 41279
in
2018-05-09 11:31:32 +02:00
Zmq.Socket.close dummy_socket;
Zmq.Context.terminate zmq_context;
2016-02-19 00:20:28 +01:00
result
in
2017-11-27 16:31:00 +01:00
2016-02-19 00:20:28 +01:00
let time_start =
Time.now ()
in
2014-10-10 00:26:49 +02:00
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
2016-02-19 19:04:03 +01:00
begin
Printf.printf "\nPossible choices:\n";
List.iter executables ~f:(fun (x,_) -> Printf.printf "* %s\n%!" x);
failwith ("Executable "^exe^" not found")
end;
2014-10-10 00:26:49 +02:00
2015-12-07 22:03:33 +01:00
Printf.printf "%s\n" (Time.to_string time_start);
2014-10-10 00:26:49 +02:00
Printf.printf "===============\nQuantum Package\n===============\n\n";
2015-12-07 22:03:33 +01:00
Printf.printf "Git Commit: %s\n" Git.message;
Printf.printf "Git Date : %s\n" Git.date;
Printf.printf "Git SHA1 : %s\n" Git.sha1;
Printf.printf "\n\n%!";
2014-10-10 00:26:49 +02:00
2015-12-07 22:03:33 +01:00
(** Check input *)
2017-11-28 15:51:21 +01:00
if (not slave) then
begin
match (Sys.command ("qp_edit -c "^ezfio_file)) with
| 0 -> ()
| i -> failwith "Error: Input inconsistent\n"
end;
2015-12-07 22:03:33 +01:00
2016-10-12 11:26:21 +02:00
let qp_run_address_filename =
Filename.concat (Qpackage.ezfio_work ezfio_file) "qp_run_address"
in
let () =
if slave then
try
let address =
In_channel.read_all qp_run_address_filename
|> String.strip
in
Unix.putenv ~key:"QP_RUN_ADDRESS_MASTER" ~data:address
with Sys_error _ -> failwith "No master is not running"
2015-12-07 22:03:33 +01:00
in
2016-10-12 11:26:21 +02:00
(** Start task server *)
2015-12-07 22:03:33 +01:00
let task_thread =
let thread =
Thread.create ( fun () ->
TaskServer.run port_number )
in
thread ();
in
2017-11-27 17:18:14 +01:00
let address =
Printf.sprintf "tcp://%s:%d" (Lazy.force TaskServer.ip_address) port_number
in
2015-12-07 22:03:33 +01:00
Unix.putenv ~key:"QP_RUN_ADDRESS" ~data:address;
2016-10-12 11:26:21 +02:00
let () =
if (not slave) then
Out_channel.with_file qp_run_address_filename ~f:(
fun oc -> Out_channel.output_lines oc [address])
in
2015-12-07 22:03:33 +01:00
(** Run executable *)
2016-02-19 00:20:28 +01:00
let prefix =
match Sys.getenv "QP_PREFIX" with
| Some x -> x^" "
| None -> ""
and exe =
2014-10-10 00:26:49 +02:00
match (List.find ~f:(fun (x,_) -> x = exe) executables) with
2016-02-19 00:20:28 +01:00
| Some (_,x) -> x^" "
2016-02-19 19:04:03 +01:00
| None -> assert false
2014-10-10 00:26:49 +02:00
in
2017-06-19 09:42:52 +02:00
let exit_code =
2017-11-27 16:31:00 +01:00
match (Sys.command (prefix^exe^ezfio_file)) with
2017-06-19 09:42:52 +02:00
| 0 -> 0
| i -> (Printf.printf "Program exited with code %d.\n%!" i; i)
in
2014-10-10 00:26:49 +02:00
2015-12-07 22:03:33 +01:00
TaskServer.stop ~port:port_number;
Thread.join task_thread;
2016-10-12 11:26:21 +02:00
if (not slave) then
Sys.remove qp_run_address_filename;
2015-12-07 22:03:33 +01:00
2014-10-10 00:26:49 +02:00
let duration = Time.diff (Time.now()) time_start
2017-08-18 18:28:33 +02:00
|> Time.Span.to_string in
2017-06-19 09:42:52 +02:00
Printf.printf "Wall time : %s\n\n" duration;
2017-06-19 20:38:28 +02:00
if (exit_code <> 0) then
exit exit_code
2014-10-10 00:26:49 +02:00
let spec =
let open Command.Spec in
empty
2016-10-12 11:26:21 +02:00
+> flag "slave" no_arg
2017-11-22 17:07:16 +01:00
~doc:(" Required for slave tasks")
2015-11-25 10:46:53 +01:00
+> anon ("executable" %: string)
2014-10-10 00:26:49 +02:00
+> anon ("ezfio_file" %: string)
;;
2016-02-19 00:20:28 +01:00
2014-10-10 00:26:49 +02:00
let () =
Command.basic_spec
2014-10-10 00:26:49 +02:00
~summary: "Quantum Package command"
~readme:( fun () -> "
2014-10-21 23:23:37 +02:00
Executes a Quantum Package binary file among these:\n\n"
2014-10-10 00:26:49 +02:00
^ (Lazy.force Qpackage.executables
|> List.map ~f:(fun (x,_) -> Printf.sprintf " * %s" x )
|> String.concat ~sep:"\n"
)
)
spec
2017-11-27 16:31:00 +01:00
(fun slave exe ezfio_file () ->
run slave exe ezfio_file
2014-10-10 00:26:49 +02:00
)
2015-12-07 22:03:33 +01:00
|> Command.run ~version: Git.sha1 ~build_info: Git.message
2014-10-10 00:26:49 +02:00
2015-12-07 22:03:33 +01:00