Startup wasm worker all at once

This commit is contained in:
msqr1
2024-09-27 17:34:30 -07:00
parent 4f78f596ae
commit e3f19bc6cf
9 changed files with 25 additions and 28 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
#include "Util.h"
#include <vosk_api.h>
#include "vosk_api.h"
struct CommonModel {
bool normalMdl;

View File

@@ -1,6 +1,7 @@
#include "Util.h"
#include <emscripten/wasm_worker.h>
#include <emscripten/em_js.h>
#include "emscripten/wasm_worker.h"
#include "emscripten/em_js.h"
EM_JS(void, mtFireEv, (int index, int typeIdx, int content), {
objs[index].dispatchEvent(new CustomEvent(events[typeIdx], { "detail" : content == 0 ? null : UTF8ToString(content) }));
@@ -48,9 +49,9 @@ int untar(unsigned char* tar, int tarSize, const std::string& storepath) {
}
return Successful;
}
void Worker::startup(int _fn, int _pool) {
static void workerStartup(int _pool) {
WorkerPool& pool{*reinterpret_cast<WorkerPool*>(_pool)};
std::function<void()>& fn{*reinterpret_cast<std::function<void()>*>(_fn)};
std::function<void()> fn;
while(!pool.done) {
// Wait until unlocked
emscripten_atomic_wait_u32(&pool.qLock, true, -1);
@@ -70,15 +71,24 @@ void Worker::startup(int _fn, int _pool) {
fn();
}
}
using _startupFn = void(*)(int);
EM_JS(void, startupWorkers, (_startupFn startupFn, WorkerPool* pool), {
Object.values(_wasmWorkers).forEach(worker => {
worker.postMessage({
"_wsc": startupFn,
"x": [ pool ]
});
})
})
static constexpr int workerStack{32768};
static std::array<std::byte, MAX_WORKERS * workerStack> stacks;
#undef MAX_WORKERS
WorkerPool::WorkerPool() {
for(unsigned int i = 0; i < workers.size(); i++) {
workers[i].handle = emscripten_create_wasm_worker(&stacks[i * workerStack], workerStack);
emscripten_wasm_worker_post_function_vii(workers[i].handle, Worker::startup, reinterpret_cast<int>(&workers[i].fn),reinterpret_cast<int>(this));
for(int i{}; i < MAX_WORKERS; ++i) {
emscripten_create_wasm_worker(&stacks[i * workerStack], workerStack);
}
startupWorkers(workerStartup, this);
}
#undef MAX_WORKERS
WorkerPool::~WorkerPool() {
done = true;
emscripten_atomic_store_u32(&qLock, false);

View File

@@ -1,11 +1,11 @@
#pragma once
#include <filesystem>
#include <functional>
#include <variant>
#include <fstream>
#include <functional>
#include <emscripten/atomic.h>
#include <emscripten/console.h>
#include "emscripten/atomic.h"
#include "emscripten/console.h"
namespace fs = std::filesystem;
@@ -30,12 +30,6 @@ enum UntarStatus {
FailedWrite,
FailedClose
};
struct WorkerPool;
struct Worker {
int handle;
std::function<void()> fn;
static void startup(int _fn, int _pool);
};
#ifndef MAX_WORKERS
#define MAX_WORKERS 1
#endif
@@ -43,7 +37,6 @@ struct WorkerPool {
bool qLock{true}; // True is locked, false is unlocked
bool done{};
std::queue<std::function<void()>> taskQ;
std::array<Worker, MAX_WORKERS> workers;
WorkerPool();
~WorkerPool();
void exec(std::function<void()> fn);

View File

@@ -71,12 +71,6 @@ if [ ! -d "$VOSK" ]; then
emar -rcs vosk.a ${VOSK_FILES//.cc/.o}
fi
cd "$VOSK"/src
VOSK_FILES="recognizer.cc language_model.cc model.cc spk_model.cc vosk_api.cc" &&
em++ -O3 -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals -Wno-deprecated -I. -I"$KALDI"/src -I"$OPENFST"/include recognizer.cc -c &&
emar -rcs vosk.a ${VOSK_FILES//.cc/.o}
echo "GOod"
cd "$SRC" &&
em++ Util.cc CommonModel.cc Recognizer.cc Bindings.cc -O3 -std=c++23 -fno-rtti -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 -DMAX_WORKERS="$MAX_THREADS" -sWASMFS -sWASM_BIGINT -sMODULARIZE -sWASM_EXNREF -sTEXTDECODER=2 -sWASM_WORKERS=2 -sEVAL_CTORS=2 -sINITIAL_MEMORY="$INITIAL_MEMORY" -sALLOW_MEMORY_GROWTH -sPOLYFILL=0 -sEXIT_RUNTIME=0 -sINVOKE_RUN=0 -sSUPPORT_LONGJMP=0 -sINCOMING_MODULE_JS_API=wasmMemory,instantiateWasm,wasm -sEXPORT_NAME=loadVosklet -sMALLOC=emmalloc -sENVIRONMENT=web,worker -I. -I"$VOSK"/src -L"$KALDI"/src -l:online2/kaldi-online2.a -l:decoder/kaldi-decoder.a -l:ivector/kaldi-ivector.a -l:gmm/kaldi-gmm.a -l:tree/kaldi-tree.a -l:feat/kaldi-feat.a -l:cudamatrix/kaldi-cudamatrix.a -l:lat/kaldi-lat.a -l:lm/kaldi-lm.a -l:rnnlm/kaldi-rnnlm.a -l:hmm/kaldi-hmm.a -l:nnet3/kaldi-nnet3.a -l:transform/kaldi-transform.a -l:matrix/kaldi-matrix.a -l:fstext/kaldi-fstext.a -l:util/kaldi-util.a -l:base/kaldi-base.a -L"$OPENFST"/lib -l:libfst.a -l:libfstngram.a -L"$CLAPACK_WASM" -l:CBLAS/lib/cblas.a -l:CLAPACK-3.2.1/lapack.a -l:CLAPACK-3.2.1/libcblaswr.a -l:f2c_BLAS-3.8.0/blas.a -l:libf2c/libf2c.a -L"$VOSK"/src -l:vosk.a -lembind -flto -msimd128 -matomics -mbulk-memory -mreference-types -mnontrapping-fptoint -mextended-const -msign-ext -mmutable-globals --pre-js Wrapper.js --closure 1 -o ../Vosklet.js
cd .. &&