Nimesh 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Enroll no:20BEIT30118

KADI SARVA VISHWAVIDYALAYA


LDRP Institute of Technology and Research, Gandhinagar
Practical Index
Subject Code: IT801‐N
Subject Title: Distributed and Parallel Computing (DPC)
Sr. No Practical
Name of Experiment Faculty Sign
Date

1 Parallel Odd‐Even Transposition Sort.

2 Mandelbrot Set Computation

3 N‐Body Simulation

4 Heat Distribution Simulation

5 Sample MPI program in C

6 Implement simple cluster using master and slave node

7 Divide and conquer parallel computing program using C

8 Implement sample program for load balancing

9 OpenMP concepts implementation

10 Implement distributed shared memory

1
Enroll no:20BEIT30118

PRACTICAL: 1
AIM: - Parallel Odd‐Even Transposition Sort.

Explanation of the concept: Odd-Even Transposition Sort is a parallel sorting


algorithm.
• It is based on the Bubble Sort technique, which compares every 2
consecutive numbers in the array and swap them if first is greater than the
second to get an ascending order array.
• It consists of 2 phases – the odd phase and even phase:
• Odd phase: Every odd indexed element is compared with the next even
indexed element (considering 1-based indexing).
Even phase: Every even indexed element is compared with the next odd indexed
element.
CODE:
// A C++ Program to implement Odd-Even / Brick Sort
#include<bits/stdc++.h>
using namespace std;

// A function to sort the algorithm using Odd Even sort


void oddEvenSort(int arr[], int n)
{
bool isSorted = false; // Initially array is unsorted

while (!isSorted)
{
isSorted = true;

// Perform Bubble sort on odd indexed element


for (int i=1; i<=n-2; i=i+2)
{
if (arr[i] > arr[i+1])
{
swap(arr[i], arr[i+1]);
isSorted = false;
}
}

// Perform Bubble sort on even indexed element


for (int i=0; i<=n-2; i=i+2)
{

2
Enroll no:20BEIT30118

if (arr[i] > arr[i+1])


{
swap(arr[i], arr[i+1]);
isSorted = false;
}
}
}

return;
}

// A utility function ot print an array of size n


void printArray(int arr[], int n)
{
for (int i=0; i < n; i++)
cout << arr[i] << " ";
cout << "\n";
}

// Driver program to test above functions.


int main()
{
int arr[] = {34, 2, 10, -9};
int n = sizeof(arr)/sizeof(arr[0]);

oddEvenSort(arr, n);
printArray(arr, n);

return (0);
}

Output:
-9 2 10 34

3
Enroll no:20BEIT30118

We demonstrate the above algorithm using the below illustration on the array
= {3, 2, 3, 8, 5, 6, 4, 1}

Time Complexity: O(N2) where, N = Number of elements in the input array.


Auxiliary Space: O(1). Just like bubble sort this is also an in-place algorithm.

4
Enroll no:20BEIT30118

PRACTICAL: 2
AIM: Mandelbrot Set Computation.
CODE:
#include <iostream>
#include <fstream> /* for ofstream */
#include <complex> /* for fractal arithmetic */

/**
A linear function in 2 dimensions: returns a double as a function of (x,y).
*/
class linear2d_function {
public:
double a,b,c;
void set(double a_,double b_,double c_) {a=a_;b=b_;c=c_;}
linear2d_function(double a_,double b_,double c_) {set(a_,b_,c_);}
double evaluate(double x,double y) const {return x*a+y*b+c;}
};

int foo(void)
{
// Figure out how big an image we should render:
int wid=350, ht=256;

// Create a PPM output image file header:


std::ofstream out("out.ppm",std::ios_base::binary);
out<<"P6\n"
<<wid<<" "<<ht<<"\n"
<<"255\n";

// Set up coordinate system to render the Mandelbrot Set:


double scale=3.0/wid;
linear2d_function fx(scale,0.0,-scale*wid/2); // returns c given pixels
linear2d_function fy(0.0,scale,-1.0);

for (int y=0;y<ht;y++)


for (int x=0;x<wid;x++) {
/* Walk this Mandelbrot Set pixel */
typedef std::complex<double> COMPLEX;
COMPLEX c(fx.evaluate(x,y),fy.evaluate(x,y));
COMPLEX z(0.0);
int count;
enum {max_count=100};
for (count=0;count<max_count;count++) {
z=z*z+c;
if ((z.real()*z.real()+z.imag()*z.imag())>=4.0) break;
}

/* Figure out the output pixel color */


unsigned char r,g,b;
r=(unsigned char)(z.real()*(256/2.0));

5
Enroll no:20BEIT30118

g=(unsigned char)(z.imag()*(256/2.0));
b=(unsigned char)(((z.real()*z.real()+z.imag()*z.imag()))*256);
out<<r<<g<<b;
}

return 0;
}

