diff --git a/lib/sasacore/main.ml b/lib/sasacore/main.ml index 0ff817e24ba89dc43ac350ec1187344a11a1e653..c719fac434c235323915228b2a1ca54ee7f22056 100644 --- a/lib/sasacore/main.ml +++ b/lib/sasacore/main.ml @@ -1,4 +1,4 @@ -(* Time-stamp: <modified the 08/10/2019 (at 10:18) by Erwan Jahier> *) +(* Time-stamp: <modified the 08/10/2019 (at 16:28) by Erwan Jahier> *) open Register @@ -203,7 +203,7 @@ let (make : bool -> string array -> 'v t) = Register.set_diameter (fun () -> Diameter.get g); Register.verbose_level := args.verbose; - Random.init args.seed; + if !Register.verbose_level > 0 then Printf.eprintf "nodes: %s\nedges:\n" nstr; if dynlink then ( @@ -254,11 +254,10 @@ let (make : bool -> string array -> 'v t) = flush oc; close_out oc; exit 0); + let seed = seed_get args in let oc = if args.rif then stderr else stdout in - let seed_oc = open_out "sasa.seed" in - Printf.fprintf seed_oc "%i\n" args.seed; flush seed_oc; close_out seed_oc; Printf.fprintf oc "%s" (Mypervasives.entete "#" SasaVersion.str SasaVersion.sha); - Printf.fprintf oc "#seed %i\n" args.seed; + Printf.fprintf oc "#seed %i\n" seed; let inputs_decl = get_inputs_rif_decl args pl in Printf.printf "#inputs %s\n" (String.concat " " diff --git a/lib/sasacore/sasArg.ml b/lib/sasacore/sasArg.ml index 620d1d1188b5f0430eefd86613a55001eebc1935..06d613f250d20f6e79d9710c1ed558271ca0cbc8 100644 --- a/lib/sasacore/sasArg.ml +++ b/lib/sasacore/sasArg.ml @@ -1,4 +1,4 @@ -(* Time-stamp: <modified the 07/10/2019 (at 23:53) by Erwan Jahier> *) +(* Time-stamp: <modified the 08/10/2019 (at 21:51) by Erwan Jahier> *) type t = { @@ -7,7 +7,8 @@ type t = { mutable verbose: int; mutable demon: Demon.t; mutable rif: bool; - mutable seed: int; + mutable seed: int option; + mutable replay_seed: bool; mutable ifi: bool; mutable gen_lutin: bool; mutable gen_oracle: bool; @@ -37,7 +38,8 @@ let (make_args : unit -> t) = verbose = 0; demon = Demon.Distributed; rif = false; - seed = (Random.self_init (); Random.int 1073741823); + seed = None; + replay_seed = false; ifi = false; gen_lutin = false; gen_oracle = false; @@ -88,6 +90,55 @@ let (mkopt : t -> string list -> ?hide:bool -> ?arg:string -> Arg.spec -> else opt._user_man <- (col1, ml)::opt._user_man let myexit i = exit i +(*******************************************************************************) +(* seeds stuff *) + +let seed_set args s = + (match s with + | Some i -> + Printf.fprintf stderr "The sasa random engine seed is set to %i\n" i; + Random.init i; + flush stderr; + | None -> () + ); + args.seed <- s + +let seed_file_name args = + Printf.sprintf "sasa-%s.seed" args.topo + +(* for --replay *) +let reset_the_seed_to_last args = + let f = seed_file_name args in + try + let ic = open_in f in + let seed = int_of_string (input_line ic) in + args.seed <- Some seed; + Printf.eprintf "Replay the sasa run using the seed in %s\n" f; + flush stderr; + true + with _ -> + Printf.eprintf "W: cannot recover the seed in %s\n" f; + flush stderr; + false + +let rec seed_get args = match args.seed with + | Some i -> i + | None -> + (* No seed is set: + - in -replay mode, we first try to read the seed in the seed file + - otherwise, we create a random seed and save if into args, and + into a file for -replay *) + if args.replay_seed && reset_the_seed_to_last args then (seed_get args) else + let seed = Random.self_init (); Random.int 1073741823 in + let seed_file = seed_file_name args in + let oc = open_out seed_file in + Printf.fprintf oc "%d\n%s\n" seed (Mypervasives.entete "#" SasaVersion.str SasaVersion.sha); + flush oc; + close_out oc; + seed_set args (Some seed); + seed + +(*******************************************************************************) (*** User Options Tab **) let (mkoptab : string array -> t -> unit) = @@ -118,8 +169,12 @@ let (mkoptab : string array -> t -> unit) = ["Display only outputs on stdout (i.e., behave as a rif input file)"]; mkopt args ["--seed";"-seed"] - (Arg.Int(fun i -> args.seed <- i)) - ["Set the pseudo-random generator seed of build-in demons"]; + (Arg.Int(fun i -> seed_set args (Some i))) + ["Set the pseudo-random generator seed of build-in demons (wins over --replay)"]; + + mkopt args ["--replay";"-replay"] + (Arg.Unit(fun () -> args.replay_seed <- true)) + ["Use the last generated seed to replay the last run"]; mkopt args ~hide:true ["--gen-lutin-demon";"-gld"] (Arg.Unit(fun () -> args.gen_lutin <- true)) diff --git a/lib/sasacore/sasArg.mli b/lib/sasacore/sasArg.mli index 25d56e192fd5ce2f9277abf3c0a358786fe8292a..3c3d25935d1b1d9049add2d14c7105c7c6e1d015 100644 --- a/lib/sasacore/sasArg.mli +++ b/lib/sasacore/sasArg.mli @@ -1,4 +1,4 @@ -(* Time-stamp: <modified the 06/09/2019 (at 17:17) by Erwan Jahier> *) +(* Time-stamp: <modified the 08/10/2019 (at 16:11) by Erwan Jahier> *) type t = { mutable topo: string; @@ -6,7 +6,8 @@ type t = { mutable verbose: int; mutable demon: Demon.t; mutable rif: bool; - mutable seed: int; + mutable seed: int option; + mutable replay_seed: bool; mutable ifi: bool; mutable gen_lutin: bool; mutable gen_oracle: bool; @@ -22,6 +23,7 @@ type t = { mutable _margin : int; } +val seed_get : t -> int val usage_msg : string -> string val parse : string array -> t diff --git a/test/Makefile.inc b/test/Makefile.inc index e123147b8dc45bbdce8cac9ea79af485e9d38388..c1a5275ad48c9cc89e317faffb87ec05a4553574 100644 --- a/test/Makefile.inc +++ b/test/Makefile.inc @@ -1,4 +1,4 @@ -# Time-stamp: <modified the 07/10/2019 (at 11:37) by Erwan Jahier> +# Time-stamp: <modified the 08/10/2019 (at 21:40) by Erwan Jahier> DIR=../../_build/install/default @@ -29,7 +29,7 @@ g:gnuplot s:sim2chrogtk genclean: - rm -f *.cmxs sasa *.cm* *.o *.pdf *.rif *.gp *.log *.dro *.seed *.c sasa-*.dot + rm -f *.cmxs sasa *.cm* *.o *.pdf *.rif *.gp *.log *.dro *.seed *.c *.h sasa-*.dot rm -f rdbg-session*.ml luretteSession*.ml *.lut a.out *.cov - + rm -f *.exec *.sh ################################################################################## diff --git a/test/coloring/Makefile b/test/coloring/Makefile index 69c78c36dc47cac722b466eb41fe8661c6f5da9f..d91965c63f9235d8ebdde4e01b9bc99d5a865d48 100644 --- a/test/coloring/Makefile +++ b/test/coloring/Makefile @@ -1,4 +1,4 @@ -# Time-stamp: <modified the 07/10/2019 (at 23:49) by Erwan Jahier> +# Time-stamp: <modified the 08/10/2019 (at 21:58) by Erwan Jahier> sasa=$(DIR)/bin/sasa -l 100 @@ -16,28 +16,28 @@ gnuplot: coloring.rif lurette: ring.cmxs ring_oracle.lus lurette -o lurette-ring.rif \ - -sut "sasa ring.dot -rif -lcd " \ - -oracle "lv6 ring_oracle.lus -n oracle -exec" + -sut "sasa ring.dot -rif -lcd " \ + -oracle "lv6 ring_oracle.lus -n oracle -exec" test100: for i in $$(seq 100); do make lurette && make clean; done rdbg: ring.ml rdbg -o ring.rif \ - -env "$(sasa) ring.dot -lcd" + -env "$(sasa) --replay ring.dot -lcd" -rdbg1: ring.cmxs ring_oracle.lus +rdbg1: ring.ml ring_oracle.lus rdbg -o rdbg-ring.rif \ - -sut "sasa ring.dot -rif -lcd " \ + -sut "sasa --replay ring.dot -rif -lcd " \ -oracle "lv6 ring_oracle.lus -n oracle -exec" rdbg2: ring.cmxs ring.lut rdbg -o ring.rif \ - -env "$(sasa) ring.dot -custd -rif" \ + -env "$(sasa) --replay ring.dot -custd -rif" \ -sut-nd "lutin ring.lut -n distributed" clean: genclean - rm -f ring.lut ring.ml ring_oracle.ml + rm -f ring.lut ring.ml ring_oracle.ml -include ../Makefile.inc diff --git a/test/coloring/ring_oracle.lus b/test/coloring/ring_oracle.lus index e6c9cab9ac7fd515c9d91244da3e4f3025947073..cb8b92ae497e8444fa33d93218a7fd3f83658ddf 100644 --- a/test/coloring/ring_oracle.lus +++ b/test/coloring/ring_oracle.lus @@ -34,3 +34,4 @@ let ok, r = coloring_oracle<<an,pn>>(Enab,Acti); tel +