From 97e47f8a85fc0d5fc5297d951040e3a36bace788 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Wed, 12 Jan 2022 12:48:08 +0100 Subject: [PATCH] Accelerated qmcchem result --- ocaml/Block.ml | 92 +++++++++++++++++++++++--------------- ocaml/Qmcchem_result.ml | 2 +- ocaml/Sample.ml | 4 +- ocaml/qptypes_generator.ml | 4 +- 4 files changed, 62 insertions(+), 40 deletions(-) diff --git a/ocaml/Block.ml b/ocaml/Block.ml index 6790df7..3a7ad79 100644 --- a/ocaml/Block.ml +++ b/ocaml/Block.ml @@ -94,39 +94,37 @@ let to_bytes b = let result = Bytes.concat Bytes.empty (zero :: l) in - Bytes.set_int64_le result 0 (Int64.of_int ((Bytes.length result) - 8)); + Bytes.set_int64_ne result 0 (Int64.of_int ((Bytes.length result) - 8)); result -let read_bytes b = +let read_bytes b idx = (* Reads m, the first 8 bytes as an int64 containing the number of bytes to read. Then, read the next m bytes and return a tuple containing the decoded data and the rest. *) - let l = Bytes.length b in + let l = (Bytes.length b) - idx in if l < 8 then - failwith "Zero-sized bytes" + None else let m = - Bytes.get_int64_le b 0 + Bytes.get_int64_ne b idx |> Int64.to_int in - let nl = l-m-8 in - if nl > 0 then - (Bytes.sub b 8 m, Some (Bytes.sub b (8+m) nl)) - else - (Bytes.sub b 8 m, None) + Some (Bytes.sub b (idx+8) m, idx+8+m) let of_bytes b = +(* let rec loop accu s = match read_bytes s with - | data, None -> List.rev (data :: accu) - | data, (Some rest) -> loop (data :: accu) rest + | None -> [] + | Some (data, None) -> (data :: accu) + | Some (data, (Some rest)) -> loop (data :: accu) rest in let result = match loop [] b with - | property :: value :: weight :: pid :: block_id :: compute_node :: [] -> + | compute_node :: block_id :: pid :: weight :: value :: property :: [] -> Some { property = Property.of_bytes property; value = Sample.of_bytes value; @@ -137,6 +135,33 @@ let of_bytes b = } | _ -> None in +*) + let get_x s idx = + match read_bytes s idx with + | Some ( data, i1) -> data, i1 + | _ -> raise Exit + in + + + let result = + let idx=0 in + try + let property, idx = get_x b idx in + let value , idx = get_x b idx in + let weight , idx = get_x b idx in + let pid , idx = get_x b idx in + let block_id, idx = get_x b idx in + let compute_node, i5 = get_x b idx in + Some + { property = Property.of_bytes property; + value = Sample.of_bytes value; + weight = Weight.of_bytes weight; + pid = int_of_bytes pid; + block_id = Block_id.of_bytes block_id; + compute_node = Compute_node.of_bytes compute_node; + } + with Exit -> None + in result @@ -195,39 +220,26 @@ let update_raw_data ?(locked=true) () = ) result in - let rec transform new_list = function - | [] -> new_list - | head :: tail -> - let head = String.trim head in - let item = of_string head in - match item with - | None -> transform new_list tail - | Some x -> transform (x::new_list) tail - in - if Qmcchem_config.binary_io then begin let result = - let rec aux buf accu = + let rec aux buf idx accu = (* Read one block *) - let item, rest = - read_bytes buf - in - match of_bytes item with - | None -> [] - | Some item -> - match rest with - | None -> List.rev (item::accu) - | Some rest -> (aux [@tailcall]) rest (item::accu) + match read_bytes buf idx with + | None -> List.rev accu + | Some (item, new_idx) -> + match of_bytes item with + | None -> List.rev accu + | Some item -> (aux [@tailcall]) buf new_idx (item::accu) in List.concat_map (fun filename -> let ic = open_in filename in let length = in_channel_length ic in let result = - if length > 0 then + if length > 0 then let buf = Bytes.create length in really_input ic buf 0 length; - aux buf [] + aux buf 0 [] else [] in close_in ic; @@ -237,6 +249,16 @@ let update_raw_data ?(locked=true) () = end else begin + let rec transform new_list = function + | [] -> new_list + | head :: tail -> + let head = String.trim head in + let item = of_string head in + match item with + | None -> transform new_list tail + | Some x -> transform (x::new_list) tail + in + let result = let rec aux ic accu = let l = diff --git a/ocaml/Qmcchem_result.ml b/ocaml/Qmcchem_result.ml index 102bae7..06307aa 100644 --- a/ocaml/Qmcchem_result.ml +++ b/ocaml/Qmcchem_result.ml @@ -139,7 +139,7 @@ let display_summary ~range = and print_property property = let p = Random_variable.of_raw_data ~range property in - Printf.printf "%20s : %s\n" + Printf.printf "%20s : %s\n%!" (Property.to_string property) (Random_variable.to_string p) in diff --git a/ocaml/Sample.ml b/ocaml/Sample.ml index f1f6c51..60f3ced 100644 --- a/ocaml/Sample.ml +++ b/ocaml/Sample.ml @@ -50,7 +50,7 @@ let to_bytes = function let b = Bytes.create (8 * Array.length x) in Array.iteri (fun i x -> Int64.bits_of_float x - |> Bytes.set_int64_le b (i*8) ) x; + |> Bytes.set_int64_ne b (i*8) ) x; b let of_bytes b = @@ -59,6 +59,6 @@ let of_bytes b = One_dimensional x | l -> let len = l/8 in Multidimensional ( Array.init len (fun i -> - Bytes.get_int64_le b (i*8) + Bytes.get_int64_ne b (i*8) |> Int64.float_of_bits ), len ) diff --git a/ocaml/qptypes_generator.ml b/ocaml/qptypes_generator.ml index 3e93818..b5f44c7 100644 --- a/ocaml/qptypes_generator.ml +++ b/ocaml/qptypes_generator.ml @@ -177,7 +177,7 @@ let input_ezfio = " let untouched = " let bytes_of_int64 i = let result = Bytes.create 8 in - Bytes.set_int64_le result 0 i; + Bytes.set_int64_ne result 0 i; result let bytes_of_int i = @@ -186,7 +186,7 @@ let bytes_of_int i = let int64_of_bytes b = - Bytes.get_int64_le b 0 + Bytes.get_int64_ne b 0 let int_of_bytes b =