OpenMP – Overview
Explanation
OpenMP (Open Multi-Processing) is an API that supports parallel programming in C, C++, and Fortran for
shared-memory architectures. It allows developers to split tasks into multiple threads that can run
simultaneously on different CPU cores, improving performance for computationally intensive applications.
Key Features:
• Easy to integrate with existing C/C++ code using compiler directives ( #pragma omp )
• Supports parallel loops, task parallelism, and synchronization
• Provides runtime library functions to get thread IDs, number of threads, processors, etc.
• Allows control of workload distribution using scheduling strategies (static, dynamic, guided)
Assignment-1: OpenMP Program (a–h)
#include <stdio.h>
#include <omp.h>
int main() {
// (a) Hello World in multiple threads
printf("\n(a) Hello World from multiple threads:\n");
#pragma omp parallel
{
printf("Hello World from Thread %d\n", omp_get_thread_num());
}
// (b) Number of threads running currently
printf("\n(b) Number of threads currently running:\n");
#pragma omp parallel
{
#pragma omp single
printf("Threads running: %d\n", omp_get_num_threads());
}
// (c) Maximum number of threads
printf("\n(c) Maximum number of threads:\n");
printf("Max threads: %d\n", omp_get_max_threads());
// (d) Thread IDs
printf("\n(d) Thread IDs:\n");
#pragma omp parallel
{
1
printf("Thread ID: %d\n", omp_get_thread_num());
}
// (e) Number of processor cores
printf("\n(e) Number of processor cores in system:\n");
printf("Processor cores: %d\n", omp_get_num_procs());
// (f) Set number of threads to be executed
printf("\n(f) Setting number of threads to 4:\n");
omp_set_num_threads(4);
#pragma omp parallel
{
printf("Thread %d of %d is running\n", omp_get_thread_num(),
omp_get_num_threads());
}
// (g) Test if a parallel function is running
printf("\n(g) Test if code is running in parallel:\n");
#pragma omp parallel
{
if (omp_in_parallel())
printf("Thread %d says: Yes, running in parallel!\n",
omp_get_thread_num());
else
printf("Not running in parallel.\n");
}
// (h) Parallelize a simple for loop
printf("\n(h) Parallelized for loop:\n");
int i;
#pragma omp parallel for schedule(static)
for (i = 1; i <= 8; i++) {
printf("Thread %d processing i = %d\n", omp_get_thread_num(), i);
}
return 0;
}
Sample Output
(Order of lines may vary due to parallel execution)
(a) Hello World from multiple threads:
Hello World from Thread 0
2
Hello World from Thread 2
Hello World from Thread 1
Hello World from Thread 3
(b) Number of threads currently running:
Threads running: 4
(c) Maximum number of threads:
Max threads: 8
(d) Thread IDs:
Thread ID: 0
Thread ID: 1
Thread ID: 2
Thread ID: 3
(e) Number of processor cores in system:
Processor cores: 8
(f) Setting number of threads to 4:
Thread 0 of 4 is running
Thread 1 of 4 is running
Thread 2 of 4 is running
Thread 3 of 4 is running
(g) Test if code is running in parallel:
Thread 0 says: Yes, running in parallel!
Thread 1 says: Yes, running in parallel!
Thread 2 says: Yes, running in parallel!
Thread 3 says: Yes, running in parallel!
(h) Parallelized for loop:
Thread 0 processing i = 1
Thread 2 processing i = 3
Thread 1 processing i = 2
Thread 3 processing i = 4
Thread 0 processing i = 5
Thread 2 processing i = 7
Thread 1 processing i = 6
Thread 3 processing i = 8
Applications of OpenMP
1. Scientific Computing – Weather forecasting, molecular dynamics simulations
2. Engineering Simulations – Fluid dynamics, structural analysis
3
3. Big Data Analytics – Parallel data processing, real-time analytics
4. Image & Video Processing – Medical imaging, object detection
5. Machine Learning – Speeding up training on CPU-based systems
6. Signal Processing – Audio/video compression, noise reduction
Compilation Command:
gcc -fopenmp openmp_assessment.c -o openmp_assessment
./openmp_assessment