okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e28889d12
main
1/** 2* The host's copy of the roles rules it needs to WRITE a model's settings (src/lib/gpu-roles.ts has the screen's, strata_control/gpu_roles.py the 3* Terminal's and the server's; tests/data/gpu_roles_vectors.json is run against all three). 4* Roles are { [card number]: 'main' | 'helper' | 'off' }. The engine takes the cards in stage order: Helpers, then the Main card LAST. 5*/ 6export const ROLES = [ 'main' , 'helper' , 'off' ]; 7const mem = c => Number ( c . vram_gb ) || 0 ; 8const speed = c => ( typeof c . speed === 'number' && c . speed > 0 ?c . speed :0 ); 9const nums = roles => Object . keys ( roles ). map ( Number ). sort (( a , b ) => a - b ); 10const onList = roles => nums ( roles ). filter ( i => roles [ i ] !== 'off' ); 11const shortName = c => { let n = String ( c . name || `GPU ${ c . index + 1 } ` ); for ( const j of [ 'NVIDIA GeForce ' , 'NVIDIA ' , 'GeForce ' , 'AMD Radeon ' , 'AMD ' , 'Intel(R) ' , 'Intel ' ]) if ( n . startsWith ( j )) { n = n . slice ( j . length ); break ; } return n . trim (); }; 12 13export function parseSaved ( value ) { 14if ( ! value || typeof value !== 'object' || Array . isArray ( value )) return null ; 15const out = {}; 16for ( const [ k , v ] of Object . entries ( value )) { 17if ( ! /^\d + $ / . test ( k . trim ()) || ! ROLES . includes ( String ( v ). toLowerCase ())) return null ; 18out [ Number ( k )] = String ( v ). toLowerCase (); 19} 20if ( ! Object . keys ( out ). length || Object . values ( out ). filter ( r => r === 'main' ). length > 1 ) return null ; 21return out ; 22} 23 24export function normalize ( roles , cards ) { 25const cs = new Map ( cards . map ( c => [ c . index , c ])); 26const out = {}; 27for ( const i of nums ( roles )) if ( cs . has ( i )) out [ i ] = ROLES . includes ( roles [ i ]) ?roles [ i ] :'off' ; 28const list = onList ( out ), mains = list . filter ( i => out [ i ] === 'main' ); 29if ( list . length && ! mains . length ) { 30const best = [ ...list ]. sort (( a , b ) => speed ( cs . get ( b )) - speed ( cs . get ( a )) || Math . round ( mem ( cs . get ( b ))) - Math . round ( mem ( cs . get ( a ))) || a - b )[ 0 ]; 31out [ best ] = 'main' ; 32} 33for ( const extra of mains . slice ( 1 )) out [ extra ] = 'helper' ; 34if ( list . length === 1 ) out [ list [ 0 ]] = 'main' ; 35return out ; 36} 37 38export function order ( roles , cards ) { 39const cs = new Map ( cards . map ( c => [ c . index , c ])); 40const r = normalize ( roles , cards ); 41const helpers = nums ( r ). filter ( i => r [ i ] === 'helper' ). sort (( a , b ) => speed ( cs . get ( a )) - speed ( cs . get ( b )) || a - b ); 42return [ ...helpers , ...nums ( r ). filter ( i => r [ i ] === 'main' )]; 43} 44 45/** "gpu_roles" plus the legacy mirror ("gpu" Main last, "gpu_order": "as_given"); nothing on: "gpu": [] and "gpu_none": true. */ 46export function mirror ( roles , cards ) { 47const r = normalize ( roles , cards ), lst = order ( r , cards ); 48const out = { gpu_roles :Object . fromEntries ( nums ( r ). map ( i => [ String ( i ), r [ i ]])), gpu :[], gpu_cards :{} }; 49if ( lst . length ) { out . gpu = lst . length === 1 ?lst [ 0 ] :lst ; out . gpu_order = 'as_given' ; } else out . gpu_none = true ; 50for ( const c of cards ) if ( c . index in r ) out . gpu_cards [ String ( c . index )] = [ shortName ( c ), c . bus ?? '' ]. filter ( Boolean ). join ( '|' ); 51return out ; 52}