Accelerated qmcchem result

This commit is contained in:
Anthony Scemama 2022-01-12 12:48:08 +01:00
parent 6798019161
commit 97e47f8a85
4 changed files with 62 additions and 40 deletions

View File

@ -94,39 +94,37 @@ let to_bytes b =
let result = let result =
Bytes.concat Bytes.empty (zero :: l) Bytes.concat Bytes.empty (zero :: l)
in 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 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. (* 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. 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 if l < 8 then
failwith "Zero-sized bytes" None
else else
let m = let m =
Bytes.get_int64_le b 0 Bytes.get_int64_ne b idx
|> Int64.to_int |> Int64.to_int
in in
let nl = l-m-8 in Some (Bytes.sub b (idx+8) m, idx+8+m)
if nl > 0 then
(Bytes.sub b 8 m, Some (Bytes.sub b (8+m) nl))
else
(Bytes.sub b 8 m, None)
let of_bytes b = let of_bytes b =
(*
let rec loop accu s = let rec loop accu s =
match read_bytes s with match read_bytes s with
| data, None -> List.rev (data :: accu) | None -> []
| data, (Some rest) -> loop (data :: accu) rest | Some (data, None) -> (data :: accu)
| Some (data, (Some rest)) -> loop (data :: accu) rest
in in
let result = let result =
match loop [] b with match loop [] b with
| property :: value :: weight :: pid :: block_id :: compute_node :: [] -> | compute_node :: block_id :: pid :: weight :: value :: property :: [] ->
Some Some
{ property = Property.of_bytes property; { property = Property.of_bytes property;
value = Sample.of_bytes value; value = Sample.of_bytes value;
@ -137,6 +135,33 @@ let of_bytes b =
} }
| _ -> None | _ -> None
in 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 result
@ -195,39 +220,26 @@ let update_raw_data ?(locked=true) () =
) result ) result
in 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 if Qmcchem_config.binary_io then
begin begin
let result = let result =
let rec aux buf accu = let rec aux buf idx accu =
(* Read one block *) (* Read one block *)
let item, rest = match read_bytes buf idx with
read_bytes buf | None -> List.rev accu
in | Some (item, new_idx) ->
match of_bytes item with match of_bytes item with
| None -> [] | None -> List.rev accu
| Some item -> | Some item -> (aux [@tailcall]) buf new_idx (item::accu)
match rest with
| None -> List.rev (item::accu)
| Some rest -> (aux [@tailcall]) rest (item::accu)
in in
List.concat_map (fun filename -> List.concat_map (fun filename ->
let ic = open_in filename in let ic = open_in filename in
let length = in_channel_length ic in let length = in_channel_length ic in
let result = let result =
if length > 0 then if length > 0 then
let buf = Bytes.create length in let buf = Bytes.create length in
really_input ic buf 0 length; really_input ic buf 0 length;
aux buf [] aux buf 0 []
else [] else []
in in
close_in ic; close_in ic;
@ -237,6 +249,16 @@ let update_raw_data ?(locked=true) () =
end end
else else
begin 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 result =
let rec aux ic accu = let rec aux ic accu =
let l = let l =

View File

@ -139,7 +139,7 @@ let display_summary ~range =
and print_property property = and print_property property =
let p = Random_variable.of_raw_data ~range property let p = Random_variable.of_raw_data ~range property
in in
Printf.printf "%20s : %s\n" Printf.printf "%20s : %s\n%!"
(Property.to_string property) (Property.to_string property)
(Random_variable.to_string p) (Random_variable.to_string p)
in in

View File

@ -50,7 +50,7 @@ let to_bytes = function
let b = Bytes.create (8 * Array.length x) in let b = Bytes.create (8 * Array.length x) in
Array.iteri (fun i x -> Array.iteri (fun i x ->
Int64.bits_of_float x Int64.bits_of_float x
|> Bytes.set_int64_le b (i*8) ) x; |> Bytes.set_int64_ne b (i*8) ) x;
b b
let of_bytes b = let of_bytes b =
@ -59,6 +59,6 @@ let of_bytes b =
One_dimensional x One_dimensional x
| l -> let len = l/8 in | l -> let len = l/8 in
Multidimensional ( Array.init len (fun i -> Multidimensional ( Array.init len (fun i ->
Bytes.get_int64_le b (i*8) Bytes.get_int64_ne b (i*8)
|> Int64.float_of_bits ), |> Int64.float_of_bits ),
len ) len )

View File

@ -177,7 +177,7 @@ let input_ezfio = "
let untouched = " let untouched = "
let bytes_of_int64 i = let bytes_of_int64 i =
let result = Bytes.create 8 in let result = Bytes.create 8 in
Bytes.set_int64_le result 0 i; Bytes.set_int64_ne result 0 i;
result result
let bytes_of_int i = let bytes_of_int i =
@ -186,7 +186,7 @@ let bytes_of_int i =
let int64_of_bytes b = let int64_of_bytes b =
Bytes.get_int64_le b 0 Bytes.get_int64_ne b 0
let int_of_bytes b = let int_of_bytes b =