qwt 0.4.0

Rust implementation of Quad Wavelet Tree
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use std::{fs, path::Path};

use clap::Parser;
use mem_dbg::{MemSize, SizeFlags};
use qwt::{
    perf_and_test_utils::{
        gen_queries, gen_range_queries, gen_rank_queries, gen_select_queries, type_of,
        TimingQueries,
    },
    quadwt::RSforWT,
    utils::{msb, text_remap},
    AccessUnsigned, HQWT256Pfs, HQWT512Pfs, HuffQWaveletTree, OccsRangeUnsigned, QWT256Pfs,
    QWT512Pfs, QWaveletTree, RankUnsigned, SelectUnsigned, HQWT256, HQWT512, HWT, QWT256, QWT512,
    WT,
};
use serde::{Deserialize, Serialize};

const N_RUNS: usize = 10;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
    /// Spacify the input filename (The Index is saved with the extension ".qwt")
    #[clap(short, long, value_parser)]
    input_file: String,
    /// Specify the number of randomly generated queries to run
    #[clap(short, long, value_parser)]
    #[arg(default_value_t = 10000000)]
    n_queries: usize,
    /// Check the correctness by running a slow test
    #[arg(short, long)]
    test_correctness: bool,
    /// Run occs_range queries
    #[arg(short, long)]
    occs_range: bool,
    /// Run rank queries
    #[arg(short, long)]
    rank: bool,
    /// Run get queries
    #[arg(short, long)]
    access: bool,
    /// Run select queries
    #[arg(short, long)]
    select: bool,
    #[arg(long)]
    wt: bool,
    #[arg(long)]
    hwt: bool,
    #[arg(long)]
    qwt256: bool,
    #[arg(long)]
    qwt256pfs: bool,
    #[arg(long)]
    qwt512: bool,
    #[arg(long)]
    qwt512pfs: bool,
    #[arg(long)]
    hqwt256: bool,
    #[arg(long)]
    hqwt256pfs: bool,
    #[arg(long)]
    hqwt512: bool,
    #[arg(long)]
    hqwt512pfs: bool,
    #[arg(long)]
    all_structs: bool,
}

pub fn load_or_build_and_save_qwt<DS>(
    output_filename: &str,
    text: &[<DS as AccessUnsigned>::Item],
) -> DS
where
    DS: Serialize
        + for<'a> Deserialize<'a>
        + From<Vec<<DS as AccessUnsigned>::Item>>
        + AccessUnsigned,
    <DS as AccessUnsigned>::Item: Clone,
{
    let ds: DS;
    let path = Path::new(&output_filename);
    if path.exists() {
        println!(
            "The data structure already exists. Filename: {}. I'm going to load it ...",
            output_filename
        );
        let serialized = fs::read(path).unwrap();
        println!("Serialized size: {:?} bytes", serialized.len());
        ds = bincode::deserialize::<DS>(&serialized).unwrap();
    } else {
        let mut t = TimingQueries::new(1, 1); // measure building time
        t.start();
        ds = DS::from(text.to_owned());
        t.stop();
        let (t_min, _, _) = t.get();
        println!("Construction time {:?} millisecs", t_min / 1000000);

        let serialized = bincode::serialize(&ds).unwrap();
        println!("Serialized size: {:?} bytes", serialized.len());
        fs::write(path, serialized).unwrap();
    }

    ds
}

fn test_correctness<
    T: AccessUnsigned<Item = u8> + RankUnsigned<Item = u8> + SelectUnsigned<Item = u8> + MemSize,
>(
    ds: &T,
    sequence: &[u8],
) {
    print!("\nTesting correctness... ");
    for (i, &symbol) in sequence.iter().enumerate() {
        assert_eq!(ds.get(i), Some(symbol));
        let rank = ds.rank(symbol, i).unwrap();
        let s = ds.select(symbol, rank).unwrap();
        assert_eq!(s, i);
    }
    println!("Everything is ok!\n");
}

