
How to Install and Setup MPI Environment in WSL?
In the previous tutorial I have shown how to install a linux distribution as windows subsystem. After installing a linux distribution you need to install and setup MPI. MPI has a wide variation of functions. So, if you don’t install MPI resources you will see an error while running a MPI program.

Follow the instructions below to configure MPI Environment.
1. Open Your Terminal
Go to the directory you want to setup your environment. You can do it anywhere. But it is better to setup your environment there where you want to keep your programs. So, go to that directory and enter wsl
in the address box of windows file explorer.
You will see a command line interface there.
2. Update Your System
If you have downloaded the updated version of the system then you don’t need to update the system. But it is a better practice to complete this step to overcome the possible problems. You need to run the following command:
$ sudo apt-get update
3. Install MPI on Your System
To start MPI Programming you need to install MPI resources on your system. Those resources will run all MPI functions. So, if you miss this step you will not able to compile or run a single program. This is the main step of MPI programming. Run the below command to install MPI on your system.
$ sudo apt install openmpi-bin libopenmpi-dev
This step may ask upto 5 minutes to complete and requires almost 100 MB space on your disk. Press ‘Y’ as response.
After this step you can run a C program file. But you need to setup one more step if you want to run C++ files.
In this tutorial series I will do all programs in C++. So, if you are following me also go through the below steps.
4. Install g++ for C++
C++ is more easy and compatible with anything rather than C. So, it is always better if you can write your program in C++. To write MPI programs in C++ will let you use some advanced features.
To compile and run C++ files of MPI you need to install g++ compiler. Follow the command below to install g++.
$ sudo apt install g++
This command will ask to ensure the use of around 50 MB memory space. Press ‘Y’ to allow the installation.
5. Compile MPI Program
If you have completed the above task correctly then your environment has been set successfully. So, you can now compile any program. I will teach about writing and understanding MPI program in next step. In this step I am giving an overview to the commands only.
To compile a MPI program written in C run the command:
$ mpicc filename.c -o outputfile
To compile a MPI program written in C++ run the command:
$ mpiCC filename.cpp -o outputfile
or,
$ mpic++ filename.cpp -o outputfile
Command keywords meanings are as following:
mpicc – Instruction to compile C file
mpiCC – Instruction to compile C++ file
mpic++ – Instruction to compile C++ file
filename – The name of your program file
.c – Extension for C program file
.cpp – Extension for C++ program file
-o – Command to create an output file
outputfile – The name of your output file.
N.B. Your output file can have a .o or .out extension at the end of file name. But that is optional as the file is not readable by human.
If you provide here different file name you can save your output file. That means you can run a file again without compiling it.
If you use same name for output file then your file will contain the output of the latest program. And to run any previous program you need to compile that again.
6. Run MPI Program
After compiling a file you need to run it to see the output. Compiling a program means the program is converted into machine language. To see the output you have to run the following command:
$ mpirun -np noofproc ./outputfile
or,
$ mpirun -np noofproc outputfile
or,
$ mpirun -n noofproc outputfile
or,
$ mpiexec -n noofproc outputfile
Here the command keywords are explained:
mpirun – Command to run MPI program (compiled output file)
mpiexec – Command to execute MPI
-np – Command for number of process
-n – Command for number of process or core
noofproc – No of Process (It is a number depending on the core of your PC. If you exceed the number of core on your PC this will through an error. So, if you don’t know the number of core on your PC provide ‘2’ here always. Each PC have at least 2 core.
outputfile – The file compiled in previous step
Additional Basic Commands in Linux
To navigate nextfolder
use the following command:
$ cd nextfolder
To navigate parent folder use the following command:
$ cd ..
mnt
means the root folder of your PC. This contains all drives.
To see a list of folder and files in current directory use the following command:
$ ls
Common Errors You May Face
gcc: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory
This error means you are trying to compile a C++ program without installing C++ for linux distribution. To resolve this error run the following command and continue:
$ sudo apt install g++
/usr/bin/ld: /tmp/ccfySlSX.o: in function `main’: – undefined reference to `operator new
Many more error texts will appear with these texts. In this case the exact error is not specified. That means the whole program is considered as uncompilable.
This happens when you use a mpicc
command to compile C++ file or mpic++/mpiCC
command to compile C file.
Use the correct command to overcome this error.
Hope you are well handed to compile and run MPI programs. Lets be prepare for up-next.