0% found this document useful (0 votes)
6 views14 pages

OpenMP, MPI, and POSIX Threads Examples

The document provides examples of parallel programming using OpenMP, MPI, and POSIX Threads. It includes code snippets for basic operations like vector addition, multiplication, and sorting algorithms (merge and quick sort) using threads and processes. Each section outlines how to compile and run the programs, demonstrating the use of parallelism in different programming models.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views14 pages

OpenMP, MPI, and POSIX Threads Examples

The document provides examples of parallel programming using OpenMP, MPI, and POSIX Threads. It includes code snippets for basic operations like vector addition, multiplication, and sorting algorithms (merge and quick sort) using threads and processes. Each section outlines how to compile and run the programs, demonstrating the use of parallelism in different programming models.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Parallel Programs (PDC)

Open MP :-

[Link] Open MP?


vi filename.c
#include <stdio.h>
#include <omp.h>
int main() {
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
printf("Hello from thread %d\n", thread_id);
}
return 0;
}
Compile: cc -fopenmp filename.c -o openmp
Run: ./openmp

[Link] Addition?
vi filename.c
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 4
int main() {
int A[N][N], x[N], y[N];
for (int i = 0; i < N; i++) {
x[i] = i + 1;
for (int j = 0; j < N; j++) {
A[i][j] = (i + 1) * (j + 1);
}
}
printf("Matrix A:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("Vector x:\n");
for (int i = 0; i < N; i++) {
printf("%d ", x[i]);
}
printf("\n");
double start_time = omp_get_wtime();
#pragma omp parallel for
for (int i = 0; i < N; i++) {
y[i] = 0;
for (int j = 0; j < N; j++) {
y[i] += A[i][j];
}
y[i] += x[i];
}
double end_time = omp_get_wtime();
printf("Time taken for matrix-vector addition: %f seconds\n", end_time -
start_time);
printf("Result vector y (A + x):\n");
for (int i = 0; i < N; i++) {
printf("y[%d] = %d\n", i, y[i]);
}
return 0;
}
Compile: cc -fopenmp filename.c -o openmp
Run: ./openmp

[Link] Multiplication?
vi filename.c
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 4
int main() {
int A[N][N], x[N], y[N];
for (int i = 0; i < N; i++) {
x[i] = i + 1;
for (int j = 0; j < N; j++) {
A[i][j] = (i + 1) * (j + 1);
}
}
printf("Matrix A:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("Vector x:\n");
for (int i = 0; i < N; i++) {
printf("%d ", x[i]);
}
printf("\n");
double start_time = omp_get_wtime();
#pragma omp parallel for
for (int i = 0; i < N; i++) {
y[i] = 0;
for (int j = 0; j < N; j++) {
y[i] += A[i][j] * x[j];
}
}
double end_time = omp_get_wtime();
printf("Time taken for matrix-vector multiplication: %f seconds\n", end_time
- start_time);
printf("Result vector y:\n");
for (in
t i = 0; i < N; i++) {
printf("y[%d] = %d\n", i, y[i]);
}
return 0;
}
Compile: cc -fopenmp filename.c -o openmp
Run: ./openmp

MPI

[Link] MPI?
vi filename.c
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
MPI_Finalize();
return 0;
}
Compile: mpicc filename.c -o mpi
Run: ./mpi
[Link]-Blocking Broadcast?
vi filename.c
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
int rank, size, broadcast_value;
MPI_Request request;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
broadcast_value = 42;
printf("Process %d initiating non-blocking broadcast of value %d\n", rank,
broadcast_value);
}
MPI_Ibcast(&broadcast_value, 1, MPI_INT, 0, MPI_COMM_WORLD,
&request);
printf("Process %d is doing some independent work while waiting for the
broadcast to complete.\n", rank);
MPI_Wait(&request, &status);
printf("Process %d received the broadcasted value: %d\n", rank,
broadcast_value);
MPI_Finalize();
return 0;
}
Compile: mpicc filename.c -o mpi
Run: ./mpi
[Link] Synchronization?
vi filename.c
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
int rank, size, broadcast_value;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("Process %d is waiting at the barrier...\n", rank);
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) {
broadcast_value = 100;
printf("Process %d broadcasting value %d to all processes\n", rank,
broadcast_value);
}
MPI_Bcast(&broadcast_value, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("Process %d received the broadcasted value: %d\n", rank,
broadcast_value);
MPI_Finalize();
return 0;
}
Compile: mpicc filename.c -o mpi
Run: ./mpi
POSIX Threads

