Chaos Corona Forum

Chaos Corona for 3ds Max => [Max] Corona Goodies - User Contributions => Topic started by: Nik on 2015-09-03, 14:23:49

Title: Priority Affinity switch
Post by: Nik on 2015-09-03, 14:23:49
Download script (https://dl.dropboxusercontent.com/s/x7a57o81sjuk9y4/NSmacro_PrioritySwitch.mcr?dl=1)

Hi, guys. Maybe somebody will find my small script useful.

Button on the toolbar:
Enabled - 3dsmax priority low, affinity -1 core
Disabled - 3dsmax proirity normal, affinity max

Drag in viewport to install. Check NikScripts category - Priority Switch and add it to toolbar.

Title: Re: Priority Affinity switch
Post by: atelieryork on 2015-09-03, 15:29:48
Thanks! One issue - once I enable it, it works, but then if I press it again it doesn't go back to full affinity. It seems locked.
Title: Re: Priority Affinity switch
Post by: Nik on 2015-09-03, 15:32:56
Can you send me sysinfo.systemAffinity answer in the Listener?
F11 -> sysinfo.systemAffinity -> copy next string
Title: Re: Priority Affinity switch
Post by: atelieryork on 2015-09-03, 15:35:59
281474976710655P
Title: Re: Priority Affinity switch
Post by: Nik on 2015-09-03, 16:46:43
Can somebody explain me how to control CPU cores via maxscript when I got >32 cores?
It seems there is a bug and maximum cores that "sysinfo.processAffinity" can handle is 32...
Title: Re: Priority Affinity switch
Post by: Ondra on 2015-09-03, 17:19:37
I dont know about maxscript, but in C there is separate method for using more than 64 cores, available only in windows 7 and newer OS.
Title: Re: Priority Affinity switch
Post by: Nik on 2015-09-03, 17:33:16
Google and MaxScript documentation doesn't helped too much. I hope somebody will able to help me here.

Thanks for answer, Ondra. Can you please explain why Corona behaves so strange when I use "# of threads" feature?
If I set it to 1 then one thread must render my scene at full speed, and other threads must be free. This is how TaskManager Affinity behaves.
But Corona let all cores render, and total CPU loading is something about 13%. So it doesn't free threads? It just controls cpu loading?
Look at the attached screenshot. All cores are busy, but loading isn't full, it's kinda float in time. TaskMan shows total cpu load as 11-13%
Title: Re: Priority Affinity switch
Post by: Ondra on 2015-09-03, 22:08:51
You are confusing threads and cores. You typically have around 8 cores, and each one can run just one thread at single time. If an application spawns several threads, these are either waiting on something (such as user input), or want to run (e.g. render something). All runnable threads in the whole system - from all applications - compete for the fixed number of cores to run on. There are hundreds or thousands of threads, but most of them are waiting most of the time. If there are more threads that want to run than number of cores, then the threads take turns in running - 8 run at the same time, others wait and replace them after few milliseconds (200 by default in windows)

Setting number of threads to 1 makes Corona do heavy lifting (= rendering calculation) in just one thread. The operating system is free to schedule this one thread to run on any CPU core, and it usually periodically moves the thread around all available cores. You dont want to have only one CPU core to run all singlethreaded applications, as it could cause problems with for example overheating one part of the CPU die.

You can artificially limit which CPU core can each thread run on, but trust me, you dont want to do it - operating system does much better job at scheduling than you could do. If you limit Corona to just one specific core, and that core is not available, then it will have to wait, and you will lose performance
Title: Re: Priority Affinity switch
Post by: Nik on 2015-09-03, 22:26:34
I need to read this answer 3-4 times to totally understand it, and I will :) I know that threads != cores, but I didn't know what exactly the thread is. Thanks.
So maybe I don't need to change Affinity in this primitive manner. Maybe you can make ".system_numThreads" accessible during render? Because now changing this value while rendering doesn't change anything until I restart render.

Unfortunately Corona hangs other software while rendering, despite 3dsmax low priority and ".system_lowThreadPriority = true". Affinity "-1 thread" solves this problem very well for me.
Title: Re: Priority Affinity switch
Post by: Ondra on 2015-09-03, 22:39:54
Think of it as workers vs. workstations - an office has fixed number of seats, but variable number of people can come on each day. If too many come - some will have to wait for their turn (200 milliseconds), kick somebody out, and take his seat ;). If for some reason you forbid 2 people from sitting anywhere but 1 seat, they will have to fight for it even if other seats are empty - so you dont limit thread affinity ;)
Title: Re: Priority Affinity switch
Post by: Nik on 2015-09-03, 23:00:41
My example with only 1 thread was a mistake. I'm not insane and usually reserve only "one seat" for chrome + photoshop + ae etc :) It's enough.
Of course I don't want exactly 1 seat, but it seems Corona is a hardworker and she REALLY doesn't want to share any seat, she needs all of them! :) I like her aspiration to make render faster, but sometimes when I render I need to do something else. But not all the time. And I don't know when :) And then again I want to render as fast as she can. So, when I need one seat - I change affinity in taskmanager because she doesn't care about low priority. Kicking Corona off the seat is ok, but I need 5 clicks every time. Well, now I got script for myself. But it doesn't work on Xeons. I'm not sure I'm able to fix it.

Thats why I request a little feature - interactive "# of thread" while rendering. If it's possible of course.

P.S. You know, many people decrease affinity for Corona. I know that from forums.
Title: Re: Priority Affinity switch
Post by: romullus on 2015-09-03, 23:12:47
In my experience more often than not Corona behaves very nicely under low priority. Sometimes i even don't notice that rendering is going on. But there is times when it's really CPU hungry and makes working with pc nearly impossible. Not sure what causes this, maybe constant build change?
Title: Re: Priority Affinity switch
Post by: Ondra on 2015-09-04, 00:00:11
it may not work on xeon because they use hyperthreading, which allows 2 threads to share 1 core. It reports as 2 separate cores ("logical cores"), but if 1 is busy, the second one will get slower because it shares some stuff.
Title: Re: Priority Affinity switch
Post by: Nik on 2015-09-04, 11:56:12
I posted script on Scriptspot to find people who can help me. If somebody have idea how to fix this bug - please leave comments there.
http://www.scriptspot.com/3ds-max/scripts/affinity-priority-switch (http://www.scriptspot.com/3ds-max/scripts/affinity-priority-switch)
Title: Re: Priority Affinity switch
Post by: Nik on 2015-09-11, 13:31:37
I fixed script and now it must work on >32 threads PC.
There was a bug in maxscript so I've used dotnet to control Affinity. Please try and tell me if bug is still there.
Title: Re: Priority Affinity switch
Post by: atelieryork on 2015-09-14, 12:16:26
Hi Nik,

Script works fine here on my machine. Good job, thank you :)
Title: Re: Priority Affinity switch
Post by: atelieryork on 2015-10-06, 10:49:48
Works perfectly, thank you :) I have requested that maybe this script could be bundled with nguyendungvl's Corona Toolbar. Would be great to have it there for easy access ;)
Title: Re: Priority Affinity switch
Post by: Nik on 2015-10-06, 11:00:21
I don't mind :)