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

KonstantinExperimental, alternative busy wait implementationcacec67

master
1.4 KiB66 linesraw
1#include "stdafx.h"
2#include "DelayExecution.h"
3
4namespace
5{
6	constexpr bool useHighRezTimer = false;
7
8	constexpr int64_t sleepMicroseconds = 200;
9
10	inline HRESULT sleepImpl( HANDLE timer )
11	{
12		constexpr int64_t sleepTicks = sleepMicroseconds * 10;
13
14		LARGE_INTEGER li;
15		// Negative values indicate relative time
16		li.QuadPart = -sleepTicks;
17		if( !SetWaitableTimerEx( timer, &li, 0, nullptr, nullptr, nullptr, 0 ) )
18			return getLastHr();
19		const DWORD res = WaitForSingleObject( timer, 50 );
20		if( res == WAIT_OBJECT_0 )
21			return S_OK;
22		if( res == WAIT_FAILED )
23			return getLastHr();
24		return E_FAIL;
25	}
26}
27
28void DelayExecution::sleepOnTheTimer( const DelayExecution& delay )
29{
30	HRESULT hr = sleepImpl( delay.timer );
31	if( SUCCEEDED( hr ) )
32		return;
33	logWarningHr( hr, u8"DelayExecution.sleepOnTheTimer" );
34}
35
36void DelayExecution::spinWait( const DelayExecution& )
37{
38	for( size_t i = 0; i < 1024; i++ )
39		_mm_pause();
40}
41
42void DelayExecution::sleep( const DelayExecution& )
43{
44	Sleep( 0 );
45}
46
47DelayExecution::DelayExecution()
48{
49	if constexpr( useHighRezTimer )
50	{
51		constexpr DWORD flags = CREATE_WAITABLE_TIMER_HIGH_RESOLUTION;
52		HANDLE h = CreateWaitableTimerEx( nullptr, nullptr, flags, TIMER_ALL_ACCESS );
53		if( nullptr != h )
54		{
55			timer.Attach( h );
56			pfn = &sleepOnTheTimer;
57			return;
58		}
59
60		const HRESULT hr = getLastHr();
61		logWarningHr( hr, u8"CreateWaitableTimerEx" );
62	}
63
64	pfn = &spinWait;
65	// pfn = &sleep;
66}