-
Notifications
You must be signed in to change notification settings - Fork 8
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
add simd example #13
Open
attacker0211
wants to merge
2
commits into
main
Choose a base branch
from
tphan/simd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add simd example #13
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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"] |
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,23 @@ | ||
# 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-wasi | ||
``` | ||
|
||
## Create Function | ||
|
||
```sql | ||
CREATE FUNCTION `u64x2-dot` AS WASM FROM INFILE 'target/wasm32-unknown-unknown/debug/simd.wasm' WITH WIT FROM INFILE 'simd.wit' | ||
CREATE FUNCTION `u64x2-inner` RETURNS TABLE AS WASM FROM INFILE 'target/wasm32-unknown-unknown/debug/simd.wasm' WITH WIT FROM INFILE 'simd.wit' | ||
``` | ||
|
||
## Example Queries | ||
|
||
```sql | ||
SELECT * FROM `u64x2-dot`([1,2,3], [0,5,6]); | ||
SELECT * FROM `u64x2-inner`([1,2,3], [3,4,5]); | ||
``` |
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,4 @@ | ||
u64x2-scalar-mul: func(a: u64, b: list<u64>) -> list<u64> | ||
u64x2-dot: func(a: list<u64>, b: list<u64>) -> u64 | ||
u64x2-inner: func(a: list<u64>, b: list<u64>) -> list<u64> | ||
u64x2-mat-mul: func(a: list<list<u64>>, b: list<list<u64>>) -> list<list<u64>> | ||
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,89 @@ | ||
#[cfg(target_arch = "wasm32")] | ||
wit_bindgen_rust::export!("simd.wit"); | ||
|
||
struct Simd; | ||
|
||
use core::arch::wasm32::*; | ||
|
||
impl simd::Simd for Simd { | ||
fn u64x2_scalar_mul(a: u64, b: Vec<u64>) -> Vec<u64> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add docstrings to each function explaining it's purpose |
||
let va: v128 = u64x2_splat(a); | ||
let n = b.len(); | ||
let mut res: Vec<u64> = vec![0; b.len()]; | ||
let mut i = 0; | ||
while i + 1 < n { | ||
let vb: v128 = u64x2(b[i], b[i + 1]); | ||
let s: v128 = u64x2_mul(va, vb); | ||
res[i] = u64x2_extract_lane::<0>(s); | ||
res[i + 1] = u64x2_extract_lane::<0>(s); | ||
i += 2; | ||
} | ||
for j in 1..(n % 2 + 1) { | ||
res[n - j] = a * b[n - j]; | ||
} | ||
res | ||
} | ||
|
||
fn u64x2_dot(a: Vec<u64>, b: Vec<u64>) -> u64 { | ||
assert!(a.len() == b.len()); | ||
let n = a.len(); | ||
let mut sum: v128 = u64x2(0, 0); | ||
let mut i = 0; | ||
while i + 1 < n { | ||
let va: v128 = u64x2(a[i], a[i + 1]); | ||
let vb: v128 = u64x2(b[i], b[i + 1]); | ||
sum = u64x2_add(sum, u64x2_mul(va, vb)); | ||
i += 2; | ||
} | ||
for j in 1..(n % 2 + 1) { | ||
return u64x2_extract_lane::<0>(sum) | ||
+ u64x2_extract_lane::<1>(sum) | ||
+ a[n - j] * b[n - j]; | ||
} | ||
u64x2_extract_lane::<0>(sum) + u64x2_extract_lane::<1>(sum) | ||
} | ||
|
||
fn u64x2_inner(a: Vec<u64>, b: Vec<u64>) -> Vec<u64> { | ||
assert!(a.len() == b.len()); | ||
let n = a.len(); | ||
let mut res = vec![0; n]; | ||
let mut i = 0; | ||
while i + 1 < n { | ||
let va: v128 = u64x2(a[i], a[i + 1]); | ||
let vb: v128 = u64x2(b[i], b[i + 1]); | ||
let m: v128 = u64x2_mul(va, vb); | ||
res[i] = u64x2_extract_lane::<0>(m); | ||
res[i + 1] = u64x2_extract_lane::<1>(m); | ||
i += 2; | ||
} | ||
for j in 1..(n % 2 + 1) { | ||
res[n - 1] = a[n - j] * b[n - j]; | ||
} | ||
res | ||
} | ||
|
||
fn u64x2_mat_mul(a: Vec<Vec<u64>>, b: Vec<Vec<u64>>) -> Vec<Vec<u64>> { | ||
assert!(a.len() > 0 && b.len() > 0); | ||
assert!(a[0].len() == b.len()); | ||
|
||
let mut res = vec![vec![0; a.len()]; b[0].len()]; | ||
let n = a.len(); | ||
let m = b.len(); | ||
for i in 0..n { | ||
for j in 0..b[0].len() { | ||
let mut k = 0; | ||
while k + 1 < m { | ||
let va: v128 = u64x2(a[i][k], a[i][k + 1]); | ||
let vb: v128 = u64x2(b[k][j], b[k + 1][j]); | ||
let m: v128 = u64x2_mul(va, vb); | ||
res[i][j] += u64x2_extract_lane::<0>(m) + u64x2_extract_lane::<1>(m); | ||
k += 2; | ||
} | ||
for t in 1..(m % 2 + 1) { | ||
res[i][j] += a[i][m - t] * b[m - t][j]; | ||
} | ||
} | ||
} | ||
res | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
rather than using list you should use singlestore compatible packed 64 bit vectors:
https://docs.singlestore.com/db/v7.8/en/reference/sql-reference/vector-functions/vector-functions.html