diff --git a/lib/sasacore/sasArg.ml b/lib/sasacore/sasArg.ml index d2c451170747353ac5698b31e43213e2366518b9..2d20f7da62bfb57eb9bd15d85c838063fea39278 100644 --- a/lib/sasacore/sasArg.ml +++ b/lib/sasacore/sasArg.ml @@ -1,4 +1,4 @@ -(* Time-stamp: <modified the 02/04/2021 (at 15:45) by Erwan Jahier> *) +(* Time-stamp: <modified the 20/05/2021 (at 09:02) by Erwan Jahier> *) type t = { @@ -9,8 +9,6 @@ type t = { mutable rif: bool; mutable no_data_file: bool; mutable quiet: bool; - mutable seed: int option; - mutable replay_seed: bool; mutable ifi: bool; mutable gen_lutin: bool; mutable gen_oracle: bool; @@ -42,8 +40,6 @@ let (make_args : unit -> t) = rif = false; no_data_file = false; quiet = false; - seed = None; - replay_seed = false; ifi = false; gen_lutin = false; gen_oracle = false; @@ -95,58 +91,6 @@ let (mkopt : t -> string list -> ?hide:bool -> ?arg:string -> Arg.spec -> let myexit i = exit i (*******************************************************************************) -(* seeds stuff *) - -let seed_set args s = - (match s with - | Some i -> - if args.verbose>0 then - Printf.fprintf stderr " [sasa] 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; - seed_set args (Some seed); - Printf.eprintf " [sasa] Replay the sasa run using the seed in %s\n" f; - flush stderr; - true - with _ -> - Printf.eprintf " [sasa] 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) = @@ -197,11 +141,11 @@ let (mkoptab : string array -> t -> unit) = ["Do not generate any data file"]; mkopt args ["--seed";"-seed"] - (Arg.Int(fun i -> seed_set args (Some i))) + (Arg.Int(fun i -> Seed.set i)) ["Set the pseudo-random generator seed of build-in daemons (wins over --replay)"]; mkopt args ["--replay";"-replay"] - (Arg.Unit(fun () -> args.replay_seed <- true)) + (Arg.Unit(fun () -> Seed.replay_seed := true)) ["Use the last generated seed to replay the last run"]; mkopt args ~hide:true ["--gen-lutin-daemon";"-gld"] diff --git a/lib/sasacore/sasArg.mli b/lib/sasacore/sasArg.mli index 2e43d2b12a8427562acb3f95e3a7267010d5c6d4..efabb6caea62354bab03aa01aa1dba0444174c12 100644 --- a/lib/sasacore/sasArg.mli +++ b/lib/sasacore/sasArg.mli @@ -1,4 +1,4 @@ -(* Time-stamp: <modified the 22/01/2020 (at 10:02) by Erwan Jahier> *) +(* Time-stamp: <modified the 20/05/2021 (at 09:02) by Erwan Jahier> *) type t = { mutable topo: string; @@ -8,8 +8,6 @@ type t = { mutable rif: bool; mutable no_data_file: bool; mutable quiet: bool; - mutable seed: int option; - mutable replay_seed: bool; mutable ifi: bool; mutable gen_lutin: bool; mutable gen_oracle: bool; @@ -25,7 +23,6 @@ type t = { mutable _margin : int; } -val seed_get : t -> int val usage_msg : string -> string val parse : string array -> t diff --git a/lib/sasacore/seed.ml b/lib/sasacore/seed.ml new file mode 100644 index 0000000000000000000000000000000000000000..ce945185f1e9baee8bfd8ade37b4bac1fb491ce1 --- /dev/null +++ b/lib/sasacore/seed.ml @@ -0,0 +1,50 @@ +(* Time-stamp: <modified the 20/05/2021 (at 09:23) by Erwan Jahier> *) + +let seed = ref None +let replay_seed = ref false +let verbose = true + +let set s = + if verbose then + Printf.fprintf stderr " [sasa] The sasa random engine seed is set to %i\n%!" s; + Random.init s; + seed := Some s + +let seed_file_name label = + Printf.sprintf "sasa-%s.seed" label + +(* for --replay *) +let reset_the_seed_to_last label = + let f = seed_file_name label in + try + let ic = open_in f in + let seed = int_of_string (input_line ic) in + set seed; + Printf.eprintf " [sasa] Replay the sasa run using the seed in %s\n" f; + flush stderr; + true + with _ -> + Printf.eprintf " [sasa] W: cannot recover the seed in %s\n" f; + flush stderr; + false + +let rec (get : string -> int) = + fun label -> + match !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 !replay_seed && reset_the_seed_to_last label then (get label) else ( + let seed = Random.self_init (); Random.int 1073741823 in + let seed_file = seed_file_name label 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; + set seed; + seed + ) diff --git a/lib/sasacore/seed.mli b/lib/sasacore/seed.mli new file mode 100644 index 0000000000000000000000000000000000000000..0609aebf020baa3fe6bc13a3dcf13da6aa3d2693 --- /dev/null +++ b/lib/sasacore/seed.mli @@ -0,0 +1,9 @@ +(* Time-stamp: <modified the 20/05/2021 (at 09:25) by Erwan Jahier> *) + +val set : int -> unit + +(** The string is used to create a file name to save/restore the seed + when the --replay option is used *) +val get : string -> int + +val replay_seed : bool ref diff --git a/lib/sasacore/simuState.ml b/lib/sasacore/simuState.ml index bd1d98752e80d459ab4911085060a8868059369a..390be6bc1d443ab00cfe56bb08308d5c7fc2f41f 100644 --- a/lib/sasacore/simuState.ml +++ b/lib/sasacore/simuState.ml @@ -1,4 +1,4 @@ -(* Time-stamp: <modified the 04/05/2021 (at 08:36) by Erwan Jahier> *) +(* Time-stamp: <modified the 20/05/2021 (at 09:09) by Erwan Jahier> *) open Register @@ -198,7 +198,7 @@ let (make : bool -> string array -> 'v t) = flush stdout; exit 2 in - let seed = seed_get args in + let seed = Seed.get args.topo in try let dynlink = if args.output_algos then false else dynlink in let dot_file = args.topo in