# InequalityPDC1

### InequalityPDC1

=======================================LAB 1=======================================
Q. Write an OpenMP program to do Vector addition and dot product using both Reduction and Critical clauses. Compare the Execution time for different sizes of an array.

A.
#include <omp.h>
#define CS 100
#define N 1000
void main ()
{
int i, ch;
float a[N], b[N], c[N];
/* Some initializations */
for (i=0; i < N; i++)
a[i] = b[i] = i * 1.0;
ch = CS;
#pragma omp parallel shared(a,b,c,ch) private(i)
{
#pragma omp for schedule(dynamic,ch) nowait
for (i=0; i < N; i++)
c[i] = a[i] + b[i];
print (c[i]);
print (“\n”);
} /* end of parallel section */
}

=======================================LAB 2=======================================
QUESTION

Write a parallel Program using OpenMP to do Matrix addition and Subtraction using sections without barrier construct.

CODE
#include<stdio.h>
#include<omp.h>
int main()
{
//Declarations
int a[3][3] , b[3][3] , sum[3][3] , diff[3][3] , i , j,i1,j1,i2,j2 ;
//Initialization of matrix A
printf("Enter the matrix A (3X3):\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&(a[i][j]));
}
}
//Initialization of matrix B
printf("Enter the matrix B (3X3) :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&(b[i][j]));
}
}
#pragma omp parallel sections
{
#pragma omp section
{
#pragma omp parallel for
for(i1=0;i1<3;i1++)
{
#pragma omp parallel
for(j1=0;j1<3;j1++)
{
sum[i1][j1]=a[i1][j1]+b[i1][j1];
}
}
}
//Matrix Subtraction
#pragma omp section
{
#pragma omp parallel for
for(i2=0;i2<3;i2++)
{
#pragma omp parallel for
for(j2=0;j2<3;j2++)
{
diff[i2][j2]=a[i2][j2]-b[i2][j2];
}
}
}
}
// Print Statements
printf("\nA + B :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ", sum[i][j]);
}
printf("\n");
}
printf("\nA - B :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ", diff[i][j]);
}
printf("\n");
}
}

=======================================LAB 3=======================================
Q) Write a parallel Program using OpenMP to do quicksort in parallel.
CODE:
#include<stdio.h>
#include<omp.h>
#include<time.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
} int partition (
int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
#pragma omp parallel for //Making the swap parallel(automatic reduction applied)
for (int j = low; j <= high- 1; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
} swap(&arr[i
+
1], &
arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
#pragma omp parallel sections //The following occurs in parallel
{
#pragma
omp section
quickSort(arr, low, pi - 1);
#pragma
omp section
quickSort(arr, pi + 1, high);
}
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
} int main()
{
int arr[5],i;
time_t start_t, end_t ,random_seed;
double diff_t;
srand((unsigned) time(&random_seed));
#pragma parallel for
//Parallel generation of random numbers
for(i=0;i<5;i++)
{
arr[i]=rand() % 100;
} int n
=
sizeof(arr)/sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, n);
time(&start_t);
//Start time
quickSort(arr, 0, n-1);
time(&end_t);
//End timediff_t = difftime(end_t, start_t); //Time taken to sort
printf("Sorted array: \n");
printArray(arr, n);
printf("Time taken: %f\n",diff_t);
return 0;
}

=======================================LAB 4=======================================

QUESTION:
Implement parallel binary search using MPI+ OpenMP
CODE:
•Given a pre-defined sorted array we have to find if element ‘19’ is present in it usingparallel programming.

