-
Notifications
You must be signed in to change notification settings - Fork 11
add simd example #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
add simd example #13
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [package] | ||
| name = "simd" | ||
| version = "0.1.0" | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen.git" } | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Simd Rust | ||
|
|
||
| A simple simd example using [core::arch::wasm32](https://doc.rust-lang.org/core/arch/wasm32/index.html#simd) | ||
|
|
||
| ## Build | ||
|
|
||
| ```sh | ||
| cargo build --target wasm32-unknown-unknown | ||
| ``` | ||
|
|
||
| ## Create Function | ||
|
|
||
| ```sql | ||
| CREATE FUNCTION mul AS WASM FROM INFILE 'target/wasm32-unknown-unknown/debug/simd.wasm' WITH WIT FROM INFILE 'simd.wit' | ||
| CREATE FUNCTION dot AS WASM FROM INFILE 'target/wasm32-unknown-unknown/debug/simd.wasm' WITH WIT FROM INFILE 'simd.wit' | ||
| CREATE FUNCTION `inner` RETURNS TABLE AS WASM FROM INFILE 'target/wasm32-unknown-unknown/debug/simd.wasm' WITH WIT FROM INFILE 'simd.wit' | ||
| ``` | ||
|
|
||
| ## Example Queries | ||
|
|
||
| ```sql | ||
| SELECT mul(3,4); | ||
| SELECT dot([1,2,3], [0,5,6]); | ||
| SELECT * FROM `inner`([1,2,3], [3,4,5]); | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| mul: func(a: u64, b: u64) -> u64 | ||
| dot: func(a: list<u64>, b: list<u64>) -> u64 | ||
| inner: func(a: list<u64>, b: list<u64>) -> list<u64> | ||
| mmul: func(a: list<list<u64>>, b: list<list<u64>>) -> list<list<u64>> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #[cfg(target_arch = "wasm32")] | ||
| wit_bindgen_rust::export!("simd.wit"); | ||
|
|
||
| struct Simd; | ||
|
|
||
| use core::arch::wasm32::*; | ||
|
|
||
| impl simd::Simd for Simd { | ||
| fn mul(a: u64, b: u64) -> u64 { | ||
| let va: v128 = u64x2_splat(a); | ||
| let vb: v128 = u64x2_splat(b); | ||
| let c = u64x2_extract_lane::<1>(i64x2_mul(va, vb)); | ||
| c | ||
| } | ||
| fn dot(a: Vec<u64>, b: Vec<u64>) -> u64 { | ||
| assert!(a.len() == b.len()); | ||
| let mut sum: u64 = 0; | ||
| for i in 0..a.len() { | ||
| sum += Self::mul(a[i], b[i]); | ||
| } | ||
| sum | ||
| } | ||
|
||
| fn inner(a: Vec<u64>, b: Vec<u64>) -> Vec<u64> { | ||
| assert!(a.len() == b.len()); | ||
| let mut res = vec![0; a.len()]; | ||
| for i in 0..a.len() { | ||
| res[i] = Self::mul(a[i], b[i]); | ||
| } | ||
| res | ||
| } | ||
| fn mmul(a: Vec<Vec<u64>>, b: Vec<Vec<u64>>) -> Vec<Vec<u64>> { | ||
| if a.len() == 0 && b.len() == 0 { | ||
| return Vec::with_capacity(0); | ||
| } | ||
| assert!(a[0].len() == b.len()); | ||
|
|
||
| let mut res = vec![vec![0; a.len()]; b[0].len()]; | ||
| for i in 0..a.len() { | ||
| for j in 0..b[0].len() { | ||
| for k in 0..b.len() { | ||
| res[i][j] += Self::mul(a[i][k], b[k][j]); | ||
| } | ||
| } | ||
| } | ||
| res | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is technically a scalar multiplication - it fills all lanes with the same value and then extracts just one value out of the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for your review. I wasn't familiar with simd and made a mistake. Would appreciate feedbacks on the new code.