[Link] Thread?
vi filename.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* thread_function(void* arg) {
int thread_id = *((int*)arg);
printf("Hello from thread %d!\n", thread_id);
pthread_exit(NULL);
}
int main() {
pthread_t threads[3];
int thread_ids[3];
int num_threads = 3;
for (int i = 0; i < num_threads; i++) {
thread_ids[i] = i;
int rc = pthread_create(&threads[i], NULL, thread_function,
(void*)&thread_ids[i]);
if (rc) {
fprintf(stderr, "Error creating thread %d: %d\n", i, rc);
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < num_threads; i++) {
int rc = pthread_join(threads[i], NULL);
if (rc) {
fprintf(stderr, "Error joining thread %d: %d\n", i, rc);
exit(EXIT_FAILURE);
}
}
printf("All threads completed.\n");
return 0;
}
Compile: cc -pthread filename.c -o pthread
Run: ./pthread

[Link] Sort?
vi filename.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX 100000
int arr[MAX];
int temp[MAX];
int n;
typedef struct {
int left;
int right;
} ThreadData;
void merge(int left, int mid, int right) {
int i = left, j = mid + 1, k = left;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (i = left; i <= right; i++) {
arr[i] = temp[i];
}
}
void *merge_sort(void *arg) {
ThreadData *data = (ThreadData *)arg;
int left = data->left;
int right = data->right;
if (left < right) {
int mid = left + (right - left) / 2;
ThreadData left_data = {left, mid};
ThreadData right_data = {mid + 1, right};
pthread_t left_thread, right_thread;
pthread_create(&left_thread, NULL, merge_sort, &left_data);
pthread_create(&right_thread, NULL, merge_sort, &right_data);
pthread_join(left_thread, NULL);
pthread_join(right_thread, NULL);
merge(left, mid, right);
}
return NULL;
}
int main() {
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
ThreadData data = {0, n - 1};
pthread_t main_thread;
pthread_create(&main_thread, NULL, merge_sort, &data);
pthread_join(main_thread, NULL);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Compile: cc -pthread filename.c -o pthread
Run: ./pthread
[Link] Sort?
vi filename.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX 100000
int arr[MAX];
int n;
typedef struct {
int left;
int right;
} ThreadData;
int partition(int left, int right) {
int pivot = arr[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[right];
arr[right] = temp;
return i + 1;
}
void *quick_sort(void *arg) {
ThreadData *data = (ThreadData *)arg;
int left = data->left;
int right = data->right;
if (left < right) {
int pivot = partition(left, right);
ThreadData left_data = {left, pivot - 1};
ThreadData right_data = {pivot + 1, right};
pthread_t left_thread, right_thread;
pthread_create(&left_thread, NULL, quick_sort, &left_data);
pthread_create(&right_thread, NULL, quick_sort, &right_data);
pthread_join(left_thread, NULL);
pthread_join(right_thread, NULL);
}
return NULL;
}
int main() {
printf("Enter number of elements: ");
scanf("%d", &n);
if (n > MAX) {
printf("Error: Number of elements exceeds maximum allowed (%d).\n",
MAX);
return 1;
}
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
ThreadData data = {0, n - 1};
pthread_t main_thread;
pthread_create(&main_thread, NULL, quick_sort, &data);
pthread_join(main_thread, NULL);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Compile: cc -pthread filename.c -o pthread
Run: ./pthread

You might also like