0% found this document useful (0 votes)
4 views4 pages

Govind 1

The document provides an overview of pthreads and OpenMP, highlighting their functionalities and usage in high-performance computing. Pthreads is a standard API for creating concurrent processes, while OpenMP is an API for parallel programming that simplifies the development of multi-threaded applications. The document includes example code for both pthreads and OpenMP, demonstrating their capabilities in managing threads and parallel execution.

Uploaded by

reddygovind4550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Govind 1

The document provides an overview of pthreads and OpenMP, highlighting their functionalities and usage in high-performance computing. Pthreads is a standard API for creating concurrent processes, while OpenMP is an API for parallel programming that simplifies the development of multi-threaded applications. The document includes example code for both pthreads and OpenMP, demonstrating their capabilities in managing threads and parallel execution.

Uploaded by

reddygovind4550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Faculty of Engineering & Technology

Subject Name: High Performance


Computing Subject Code: 303105356
B-Tech-CSE (AI) 3rd Year 6th Semester

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.

Function of pthreads and libraries.


 pthread_create: used to create a new thread
 pthread_exit: used to terminate a thread
 pthread_join: used to wait for the termination of a thread.
 pthread_self: used to get the thread id of the current thread.
 pthread_equal: compares whether two threads are the same or not. If the two threads are equal,
the function returns a non-zero value otherwise zero.
 pthread_detach: used to detach a thread. A detached thread does not require a thread to join on
terminating. The resources of the thread are automatically released after terminating if the
thread is detached.

1)Simple pthread program


%%writefile govind_10.c
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 10
void *PrintHello(void *threadid)
{
int tid;
tid = (int)threadid;
printf("Hello World! It's me, thread #%d!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[]) {
Name: KISTIPATI GOVINDREDDY
1
Enrollment No: 2203031240607
Division: 6AI8
Faculty of Engineering & Technology
Subject Name: High Performance
Computing Subject Code: 303105356
B-Tech-CSE (AI) 3rd Year 6th Semester

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:

Name: KISTIPATI GOVINDREDDY


2
Enrollment No: 2203031240607
Division: 6AI8
Faculty of Engineering & Technology
Subject Name: High Performance
Computing Subject Code: 303105356
B-Tech-CSE (AI) 3rd Year 6th Semester

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:

Splits code into multiple threads that run concurrently.

2. Workload Distribution:

Divides tasks among threads using constructs like:

Work-sharing: #pragma omp for, #pragma omp sections

Task parallelism: #pragma omp task

3. Synchronization:

Provides constructs to manage thread interactions and avoid race conditions.

4. Thread Management:

Handles creation, execution, and destruction of threads.

Functions like omp_set_num_threads() control the number of threads.

5. Dynamic Thread Adjustment:

Supports dynamic adjustment of the number of threads based on workload using functions like
omp_set_dynamic().

6. Environment Control:

Environment variables like OMP_NUM_THREADS and OMP_SCHEDULE allow runtime


configuration.

7. Performance Monitoring:

Offers tools for measuring execution time and thread efficiency (e.g., omp_get_wtime()).

Name: KISTIPATI GOVINDREDDY


3
Enrollment No: 2203031240607
Division: 6AI8
Faculty of Engineering & Technology
Subject Name: High Performance
Computing Subject Code: 303105356
B-Tech-CSE (AI) 3rd Year 6th Semester
8. Nested Parallelism:

Allows parallel regions inside other parallel regions with omp_set_nested().

Simple C program To Demonstrate OpenMp

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

You might also like