OUTPUT:

2 processes: compute 7.7 ms; total 11.9 ms

4 processes: compute 4.3 ms; total 12.9 ms

8 processes: compute 2.1 ms; total 15.2 ms

10 processes: compute 1.8 ms; total 13.6 ms

20 processes: compute 1.0 ms; total 20.0 ms

6
Enroll no:20BEIT30118

PRACTICAL: 3
AIM: N‐Body Simulation
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct
{
double x, y, z;
}vector;

double *masse;
vector *pos, *vel, *forza;
int main(int argc, char** argv){
double epsilon, dt, tmax,t;
double dx, dy, dz, dist, invdist, invdist3;
int N, i, l, m, n, s, j;
double a, b;
vector *k1, *k2, *k3, *k4, *w1, *w2, *w3, *w4, *pos1, *vel1;
if(argc!=5)
{
fprintf(stdout,"Il programma prende in input il softening, il passo d'integrazione, il
tempo massimo d'integrazione e il numero di corpi del sistema\n", argv[0]);
exit(1);
}
epsilon=strtod(argv[1],NULL);
dt=strtod(argv[2],NULL);
tmax=strtod(argv[3],NULL);
N=strtod(argv[4],NULL);

FILE* fp=fopen("Cond_ini.out", "r");


if(fp==NULL){
perror("Errore: file non trovato\n");
exit(1);
}
masse=(double*)malloc(N*sizeof(double));
pos=(vector*)malloc(N*sizeof(vector));
vel=(vector*)malloc(N*sizeof(vector));
forza=(vector*)malloc(N*sizeof(vector));
k1=(vector*)malloc(N*sizeof(vector));
k2=(vector*)malloc(N*sizeof(vector));
k3=(vector*)malloc(N*sizeof(vector));
k4=(vector*)malloc(N*sizeof(vector));
w1=(vector*)malloc(N*sizeof(vector));
w2=(vector*)malloc(N*sizeof(vector));
w3=(vector*)malloc(N*sizeof(vector));
w4=(vector*)malloc(N*sizeof(vector));
pos1=(vector*)malloc(N*sizeof(vector));
vel1=(vector*)malloc(N*sizeof(vector));

7
Enroll no:20BEIT30118

for(i=0;i<N;i++)
{
fscanf(fp,"%lf %lf %lf %lf %lf %lf %lf", &masse[i], &pos[i].x, &pos[i].y, &pos[i].z,
&vel[i].x, &vel[i].y, &vel[i].z);
}
fclose(fp);

printf("Condizioni iniziali:\n");
for(l=0;l<N;l++){
printf("%lf %lf %lf %lf %lf %lf %lf\n", masse[l], pos[l].x, pos[l].y, pos[l].z, vel[l].x,
vel[l].y, vel[l].z);
}

for(t=0;t<tmax;t+=dt){
for(m=0;m<N;m++){
for(n=0;n<N;n++){
if(m!=n){

k1[n].x=dt*vel[n].x;
k1[n].y=dt*vel[n].y;
k1[n].z=dt*vel[n].z;

dx=pos[n].x-pos[m].x;
dy=pos[n].y-pos[m].y;
dz=pos[n].z-pos[m].z;
dist=(dx*dx)+(dy*dy)+(dz*dz)+(epsilon*epsilon);
invdist=(1/sqrt(dist));
invdist3=(invdist*invdist*invdist);
forza[n].x=dx*invdist3*masse[n];
forza[n].y=dy*invdist3*masse[n];
forza[n].z=dz*invdist3*masse[n];

w1[n].x=dt*forza[n].x;
w1[n].y=dt*forza[n].y;
w1[n].z=dt*forza[n].z;

pos1[n].x=pos[n].x+(0.5*k1[n].x);
pos1[n].y=pos[n].y+(0.5*k1[n].y);
pos1[n].z=pos[n].z+(0.5*k1[n].z);
vel1[n].x=vel[n].x+(0.5*w1[n].x);
vel1[n].y=vel[n].y+(0.5*w1[n].y);
vel1[n].z=vel[n].z+(0.5*w1[n].z);

k2[n].x=dt*(vel[n].x+(0.5*w1[n].x));
k2[n].y=dt*(vel[n].y+(0.5*w1[n].y));
k2[n].z=dt*(vel[n].z+(0.5*w1[n].z));

dx=pos1[n].x-pos[m].x;
dy=pos1[n].y-pos[m].y;
dz=pos1[n].z-pos[m].z;

8
Enroll no:20BEIT30118

dist=(dx*dx)+(dy*dy)+(dz*dz)+(epsilon*epsilon);
invdist=(1/sqrt(dist));
invdist3=(invdist*invdist*invdist);
forza[n].x=dx*invdist3*masse[n];
forza[n].y=dy*invdist3*masse[n];
forza[n].z=dz*invdist3*masse[n];

w2[n].x=dt*forza[n].x;
w2[n].y=dt*forza[n].y;
w2[n].z=dt*forza[n].z;

pos1[n].x=pos[n].x+(0.5*k2[n].x);
pos1[n].y=pos[n].y+(0.5*k2[n].y);
pos1[n].z=pos[n].z+(0.5*k2[n].z);
vel1[n].x=vel[n].x+(0.5*w2[n].x);
vel1[n].y=vel[n].y+(0.5*w2[n].y);
vel1[n].z=vel[n].z+(0.5*w2[n].z);

k3[n].x=dt*(vel[n].x+(0.5*w2[n].x));
k3[n].y=dt*(vel[n].y+(0.5*w2[n].y));
k3[n].z=dt*(vel[n].z+(0.5*w2[n].z));

dx=pos1[n].x-pos[m].x;
dy=pos1[n].y-pos[m].y;
dz=pos1[n].z-pos[m].z;
dist=(dx*dx)+(dy*dy)+(dz*dz)+(epsilon*epsilon);
invdist=(1/sqrt(dist));
invdist3=(invdist*invdist*invdist);
forza[n].x=dx*invdist3*masse[n];
forza[n].y=dy*invdist3*masse[n];
forza[n].z=dy*invdist3*masse[n];

w3[n].x=dt*forza[n].x;
w3[n].y=dt*forza[n].y;
w3[n].z=dt*forza[n].z;

pos1[n].x=pos[n].x+(k3[n].x);
pos1[n].y=pos[n].y+(k3[n].y);
pos1[n].z=pos[n].z+(k3[n].z);
vel1[n].x=vel[n].x+(w3[n].x);
vel1[n].y=vel[n].y+(w3[n].y);
vel1[n].z=vel[n].z+(w3[n].z);

k4[n].x=dt*(vel[n].x+w3[n].x);
k4[n].y=dt*(vel[n].y+w3[n].y);
k4[n].z=dt*(vel[n].z+w3[n].z);

dx=pos1[n].x-pos[m].x;
dy=pos1[n].y-pos[m].y;

9
Enroll no:20BEIT30118

dz=pos1[n].z-pos[m].z;
dist=(dx*dx)+(dy*dy)+(dz*dz)+(epsilon*epsilon);
invdist=(1/sqrt(dist));
invdist3=(invdist*invdist*invdist);
forza[n].x=dx*invdist3*masse[n];
forza[n].y=dy*invdist3*masse[n];
forza[n].z=dy*invdist3*masse[n];

w4[n].x=dt*forza[n].x;
w4[n].y=dt*forza[n].y;
w4[n].z=dt*forza[n].z;

a=k1[n].x+(2*k2[n].x)+(2*k3[n].x)+k4[n].x;
a=a/6;
pos1[n].x=pos[n].x+a;
a=k1[n].y+(2*k2[n].y)+(2*k3[n].y)+k4[n].y;
a=a/6;
pos1[n].y=pos[n].y+a;
a=k1[n].z+(2*k2[n].z)+(2*k3[n].z)+k4[n].z;
a=a/6;
pos1[n].z=pos[n].z+a;
b=w1[n].x+(2*w2[n].x)+(2*w3[n].x)+w4[n].x;
b=b/6;
vel1[n].x=vel[n].x+a;
b=w1[n].y+(2*w2[n].y)+(2*w3[n].y)+w4[n].y;
b=b/6;
vel1[n].y=vel[n].y+a;
b=w1[n].z+(2*w2[n].z)+(2*w3[n].z)+w4[n].z;
b=b/6;
vel1[n].z=vel[n].z+a;
}
}
for(j=0;j<N;j++) {
forza[j].x=0;
forza[j].y=0;
forza[j].z=0;
}
}
for(i=0;i<N;i++){
pos[i].x=pos1[i].x;
pos[i].y=pos1[i].y;
pos[i].z=pos1[i].z;
vel[i].x=vel1[i].x;
vel[i].y=vel1[i].y;
vel[i].z=vel1[i].z;
printf("%lf %lf %lf %lf %lf %lf %lf\n", t, pos[i].x, pos[i].y, pos[i].z, vel[i].x, vel[i].y,
vel[i].z);
/*forza[i].x=0;
forza[i].y=0;
forza[i].z=0;*/

10
Enroll no:20BEIT30118

}
}
}

OUTPUT: And here's a plot of orbits at different tmax:


tmax=5

tmax=2

tmax=3

11
Enroll no:20BEIT30118

12
Enroll no:20BEIT30118

PRACTICAL: 4
AIM: Heat Distribution Simulation
CODE:
Program in C of a Heat Transfer problem by using Finite Difference
Method (FDM)
Consider a large Uranium Plate of thickness, L=4 cm and thermal conductivity,
k=28 W/m.Degree Cel in which Heat is generated uniformly at constant rate of
Hg=5x10^6 W/m^3. One side of the plate is maintained at 0 Degree Cel by iced
water while the other side is subjeted to convection…

Consider a large Uranium Plate of thickness, L=4 cm and thermal conductivity,


k=28 W/m.Degree Cel in which Heat is generated uniformly at constant rate of
Hg=5x10^6 W/m^3. One side of the plate is maintained at 0 Degree Cel by iced
water while the other side is subjeted to convection to an environmet at 30 Degree
Cel with a heat transfer coefficient of h=45 W/m^2.Degree Cel. Considering a
total of 5 equally spaced nodes in the medium,two at the boundaries and three at
the middle, estimate the surface temperatures of the plate at each nodes under
steady conditions using FDM and write the respective program in C

int main()
{
int i,j,m,n;
float L,dX,K,Hg,h;
printf("\nEnter the total Thickness in Meter (L)\t");
scanf("%f",&L);
printf("\nEnter the Number of nodes (n)\t ");
scanf("\n%d",&n);
dX=L/(n-1);
printf("\nLength of the Division (dX) is %f\t",dX);
float T[n];
printf("\nEnter the Initial Temperature (T1) in Degree Celsius\t");
scanf("%f",&T[1]);
printf("\nEnter the Ambient Temperature (T_amb) in Degree Celsius\t");
scanf("%f",&T[n+1]);
printf("\nEnter the Heat Generation (Hg) in W/m3\t");
scanf("%f",&Hg);
printf("\nEnter the Thermal Conductivity (K) in W/m-C\t");
scanf("%f",&K);
printf("\nEnter the Convective HT Coeff (h) in W/m2-C\t");
scanf("%f",&h);
printf("\nEnter the number of iterations (m)\t ");
scanf("%d",&m);

13
Enroll no:20BEIT30118

for(i=1;i<=n;i++)
{
T[i]=0;
}
for(j=0;j<=m;j++)
{
for(i=2;i<=n-1;i++)
{
T[i]=(((Hg*dX*dX)/(2*K))+((T[i-1]+T[i+1])/2));
}
T[n]=(((K*T[n-1])/dX)+(h*T[n+1])+(Hg*(dX/2)))*(dX/(K+(dX*h)));
}

for(i=1;i<=n+1;i++)
{
printf("\nNodal Temperature in Degree Celsius T%d\t",i);
printf("%f\n",T[i]);
}
}
OUTPUT

14
Enroll no:20BEIT30118

PRACTICAL: 5
AIM: Sample MPI program in C
CODE:
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv)
{
// Initialize the MPI environment
MPI_Init(NULL, NULL);

// Get the number of processes


int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

// Get the rank of the process


int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

// Get the name of the processor


char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);

