Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/rust/simd/Cargo.toml
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"]
23 changes: 23 additions & 0 deletions examples/rust/simd/README.md
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]);
```
4 changes: 4 additions & 0 deletions examples/rust/simd/simd.wit
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>>
Comment on lines +1 to +4
Copy link
Contributor

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

89 changes: 89 additions & 0 deletions examples/rust/simd/src/lib.rs
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> {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
}