-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
357 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use elapsed::ElapsedDuration; | ||
use memmap::MmapOptions; | ||
use std::error::Error; | ||
use std::fs::File; | ||
use std::time::Instant; | ||
#[cfg(feature = "tracing")] | ||
use tracing::Level; | ||
#[cfg(feature = "tracing")] | ||
use tracing_subscriber::fmt; | ||
|
||
use rkyv_08::{from_bytes_unchecked, rancor::Error as RkyvError}; | ||
|
||
// use kiddo::immutable::float::kdtree::ArchivedImmutableKdTree; | ||
use kiddo::immutable::float::kdtree::ImmutableKdTree; | ||
use kiddo::SquaredEuclidean; | ||
|
||
type Tree = ImmutableKdTree<f64, u32, 3, 256>; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> | ||
where | ||
{ | ||
#[cfg(feature = "tracing")] | ||
let subscriber = fmt().with_max_level(Level::TRACE).without_time().finish(); | ||
#[cfg(feature = "tracing")] | ||
tracing::subscriber::set_global_default(subscriber)?; | ||
|
||
let query = [0.123f64, 0.456f64, 0.789f64]; | ||
|
||
let start = Instant::now(); | ||
|
||
// memmap the file into a buffer | ||
let buf = | ||
unsafe { MmapOptions::new().map(&File::open("./examples/immutable-test-tree-r08.rkyv")?)? }; | ||
|
||
// TODO: unsatisfied trait bounds when trying to call nearest_one on archived tree | ||
|
||
// You can use the safe API for fast zero-copy deserialization | ||
// let archived_tree = rkyv_08::access::<ArchivedImmutableKdTree<f64, u32, 3, 256>, RkyvError>(&buf[..]).unwrap(); | ||
|
||
// zero-copy deserialization into archived type | ||
// let archived_tree = | ||
// unsafe { rkyv_08::access_unchecked::<ArchivedImmutableKdTree<f64, u32, 3, 256>>(&buf) }; | ||
|
||
// perform a query | ||
// let nearest_neighbour = archived_tree.nearest_one::<SquaredEuclidean>(&query); | ||
|
||
// println!( | ||
// "total elapsed: {}\n\n", | ||
// ElapsedDuration::new(start.elapsed()) | ||
// ); | ||
// println!( | ||
// "Nearest item to query (archived): {:?}", | ||
// nearest_neighbour.item | ||
// ); | ||
|
||
// full deserialization | ||
let tree = unsafe { from_bytes_unchecked::<Tree, RkyvError>(&buf) }?; | ||
|
||
// perform a query | ||
let nearest_neighbour = tree.nearest_one::<SquaredEuclidean>(&query); | ||
|
||
println!( | ||
"Nearest item to query (deserialized): {:?}", | ||
nearest_neighbour.item | ||
); | ||
println!( | ||
"total elapsed: {}\n\n", | ||
ElapsedDuration::new(start.elapsed()) | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use elapsed::ElapsedDuration; | ||
// use memmap::MmapOptions; | ||
use rand::Rng; | ||
use rand_chacha::rand_core::SeedableRng; | ||
use rkyv_08::{rancor::Error as RkyvError, to_bytes}; | ||
use std::error::Error; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::time::Instant; | ||
#[cfg(feature = "tracing")] | ||
use tracing::Level; | ||
#[cfg(feature = "tracing")] | ||
use tracing_subscriber::fmt; | ||
use ubyte::ToByteUnit; | ||
|
||
use kiddo::float::distance::SquaredEuclidean; | ||
use kiddo::immutable::float::kdtree::ImmutableKdTree; | ||
|
||
const NUM_ITEMS: usize = 50_000_000; | ||
|
||
type Tree = ImmutableKdTree<f64, u32, 3, 256>; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
#[cfg(feature = "tracing")] | ||
let subscriber = fmt().with_max_level(Level::TRACE).without_time().finish(); | ||
#[cfg(feature = "tracing")] | ||
tracing::subscriber::set_global_default(subscriber)?; | ||
|
||
let query = [0.123f64, 0.456f64, 0.789f64]; | ||
|
||
// build and serialize a large ImmutableKdTree | ||
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(1); | ||
let content_to_add: Vec<[f64; 3]> = (0..NUM_ITEMS).map(|_| rng.gen::<[f64; 3]>()).collect(); | ||
|
||
let start = Instant::now(); | ||
let tree: Tree = ImmutableKdTree::new_from_slice(&content_to_add); | ||
println!( | ||
"Populated ImmutableKdTree with {} items ({})", | ||
tree.size(), | ||
ElapsedDuration::new(start.elapsed()) | ||
); | ||
|
||
let nearest_neighbour = tree.nearest_one::<SquaredEuclidean>(&query); | ||
|
||
println!("Nearest item to query: {:?}", nearest_neighbour.item); | ||
|
||
let start = Instant::now(); | ||
|
||
let buf = to_bytes::<RkyvError>(&tree)?; | ||
|
||
let mut file = File::create("./examples/immutable-test-tree-r08.rkyv")?; | ||
file.write_all(&buf) | ||
.expect("Could not write serialized rkyv to file"); | ||
|
||
let file_size = file.metadata().unwrap().len().bytes(); | ||
println!( | ||
"Serialized k-d tree to rkyv file 'immutable-test-tree-r08.rkyv' ({}). File size: {:.2}", | ||
ElapsedDuration::new(start.elapsed()), | ||
file_size | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,6 @@ | |
pub mod kdtree; | ||
#[doc(hidden)] | ||
pub mod query; | ||
|
||
#[cfg(feature = "rkyv_08")] | ||
mod rkyv_aligned_vec; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.