Step 1: Open Terminal:
- Press
Ctrl + Alt + T
to open the Terminal on Ubuntu.
Step 2: Install GCC, G++, OpenMP, and MPI packages:
- Copy and paste this command in your terminal:
sudo apt update && sudo apt upgrade -y && sudo apt install -y build-essential gcc g++ gfortran openmpi-bin openmpi-common libopenmpi-dev
What it does:
apt update
: Updates package listapt upgrade -y
: Upgrades installed packagesapt install
: Installs:gcc
,g++
: For C/C++gfortran
: For Fortranopenmpi-*
: For MPI (compiler and runtime)build-essential
: Compiler tools (make, linker, etc.)
Step 3: Install Gedit (Text Editor):
sudo apt install -y gedit
Step 4: Write Your Code:
- You can create and open a file like this and paste or write your code:
▶ For OpenMP (1-4 Programs only):
gedit openmp_hello.c
▶ For MPI (6-9 Programs only):
gedit openmpi_hello.c
Step 5: Compile the Code:
▶ Compile OpenMP:
gcc -fopenmp openmp_hello.c -o openmp_hello
- gcc → GNU C Compiler
- -fopenmp → Enables OpenMP support in the compiler
- openmp_hello.c → The source code file to compile
- -o openmp_hello → Names the output executable as “openmp_hello”
▶ Compile MPI:
mpicc mpi_hello.c -o mpi_hello
- mpicc → MPI C compiler (wrapper for gcc with MPI support)
- mpi_hello.c → The source code file to compile
- -o mpi_hello → Names the output executable as “mpi_hello”
Step 6: Run the Program:
▶ Run OpenMP:
OMP_NUM_THREADS=4 && ./mpi_hello
- OMP_NUM_THREADS=4 → Sets the number of OpenMP threads to 4 (for hybrid MPI + OpenMP programs)
- && → Executes the next command only if the previous one runs successfully.
- ./mpi_hello → Runs the compiled MPI (or hybrid MPI+OpenMP) executable
▶ Run MPI:
mpirun -np 4 ./mpi_hello
- mpirun → Runs an MPI program using multiple processes
- -np 4 → Specifies to run with 4 parallel processes
- ./mpi_hello → The compiled MPI executable to run