Skip to content

Instantly share code, notes, and snippets.

@robertaboukhalil
Last active October 15, 2021 03:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertaboukhalil/85ffcc2401d9d375b3f95efc395edade to your computer and use it in GitHub Desktop.
Save robertaboukhalil/85ffcc2401d9d375b3f95efc395edade to your computer and use it in GitHub Desktop.
2020-12-17-wasm-simd-test.md

SIMD Smith-Waterman in the browser

Article: https://medium.com/@robaboukhalil/webassembly-and-simd-7a7daa4f2ecd

Generate sample data

# Download sample files from bowtie2 repo
curl -o data/ref.fa "https://raw.githubusercontent.com/BenLangmead/bowtie2/master/example/reference/lambda_virus.fa"
curl -o data/query.fastq "https://raw.githubusercontent.com/BenLangmead/bowtie2/master/example/reads/reads_1.fq"

# Generate files with progressively more reads
nb_reads=(5 25 100 200 300 400 500)
for i in ${nb_reads[@]}; do
  head -n$((i * 4)) "data/query.fastq" > "data/reads_${i}.fastq"
done

pre.js

var Module = typeof Module !== "undefined" ? Module : {};
Module.print = function(d) { };

Compile to WebAssembly

Using Emscripten 2.0.10

git clone https://github.com/mengyao/Complete-Striped-Smith-Waterman-Library.git
cd Complete-Striped-Smith-Waterman-Library/
git clone https://github.com/simd-everywhere/simde.git

# Shared flags
FLAGS="-O3 -pipe -s TOTAL_MEMORY=200MB -s EXTRA_EXPORTED_RUNTIME_METHODS=['callMain'] -s INVOKE_RUN=0 -s USE_ZLIB=1 --preload-file data/ --pre-js ./pre.js"

# With simd:
git stash && rm *.{wasm,html} && make clean
emmake make ssw_simd_on.html CC=emcc PROG=ssw_simd_on.html CFLAGS="$FLAGS -msse2 -msimd128"
/emsdk/upstream/bin/wasm-dis ssw_simd_on.wasm | grep v128 | wc -l  # 51

# Without simd:
git stash && rm *.{wasm,html} && make clean
sed -i -e 's|include <emmintrin.h>|include "simde/simde/x86/sse2.h"|g' *.c *.h
sed -i -e 's|__m|simde__m|g' *.c *.h
sed -i -e 's|_mm_|simde_mm_|g' *.c *.h
emmake make ssw_simd_off.html CC=emcc PROG=ssw_simd_off.html CFLAGS="$FLAGS"
/emsdk/upstream/bin/wasm-dis ssw_simd_off.wasm | grep v128 | wc -l  # 0

Compile to binary

make clean
make ssw_test_simd_on PROG=ssw_test_simd_on

Benchmark

Use default gap penalties: parameters = /data/ref.fa /data/read_${i}.fastq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment