Govind 1
Govind 1
Practical-10
Aim: Demonstration of openMP and Pthread fuctions.
What is pthread ?
• Pthread full form is Posix thread.
• Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported
by most vendors.
• It enables the creation of a new concurrent process flow.
pthread_t threads[NUM_THREADS];
int rc;
int t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
}
}
pthread_exit(NULL);
}
Output:
2)What is OpenMp?
OpenMP (Open Multi-Processing) is an API (Application Programming Interface) that supports
multi-platform shared memory multiprocessing programming in C, C++, and Fortran. It allows
developers to write parallel applications using a simple set of compiler directives, library routines,
and environment variables. OpenMP is used to efficiently parallelize code to take advantage of
multi-core and multi-processor systems.
Functions Of OpenMp
1. Parallel Execution:
2. Workload Distribution:
3. Synchronization:
4. Thread Management:
Supports dynamic adjustment of the number of threads based on workload using functions like
omp_set_dynamic().
6. Environment Control:
7. Performance Monitoring:
Offers tools for measuring execution time and thread efficiency (e.g., omp_get_wtime()).
Source Code:
%%writefile Govind_10.c
#include <omp.h>
#include <stdio.h>
#include<stdlib.h>
int main(int argc,char* argv[])
{
//Beginning of parallel region
#pragma omp parallel
{
printf("Hello World... from thread = %d\n ",omp_get_thread_num());
}
}
Output:
Conclusion:
OpenMP is a powerful and easy-to-use tool for implementing parallelism in applications on
shared-memory architectures. It provides a high-level interface for developers to optimize
performance by leveraging multicore processors. OpenMP's simplicity, portability, and flexibility
make it an excellent choice for adding parallelism to existing sequential programs incrementally,
enabling significant performance improvements without extensive reworking of the code.
Name: KISTIPATI GOVINDREDDY
4
Enrollment No: 2203031240607
Division: 6AI8