# Passing in user inputs with mpirun

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!

• When you say "user input", do you mean "stuff your program reads from the console"? "User input" could of course also be "stuff read from a parameter file"... May 31 at 22:42
• Stuff your program reads from the console. Like I run the code and it says how many qubits do you want in your system and I input the number of qubits I want in my system. May 31 at 22:47
• This seems more a programming than a computational science question. Jun 1 at 14:47

## 2 Answers

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.

• Thank you for the answer, but I am not sure what this does. Would you mind explaining? Jun 1 at 1:02
• Instead of running your program you run a shell script that calls your program. Then you let the shell script provide input to the program. Jun 1 at 20:48

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.

• So if I wanted to build a quantum circuit simulator in a parallel environment I would have to hard code in the system and gates I want and then run that way instead of it be an interactive experience? That makes a lot of sense. Thank you! Jun 2 at 0:35
• @Orphea12 Or ask users to write their commands into an input file that the program reads at run-time. That's what most MPI programs do: Read their inputs from either an input file or from the command line. Jun 2 at 2:23