-   Linux - Newbie (
-   -   How to identify current CPU in a parallel machine (

Feynman 09-01-2010 11:19 PM

How to identify current CPU in a parallel machine
I do not know much about parallel computing, but I do have access to a few cpus. I am trying to write a program compatible for multiple cpus, but to do it, I need to ensure unique directories. I have figured out that the time of creation, process id, and cpu id form a unique combination I can use to name files (up to the degree of accuracy of the internal timer).
Anyway, I am using perl. I know how to find the process id and I know I can get the time pretty easily, but as I said, I do not know much about hardware or parallel computing. All I need is a way to identify each cpu in the system and I am set for half of my program (the other half is significantly more straightforward and is mainly about text parsing.)

So anyway using terminal, perl, etc., I could access this identification? Or could someone at least tell me the name of what I am looking for? I have seen the words "ip" "MAC" "hostid" and a bunch of other "id" things come up in my googling, but I really cannot figure out which is the one relevent to me.

Thank you.

syg00 09-01-2010 11:36 PM

IIRC, strictly speaking you can never find out what CPU you are on (when there is more than one). To find out you have to be interrupted (!!!), and there's no guarantee you'll be redispatched on the same one.
Highly likely but not guaranteed.

If that is good enough, have a look at /proc/<pid>/stat for processor.

Tinkster 09-01-2010 11:39 PM

Do any of these help?


chrism01 09-01-2010 11:44 PM

On a *nix system, pid is guaranteed to be unique at any given time (that's how the kernel keeps track). If the sequence num wraps around, the kernel will not create a 2nd process with the same pid as a current process; it merely skips that number. Usually pids are up to 4 digits long (maybe 5 on some systems?).
Adding the time in epoch seconds would be sufficient to ensure uniqueness across time if the dirs last acrosss that timespan.
cpu-id is not required.
If you are using threads instead of sub-processes, then concat the thread id to the name ie pid-tid-timestamp_in_seconds.

Feynman 09-02-2010 03:20 PM

chrism01--that should be fine ASSUMING more than one cpu cannot use the same PID at the same time. I figured they could, but like I said, I do not know much about parallel computing.


chrism01 09-03-2010 01:37 AM

As I said, it depends if you are using threads or not.

On a multi-processor system and using only full Unix processes (not threads), a given process will normally be moved back and forth between cpus by the kernel, depending on the load on each cpu at each pt in time.
IE, it can only be running on one cpu at at any given pt in time (where cpu = core for multi-core chips).

If you use threads, they do not have separate process ids; they are Light Weight Processes (LWPs) within their owning Unix process (ie 1 pid = many threads).
In this case, each thread can run on separate cpus (at the same time), so the owning process appears to run on multiple cpus simultaneously.

Note that there is a sched_setaffinity option in C for processes, but it's rarely used by programmers.
Normally the kernel does a better job of schduling that you would manually.

For Perl, there is no builtin cmd, but there is a Module or just search for others at

Feynman 09-03-2010 10:57 AM

Ok, thanks for the info!

All times are GMT -5. The time now is 12:12 PM.