SwingWorker: All for one OR one for all?

So is it "all for one" or "one for all"? Well, it depends. It depends on how many processors/cores you have, and it depends on the operating system you have. Let me explain myself.

As you know (Sun's) Java schedules threads in as many processors as you own, at least on most operating systems that take advantage of multi-processor systems:

A major advantage of using native OS threads and scheduling is the ability to take advantage of native OS multiprocessing support transparently. Because the Java HotSpot VM is designed to be insensitive to race conditions caused by preemption and/or multiprocessing while executing Java programming language code, the Java programming language threads will automatically take advantage of whatever scheduling and processor allocation policies the native OS provides.

So, if you have different processors and run different Java threads you automagically take advantage of all those processors.

One for all case

You are running on a multiprocessor system, and one thread may be scheduled in any of those processors; this is the "one for all" case.

All for one case

You run on a single processor system, and thus we are in the "All for one" situation, all threads are scheduled in the same processor.

Mandelbrot and Amdahl's law

So what has all this well-known facts have to do with Mandelbrot? What about Amdahl?

The Mandelbrot fractal is highly parallelizable. Computing the Mandelbrot fractal means basically examining each dot in a complex plane; and each one of those computations for each point in the complex plane has no relation with the rest of the complex plane. So, why not schedule different threads for different regions in the plane? That would allow us to take advantage of multi-processor (multi-core) systems and make things faster, right?

Assume that computing the Mandelbrot fractal takes 95% of time and that rendering it (on the single Swing thread, which is not parallelizable) requires 5% of the time. (These are ball park estimates, obviously, Swing is probably faster than that). According to Amdahl's law we can speed up the Mandelbrot explorer by a factor of 1.9 in a system with 2 processors, a factor of 2.7 in a system with 3, and a (maximum) factor of 20 with lots of processors. That's quite impressive, right?

Obviously the Mandelbrot speed-up is observable in the "one for all" case, where we are running in multiprocessor systems.

But anyway we can also see some speedup in the "all for one" case, when we run on a single processor. Why you ask? Well, because there're regions of the complex plane that are faster to compute and require less iterations, so those will be finished earlier and will be rendered earlier. So the "apparent performance" of the Mandelbrot Explorer will be better. Of course spanning different threads will some more JVM work (synchronization, scheduling), but I think this little overhead is worth the effort.

You can take advantage, too

So, to summarize, you can take advantage of scheduling SwingWorkers even on single-processor boxes. An application that runs different queries against a database (J2EE server, web-service server, RSS feed server, whatever) will greatly benefit of parallelization.

An example

So I have modified the Mandelbrot Explorer program to span different SwingWorkers to compute the Mandelbrot fractal. You can choose between 1x threads, 4x threads or 16x threads to compute the fractal (Choose "Configuration/Configure" in the menu). If things are too fast to notice you may try selecting a different detail level as well. Launch the JDK 5.0 demo here. It's still somewhat buggy but I won't be able to post until next week and I prefer posting it today for you to enjoy . Source code will be available by next week, as soon as I correct some little bugs.

If you own a dual-core system or may run this on a multiprocessor system I would greatly appreciate feedback on performance. I would also like to know if all your cores show activity when exploring the Mandelbrot fractal and, if so, which operating system (and JVM) you're running on. Thanks!

Happy Swing threading,
Antonio

Comentarios:

Just ran some quick tests, I'll comment on the article on my next break:

<table cellspacing=0 cellpadding=0 style="border: 1px #000 solid; color: black;"> <thead style="background:#BDB76B; "> <tr> <th style="border-bottom: 1px black solid; border-right: 1px black solid;">  # Threads  </th> <th style="border-bottom: 1px black solid; border-right: 1px black solid;">    DC time </th> <th style="border-bottom: 1px black solid; border-right: 1px black solid;">    SC time   </th> <th style="border-bottom: 1px black solid; border-right: 1px black solid;">    DL    </th> <th style="border-bottom: 1px black solid; border-right: 1px black solid;">  DC Usage  </th> <th style="border-bottom: 1px black solid;">  SC Usage  </th> </tr> </thead> <tbody style="color: black; vertical-align: middle; text-align: center;"> <tr style="background: #C0C0C0;"> <td style="border-right: 1px black solid;"> 1 </td> <td style="margin-left: 2px; border-right: 1px black solid;"> 5.781 </td> <td style="border-right: 1px black solid;"> 5.15 </td> <td style="border-right: 1px black solid;"> 2500 </td> <td style="border-right: 1px black solid;"> 47% </td> <td> 100% </td> </tr> <tr style="background: #C0C0C0;"> <td style="border-right: 1px black solid;"> 4 </td> <td style="border-right: 1px black solid;"> 3.334 </td> <td style="border-right: 1px black solid;"> 5.578 </td> <td style="border-right: 1px black solid;"> 2500 </td> <td style="border-right: 1px black solid;"> 98% </td> <td> 100% </td> </tr> <tr style="background: #C0C0C0;"> <td style="border-bottom: 1px black solid; border-right: 1px black solid;"> 16 </td> <td style="border-bottom: 1px black solid; border-right: 1px black solid;"> 3.192 </td> <td style="border-bottom: 1px black solid; border-right: 1px black solid;"> 4.984 </td> <td style="border-bottom: 1px black solid; border-right: 1px black solid;"> 2500 </td> <td style="border-bottom: 1px black solid; border-right: 1px black solid;"> 99% </td> <td style="border-bottom: 1px black solid;"> 100% </td> </tr> <tr style="background: #ADD8E6;"> <td style="border-right: 1px black solid;"> 1 </td> <td style="border-right: 1px black solid;"> 2.703 </td> <td style="border-right: 1px black solid;"> 2.828 </td> <td style="border-right: 1px black solid;"> 500 </td> <td style="border-right: 1px black solid;"> 32% </td> <td> 100% </td> </tr> <tr style="background: #ADD8E6;"> <td style="border-right: 1px black solid;"> 4 </td> <td style="border-right: 1px black solid;"> 0.875 </td> <td style="border-right: 1px black solid;"> 1.422 </td> <td style="border-right: 1px black solid;"> 500 </td> <td style="border-right: 1px black solid;"> 98% </td> <td> 100% </td> </tr> <tr style="background: #ADD8E6;"> <td style="border-right: 1px black solid;"> 16 </td> <td style="border-right: 1px black solid;"> 0.83 </td> <td style="border-right: 1px black solid;"> 1.391 </td> <td style="border-right: 1px black solid;"> 500 </td> <td style="border-right: 1px black solid;"> 99% </td> <td> 100% </td> </tr> </tbody> </table>

Key: DC - Duel Core, SC - Single Core, DL - Detail Level
Just noticed the white text on preview, I don't have time to guess why it is, I'm guessing something in default CSS for this blog changed the default text color. Oh well. :)