// Print off a hello world message


printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);

// Finalize the MPI environment.


MPI_Finalize();
}

15
Enroll no:20BEIT30118

OUTPUT :

16
Enroll no:20BEIT30118

PRACTICAL: 6
AIM: Implement simple cluster using master and slave node
CODE:
Master server code : Pr6ser.c

#include <stdlib.h>
#include <stdio.h>
#include "mpi.h"
#include<string.h>
int main(int argc, char **argv)
{
MPI_Comm client;
MPI_Status status;
char port_name[MPI_MAX_PORT_NAME],str[50],ch,temp;
int size, again, i,j;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != 1) {
fprintf(stderr, "Server too big");
exit(EXIT_FAILURE);
}
MPI_Open_port(MPI_INFO_NULL, port_name);
printf("Server available at port: %s\n", port_name);
i=0;
while (1) {
MPI_Comm_accept(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD,
&client);
again = 1;
while (again) {
MPI_Recv(&ch, 1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG,
client, &status);
switch (status.MPI_TAG) {
case 0:
MPI_Comm_free(&client);
MPI_Close_port(port_name);
MPI_Finalize();
return 0;
case 1:
printf("\nReceived String: %s\n",str);
// reverse the string
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReversed string is : %s\n",str);
// send the reversed string to client (character by character)
for (i = 0; i < strlen(str); i++) {
ch=str[i];
MPI_Send(&ch, 1, MPI_CHAR, 0, 2, client);
}
//send tag=1 to indicate end of string
MPI_Send(&ch, 1, MPI_CHAR, 0, 1, client);
MPI_Comm_disconnect(&client);
again = 0;

17
Enroll no:20BEIT30118

strcpy(str,"");
i=0;
break;
case 2:
printf("Received character: %c\n", ch);
str[i]=ch;
i++;
// add null character at the end of string
str[i]='\0';
break;
default:
/* Unexpected message type */
MPI_Abort(MPI_COMM_WORLD, 1);
}
}
}
}

