preprocessor number of cpus

K

Klaas Vantournhout

Hi,

I am currently using OpenMP (gcc 4.2.0) to do parallel computing on a
computer with a certain amount of cpu's.

I need the total number of cpu's as a variable in my code. So I was
wondering how I could do this. Does there exists a system call for that
or something or is the best way just to take this out of /proc/cpuinfo?

I also would like to use some preprocessor code in it to cancel out the
pragma directive of omp when I only have one cpu. Is there a way to
do this?



Thanks
Klaas
 
P

Puppet_Sock

Klaas said:
Hi,

I am currently using OpenMP (gcc 4.2.0) to do parallel computing on a
computer with a certain amount of cpu's.

I need the total number of cpu's as a variable in my code. So I was
wondering how I could do this. Does there exists a system call for that
or something or is the best way just to take this out of /proc/cpuinfo?

I also would like to use some preprocessor code in it to cancel out the
pragma directive of omp when I only have one cpu. Is there a way to
do this?

Not in a standard compliant way. You will need to ask this in
a group that is concerned with your particular cpu, or OpenMP
(I presume that's the multi-cpu lib), or possibly your platform,
OS, or hardware. You need to go outside the standard language,
so you need to go outside of htis news group.
Socks
 
A

Andre Kostur

Puppet_Sock said:
Not in a standard compliant way. You will need to ask this in
a group that is concerned with your particular cpu, or OpenMP
(I presume that's the multi-cpu lib), or possibly your platform,
OS, or hardware. You need to go outside the standard language,
so you need to go outside of htis news group.
Socks

Note that presumably you want to know the number of CPUs at runtime, not
compile time. Thus a preprocessor pragma (or variable) would be nearly
useless to you. Most programs don't much care about the compiling
environment... they usually care about the execution environment.
 
K

Klaas Vantournhout

Andre said:
Note that presumably you want to know the number of CPUs at runtime, not
compile time. Thus a preprocessor pragma (or variable) would be nearly
useless to you. Most programs don't much care about the compiling
environment... they usually care about the execution environment.

On the contrary, I would like to obtain it at compile time. That is why
I was wondering if there is some kind of preprocessor variable that
contains the number of cpus dynamically depending on the machine you
compile it on.


If this is possible I would be able to use the following code to exclude
the OpenMP (if I am right of course)

#if NCPU != 1
#include <omp.h>
#endif

....

#if NCPU != 1
#pragma omp parallel private(tid)
#endif

....

So does there exist a predefined number of processors or is this non
existent and do I have to use a define directive myself. And if this is
the case, can I at compile time change it automatically by , lets say a
make script?

Thanks

Klaas
 
I

Ian Collins

Klaas said:
Andre Kostur wrote:




On the contrary, I would like to obtain it at compile time. That is why
I was wondering if there is some kind of preprocessor variable that
contains the number of cpus dynamically depending on the machine you
compile it on.


If this is possible I would be able to use the following code to exclude
the OpenMP (if I am right of course)

#if NCPU != 1
#include <omp.h>
#endif

...

#if NCPU != 1
#pragma omp parallel private(tid)
#endif

...

So does there exist a predefined number of processors or is this non
existent and do I have to use a define directive myself. And if this is
the case, can I at compile time change it automatically by , lets say a
make script?
No. It's of no value in most cases.

You can set own and change it at compile time, but in a non-standard
way. Many compilers use -D to define preprocessor constants.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top