fn test_occs_range_naive_latency<T: RankUnsigned<Item = u8> + MemSize>(
    ds: &T,
    n: usize,
    queries: &[(usize, usize)],
    file: String,
    sigma: u8,
) {
    let mut t = TimingQueries::new(N_RUNS, queries.len());

    for _ in 0..N_RUNS {
        t.start();
        for &(sp, ep) in queries.iter() {
            let it = (0..sigma).map(|s| {
                let lo = unsafe { ds.rank_unchecked(s, sp) };
                let hi = unsafe { ds.rank_unchecked(s, ep) };
                (s, hi - lo)
            });

            for out in it {
                std::hint::black_box(out);
            }
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
    "RESULT algo={} exp=occs_range_naive_latency input={} n={} logn={:?} min_time_ns={} max_time_ns={} avg_time_ns={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
    file,
        n,
        msb(n),
        t_min,
        t_max,
        t_avg,
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );
}

fn test_occs_range_latency<T: OccsRangeUnsigned<Item = u8> + MemSize>(
    ds: &T,
    n: usize,
    queries: &[(usize, usize)],
    file: String,
) {
    let mut t = TimingQueries::new(N_RUNS, queries.len());

    for _ in 0..N_RUNS {
        t.start();
        for &(sp, ep) in queries.iter() {
            for out in unsafe { ds.occs_range_unchecked(sp..ep) } {
                std::hint::black_box(out);
            }
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
    "RESULT algo={} exp=occs_range_latency input={} n={} logn={:?} min_time_ns={} max_time_ns={} avg_time_ns={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
    file,
        n,
        msb(n),
        t_min,
        t_max,
        t_avg,
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );
}

fn test_rank_latency<T: RankUnsigned<Item = u8> + MemSize>(
    ds: &T,
    n: usize,
    queries: &[(usize, u8)],
    file: String,
) {
    let mut result = 0;
    let mut t = TimingQueries::new(N_RUNS, queries.len());

    for _ in 0..N_RUNS {
        t.start();
        for &(pos, symbol) in queries.iter() {
            let i = (pos + result) % n;
            result = unsafe { ds.rank_unchecked(symbol, i) };
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
    "RESULT algo={} exp=rank_latency input={} n={} logn={:?} min_time_ns={} max_time_ns={} avg_time_ns={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
    file,
        n,
        msb(n),
        t_min,
        t_max,
        t_avg,
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );

    println!("Result: {}", result);
}

fn test_rank_prefetch_latency<RS, const WITH_PREFETCH_SUPPORT: bool>(
    ds: &HuffQWaveletTree<u8, RS, WITH_PREFETCH_SUPPORT>,
    n: usize,
    queries: &[(usize, u8)],
    file: String,
) where
    RS: RSforWT + MemSize,
    Vec<RS>: MemSize,
{
    let mut result = 0;
    let mut t = TimingQueries::new(N_RUNS, queries.len());

    for _ in 0..N_RUNS {
        t.start();
        for &(pos, symbol) in queries.iter() {
            let i = (pos + result) % n;
            result = unsafe { ds.rank_prefetch_unchecked(symbol, i) };
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
    "RESULT algo={} exp=rank_prefetch_latency input={} n={} logn={:?} min_time_ns={} max_time_ns={} avg_time_ns={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
    file,
        n,
        msb(n),
        t_min,
        t_max,
        t_avg,
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );

    println!("Result: {}", result);
}

fn test_rank_prefetch_latency_qwt<RS, const WITH_PREFETCH_SUPPORT: bool>(
    ds: &QWaveletTree<u8, RS, WITH_PREFETCH_SUPPORT>,
    n: usize,
    queries: &[(usize, u8)],
    file: String,
) where
    RS: RSforWT + MemSize,
    Vec<RS>: MemSize,
{
    let mut result = 0;
    let mut t = TimingQueries::new(N_RUNS, queries.len());

    for _ in 0..N_RUNS {
        t.start();
        for &(pos, symbol) in queries.iter() {
            let i = (pos + result) % n;
            result = unsafe { ds.rank_prefetch_unchecked(symbol, i) };
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
    "RESULT algo={} exp=rank_prefetch_latency input={} n={} logn={:?} min_time_ns={} max_time_ns={} avg_time_ns={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
    file,
        n,
        msb(n),
        t_min,
        t_max,
        t_avg,
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );

    println!("Result: {}", result);
}

fn test_select_latency<T: SelectUnsigned<Item = u8> + MemSize>(
    ds: &T,
    n: usize,
    queries: &[(usize, u8)],
    file: String,
) {
    let mut t = TimingQueries::new(N_RUNS, queries.len());

    let mut result = 0;
    for _ in 0..N_RUNS {
        t.start();
        for &(pos, symbol) in queries.iter() {
            let i = pos - 1 + result % 2;
            let i = std::cmp::max(1, i);
            result = unsafe { ds.select_unchecked(symbol, i - 1) };
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
	"RESULT algo={} exp=select_latency input={} n={} logn={:?} min_time_ns={} max_time_ns={} avg_time_ns={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
	file,
        n,
        msb(n),
        t_min,
        t_max,
        t_avg,
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );

    println!("Result: {}", result);
}

fn test_access_latency<T: AccessUnsigned<Item = u8> + MemSize>(
    ds: &T,
    n: usize,
    queries: &[usize],
    file: String,
) {
    let mut t = TimingQueries::new(N_RUNS, queries.len());
    let mut result: u8 = 0;
    for _ in 0..N_RUNS {
        t.start();
        for &pos in queries.iter() {
            let i = (pos * ((result as usize) + 42)) % n;
            result = unsafe { ds.get_unchecked(i) };
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
	"RESULT algo={} exp=access_latency input={} n={} logn={:?} min_time_ns={} max_time_ns={} avg_time_ns={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
	file,
        n,
        msb(n),
        t_min,
        t_max,
        t_avg,
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );
    println!("Result: {result}");
}

#[allow(dead_code)]
fn test_access_throughput<T: AccessUnsigned<Item = u8> + MemSize>(
    ds: &T,
    n: usize,
    queries: &[usize],
    file: String,
) {
    let mut t = TimingQueries::new(N_RUNS, queries.len());
    let mut result: u8 = 0;
    for _ in 0..N_RUNS {
        t.start();
        for &pos in queries.iter() {
            result += unsafe { ds.get_unchecked(pos) };
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
	"RESULT algo={} exp=access_throughput input={} n={} logn={:?} min_throughput_ms={} max_throughput_ms={} avg_throughput_ms={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
	file,
        n,
        msb(n),
        (queries.len() as f64) / (((t_max * queries.len() as u128) as f64) / 1000.0 / 1000.0),
        (queries.len() as f64) / (((t_min * queries.len() as u128) as f64) / 1000.0 / 1000.0),
        (queries.len() as f64) / (((t_avg * queries.len() as u128) as f64) / 1000.0 / 1000.0),
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );
    println!("Result: {result}");
}

#[allow(dead_code)]
fn test_select_throughput<T: SelectUnsigned<Item = u8> + MemSize>(
    ds: &T,
    n: usize,
    queries: &[(usize, u8)],
    file: String,
) {
    let mut t = TimingQueries::new(N_RUNS, queries.len());

    let mut result = 0;
    for _ in 0..N_RUNS {
        t.start();
        for &(pos, symbol) in queries.iter() {
            result += unsafe { ds.select_unchecked(symbol, pos - 1) };
        }
        t.stop()
    }

    let (t_min, t_max, t_avg) = t.get();
    println!(
	"RESULT algo={} exp=select_throughput input={} n={} logn={:?} min_throughput_ms={} max_throughput_ms={} avg_throughput_ms={} space_in_bytes={} space_in_mib={:.2} n_queries={} n_runs={}",
        type_of(&ds).chars().filter(|c| !c.is_whitespace()).collect::<String>(),
	file,
        n,
        msb(n),
        (queries.len() as f64) / (((t_max * queries.len() as u128) as f64) / 1000.0 / 1000.0),
        (queries.len() as f64) / (((t_min * queries.len() as u128) as f64) / 1000.0 / 1000.0),
        (queries.len() as f64) / (((t_avg * queries.len() as u128) as f64) / 1000.0 / 1000.0),
        ds.mem_size(SizeFlags::default()),
        ds.mem_size(SizeFlags::default()) as f64 / (1024.0 * 1024.0),
        queries.len(),
        N_RUNS
    );

    println!("Result: {}", result);
}

fn main() {
    let args = Args::parse();
    let input_filename = args.input_file;
    let mut text = std::fs::read(&input_filename).expect("Cannot read the input file.");

    let sigma = text_remap(&mut text);
    println!("sigma: {}", sigma);
    let n = text.len();
    println!("Text length: {:?}", n);

    // Generate queries
    let range_queries = gen_range_queries(args.n_queries, n);
    let rank_queries = gen_rank_queries(args.n_queries, &text);
    let access_queries = gen_queries(args.n_queries, n);
    let select_queries = gen_select_queries(args.n_queries, &text);

    macro_rules! test_ds {
        ($($t: ty: $e:ident: $rank_f:ident), *) => {
            $({
                if args.$e || args.all_structs {
                    let output_filename = input_filename.clone() + "." + stringify!($e) + ".wt";
                    let ds = load_or_build_and_save_qwt::<$t>(&output_filename, &text);

                    if args.test_correctness {
                        test_correctness(&ds, &text);
                    }

                    if args.occs_range {
                        test_occs_range_latency(&ds, n, &range_queries, input_filename.clone());
                        test_occs_range_naive_latency(&ds, n, &range_queries, input_filename.clone(), sigma as u8);
                    }

                    if args.rank {
                        $rank_f(&ds, n, &rank_queries, input_filename.clone());
                        // test_rank_throughput(&ds, n, &rank_queries, input_filename.clone());
                    }

                    if args.access {
                        test_access_latency(&ds, n, &access_queries, input_filename.clone());
                        // test_access_throughput(&ds, n, &access_queries, input_filename.clone());
                    }

                    if args.select {
                        test_select_latency(&ds, n, &select_queries, input_filename.clone());
                        // test_select_throughput(&ds, n, &select_queries, input_filename.clone());
                    }
                }
            })*
        };
    }

    test_ds!(
        WT<_>: wt: test_rank_latency,
        HWT<_>: hwt: test_rank_latency,
        QWT256<_>: qwt256: test_rank_latency,
        QWT512<_>: qwt512: test_rank_latency,
        HQWT256<_>: hqwt256: test_rank_latency,
        HQWT512<_>: hqwt512: test_rank_latency,
        QWT256Pfs<_>: qwt256pfs: test_rank_prefetch_latency_qwt,
        QWT512Pfs<_>: qwt512pfs: test_rank_prefetch_latency_qwt,
        HQWT256Pfs<_>: hqwt256pfs: test_rank_prefetch_latency,
        HQWT512Pfs<_>: hqwt512pfs: test_rank_prefetch_latency
    );
}