Client code : pr6cli


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mpi.h"
int main( int argc, char **argv )
{
MPI_Comm server;
MPI_Status status;
char port_name[MPI_MAX_PORT_NAME],str[50],ch;
int i, tag,again;
if (argc < 2) {
fprintf(stderr, "server port name required.\n");
exit(EXIT_FAILURE);
}
MPI_Init(&argc, &argv);
strcpy(port_name, argv[1]);
MPI_Comm_connect(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &server);
// accept input string
printf("\nEnter the string :\n");
scanf("%s",str);
//send string to server (character by character)
for (i = 0; i < strlen(str); i++) {
if(str[i]!='\0')
ch=str[i];
tag=2;
MPI_Send(&ch, 1, MPI_CHAR, 0, tag, server);
}
// done sending string to the server
MPI_Send(&i, 0, MPI_INT, 0, 1, server);
// Receive the reversed string from server and display it
i=0;
again=1;
while (again) {
MPI_Recv(&ch, 1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, server,
&status);
switch (status.MPI_TAG) {
case 2:
str[i]=ch;
i++;
break;
case 1: again=0;

18
Enroll no:20BEIT30118

break;
}
}
printf("\nReversed string is : %s\n\n",str);
MPI_Comm_disconnect(&server);
MPI_Finalize();
return 0;
}