Enviado por Jeffrey Olson en agosto 31, 2005 a las 11:27 AM CEST #

Oh yea, both machines are WinXP using 1.5.0 update 4.

Enviado por Jeffrey Olson en agosto 31, 2005 a las 11:52 AM CEST #

Wonder if, at all, hyperthreading would have an inpact on this? My machine at home is same as my single core machine here at work, cept has hyper threading, I'll give it a try there and see what happens.

Enviado por Jeffrey Olson en agosto 31, 2005 a las 02:54 PM CEST #

I ran it on a hyperthreaded Dell machine JDK 5.0 Windows box. It definitely used both processors in the task manager window. I tried 4 threads vs. 1 thread, and there was a difference. Sometimes a lot somtimes only a little. It could be because I was running a lot of other things while I was exploring.

Enviado por chubbard en agosto 31, 2005 a las 03:18 PM CEST #

Are you going to post the code? I wouldn't mind taking a look at it.

Enviado por chubbard en agosto 31, 2005 a las 03:20 PM CEST #

He said he was going too, but it would be sometime next week as he wants to clean it up a bit and fix bugs. (Hoping its a he given the name, sorry if I'm incorrect)


The thing with hyperthread is its not really two processors, its more of an abstraction to fake the ability to execute two threads at once but its still a true single core machine. I would hope for improvement versus a non-HT machine, but as you pointed out, and is very true, depending on how busy your machine is it can get bogged down and show little to no improvement. They aren't, obviously, the same as a duel/multi core, or duel/multi cpu, machine but they attempt to do more with what they have then the average machine. Always have wondered what a duel core/cpu using HT in each core/cpu would achieve if anything much at all as there is a diminishing returns for most things with this type of stuff.

Just my take on it, and I might be wrong on it. :)

Enviado por Jeffrey Olson en agosto 31, 2005 a las 03:31 PM CEST #

Enviar un comentario:
Los comentarios han sido deshabilitados.
About

swinger

Search

Archives
« abril 2014
lunmarmiéjueviesábdom
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Hoy