How can I retrieve the number of cores?

M

Martin Gerner

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.
 
L

Lothar Kimmeringer

Martin said:
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'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
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Tom Hawtin

Martin said:
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
 
T

Tom Hawtin

Lothar said:
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
 
L

Lothar Kimmeringer

Tom said:
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.

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


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Martin Gerner

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.
 
M

Martin Gerner

I don't want to spawn more threads than
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.
 
D

Daniel Dyer

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?)

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

Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top