19
Enroll no:20BEIT30118

Output :-

20
Enroll no:20BEIT30118

PRACTICAL: 7
AIM:Divide and conquer parallel computing program using C

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define SIZE 10000

int binary_search(int arr[], int start, int end, int target) {


if (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
return binary_search(arr, mid + 1, end, target);
} else {
return binary_search(arr, start, mid – 1, target);
}
}
return -1;
}

int main() {
int arr[SIZE];
for (int I = 0; I < SIZE; i++) {
arr[i] = I;
}

int target = SIZE – 1;

int found_index = -1;

#pragma omp parallel


{
#pragma omp single
{
found_index = binary_search(arr, 0, SIZE – 1, target);
}
}

21
Enroll no:20BEIT30118

if (found_index == -1) {
printf(“Target not found.\n”);
} else {
printf(“Target found at index %d.\n”, found_index);
}

return 0;
}

Output:-
Target found at index 9999

22
Enroll no:20BEIT30118

PRACTICAL 8
Aim: Implement sample program for load balancing

Server : pr8ser.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>

#define MAX_MESSAGE_SIZE 1024


#define BACKLOG 10

int main(int argc, char **argv) {


if (argc != 2) {
printf("Usage: %s [port]\n", argv[0]);
exit(EXIT_FAILURE);
}

int port = atoi(argv[1]);

// Create socket
int server_fd;
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}

// Bind socket to port


struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(port);

