10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-09-27 03:51:01 +02:00
quantum_package/ocaml/qp_run.ml

179 lines
4.6 KiB
OCaml
Raw Normal View History

2016-02-19 00:20:28 +01:00
open Qputils
(* Environment variables :
QP_TASK_DEBUG=1 : debug task server
*)
2014-10-10 00:26:49 +02:00
let print_list () =
2019-01-14 15:20:51 +01:00
Lazy.force Qpackage.executables
|> List.iter (fun (x,_) -> Printf.printf " * %s\n" x)
2014-10-10 00:26:49 +02:00
2019-01-14 15:20:51 +01:00
let () =
2016-02-19 00:20:28 +01:00
Random.self_init ()
2019-01-13 14:57:53 +01:00
let run slave ?prefix exe ezfio_file =
2016-02-19 00:20:28 +01:00
(** Check availability of the ports *)
2019-01-14 15:20:51 +01:00
let port_number =
2016-02-19 00:20:28 +01:00
let zmq_context =
2018-05-09 11:31:32 +02:00
Zmq.Context.create ()
2016-02-19 00:20:28 +01:00
in
2019-01-14 15:20:51 +01:00
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 =
2019-01-14 15:20:51 +01:00
try
2019-01-11 19:10:12 +01:00
List.iter (fun i ->
2019-01-14 15:20:51 +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;
2019-01-11 19:10:12 +01:00
) [ 0;1;2;3;4;5;6;7;8;9 ] ;
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
2019-01-14 15:20:51 +01:00
let result =
2016-02-19 00:20:28 +01:00
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
2019-01-14 15:20:51 +01:00
let time_start =
2019-01-11 19:10:12 +01:00
Core.Time.now ()
2016-02-19 00:20:28 +01:00
in
2014-10-10 00:26:49 +02:00
2019-01-11 19:10:12 +01:00
if (not (Sys.file_exists ezfio_file)) then
2014-10-10 00:26:49 +02:00
failwith ("EZFIO directory "^ezfio_file^" not found");
let executables = Lazy.force Qpackage.executables in
2019-01-11 19:10:12 +01:00
if (not (List.exists (fun (x,_) -> x = exe) executables)) then
2016-02-19 19:04:03 +01:00
begin
Printf.printf "\nPossible choices:\n";
2019-01-11 19:10:12 +01:00
List.iter (fun (x,_) -> Printf.printf "* %s\n%!" x) executables;
2016-02-19 19:04:03 +01:00
failwith ("Executable "^exe^" not found")
end;
2014-10-10 00:26:49 +02:00
2019-01-11 19:10:12 +01:00
Printf.printf "%s\n" (Core.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;
2019-01-03 23:24:41 +01:00
Printf.printf "EZFIO Dir : %s\n" ezfio_file;
2015-12-07 22:03:33 +01:00
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
2019-01-14 15:20:51 +01:00
let qp_run_address_filename =
2016-10-12 11:26:21 +02:00
Filename.concat (Qpackage.ezfio_work ezfio_file) "qp_run_address"
in
2019-01-14 15:20:51 +01:00
let () =
2016-10-12 11:26:21 +02:00
if slave then
try
2019-01-14 15:20:51 +01:00
let address =
2019-01-11 19:10:12 +01:00
Core.In_channel.read_all qp_run_address_filename
|> String.trim
2016-10-12 11:26:21 +02:00
in
2019-01-11 19:10:12 +01:00
Unix.putenv "QP_RUN_ADDRESS_MASTER" address
2016-10-12 11:26:21 +02:00
with Sys_error _ -> failwith "No master is not running"
2015-12-07 22:03:33 +01:00
in
2019-01-14 15:20:51 +01:00
2016-10-12 11:26:21 +02:00
(** Start task server *)
2015-12-07 22:03:33 +01:00
let task_thread =
2019-01-14 15:20:51 +01:00
let thread =
Thread.create ( fun () ->
2015-12-07 22:03:33 +01:00
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
2019-01-11 19:10:12 +01:00
Unix.putenv "QP_RUN_ADDRESS" address;
2019-01-14 15:20:51 +01:00
let () =
2016-10-12 11:26:21 +02:00
if (not slave) then
2019-01-11 19:10:12 +01:00
Core.Out_channel.with_file qp_run_address_filename ~f:(
2019-01-14 15:20:51 +01:00
fun oc -> Core.Out_channel.output_lines oc [address])
2016-10-12 11:26:21 +02:00
in
2015-12-07 22:03:33 +01:00
(** Run executable *)
2019-01-14 15:20:51 +01:00
let prefix =
2019-01-13 14:57:53 +01:00
match prefix with
| Some x -> x^" "
| None -> ""
2016-02-19 00:20:28 +01:00
and exe =
2019-01-11 19:10:12 +01:00
match (List.find (fun (x,_) -> x = exe) executables) with
| (_,exe) -> exe^" "
2014-10-10 00:26:49 +02:00
in
2019-01-14 15:20:51 +01: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
2019-01-14 15:20:51 +01:00
let duration = Core.Time.diff (Core.Time.now()) time_start
2019-01-11 19:10:12 +01:00
|> Core.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
2016-02-19 00:20:28 +01:00
2014-10-10 00:26:49 +02:00
let () =
2019-01-11 19:10:12 +01:00
(* Command-line specs *)
2019-01-14 19:52:41 +01:00
let open Command_line in
begin
set_header_doc (Sys.argv.(0) ^ " - Quantum Package command");
"Executes a Quantum Package binary file among these:\n\n"
^ (Lazy.force Qpackage.executables
|> List.map (fun (x,_) -> Printf.sprintf " * %s" x )
|> String.concat "\n")
|> set_description_doc;
[ { short='s'; long="slave"; opt=Optional;
doc="Required to run slave tasks in distributed environments.";
arg=Without_arg; };
{ short='p'; long="prefix"; opt=Optional;
doc="Prefix before running the program, like gdb or valgrind.";
arg=With_arg "<string>"; };
anonymous "PROGRAM" Mandatory "Name of the QP program to be run";
anonymous "EZFIO_DIR" Mandatory "EZFIO directory";
2019-01-11 19:10:12 +01:00
]
2019-01-14 19:52:41 +01:00
|> set_specs ;
end;
2019-01-11 19:10:12 +01:00
(* Handle options *)
2019-01-14 19:52:41 +01:00
let slave = Command_line.get_bool "slave"
and prefix = Command_line.get "prefix"
2019-01-13 14:57:53 +01:00
in
2019-01-11 19:10:12 +01:00
(* Run the program *)
match Command_line.anon_args () with
2019-01-13 14:57:53 +01:00
| exe :: ezfio_file :: [] -> run slave ?prefix exe ezfio_file
2019-01-11 19:10:12 +01:00
| _ -> (Command_line.help () ; failwith "Inconsistent command line")
2019-01-14 15:20:51 +01:00
2014-10-10 00:26:49 +02:00
2015-12-07 22:03:33 +01:00