thread_switcher
thread_switcher copied to clipboard
ProcessorAffinity overflow for CPU with 32 threads
For a 5950x CPU with 16C/32T, this erros happens when switching to last thread :
2021-01-05 15:42:41.092587 switching to Thread 20/32 Core 10/16
2021-01-05 15:42:41.492951 switching to Thread 21/32 Core 11/16
2021-01-05 15:42:41.908840 switching to Thread 22/32 Core 11/16
2021-01-05 15:42:42.309161 switching to Thread 23/32 Core 12/16
2021-01-05 15:42:42.706537 switching to Thread 24/32 Core 12/16
2021-01-05 15:42:43.103898 switching to Thread 25/32 Core 13/16
2021-01-05 15:42:43.502275 switching to Thread 26/32 Core 13/16
2021-01-05 15:42:43.902639 switching to Thread 27/32 Core 14/16
2021-01-05 15:42:44.298999 switching to Thread 28/32 Core 14/16
2021-01-05 15:42:44.697387 switching to Thread 29/32 Core 15/16
2021-01-05 15:42:45.093747 switching to Thread 30/32 Core 15/16
2021-01-05 15:42:45.490107 switching to Thread 31/32 Core 16/16
2021-01-05 15:42:45.888263 switching to Thread 32/32 Core 16/16
Exception lors de la définition de «ProcessorAffinity»: «Impossible de convertir la valeur «2147483648» en type «System.IntPtr». Erreur
: «L'opération arithmétique a provoqué un dépassement de capacité.»»
Au caractère Ligne:1 : 44
+ ... CESS in GET-PROCESS prime95) { $PROCESS.ProcessorAffinity=2147483648}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
My config :
cfg = {
##########################################################################
"process_to_switch": "prime95", # name of process (e.g. "cinebench") #
"thread_num": 32, # number of threads #
"sec_between_switch": 5, # number of seconds between switching threads #
"hyper_threading": True, #
##########################################################################
}
Thanks for reporting this bug! It seems like it's a 32-bit limitation with the PowerShell ProcessorAffinity property that I use. I have not been able to find a fix. We would need to find another way to set affinity mask for a process to really fix it. For now, I've added a workaround in the latest commit which simply uses the 31st thread instead of any higher specified thread.
This should not crash AND allow your last core to be loaded just as long as the other cores, even though only one of its threads is used.
Oh and I forgot to say but I only have 6 cores, 12 threads so I can't test this. Let me know if this doesn't work the way I thought it would.
And be mindful of how I changed the config from number of threads to number of cores!
You have to saturate to 2^30 and not 2^31. It works with this code :
@property
def affinity_mask(self):
return str(2**self.index) if self.index < 31 else str(2**30)
0 to 30th threads are effectively used, the 31th is not (as expected with this simple workaround)
Thanks, off by one indeed, fixed it now