Author Topic: Multithread/Multicore Software Rendering?  (Read 13845 times)

Offline kevglass

  • byte
  • *
  • Posts: 13
    • View Profile
Multithread/Multicore Software Rendering?
« on: September 22, 2009, 11:13:12 am »
Sorry if this has been asked before, I can't find the answer on the wiki or through searching the forums.

I'm using the software renderer at the moment (very cool btw!) and was wondering if it's possible to use more than one thread/core to generate the output image if available. At the moment I'm seeing 100% usage of a single CPU on multicore machines.

Thanks for any help (and for the wonderful jpct)

Kev

PS. Just experiementing at the moment, will write up on cokeandcode soon :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Multithread/Multicore Software Rendering?
« Reply #1 on: September 22, 2009, 04:32:33 pm »
Well, i attempted to add support for multiple cores/cpus a few times in the past. Always with a different approach (split per polygon, split per scan line, split the screen into different sections)...and none was really satisfying. With version 1.19, i tried another, much simpler, approach to add at least some support for it to the software renderer. You can try it by using this version: http://www.jpct.net/download/beta/jpctapi_119pre1.zip
If you set Config.useMultipleTreads to true, the software renderer will use two cores for a tiny little bit of the rendering. The benefits aren't very impressive right now (if any...)...it helps most when using oversampling. It's still experimental, so it may improve in the future.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Multithread/Multicore Software Rendering?
« Reply #2 on: September 22, 2009, 04:41:25 pm »
BTW: It may even hurt performance to enable it... :-\

Offline kevglass

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Multithread/Multicore Software Rendering?
« Reply #3 on: September 22, 2009, 04:51:16 pm »
Thanks for that, performance is pretty reasonable for me at the moment, but I'm just trying out options :)

I thought the sectioning of the screen approach would be best. I'll certainly give it a try.

Not sure why I haven't tried JPCT before, it's really very light and easy. Very cool!

kev

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Multithread/Multicore Software Rendering?
« Reply #4 on: September 22, 2009, 05:06:25 pm »
I thought the sectioning of the screen approach would be best. I'll certainly give it a try.
Yes, i will try that myself again in the next few days and see what happens...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Multithread/Multicore Software Rendering?
« Reply #5 on: September 22, 2009, 09:43:08 pm »
I did some tests and it works out pretty well so far. With a simple test case on a Core2 Quad@3.2Ghz:

1 core:    85fps
2 cores: 140fps
3 cores: 180fps
4 cores: 210fps

Offline kevglass

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Multithread/Multicore Software Rendering?
« Reply #6 on: September 22, 2009, 09:46:57 pm »
Oooh, that sounds freaking awesome :)

Kev

Offline kevglass

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Multithread/Multicore Software Rendering?
« Reply #7 on: September 22, 2009, 11:58:53 pm »
Let me know when you have something to test, I've only got a little test racer atm, but I'd be really interested to see the results on my local box with multithreaded rendering.

http://www.cokeandcode.com/applets/jpct

Kev

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Multithread/Multicore Software Rendering?
« Reply #8 on: September 23, 2009, 10:05:49 pm »
Nice applet...and here you go: http://www.jpct.net/download/beta/jpctapi_119pre2.zip

It's all pretty experimental, but at least on my machine, the results are promising so far.

Enable it with:

Code: [Select]
Config.useMultipleThreads=true;

set the number of cores to use (i can't do this automatically, because only Java 1.4 and higher can access this information):

Code: [Select]
Config.maxNumberOfCores=<int>;

Enable a debugging output in the rendered scene that shows the load balacing, i.e. which core processes which part:

Code: [Select]
Config.mtDebug=true;

And choose between static and dynamic (fun to watch when mtDebug=true... ;D) load balancing:

Code: [Select]
Config.loadBalancingStrategy=0/1;

Please let me know how it works for you...

Offline kevglass

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Multithread/Multicore Software Rendering?
« Reply #9 on: September 23, 2009, 10:40:12 pm »
Frickin awesome, I'll upload the new applet in a moment. I get a 60fps->100fps increase, about 66%? 2 Cores here so thats fantastic.

Great work!

Kev

Offline kevglass

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Multithread/Multicore Software Rendering?
« Reply #10 on: September 23, 2009, 10:48:02 pm »
I've updated the applet with the new pre-release. I've got a bunch of folks on IRC testing it out, should get some more figures soon.

Kev

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Multithread/Multicore Software Rendering?
« Reply #11 on: September 23, 2009, 11:05:38 pm »
Cool! I've updated that 1.19pre2 version with another one that should improve dynamic load balancing (in case you are actually using it) for Java5 and above.
« Last Edit: September 23, 2009, 11:16:06 pm by EgonOlsen »

Offline kevglass

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Multithread/Multicore Software Rendering?
« Reply #12 on: September 23, 2009, 11:22:46 pm »
Ok, strange results here.

I'm using:

      Config.useMultipleThreads = true;
      Config.loadBalancingStrategy = 0;
      Config.maxNumberOfCores = Runtime.getRuntime().availableProcessors();

On my box (2 processors, 1 core each) it seems to utilise both.
On another box (1 process, 2 cores) it seems to only use one thread
On a third box (4 process, 2 cores each) it seems to use each processor, but only at 40%ish.

I've had people check what Runtime.getRuntime().availableProcessors() returns, each time it returns the number of hardware threads available (equal to the cores). So above 2, 2 and 8.

Should I be able to use more cores than processors?

Kev

Offline kevglass

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Multithread/Multicore Software Rendering?
« Reply #13 on: September 23, 2009, 11:27:24 pm »
The guy (kappaOne) with 4 processors reports that each one is being utilised to about 30-35%.

Kev

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Multithread/Multicore Software Rendering?
« Reply #14 on: September 23, 2009, 11:38:39 pm »
It's actually pretty dumb...it spawns as many worker threads as you tell it in Config.maxNumberOfCores. You may as well configure it for 32 threads (as pointless as that may be on today's hardware) and it will use 32 threads for rendering. You can easily visualize this by using Config.mtDebug=true;
In addition to the rendering, it uses up to two threads for clearing the frame- and zbuffers. Anything more than that doesn't make any sense, but it will reduce cpu usage on manycores as well as displaying the rendered image in the browser does, because not all cores will be fully utilized in that stage.
How the threads are scheduled to the cores/virtual cores is up the OS...and you'll get some overhead from the thread management itself. On my quad core, your applet uses around 55% of all four cpus. That a little less than my test cases do, but those don't include any game play and no browser overhead.