if (bind(server_fd, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {


perror("Bind failed");
exit(EXIT_FAILURE);
}

// Listen for incoming connections


if (listen(server_fd, BACKLOG) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}

printf("Server listening on port %d...\n", port);

// Handle incoming connections


while (1) {
int client_fd;

23
Enroll no:20BEIT30118

struct sockaddr_in client_address;


int client_address_size = sizeof(client_address);

if ((client_fd = accept(server_fd, (struct sockaddr *)&client_address, (socklen_t


*)&client_address_size)) < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}

printf("Client connected: %s:%d\n", inet_ntoa(client_address.sin_addr),


ntohs(client_address.sin_port));

// Receive message from client


char message[MAX_MESSAGE_SIZE];
int bytes_received;

if ((bytes_received = read(client_fd, message, MAX_MESSAGE_SIZE)) < 0) {


perror("Read from client failed");
exit(EXIT_FAILURE);
}

message[bytes_received] = '\0';
printf("Received message from client: %s\n", message);

// Send response to client


// char response[] = "Hello from server!";
//char response[] = message;
if (write(client_fd, message, strlen(message)) < 0) {
perror("Write to client failed");
exit(EXIT_FAILURE);
}

printf("Sent response to client: %s\n", message);

// Close connection
close(client_fd);
}

// Close server socket


close(server_fd);

return 0;
}

Client : pr8cli.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>

24
Enroll no:20BEIT30118

#define MAX_MESSAGE_SIZE 1024

int main(int argc, char **argv) {


if (argc != 3) {
printf("Usage: %s [host] [port]\n", argv[0]);
exit(EXIT_FAILURE);
}

char *host = argv[1];


int port = atoi(argv[2]);

// Create socket
int client_fd;
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}

// Connect to load balancer


struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(host);
address.sin_port = htons(port);

if (connect(client_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {


perror("Connection to load balancer failed");
exit(EXIT_FAILURE);
}

printf("Connected to load balancer on %s:%d\n", host, port);

// Send message to load balancer


char message[MAX_MESSAGE_SIZE];
printf("Enter message: ");
fgets(message, MAX_MESSAGE_SIZE, stdin);

if (write(client_fd, message, strlen(message)) < 0) {


perror("Write to load balancer failed");
exit(EXIT_FAILURE);
}

// Receive response from load balancer


char response[MAX_MESSAGE_SIZE];
int bytes_received;

if ((bytes_received = read(client_fd, response, MAX_MESSAGE_SIZE)) < 0) {


perror("Read from load balancer failed");
exit(EXIT_FAILURE);
}

response[bytes_received] = '\0';
printf("Response from server: %s\n", response);

25
Enroll no:20BEIT30118

close(client_fd);

return 0;
}

Output:-

26
Enroll no:20BEIT30118

27
Enroll no:20BEIT30118

PRACTICAL 9
OpenMP concepts implementation
A). Hello from Process
#include <stdio.h>
#include <omp.h>

int main(int argc, char** argv){


#pragma omp parallel
{
printf("Hello from process: %d\n", omp_get_thread_num());
}
return 0;
}
Output:-

B). Write Program to print 1 to 100 number in parallel.


#include <stdio.h>
#include <omp.h>

int main(int argc, char** argv)


{
#pragma omp parallel for
for(int i = 1; i <= 100; i++)
{
printf(" %d " ,i);
}

return 0;
}

Output:-

28
Enroll no:20BEIT30118

29
Enroll no:20BEIT30118

PRACTICAL 10
AIM: Implement distributed shared memory
File : pr10.c
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

#define PAGE_SIZE 4096


#define NUM_PAGES 1024

int main(int argc, char **argv) {


MPI_Init(&argc, &argv);

int rank, size;


MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

// Allocate DSM pages


void *dsm_pages[NUM_PAGES];
for (int i = 0; i < NUM_PAGES; i++) {
dsm_pages[i] = malloc(PAGE_SIZE);
if (dsm_pages[i] == NULL) {
perror("DSM page allocation failed");
exit(EXIT_FAILURE);
}
}

// Handle requests
while (1) {
int page_num, offset, size;
MPI_Status status;

// Receive request from another process


MPI_Recv(&page_num, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
offset = status.MPI_TAG;
size = status.MPI_ERROR;

// Send data back to the requesting process


if (page_num >= 0 && page_num < NUM_PAGES) {
void *data_ptr = dsm_pages[page_num] + offset;
MPI_Send(data_ptr, size, MPI_CHAR, status.MPI_SOURCE, 0,
MPI_COMM_WORLD);
}
}

MPI_Finalize();
return 0;
}

30
Enroll no:20BEIT30118

Output:-

31

You might also like