[SOLVED] How to identify current CPU in a parallel machine
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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 http://linux.die.net/man/2/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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.