How can my program detect, whether it was launch via mpirun - mpi

How can my MPI program detect, if it was launched as a standalone application or via mpirun?

Considering the answer and comments by semiuseless and Hristo Iliev, there is no general and portable way to do this. As a workaround, you can check for environment variables that are set by mpirun. See e.g.:
http://www.open-mpi.org/faq/?category=running#mpi-environmental-variables

There is no MPI standard way to tell the difference between an MPI application that is launched directly, or as a single rank with mpirun. See "Singleton MPI_Init" for more on this kind of MPI job.
The environment variable checking answer from Douglas is a reasonable hack...but is not portable to any other MPI implementation.

Related

All processes have my_rank = 0, how to fix?

I have a problem concerning running mpi programs. All processes claims that their rank is 0.
I have searched a lot around, and found out that this is caused by communication between the openMP version and something else, and that I have to check which mpi I invoke. But no one explains properly, in a way that works, how to fix this.
Do I have to uninstall something? In that case, what should I uninstall and how do I do it?
Do I have to install something? What and how?
If the answer is no to the previous questions, how can I then fix it?
How could this problem occur as I only did what my lecturer told me, I think?
This typically occurs when you are mixing two MPI libraries.
For example, you are using mpirun from MPICH but your app is using the libraries from Open MPI.
You should first double check that, for example
$ which mpirun
$ mpirun -np 1 ldd a.out
both should point to the same directory (e.g. same vendor and version)

are compiler and shell internal part of Unix?

I had this question on my exam, now in diagrams I saw, we have : hardware, kernel, system call interface to the kernel, then (compilers, shells, sys.libs) and on top some applications. Does OS scope include only kernel, and everything else is just some additional functions we choose to install , or does a Unix OS include everything from the list I gave above?
OS have more or less 2 definitions :
academic : OS is soft for doing a abstraction layer between
hardware and software
pragmatic : OS is soft that come with hardware when we buy it.
Compiler and shell don't enter in definition 1. It can be enter in definition 2.
And usually, users that are interesting by a compiler or a shell prefer to consider OS as asbtraction layer (academic definition).
Simple answer, No. They are not an internal part of Unix but additional functionality to help make the Operating System more usable.
The OS scope applies primarily to the kernel only.
Whilst you need a compiler to build the kernel, you don't necessarily require one for the general day to day use of the system. Most operating systems don't ship the compiler by default and instead, the kernel and applications is built on one machine and then the resulting binarys are packaged and distributed either with the computer directly (Windows/Unix) or via the internet for others to download and install (Linux/BSD)
Likewise with the shell. Although all operating systems ship with a default one (sh/bash/dash on Linux|Unix systems, Command Prompt/Powershell on Windows), most general users can go their entire lives without using it.
Having said that, if you were to delete the shell, you'll almost certainly find your system won't boot up. This is because a lot of core start-up scripts rely on the shell to stop / start the services presenting interfaces between the user and the kernel.
In summary:
You need a compiler to build the kernel and applications but not for running the OS.
You need a shell to execute applications (which also includes the compiler)

gethostname() function missing in openMPI

I had to replace mpich2 with OpenMPI due to dependency of OpenFOAM on OpenMPI.
Earlier (when using mpich2) in my code I was using gethostname() function to get the name of the machine for debugging purpose. However this function does not seem to be a standard MPI function, and is not working anymore with OpenMPI libraries. Is there any other function for getting the host name in OpenMPI, or MPI standard? I am using mpicc for compiling and mpirun for running the code.
Thanks,
Sourabh
gethostname() is defined in unistd.h that was included by mpi.h, in the previous version. That's not a feature you should rely on, since you should always explicitly include the files which define the symbols you use. Clearly you were relying on it without realizing.
However if your MPI code is supposed to run on POSIX systems only, its safe to add
#include <unistd.h>
gethostname() is POSIX2001.1 standard.
However the MPI portable solution is MPI_Get_processor_name() as shown in the comment by High Performance Mark

Torque + mpirun + resources allocation

I'm running Torque with Open MPI on a single machine with 24 cores. Why is it possible to specify in my job,sh, for instance, nodes=1:ppn:2 and still be able to run a job specified by mpirun -np 12 WhatEverCommand? In such case the job is executed on 12 cores, even though the "nodes" says 2 cpus.
Doesn't specifying the "nodes" option make any restrictions on the resources to be used by the submitted job? If it doesn't, then how to prevent users from violating the server rules by overriding the declared resources?
On the other hand - specifying the nodes=1:ppn=8 and mpirun without "-np" option, gives me only 1 cpu running the job.
Am I that bad and missing something fundamental here?
By default, OpenMPI doesn't integrate with Torque at all. You have to compile OpenMPI using the --with-tm configure option, which doesn't seem to be enabled in most distro packages. The OpenMPI project mentions Torque integration in its FAQs on building and running OpenMPI.
Similarly, Torque doesn't actually restrict access to CPUs unless cpuset support is enabled. Again, this seems absent in most distro packages. This is why your OpenMPI app, when compiled without Torque integration, can hit all the cores without restriction.
Building both packages from source is not too difficult, so it's worth researching the configure options and building the support that makes sense for you.

Is there MPI stub library?

I have C source with MPI calls.
I wonder can I get sequential program from the source by linking with some MPI stub library? Where can I get this lib?
Most correctly-written MPI programs should not depend on the number of processes they use to get a correct answer -- eg, if you run them on one process (mpirun -np 1 ./a.out) they should still work. So you oughtn't need a stub library - just use MPI. (If for some reason you just don't want extraneous libraries kicking around, it's certainly possible to write stubs and link against them -- I did this back in the day when setting up MPI on my laptop was a huge PITA, you could use this as a starting point and add any functionality you need. But these days, fiddling with the stub library is probably going to be more work than just using an existing MPI implementation.)
If your MPI program does not currently work correctly on one processor, a stub library probably won't help; you'll need to find the special cases that it's not handling and fix them.
I don't think this is possible. Contrary to OpenMP, programs using MPI don't necessarily run or produce the same result when you simply take away the MPI part.
PETSc contains a stub MPI library that works for one process (ie serial) execution:
http://www.mcs.anl.gov/petsc/petsc-3.2/include/mpiuni/mpi.h

Resources