Why is Parallel Programming Needed?
The End of the "Free Lunch"
For decades, computers got faster because individual processor cores got faster (Moore's Law). Old software would automatically speed up on new hardware. Around 2005, this stopped. It became too difficult to make single cores faster due to heat and power limits. The industry's solution was to add more cores to a single chip. Today, even a standard phone has multiple cores, but a normal program can only use one at a time, leaving the rest idle. Parallel programming is the key to unlocking the full power of modern hardware.
What is OpenMP?
OpenMP (Open Multi-Processing) is one of the simplest ways to write parallel code. It's a set of commands (called #pragma directives) that you add to your C/C++ code. These pragmas are simple instructions to the compiler, telling it which parts of your code (like a `for` loop) can be safely run on multiple cores at the same time. You don't have to manage threads manually OpenMP handles the complex work for you.
Prerequisites
Before writing code, you need to set up your development environment. This involves installing VS Code, the necessary extension, and a C/C++ compiler that supports OpenMP. Select your operating system below to see the specific compiler instructions.
1A. Install Visual Studio Code & C/C++ Extension
Download VS Code from code.visualstudio.com. Then, inside VS Code, go to the Extensions view (`Ctrl+Shift+X`) and install the `C/C++` extension from Microsoft.
1B. Install Compiler for Windows (MinGW-w64)
MinGW-w64 is a development toolchain that provides a version of the GCC compiler for Windows. This is necessary because Windows does not come with a built-in C/C++ compiler that supports OpenMP, so we install this to get the required tools.
- Download and run the MSYS2 installer from msys2.org.
- Open the MSYS2 terminal and run `pacman -Syu`.
- Install the toolchain with: `pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain`.
- Add the compiler to your Windows Path. This is typically `C:\msys64\ucrt64\bin`.
1B. Install Compiler for macOS (GCC via Homebrew)
The default Clang compiler does not support OpenMP. You must install GCC.
brew install gcc
1B. Install Compiler for Linux (build-essential)
GCC is usually pre-installed. If not, open your terminal and run the appropriate command for your distribution:
sudo apt update && sudo apt install build-essential
1C. Install and Setup MPI
MPI (Message Passing Interface) is required if you want to run distributed-memory parallel programs. Follow these steps for Windows setup:
- Install prerequisites - MS-MPI Redistributable + SDK.
- Download and install both the MS-MPI Redistributable (runtime) and MS-MPI SDK (headers & libs). Official Microsoft Download Center.
- Once both SDK and setup is installed, verify these paths exist:
C:\Program Files\Microsoft MPI\Bin→ Add this to your PATH variable.C:\Program Files (x86)\Microsoft SDKs\MPI\IncludeC:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64
- Edit system environment variables and add the remaining two to User Variables:
- INCLUDE →
C:\Program Files (x86)\Microsoft SDKs\MPI\Include - LIB →
C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64
- INCLUDE →
- Search for x64 Native Tools Command Prompt for VS and run:
set INCLUDE
set LIB
You should see your MPI paths listed. - if you dont see x64 Native Tools download it using Visual Studio Community with the Desktop Development with C++ workload.
- Once Installed - Under Workloads Tab - Select Desktop Development with C++ - Click Install and let it download the C++ compiler toolchain. After installation search in Start Menu for
x64 Native Tools Command Prompt for VS - Open x64 Native Tools Command Prompt for VS and Navigate to the folder where your MPI code is saved (example if file is in `E:\PC_LAB`):
cd /d "E:\PC_LAB" - Now you can compile the code using
cl 5.c msmpi.lib - Run using
mpiexec -n 2 5.exe
Project Setup
- Create a new folder on your computer (e.g., `MyOpenMPProject`).
- Open this folder in VS Code (`File > Open Folder...`).
- Create a new source file (`File > New File`) and save it with a `.c` extension (e.g., `main.c`).
Configure Build Task
Create a `tasks.json` file in your `.vscode` folder to tell VS Code how to compile your program with the `-fopenmp` flag.
{
"version": "2.0.0",
"tasks": [
{
"label": "C/C++: Build with OpenMP",
"type": "shell",
"command": "gcc",
"args": [ "-g", "-fopenmp", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ],
"group": { "kind": "build", "isDefault": true }
}
]
}
Compile & Run
Compile the Code
The easiest method is to use the build task. With your `.c` file open, press `Ctrl+Shift+B`. Alternatively, you can compile manually from the terminal. For a file named `my_program.c`, the command is:
gcc -fopenmp my_program.c -o my_program
Run the Executable
After a successful compilation, an executable file is created. Run it from the terminal:
# On Windows
./my_program.exe