yum-archive/TaSTT-Whisper

High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model

git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper

KonstantinSource codes8c4603c

master
1.4 KiB38 linesraw
1#pragma once
2
3namespace Whisper
4{
5	// A callback to offload to the thread pool
6	using pfnParallelForCallback = HRESULT( * )( int ith, void* ctx ) noexcept;
7
8	// A simple parallel for implementation; Windows includes a decent thread pool since Vista (2006)
9	HRESULT parallelFor( pfnParallelForCallback pfn, int threadsCount, void* ctx );
10
11	// Use this version when you wanna use the thread pool repeatedly, for the same work.
12	// This class caches native work handle, saving a couple of WinAPI calls.
13	class alignas( 64 ) ThreadPoolWork
14	{
15		PTP_WORK work = nullptr;
16
17		// We want these volatile fields in another cache line from the rest of the data of this class.
18		// threadIndex field is concurrently modified by different CPU cores, and these cache coherency protocols are slow.
19		// OTOH, work and callback fields of this class only change when created / destroyed, that cache line is shared by CPU cores without any performance penalty.
20		alignas( 64 ) volatile long threadIndex = 0;
21		volatile HRESULT status = E_UNEXPECTED;
22
23		static void __stdcall callbackStatic( PTP_CALLBACK_INSTANCE Instance, PVOID pv, PTP_WORK Work );
24
25	protected:
26		virtual HRESULT threadPoolCallback( int ith ) noexcept = 0;
27
28	public:
29		ThreadPoolWork() = default;
30		ThreadPoolWork( const ThreadPoolWork& ) = delete;
31
32		~ThreadPoolWork();
33
34		HRESULT create();
35
36		HRESULT parallelFor( int threadsCount ) noexcept;
37	};
38}