Debian Codegraph @

README.md

raw symbols SCIP
1 # triple_accel
2 ![Test](https://github.com/Daniel-Liu-c0deb0t/triple_accel/workflows/Test/badge.svg)
3 ![GitHub](https://img.shields.io/github/license/Daniel-Liu-c0deb0t/triple_accel)
4 ![Crates.io](https://img.shields.io/crates/v/triple_accel)
5 ![Docs.rs](https://docs.rs/triple_accel/badge.svg)
6
7 Rust edit distance routines accelerated using SIMD. Supports fast Hamming, Levenshtein,
8 restricted Damerau-Levenshtein, etc. distance calculations and string search.
9
10 Although vectorized SIMD code allows for up to 20-30x speedups over their scalar counterparts,
11 the difficulty of handling platform-dependent SIMD code makes SIMD routines less attractive.
12 The goal of this library is to provide an easy-to-use abstraction over SIMD edit distance routines
13 that fall back to scalar routines if the target CPU architecture is not supported.
14 Additionally, all limitations and tradeoffs of the edit distance routines should be provided upfront
15 so the user knows exactly what to expect.
16 Finally, this library should lead to performance boosts on both short and longer strings, so it
17 can be used for a variety of tasks, from bioinformatics to natural language processing.
18 `triple_accel` is very lightweight: it only has dependencies on other crates for benchmarking.
19 It can be built on machines without CPUs that have AVX2 or SSE4.1 support. It can also run on
20 machines without SIMD support by automatically using scalar alternatives.
21
22 ## Install
23 Add
24 ```
25 triple_accel = "*"
26 ```
27 to the `[dependencies]` section of your `Cargo.toml`. This library is available
28 [here](https://crates.io/crates/triple_accel) on crates.io.
29
30 Alternatively, you can clone this repository and run
31 ```
32 cargo build --release
33 ```
34 In general, for maximum efficiency, use `RUSTFLAGS="-C target-cpu=native"` if portability is not an issue.
35
36 ## Tests
37 You can run tests with
38 ```
39 cargo test
40 ```
41 after cloning the repository.
42
43 Continuous integration is used to ensure that the code passes all tests on the latest Linux, Windows,
44 and Mac platforms. Additionally, crate feature flags like `jewel-sse`, `jewel-avx`, `jewel-8bit`,
45 `jewel-16bit`, and `jewel-32bit` are used to override the default automatic detection of CPU features,
46 so all features can be thoroughly tested in continuous integration. The `debug` feature flag is specified,
47 so the exact underlying vector type that is used is printed.
48
49 ## Benchmarks
50 Benchmarks can be ran with
51 ```
52 cargo bench
53 ```
54
55 ## Docs
56 The docs are available [here](https://docs.rs/triple_accel). To build them on
57 your machine, run
58 ```
59 cargo doc
60 ```
61
62 ## Features
63 This library provides routines for both searching for some needle string in a haystack string
64 and calculating the edit distance between two strings. Hamming distance (mismatches only),
65 Levenshtein distance (mismatches + gaps), and restricted Damerau-Levenshtein distance
66 (transpositions + mismatches + gaps) are supported, along with arbitrary edit costs. This
67 library provides a simple interface, in addition to powerful lower-level control over the edit
68 distance calculations.
69
70 At runtime, the implementation for a certain algorithm is selected based on CPU support, going
71 down the list:
72
73 1. Vectorized implementation with 256-bit AVX vectors, if AVX2 is supported.
74 2. Vectorized implementation with 128-bit SSE vectors, if SSE4.1 is supported.
75 3. Scalar implementation.
76
77 Currently, vectorized SIMD implementations are only available for x86 or x86-64 CPUs. However,
78 after compiling this library on a machine that supports those SIMD intrinsics, the library can
79 be used on other machines.
80 Additionally, the internal data structure for storing vectors and the bit width of the values
81 in the vectors are selected at runtime for maximum efficiency and accuracy, given the lengths
82 of the input strings.
83
84 ## Limitations
85 Due to the use of SIMD intrinsics, only binary strings that are represented with `u8` bytes
86 are supported. Unicode strings are not currently supported.
87
88 ## Examples
89 `triple_accel` provides a very simple and easy to use framework for common edit distance operations.
90 Calculating the Hamming distance (number of mismatches) between two strings is extremely simple:
91 ```Rust
92 use triple_accel::*;
93
94 let a = b"abcd";
95 let b = b"abcc";
96
97 let dist = hamming(a, b);
98 assert!(dist == 1);
99 ```
100 By default, SIMD will be used if possible.
101 Similarly, we can easily calculate the Levenshtein distance (character mismatches and gaps all have
102 a cost of 1) between two strings with the following code:
103 ```Rust
104 use triple_accel::*;
105
106 let a = b"abc";
107 let b = b"abcd";
108
109 let dist = levenshtein_exp(a, b);
110 assert!(dist == 1);
111 ```
112 This uses exponential search to estimate the number of edits between `a` and `b`, which makes it
113 more efficient than the alternative `levenshtein` function when the number of edits between `a`
114 and `b` is low.
115
116 In addition to edit distance routines, `triple_accel` also provides search routines. These routines
117 return an iterator over matches that indicate where the `needle` string matches the `haystack` string.
118 `triple_accel` will attempt to maximize the length of matches that end at the same position and remove
119 shorter matches when some matches fully overlap.
120 ```Rust
121 use triple_accel::*;
122
123 let needle = b"helllo";
124 let haystack = b"hello world";
125
126 let matches: Vec<Match> = levenshtein_search(needle, haystack).collect();
127 // note: start index is inclusive, end index is exclusive!
128 assert!(matches == vec![Match{start: 0, end: 5, k: 1}]);
129 ```
130 Sometimes, it is necessary to use the slightly lower level, but also more powerful routines that
131 `triple_accel` provides. For example, it is possible to allow transpositions (character swaps) that
132 have a cost of 1, in addition to mismatches and gaps:
133 ```Rust
134 use triple_accel::levenshtein::*;
135
136 let a = b"abcd";
137 let b = b"abdc";
138 let k = 2; // upper bound on allowed cost
139 let trace_on = false; // return edit traceback?
140
141 let dist = levenshtein_simd_k_with_opts(a, b, k, trace_on, RDAMERAU_COSTS);
142 // note: dist may be None if a and b do not match within a cost of k
143 assert!(dist.unwrap().0 == 1);
144 ```
145 Don't let the name of the function fool you! `levenshtein_simd_k_with_opts` will still fall back to
146 the scalar implementation if AVX2 or SSE4.1 support is not available. It just prefers to use SIMD
147 where possible.
148
149 For most common cases, the re-exported functions are enough, and the low level functions do not
150 have to be used directly.
151
152 ## License
153 [MIT](LICENSE)
154
155 ## Contributing
156 Read the contributing guidelines [here](CONTRIBUTING.md).
157
158 ## Code of Conduct
159 Read the code of conduct [here](CODE_OF_CONDUCT.md).
160
161 ## Why the name "triple_accel"?
162 Because "Time Altar - Triple Accel" is a magical ability used by Kiritsugu Emiya to boost his speed
163 and reaction time in Fate/Zero. There are also some other references to the Fate series...