www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: kevglass on September 22, 2009, 11:13:12 am

Title: Multithread/Multicore Software Rendering?
Post by: kevglass 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 :)
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen 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 (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.
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen on September 22, 2009, 04:41:25 pm
BTW: It may even hurt performance to enable it... :-\
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass 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
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen 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...
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen 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
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass on September 22, 2009, 09:46:57 pm
Oooh, that sounds freaking awesome :)

Kev
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass 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
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen on September 23, 2009, 10:05:49 pm
Nice applet...and here you go: http://www.jpct.net/download/beta/jpctapi_119pre2.zip (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...
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass 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
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass 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
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen 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.
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass 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
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass 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
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen 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.
Title: Re: Multithread/Multicore Software Rendering?
Post by: JavaMan on September 24, 2009, 04:21:29 am
Woah, some that is awesome, and it should only get better as Intel and AMD make more core processors!
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass on September 24, 2009, 09:08:57 pm
I've cross posted this onto JGO to try and get more performance figures:

http://www.javagaming.org/index.php/topic,21275

Kev
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen on September 24, 2009, 10:52:50 pm
Great. I'm very interested in those numbers myself. I have (Core2 Quad @ 3.2Ghz) 160fps in low and 95fps in high quality mode with an average load of around 60% (around 70% in high quality mode). I tried to improve some things, reduced the threading framework's overhead and added an option to blit the rendered image to screen in multiple threads, but both changes don't really cut it, which is why i haven't updated the uploaded version right now.
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen on September 27, 2009, 01:21:13 pm
Ok, i've uploaded another beta here: http://www.jpct.net/download/beta/jpctapi_119pre2_5.zip (http://www.jpct.net/download/beta/jpctapi_119pre2_5.zip)
It uses less worker threads spawned by the threading framework to do the same amount of work, i.e. it creates a little less overhead and it has an option to do multi threaded blitting, which i thought that it was a good idea, but it doesn't seem to cut it. Enable it with Config.useMultiThreadedBlitting=true;
In addition, it adds multi threading to the per-pixel fog processing of the software renderer.
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass on September 27, 2009, 09:39:25 pm
Not much change here. However, I did get 10fps or so from  using indoor mode and no zbuffer clearing (since my game fills the screen with polys).

Kev
Title: Re: Multithread/Multicore Software Rendering?
Post by: kevglass on September 29, 2009, 09:15:34 pm
Made a update to the "game" today to add more tracks/cars etc. It's just getting out of hand:

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

Kev
Title: Re: Multithread/Multicore Software Rendering?
Post by: EgonOlsen on September 29, 2009, 10:24:23 pm
Nice! It takes some time to get used to the limited view. But after a couple of rounds, it plays pretty well.
Title: Re: Multithread/Multicore Software Rendering?
Post by: JavaMan on September 30, 2009, 01:50:59 am
Cool dude! I really like how the car slows down when I release the gas. Maybe your previous version was like that, but I really noticed it this time.
Amazing this is in software.  ;D And, great startup time. You could teach Sun a thing or two on the Java user experience.  ;), since they still haven't got it down yet: http://javafx.com/samples/ (http://javafx.com/samples/)