How to install OpenMPI on Ubuntu (18.04) Cluster
Step 0: Create user and set ip addr
Step 1: Install ssh (ssh server and ssh client for all machines)
Step 2: Install nfs (nfs server on master, nfs client on nodes)
Step 3: Install OpenMPI in all machines
Step 4: Test
Step 0
Create user account for MPI and work on it
sudo adduser mpiusersudo usermod -aG sudo mpiusersu mpiuser
Add all the hosts to server. Examples:
sudo nano /etc/hosts
Then add hostnames and ip addresses on Master machine.
Master machine is azcos1; others are worker nodes(azcos2, azcos3,azcos4,5,6)
127.0.0.1 localhost
10.1.20.1 azcos1
10.1.20.2 azcos2
10.1.20.3 azcos3
10.1.20.4 azcos4
10.1.20.5 azcos5
10.1.20.6 azcos6
You should add the master hostname on all nodes.
sudo nano /etc/hosts10.1.20.1 azcos1
Step 1 — Install ssh
- Run update for all
sudo apt-get update
sudo apt-get upgrade
2. Check ip addr and run
sudo apt-get install net-toolsifconfig
3. Install ssh server and client for all machines
sudo apt-get install openssh-server openssh-client
4. Create directory .ssh for all machines, run
mkdir ~/.ssh
chmod 700 ~/.ssh
5. Generate ssh keys for passwordless ssh ( on Master machine)
cd ~/.sshssh-keygen -t rsacp id_rsa.pub authorized_keyschmod 600 authorized_keysssh-copy-id azcos2 (hostnames of all node machines, azcos3, azcos4, azcos5, azcos6)
6. Node machines — do it all nodes
cat id_rsa (do it on master machine)vi id_rsa (paste it here)chmod 600 id_rsavi authorized_keyschmod 600 authorized_keys
7. Edit sshd_config file and restart ssh server
sudo nano /etc/ssh/sshd_config
Add this two lines.
PubkeyAuthentication yes
RSAAuthentication yes
Then restart ssh server.
sudo service ssh restart
8. Try to ssh the nodes without password
ssh azcos2 (and others)
Step 2 — Install nfs
- Install NFS server on master machine
sudo apt-get install nfs-kernel-server
2. Create a directory and run:
sudo apt-get install nfs-kernel-serversudo chown nobody:nogroup /home/mpiuser/Desktop/sharedfoldersudo chmod 777 /home/mpiuser/Desktop/sharedfolder
3. Export files
sudo nano /etc/exports
then add these lines, save:
/home/mpiuser/Desktop/sharedfolder azcos2(rw,sync,no_subtree_check)
/home/mpiuser/Desktop/sharedfolder azcos3(rw,sync,no_subtree_check)
/home/mpiuser/Desktop/sharedfolder azcos4(rw,sync,no_subtree_check)
/home/mpiuser/Desktop/sharedfolder azcos5(rw,sync,no_subtree_check)
/home/mpiuser/Desktop/sharedfolder azcos6(rw,sync,no_subtree_check)
then
sudo exportfs -a
4. Restart NFS server, run
sudo systemctl restart nfs-kernel-server
5. Check firewall status(inactive), type:
sudo ufw status
6. We should install nfs client to all the nodes.
sudo apt-get install nfs-commonsudo mkdir -p /home/mpiuser/Desktop/sharedfoldersudo chown nobody:nogroup /home/mpiuser/Desktop/sharedfoldersudo chmod 777 /home/mpiuser/Desktop/sharedfoldersudo mount azcos1:/home/mpiuser/Desktop/sharedfolder /home/mpiuser/Desktop/sharedfolder
7. Then test nfs
You can create a file in /sharedfolder. You can see this file on all machines where /home/mpiuser/Desktop/sharedfolder.
Step 3 — Install OpenMPI
1. Install gcc for all machines, run:
sudo apt-get install gccsudo apt-get install openmpi-bin openmpi-common libopenmpi-dev libgtk2.0-dev
2. Download and decompress file openmpi for all machines:
You can download file from official website (https://www.open-mpi.org/)
sudo wget https://www.open-mpi.org/software/ompi/v4.1/downloads/openmpi-4.1.1.tar.gzsudo tar -xvzf /home/mpiuser/openmpi-4.1.1.tar.gz
3. install OpenMPI for all machines
cd /home/mpiuser/Desktop/openmpi-4.1.1sudo ./configure --prefix="/home/mpiuser/.openmpi"
sudo make
sudo make install
4. Export PATH, run:
export PATH=”$PATH:/home/mpiuser/.openmpi/bin”export LD_LIBRARY_PATH=”$LD_LIBRARY_PATH:/home/mpiuser/.openmpi/lib”
5. Test by running mpirun or mpircc
If you see the below, it means successfully installed.
mpiuser@azcos6:~$ mpirun
--------------------------------------------------------------------
mpirun could not find anything to do.It is possible that you forgot to specify how many processes to run
via the "-np" argument.
--------------------------------------------------------------------
Step 4 — Test
- Compile mpi program
Go to the link , copy the code and paste it here:
vi mpi-prime.c
Compile mpi file.
mpicc mpi-prime.c -o ./outputfile
copy outputfile to the sharedfolder & check the file in the nodes sharedfolder
2. Run cluster
Go to shared folder on master machine, run:
cd /home/mpiuser/Desktop/sharedfoldermpirun --hostfile /etc/hosts -np 5 ./outputfile
if you see like below, it is ok.
mpiuser@azcos1:~/Desktop/sharedfolder$ mpirun --hostfile /etc/hosts -np 5 ./outputfile
16 August 2021 04:31:31 PMPRIME_MPI
C/MPI versionAn MPI example program to count the number of primes.
The number of processes is 5N Pi Time1 0 0.006863
2 1 0.000528
4 2 0.000504
8 4 0.000508
16 6 0.000513
32 11 0.000512
64 18 0.000511
128 31 0.000514
256 54 0.000515
512 97 0.000501
1024 172 0.000741
2048 309 0.001029
4096 564 0.002891
8192 1028 0.005456
16384 1900 0.014796
32768 3512 0.031765
65536 6542 0.115813
131072 12251 0.436057
262144 23000 1.638243
524288 43390 6.200569
1048576 82025 23.463462PRIME_MPI - Master process:
Normal end of execution.16 August 2021 04:32:03 PM
mpiuser@azcos1:~/Desktop/sharedfolder$
Thank you! :)