I am building a quantum circuit simulator using mpi4py. The simulator takes user inputs to build the initial system and quantum gates the system passes through. I can get the code to work if I do not use user inputs and just hard code in the system and gates. However, I cannot figure out how to get the code to work when I do use user inputs. If I mpirun the code with the user inputs it does not allow me to enter inputs and then the code gives an error because variables are never defined. I tried to get the user inputs and then mpirun the rest of the code but it does not recognize the variables obtained from user inputs. I tried to pass in the user input variables into my code I am mpirunning, but I also could not get this to work. I am not sure how to deal with this issue!
Input from the console may work on process zero, if that runs locally, but will certainly not work on other ranks. However, I wouldn't even do interactive input on process zero.
#!/bin/bash if [ $PMI_RANK -eq 0 ] ; then ( echo 5 ; echo 1 ; echo foo ) | $* else exec $* fi
Put that in a script and start that with mpirun:
mpirun myscript.sh myprogram
The environment variable for the MPI rank may have a different name in your MPI.
MPI is designed so that the same executable is started on a number of machines, none of which may be the one on which the
mpirun program is called. Furthermore, MPI jobs are typically put into a queue, and the actual job may run hours or days later.
As a consequence, MPI jobs are not meant to be used in an interactive mode, but all input should be provided in a way so that the program -- wherever and whenever it is actually running -- can access it in a non-interactive way.