Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

How can I retrieve the number of cores?

Thread view: 
Martin Gerner - 10 May 2007 10:40 GMT
Hi,

I am currently working on a quite computationally heavy program. As it
would be possible for me to do computational tasks in parallell, I would
like to be able to use several cores when the program is being run on
computers that have such processors.

Is there any way of getting information from the system about how many
cores the current machine is using? I don't want to spawn more threads than  
necessary, as the memory usage would go up too much.

Signature

Martin Gerner

Lothar Kimmeringer - 10 May 2007 11:08 GMT
> I am currently working on a quite computationally heavy program. As it
> would be possible for me to do computational tasks in parallell, I would
[quoted text clipped - 3 lines]
> Is there any way of getting information from the system about how many
> cores the current machine is using?

I'm not aware of a Java System-property but there is an
environment-variable "NUMBER_OF_PROCESSORS" on Windows-
systems. On Linux you can do a
grep -c "processor" < /proc/cpuinfo
to find out the number.

To solve this thing platform-independent set a system-
property "myapp.number.cpu" within the start-scripts
for the correspondent platform, so in start.bat
you do -Dmyapp.number.cpu=%NUMBER_OF_PROCESSORS%
and in start.sh you do
-Dmyapp.number.cpu=`grep -c "processor" < /proc/cpuinfo`

The System-property you can use for initializing the
number of threads to be used and do it with a default-
value of the property is not set.

> I don't want to spawn more threads than  
> necessary, as the memory usage would go up too much.

Eight threads shouldn't consume too much memory and is
sufficient for nowerdays multi-core-CPUs up to systems
with four installed CPUs.

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Tom Hawtin - 10 May 2007 12:25 GMT
> Eight threads shouldn't consume too much memory and is
> sufficient for nowerdays multi-core-CPUs up to systems
> with four installed CPUs.

Sun T1000 and T2000 (available since 2005, IIRC), are single chip
machines with up to 32 hardware threads. IIRC, Azul will sell you a
machine with 768 cores. There is no point hardwiring such assumptions.

Tom Hawtin
Lothar Kimmeringer - 10 May 2007 12:27 GMT
>> Eight threads shouldn't consume too much memory and is
>> sufficient for nowerdays multi-core-CPUs up to systems
[quoted text clipped - 3 lines]
> machines with up to 32 hardware threads. IIRC, Azul will sell you a
> machine with 768 cores. There is no point hardwiring such assumptions.

I was talking of a default-value if no value can be retrieved
otherwise. I didn't know Runtime.availableProcessors until today.

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Martin Gerner - 10 May 2007 20:55 GMT
>> I don't want to spawn more threads than  
>> necessary, as the memory usage would go up too much.
>
> Eight threads shouldn't consume too much memory and is
> sufficient for nowerdays multi-core-CPUs up to systems
> with four installed CPUs.

In my case, I believe it would. I have to crank the maximum allowed memory
space for the JVM to 256 MiB just for one thread. And it is perfectly
possible that the program will be run on significantly larger instances at
later stages.

Signature

Martin Gerner

Tom Hawtin - 10 May 2007 11:08 GMT
> Is there any way of getting information from the system about how many
> cores the current machine is using? I don't want to spawn more threads than  
> necessary, as the memory usage would go up too much.

Runtime.availableProcessors will tell you how many hardware threads the
JVM has available to it.

Tom Hawtin
Martin Gerner - 10 May 2007 20:38 GMT
Tom Hawtin <usenet@tackline.plus.com> wrote in news:4642eea7$0$8759
$ed2619ec@ptn-nntp-reader02.plus.net:

> Runtime.availableProcessors will tell you how many hardware threads the
> JVM has available to it.

Thanks, that looks to be about what I need.
I imagine that on, for example, an intel dual-core processor, that integer
would be 2? (That is - it does reflect the number of _cores_, not the
actual number of physical processors?)

It's strange that I missed it, as I did check both System and Runtime in
the API for suitable pieces.. I guess I focused too much on the functions.

Signature

Martin Gerner

Daniel Dyer - 10 May 2007 21:16 GMT
> Tom Hawtin <usenet@tackline.plus.com> wrote in news:4642eea7$0$8759
> $ed2619ec@ptn-nntp-reader02.plus.net:
[quoted text clipped - 7 lines]
> would be 2? (That is - it does reflect the number of _cores_, not the
> actual number of physical processors?)

Yes.  I have an Intel Core Duo and it returns 2 on that.

Dan.

Signature

Daniel Dyer
https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.