0% found this document useful (0 votes)
2 views8 pages

Parallel and Serial Sorting in C

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)
2 views8 pages

Parallel and Serial Sorting in C

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

Program 1

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>
#define SIZE 100000
void merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));
for (i = 0; i< n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0; j = 0; k = left;
while (i< n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i< n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
free(L);
free(R);
}
void serialMergeSort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
serialMergeSort(arr, left, mid);
serialMergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void parallelMergeSort(int arr[], int left, int right, int depth)
{
int mid;
if (left < right)
{
mid= left + (right - left) / 2;
if (depth <= 4)
{
{
parallelMergeSort(arr, left, mid, depth + 1);
parallelMergeSort(arr, mid + 1, right, depth + 1);
}}}
else
{
serialMergeSort(arr, left, mid);
serialMergeSort(arr, mid + 1, right);
}
merge(arr, left, mid, right);
}
int main()
{
int *arr_serial = (int *)malloc(SIZE * sizeof(int));
int *arr_parallel = (int *)malloc(SIZE * sizeof(int));
for (int i = 0; i< SIZE; i++)
{
int val = rand() % 100000;
arr_serial[i] = val;
arr_parallel[i] = val;
}
clock_t start_serial = clock();
serialMergeSort(arr_serial, 0, SIZE - 1);
clock_t end_serial = clock();
double time_serial = (double)((double)(end_serial-start_serial)) / CLOCKS_PER_SEC;
clock_t start_parallel = clock();
parallelMergeSort(arr_parallel, 0, SIZE - 1, 0);
clock_t end_parallel = clock();
double time_parallel = (double)(end_parallel-start_parallel)/ CLOCKS_PER_SEC;
printf("Serial Merge Sort Time : %.6f seconds\n", time_serial);
printf("Parallel Merge Sort Time : %.6f seconds\n", time_parallel);
/*
for (int i = 0; i< SIZE; i++)
{
if (arr_serial[i] != arr_parallel[i])
{
printf("Mismatch at index %d\n", i);
break;
} }
*/
free(arr_serial);
free(arr_parallel);
return 0; }
Program 2
#include<stdio.h>
#include<omp.h>
int main()
{
int n = 16,thread;
printf("\nEnter the number of tasks:");
scanf("%d",&n);
printf("\nEnter the number of threads:");
scanf("%d",&thread);
omp_set_num_threads(thread);
printf("\n -------------------------------------------\n");
#pragma omp parallel for schedule(static, 2)
for (int i = 0; i< n; i++)
{
printf("Thread %d executes iteration %d\n",omp_get_thread_num(),i);
}
return 0;
}

Program 6
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[])
{
int rank, data_send, data_recv;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
data_send = rank;
if (rank == 0)
{
MPI_Send(&data_send, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&data_recv,1, MPI_INT, 1, 0, MPI_COMM_WORLD ,
MPI_STATUS_IGNORE);
}
else if (rank == 1)
{
MPI_Send(&data_send, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
MPI_Recv(&data_recv, 1, MPI_INT, 0, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
}
printf("Process %d received %d\n", rank, data_recv);
MPI_Finalize();
return 0; }
Program 3
#include <stdio.h>
#include <omp.h>
#include <time.h>
int ser_fib (long int n){
if (n < 2) return n;
long int x, y;
x = ser_fib(n - 1);
y = ser_fib(n - 2);
return (x + y);
}
int fib(long int n){
if (n < 2) return n;
long int x, y;
#pragma omp task shared(x)
x = fib(n - 1);
#pragma omp task shared(y)
y = fib(n - 2);
#pragma omptaskwait
return (x + y); }
int main(){
long int n, result;
clock_t start, end;
double cpu_time;
printf("\n enter the value of n:");
scanf("%ld",&n);
start=clock();
result=fib(n);
end=clock();
cpu_time = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\nFibonacci(%ld) = %ld\n",n,result);
printf("\n The time used to execute the program in parallel mode= %f",cpu_time);
start=clock();
result = ser_fib(n);
end=clock();
cpu_time = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\nFibonacci(%ld) = %ld\n",n,result);
printf("\n The time used to execute the program in sequential mode=%f\n",cpu_time);
return 0;
}
Program 4
#include<stdio.h>
#include <omp.h>
#include<time.h>
int is_prime(int n)
{
if (n < 2) return 0;
for (int i = 2; i*i<= n; i++)
if (n % i == 0) return 0;
return 1;
}
int main()
{
long n = 10000000;
time_t start,end;
double cpu_time;
printf("\n The range of numbers is 1 to %ld\n",n);
printf("\n---------------------------------------------------------------\n");
start=clock();
for (int i = 1; i<= n; i++)
{
is_prime(i);
}
end = clock();
cpu_time = ((double) (end - start)) / CLOCKS_PER_SEC;
printf(" Time to compute prime numbers serially: %f\n",cpu_time);
start=clock();
#pragma omp parallel for
for (int i = 1; i<= n; i++)
{
is_prime(i);
}
end = clock();
cpu_time = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Time to compute prime numbers Parallel: %f\n",cpu_time);
return 0;
}
Program 5
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[])
{
int rank, size;
int number;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (size < 2)
{
if (rank == 0)
{
printf("This program requires at least 2 processes.\n");
}
MPI_Finalize();
return 0;
}
if (rank == 0)
{
number = 42;
printf("Process 0 is sending number %d to Process 1\n", number);
MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
}
else if (rank == 1)
{
MPI_Recv(&number, 1, MPI_INT, 0, 0,
MPI_COMM_WORLD,MPI_STATUS_IGNORE);
printf("Process 1 received number %d from Process 0\n", number);
}
MPI_Finalize();
return 0;
}
Program 7
#include <stdio.h>
#include <mpi.h>
int main(int argc, char** argv)
{
int rank, data = 0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0)
data = 100;
MPI_Bcast(&data, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("Process %d received data: %d\n",rank, data);
MPI_Finalize();
return 0;
}

Program 8
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv)
{
int rank, size, send_data[4] = {10, 20, 30, 40}, recv_data;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Scatter(send_data, 1, MPI_INT, &recv_data, 1, MPI_INT, 0,
MPI_COMM_WORLD);
printf("Process %d received: %d\n", rank, recv_data);
recv_data += 1;
MPI_Gather(&recv_data, 1, MPI_INT, send_data, 1, MPI_INT, 0,
MPI_COMM_WORLD);
if (rank == 0)
{
printf("Gathered data: ");
for (int i = 0; i< size; i++)
printf("%d ", send_data[i]);
printf("\n");
}
MPI_Finalize();
return 0;
}
Program 9
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
int rank, value, sum, max, min, prod;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
value = rank + 1;
MPI_Reduce(&value, &min, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
MPI_Reduce(&value, &max, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
MPI_Reduce(&value, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&value, &prod, 1, MPI_INT, MPI_PROD, 0, MPI_COMM_WORLD);
if (rank == 0)
printf("Sum using Reduce: %d\nMin using Reduce: %d\nMAX using Reduce:
%d\n PROD using Reduce: %d\n", sum, min, max, prod);
MPI_Allreduce(&value, &min, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
MPI_Allreduce(&value, &max, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
MPI_Allreduce(&value, &sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(&value, &prod, 1, MPI_INT, MPI_PROD, MPI_COMM_WORLD);
printf("Max using Allreduce (rank %d): %d\nMin using Allreduce: %d\nSum using
Allreduce: %d\nProd using Allreduce: %d\n", rank, max, min, sum, prod);
MPI_Finalize();
return 0;
}

You might also like