I am a beginner C++ and MPI user. I am working in an HPC environment and need to do the following:
This is the code I have written: C++ (file name- cpp2bash_test.cpp):
#include <iostream>
#include <mpi.h>
int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
int size, rank;
MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
cout << rank << endl;
MPI_Finalize();
return 0;
}
I compile the file as:
mpicxx -g -std=c++14 -o PRank cpp2bash_test.cpp
Bash:
#!/bin/sh
result=$(./PRank)
python3 bash2py_test.py $result
Finally, I submit the bash script as a PBS job. Here is the output file:
The line "Argument accepted= " is from the python script which simply accepts the rank and prints it.
To better understand why the python script gets a weird value, I changed the shell script to not accept any input from the C++ code. Instead, the C++ code simply printed its output. This is a partial screenshot of what I see in the output file:
The Python file output(not shown in the second image) is still similar to the one shown previously.
I googled for that warning but I did not really understand much of what I got to read. In fact, I don't even know if what I am reading is relevant to my problem. I suspect that the full output from the C++ code contains the warning which, when passed to Python, gets truncated to just those weird values inside the square brackets. How do I remove these warnings and pass the right values to the python script?
Note that for most queue systems, there are system variables being set that indicate process rank. Once inside your script (that is started as MPI
) you should be able to get it from the environment (you have to look into your queue system manual).
Another way is to produce some wrapped log and grep for the info, e.g.:
cout << "RANK:" << rank << endl;
Then, you can do something like this:
result=$(echo "RANK:2" | grep RANK | cut -f2 -d':')
echo $result
in your case it will be
result=$(./PRank | grep RANK | cut -f2 -d':')
echo $result