#include "stdafx.h"
#include<stdio.h>
#include<iostream>
#include<conio.h>
#include<omp.h>
using namespace std;
#define ARRSIZE 44
int main()
{
int x[] = { 3,4,7,9,19,20,27,28,29,30 }; //sorted Array int i, j, found = 0,done=0; int key = 19; //Searching fordouble start_time, run_time; for (j = 1; j <= 5; j++) {
found = 0;
start_time = omp_get_wtime();
#pragma omp parallel private(i)
{
int start, noofsteps;
#pragma omp single
printf("num of threads in action: %d\n", j);
if (found == 0)
{
noofsteps = start + (ARRSIZE / omp_get_num_threads());
if (ARRSIZE%j != 0)
noofsteps += (ARRSIZE%j);
int low = 0, high = 10;//10 is the size of array
while (low <= high)
{
int mid = (low + high) / 2;
if (a[mid] == key)
{
cout << "Element found at index:" << mid;
done = 1;
break;
}
else if (a[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
}
}
if (done == 0)
cout << "Element not available in given array";
run_time = omp_get_wtime() - start_time;
cout<<"\n"<<run_time<<" seconds "<< j <<" thread(s) \n ";
}
_getch();
return 0;
}

=======================================LAB 5=======================================
QUESTION:
Initialize an array of 100 elements. Perform the sum of the elements sharing the load among 4 processes using MPI Send and MPI Recv.

CODE:
#include <stdio.h>
#include <mpi.h>

#define max_rows 100000
#define send_data_tag 2001
#define return_data_tag 2002

int array[max_rows];
int array2[max_rows];

main(int argc, char **argv)
{
long int sum, partial_sum;
MPI_Status status;
int my_id, root_process, ierr, i, num_rows, num_procs,

// Replicting this process to create parallel processes.

// Every process executes a seperate copy of this program.

ierr = MPI_Init(&argc, &argv);

root_process = 0;

ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

if(my_id == root_process)
{

printf("please enter the number of numbers to sum: ");
scanf("%i", &num_rows);

if(num_rows > max_rows) {
printf("Too many numbers.\n");
exit(1);
}

avg_rows_per_process = num_rows / num_procs;

// initialize an array

for(i = 0; i < num_rows; i++) {
array[i] = i + 1;
}

for(an_id = 1; an_id < num_procs; an_id++) {
start_row = an_id*avg_rows_per_process + 1;
end_row = (an_id + 1)*avg_rows_per_process;

if((num_rows - end_row) < avg_rows_per_process)
end_row = num_rows - 1;

num_rows_to_send = end_row - start_row + 1;

ierr = MPI_Send( &num_rows_to_send, 1 , MPI_INT, an_id, send_data_tag, MPI_COMM_WORLD);

ierr = MPI_Send( &array[start_row], num_rows_to_send, MPI_INT, an_id, send_data_tag, MPI_COMM_WORLD);
}

// Calculating Sum

sum = 0;
for(i = 0; i < avg_rows_per_process + 1; i++)
{
sum += array[i];
}

printf("sum %i calculated by root process\n", sum);

for(an_id = 1; an_id < num_procs; an_id++) {

ierr = MPI_Recv( &partial_sum, 1, MPI_LONG, MPI_ANY_SOURCE, return_data_tag, MPI_COMM_WORLD, &status);

sender = status.MPI_SOURCE;

printf("Partial sum %i returned from process %i\n", partial_sum, sender);

sum += partial_sum;
}

printf("The grand total is: %i\n", sum);
}

else {

ierr = MPI_Recv( &num_rows_to_receive, 1, MPI_INT, root_process, send_data_tag, MPI_COMM_WORLD, &status);

ierr = MPI_Recv( &array2, num_rows_to_receive, MPI_INT, root_process, send_data_tag, MPI_COMM_WORLD, &status);

partial_sum = 0;
for(i = 0; i < num_rows_received; i++) {
partial_sum += array2[i];
}

ierr = MPI_Send( &partial_sum, 1, MPI_LONG, root_process, return_data_tag, MPI_COMM_WORLD);
}
ierr = MPI_Finalize();
}

=======================================LAB 6=======================================

QUESTION:
Write a MPI program that adds numbers stored in the data file " data.txt" in parallel.

CODE:
#include <stdlib.h>
#include <string.h>
#include "mpi.h"
#include <stdio.h>
#include <math.h>
#define MAXSIZE 100

int main(int argc, char **argv)
{
int myid, numprocs;
int data[MAXSIZE], i, x, low, high, myresult=0, result;
char fn[255];
FILE *fp;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);

if(0 == myid) {

strcpy(fn, getenv("PWD"));
strcat(fn, "/rand_data.txt");
if( NULL == (fp = fopen(fn, "r")) ) {
printf("Can't open the input file: %s\n\n", fn);
exit(1);
}
for(i=0; i<MAXSIZE; i++) {
fscanf(fp, "%d", &data[i]);
}
}

MPI_Bcast(data, MAXSIZE, MPI_INT, 0, MPI_COMM_WORLD);

x = MAXSIZE/numprocs;
low = myid * x;
high = low + x;
for(i=low; i<high; i++) {
myresult += data[i];
}
printf("I got %d from %d\n", myresult, myid);

MPI_Reduce(&myresult, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

if(0 == myid) {
printf("The sum is %d.\n", result);
}

MPI_Finalize();
}
=======================================LAB 7=======================================
RMI APPLICATION FOR CHECKING PRIME NUMBERS

Interface
package lab7;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Prime extends Remote{
public int check_prime(int no) throws RemoteException;

}

Implementation
package lab7;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Prime_Impli extends UnicastRemoteObject implements Prime{
public Prime_Impli() throws Exception{}

@Override
public int check_prime(int no) throws RemoteException {
int i,cnt=0;
for(i=2;i<=no/2;i++)
if(no%i==0)
{
cnt++;
break;
}
return (cnt);
}

}

SERVER
package lab7;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Prime_server {

/**
* @param args the command line arguments
* @throws java.rmi.RemoteException
*/
public static void main(String[] args) throws RemoteException {
try{
Registry registry=LocateRegistry.createRegistry(1234);
}
catch(Exception ex){}
}

}

CLIENT
package lab7;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class Prime_client {
public static void main(String args[]) throws NotBoundException, MalformedURLException, RemoteException, IOException{
try{
System.out.println("Enter a number : ");
int x=intf.check_prime(no);
if(x==0)
System.out.println(no+" is a prime number");
else
System.out.println(no+" is not a prime number");

}
catch(IOException | NumberFormatException | NotBoundException ex){}
}

}
Guest

### Who is online

Users browsing this forum: No registered users and 1 guest