In my application I need to find out how many processors the host system has, in order to start an appropriate numer of threads (in my case numberOfThreads = numberOfCPUs). Is there a standard means of achieving this in Java? If not, can anyone please give me a hint how to do this platform-independantly with JNI… (I never used native code, so please explain this in a fool-proof manner) Thanks a lot!
java.lang.Runtime.availableProcessors()
added in 1.4
Thanks, that will be optimal. Strange they added this functionality so late…
An other question comes to my mind. What about this new hyperthreading technology of intels P4 (I read of a 25% performance increase): will a multithreaded java application benefit from it? If that’s true, I should use the parallel algorithm with two threads even on a single processor system, hmm… how to detect a hyperthreading processor?
In my own tests I get a gain from HT nearer 10% — not enough to be worth worrying about. Possibly because my machine is a 3.06GHz model with only the 533FSB. Allegedly the 800MHz FSB machines show greater benefits from HT. Even so it still looks more like a marketing feature than something of significant value.
I think a HT processor (with it enabled) will show as having 2 processors available. You could run two threads in parallel and see if the elapsed time looks like the result of a genuine dual or a HT processor.
FYI: With my company’s non-java stuff (media encoding applications and device drivers) we found hyperthreading caused problems. Some systems only worked with hyperthreading off. The code works fine on traditional multi-processor systems.
Our general opinion around Sun, which you can buy or not, is that Intel’s so called “HT” is a cheap and cheesy attempt to respond to sun’s Sparc CMT (On-Chip Multi-Tasking), except its not nearly as functional or useful.
Jeff, I think Intel has proven that the cheap-and-cheerful approach often wins out over precise-but-expensive!
I had a co-worker once who owned a number of very high-quality 2400 baud modems - he complained that today’s tacky modems were unreliable. It seemed rude to point out how many times a 56K can resend any duff data while the 2400 sends the message just once.
But yeah, I personally wouldn’t bother optimising Java code to take better advantage of HT. May as well go back to hand-tuned assembly…
[quote]Our general opinion around Sun, which you can buy or not, is that Intel’s so called “HT” is a cheap and cheesy attempt to respond to sun’s Sparc CMT (On-Chip Multi-Tasking), except its not nearly as functional or useful.
[/quote]
Who got DEC’s work on this, in the end? I guess Intel (?), but maybe HPaq managed to hang onto it…? I vaguely recall that the DEC design had good potential, but from what little (nb: very little) I’ve seen of Intel’s current impl, I’d agree it doesn’t seem much good.
OTOH, AFAICS Intel still has major problems with the fact that they don’t have a 32-bit multiprocessor architecture (…until they get with the program and start using crossbar switches or some equivalent…), so perhaps HT / MT / HPT (whatever you want to call it ;)) will prove a way out that doesn’t involve 128Mb cache on each CPU just to keep it busy…
[quote]Jeff, I think Intel has proven that the cheap-and-cheerful approach often wins out over precise-but-expensive!..
[/quote]
But not if all youa re "buying’ is a name with no real advantage in the hadrware. AIUI this is HT. 99% hype, tiny bit of functionality that a really clever assmebly coder might be able to make some use out of in just the right situations.
Again though I’m NOT a processor expert. This is just my general imrpession from tlaking to flks who are.
[quote]…really clever assmebly coder might be able to make some use out of in just the right situations…
[/quote]
That would be, uh, a JVM engineer then?
Cas
One thing I have noticed with Hyperthreading is that comparing on a multiprocessor system, the time taken to compile byte-codes is much faster (like 10x faster). Of course, you can just put on -Xbatch and then HotSpot’s compiler runs in the foreground, which is more efficient. Basically, if the main thread is on 1 CPU and HotSpot is on the 2nd, then the communication overhead between the threads slows the optimisation down a bit. With Hyperthreading, the optimiser runs on the same CPU as the main thread, so is more efficient.
How much effect Hyperthreading has is highly dependant on your benchmark - with some server benchmarks, a 50% boost is possible, but this is rare.
btw, the latest Pentium 4 (Prescott) has a much longer pipeline making it even less suitable for server work, or more complex code in general. If you want a good development or runtime platform for Java, I’d suggest getting an Opteron or Athlon 64.
If you want more of an introduction to multi-core chip designs (like what Sun is working on), then read my recent article ^-^
http://www.aceshardware.com/read.jsp?id=60000312