Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeser committed Jul 11, 2022
1 parent ff7f32e commit 7a104c7
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 107 deletions.
41 changes: 20 additions & 21 deletions bin/bench.ml
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
open! Core
open Core_bench
open Bitarray
module V = Vectorized

let vector_sizes = [ 64; 128; 256; 512; 1024; 2048 ]

let make_unary name func =
Bench.Test.create_indexed ~name ~args:vector_sizes (fun len ->
let x = V.random len in
let x = random len in
Staged.stage (fun () -> func x))

let make_binary name func =
Bench.Test.create_indexed ~name ~args:vector_sizes (fun len ->
let x = V.random len in
let x' = V.random len in
let x = random len in
let x' = random len in
Staged.stage (fun () -> func x x'))

let all = make_unary "all" V.all
let any = make_unary "any" V.any
let lnot_ = make_unary "lnot" V.O.(lnot)
let land_ = make_binary "land" V.O.( land )
let lor_ = make_binary "lor" V.O.( lor )
let lxor_ = make_binary "lxor" V.O.( lxor )
let hamming_weight = make_unary "hamming-weight" V.hamming_weight
let hamming_distance = make_binary "hamming-distance" V.hamming_distance
let jaccard_distance = make_binary "jaccard-distance" V.jaccard_distance
let all = make_unary "all" all
let any = make_unary "any" any
let lnot_ = make_unary "lnot" O.(lnot)
let land_ = make_binary "land" O.( land )
let lor_ = make_binary "lor" O.( lor )
let lxor_ = make_binary "lxor" O.( lxor )
let hamming_weight = make_unary "hamming-weight" hamming_weight
let hamming_distance = make_binary "hamming-distance" hamming_distance
let jaccard_distance = make_binary "jaccard-distance" jaccard_distance

let get =
Bench.Test.create_indexed ~name:"get" ~args:vector_sizes (fun len ->
let x = V.random len in
let x = random len in
let i = Random.int len in
Staged.stage (fun () -> V.get x i))
Staged.stage (fun () -> get x i))

let fold =
Bench.Test.create_indexed ~name:"fold" ~args:vector_sizes (fun len ->
let x = V.random len in
Staged.stage (fun () -> V.fold ~init:() ~f:(fun _ _ -> ()) x))
let x = random len in
Staged.stage (fun () -> fold ~init:() ~f:(fun _ _ -> ()) x))

let replicate =
Bench.Test.create_with_initialization ~name:"replicate" (fun `init ->
let x = V.random 256 in
let x = random 256 in
let dx = Random.int_incl (-5) 5 in
let dy = Random.int_incl (-5) 5 in
let ct = Random.int_incl 1 5 in
fun () -> V.replicate ~w:16 ~h:16 ~dx ~dy ~ct x)
fun () -> replicate ~w:16 ~h:16 ~dx ~dy ~ct x)

let mul =
Bench.Test.create_indexed ~name:"mul"
~args:[ 1; 2; 3; 4; 5; 6; 7; 8 (* 9; 10 *) ] (fun n_blocks ->
let x = V.Blocked_matrix.create (8 * n_blocks) false in
Staged.stage (fun () -> V.Blocked_matrix.O.(x * x)))
let x = Blocked_matrix.create (8 * n_blocks) false in
Staged.stage (fun () -> Blocked_matrix.O.(x * x)))

let () =
Random.self_init ();
Expand Down
24 changes: 24 additions & 0 deletions bitarray.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "tool"
maintainer: ["[email protected]"]
depends: [
"dune" {>= "2.0"}
"base"
"fmt"
"base_quickcheck"
]
build: [
["dune" "subst"] {pinned}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
9 changes: 9 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
(lang dune 2.0)
(generate_opam_files true)
(package
(name bitarray)
(synopsis "tool")
(maintainers [email protected])
(depends
base
fmt
base_quickcheck))
2 changes: 0 additions & 2 deletions lib/bitarray.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
module type S = Bitarray_intf.S

include Vectorized
module Short = Short
1 change: 1 addition & 0 deletions lib/bitarray.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ val init_fold : f:('a -> int -> 'a * bool) -> init:'a -> int -> t
val init : f:(int -> bool) -> int -> t
val get : t -> int -> bool
val set : t -> int -> bool -> t
val set_many : t -> ((int * bool -> unit) -> unit) -> t
val fold : t -> init:'a -> f:('a -> bool -> 'a) -> 'a
val iteri : t -> f:(int -> bool -> unit) -> unit

Expand Down
84 changes: 0 additions & 84 deletions lib/bitarray_intf.ml

This file was deleted.

23 changes: 23 additions & 0 deletions lib/vectorized.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ let set t i v =
buf = Bytes.unsafe_to_string ~no_mutation_while_string_reachable:buf';
}

let set_many t xs =
let ret = ref None in
xs (fun (i, v) ->
if i < 0 || i >= t.len then
raise_s [%message "index out of bounds" (i : int) (t.len : int)];
let w = i / bits_per_word and b = i % bits_per_word in
let buf =
match !ret with
| None ->
let b = Bytes.of_string t.buf in
ret := Some b;
b
| Some b -> b
in
Bytes.set buf w (write_bit (Bytes.get buf w) b v));
match !ret with
| None -> t
| Some buf ->
{
t with
buf = Bytes.unsafe_to_string ~no_mutation_while_string_reachable:buf;
}

let one_hot ~len x = set (create len false) x true
let init ~f x = Shared.init ~init_fold ~f x

Expand Down

0 comments on commit 7a104c7

Please sign in to comment.