Nimesh 1
Nimesh 1
Nimesh 1
3 N‐Body Simulation
1
Enroll no:20BEIT30118
PRACTICAL: 1
AIM: - Parallel Odd‐Even Transposition Sort.
while (!isSorted)
{
isSorted = true;
2
Enroll no:20BEIT30118
return;
}
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}
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;
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:
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);
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
}
}
}
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…
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);
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);
}
}
}
}
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>
int main() {
int arr[SIZE];
for (int I = 0; I < SIZE; i++) {
arr[i] = I;
}
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>
// Create socket
int server_fd;
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
23
Enroll no:20BEIT30118
message[bytes_received] = '\0';
printf("Received message from client: %s\n", message);
// Close connection
close(client_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
// Create socket
int client_fd;
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket creation 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>
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>
// Handle requests
while (1) {
int page_num, offset, size;
MPI_Status status;
MPI_Finalize();
return 0;
}
30
Enroll no:20BEIT30118
Output:-
31