Assignment # 1
Due:
1.Compile and run the sample programs.
a.SendReceive.c
b.Broadcast.c
Find the output of the programs. ]
*******************************************************************
2a.Consider, add.c, that adds the numbers in an array.
Let N denote the size of the array;
P, the number of processors and
Q, the number of processes. \
Do the following:
Compile and run the program.
Output the result.
2b.Modify the program so that
data is generated by a random number generator and
store it in the array, of size N, provided by the user.
2c.Modify the program to
find the maximum element in the array and
output the result.
2d.Now, perform the following experiments:
i.
Compile and run the program for
N =1024 and
vary, P (P = 2, 4 and 8).
Get the timing for these processors. What is the relation between P and Q?
ii.
Let N = 1024,
P = 4 and
vary Q(Q = 1, 2, 4, 8,16).
iii
.Get the timings. What is the relation between P and Q?
Fix P = 4, Q = 4 and vary the data size N (N=512,1024,2048).
Get the timing results.
3.a. Modify the program, add.c, as follows:
a.Distribute the process's portion of data by using the scatter operation.
b.Compute the sum of the numbers and find the maximum number and output the results.
Perform the same experiments as described above in question 4 d).
c. What is the difference between the modified add.c and original add.c in terms of performance? Which is more efficient?
4a.
Compute the matrix multiplication of two mxm matrices.
Use the random number generator to generate a 16x16 matrix.
Assume the numbers are integers.
Create m2 processes.
Experiment with different number of processors (P = 2,4,8).
Show the output of the resulting matrix.
4b.Is this an efficient way of partitioning the matrices? Can you think of any other way? If so, explain your algorithm. Code it, run and explain the results.
********************
5.Read the article on "What's next in HPC?" Answer the following questions. If you cannot find the answers in the article, search for it on the net.
a.What is fine-grain parallelism and coarse-grain parallelism?
b.What is grid computing?
c.What is P2P computing?
d.Where do you think the future is heading and why?
6.Go to top500.org.
Indicate the top 5 supercomputers with the flops ratings and the architecture (shared memory, distributed memory, etc.).
7. Write in your own words, a paragraph on the kind of research conducted at SETI?
8.Write a one page report on the article "Why is PVM and MPI so different"? Has there been any improvements in MPI and PVM in the recent years? Explain.
1Asendreceive.c
#include
<stdio.h>
#include
<sys/time.h>
#include
"mpi.h"
int main( int
argc, char *argv[] )
{
int
rank, value, size;
MPI_Status
status;
FILE *fp;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD,
&rank );
MPI_Comm_size( MPI_COMM_WORLD,
&size );
if (rank == 0)
{
fp
= fopen("data.dat","r");
if (fp == NULL)
{
printf("Cannot open
file");
exit(0);
}
}
do
{
if (rank == 0)
{
while (!feof(fp))
{
fscanf(fp,
"%d", &value );
printf("%d\n",value);
MPI_Send( &value, 1,
MPI_INT, rank + 1, 0, MPI_COMM_WORLD );
value=0;
}
value =
-1;
MPI_Send( &value, 1,
MPI_INT, rank + 1, 0, MPI_COMM_WORLD );
}
else
{
MPI_Recv( &value, 1,
MPI_INT, rank - 1, 0, MPI_COMM_WORLD,&status );
if (
rank<size-1)
MPI_Send( &value, 1,
MPI_INT, rank + 1, 0, MPI_COMM_WORLD );
printf( "Process %d got
%d from %d\n", rank, value,status.MPI_SOURCE );
}
}while ( value>=0);
MPI_Finalize( );
return 0;
}
1b broadcast.c
#include
<stdio.h>
#include
<sys/time.h>
#include
"mpi.h"
long Time_In_Sec(void)
{
static struct timeval tp;
gettimeofday(&tp, (struct
timezone *)0);
return (tp.tv_sec + tp.tv_usec /
1000000);
}
long Time_In_MilliSec(void)
{
static struct timeval tp;
gettimeofday(&tp, (struct
timezone *)0);
return (tp.tv_sec*1000 + tp.tv_usec /
1000);
}
long Time_In_MicroSec(void)
{
static struct timeval tp;
gettimeofday(&tp, (struct
timezone *)0);
return (tp.tv_sec*1000000 + tp.tv_usec);
}
int main( int
argc, char *argv[] )
{
int
rank,value;
FILE *fp;
long stime, etime;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD,
&rank );
if (rank == 0)
{
fp
= fopen("data.dat","r");
if (fp == NULL)
{
printf("Cannot open
file");
exit(0);
}
}
stime
= Time_In_MicroSec();
do
{
if (rank == 0)
{
fscanf(fp,
"%d", &value );
//Terminating Condition
if (feof(fp))
value = -1;
}
MPI_Bcast( &value, 1, MPI_INT, 0, MPI_COMM_WORLD );
printf( "Process %d got %d\n", rank, value );
}while ( value>=0);
etime
= Time_In_MicroSec();
if ( rank == 0)
printf("\nElapsed Time = %ld microsecond\n",etime-stime);
MPI_Finalize( );
return 0;
}
1b.broadcast1.c
File:
1b.Broadcast1.c
#include
<stdio.h>
#include
<sys/time.h>
#include
"mpi.h"
long Time_In_Sec(void)
{
static struct timeval tp;
gettimeofday(&tp, (struct
timezone *)0);
return (tp.tv_sec + tp.tv_usec /
1000000);
}
long Time_In_MilliSec(void)
{
static struct timeval tp;
gettimeofday(&tp, (struct
timezone *)0);
return (tp.tv_sec*1000 + tp.tv_usec /
1000);
}
long Time_In_MicroSec(void)
{
static struct timeval tp;
gettimeofday(&tp, (struct
timezone *)0);
return (tp.tv_sec*1000000 + tp.tv_usec);
}
int main( int
argc, char *argv[] )
{
int
rank,value;
FILE *fp;
long stime, etime;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD,
&rank );
if (rank == 0)
{
fp
= fopen("data.dat","r");
if (fp == NULL)
{
printf("Cannot open
file");
exit(0);
}
}
stime
= Time_In_MicroSec();
do
{
if (rank == 0)
{
fscanf(fp,
"%d", &value );
//Terminating Condition
if (feof(fp))
value = -1;
}
etime
= Time_In_MicroSec();
printf("Endtimed Inside =%d\n",etime);
MPI_Bcast( &value, 1,
MPI_INT, 0, MPI_COMM_WORLD );
printf( "Process %d got %d\n", rank, value );
}while ( value>=0);
etime
= Time_In_MicroSec();
if ( rank == 0){
printf("\nElapsed Time = %ld microsecond\n",etime-stime);
printf("end time outside %d\n",etime);
}
MPI_Finalize( );
return 0;
}
2A.add.c
#include #include "mpi.h"#define DATASIZE 1000 int main(int argc, char *argv[] )
{ int my_rank, num_procs, index, chunk, low, high, i, my_result; int data[DATASIZE]; FILE *fp; double stime, etime; MPI_Init( &argc, &argv ); index = 0; my_result = 0; MPI_Comm_rank( MPI_COMM_WORLD, &my_rank); MPI_Comm_size( MPI_COMM_WORLD, &num_procs); stime = MPI_Wtick(); if (my_rank == 0) { fp = fopen("data.dat","r"); if (fp == NULL) { printf("Cannot open file"); exit(0); } while (!feof(fp)) { fscanf(fp,"%d",&data[index]); index++; } } MPI_Bcast(&index,1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(data,index, MPI_INT, 0, MPI_COMM_WORLD); chunk = index / num_procs; low = my_rank * chunk; high = low + chunk; printf(" process %d chunk %d low %d high %d \n", my_rank, chunk, low, high); for ( i = low; i< high ; i++) my_result += data[i]; printf(" The result from process %d is %d \n", my_rank, my_result); MPI_Finalize( ); return 0;}
Program 2b.randomaddarg.c
#include
<stdio.h>
#include
"mpi.h"
#define
DATASIZE 1000
int main(int
argc, char *argv[] )
{
int my_rank,
num_procs, index, chunk, low, high, i, my_result;
int
data[DATASIZE];
FILE *fp;
double stime, etime;
MPI_Init( &argc, &argv );
index = 0;
my_result = 0;
MPI_Comm_rank( MPI_COMM_WORLD, &my_rank);
MPI_Comm_size( MPI_COMM_WORLD, &num_procs);
stime
= MPI_Wtick();
if (my_rank == 0)
{
/*fp = fopen("data.dat","r");
if (fp == NULL)
{
printf("Cannot open
file");
exit(0);
}
while (!feof(fp))
{
fscanf(fp,"%d",&data[index]);
index++;
}*/
int
N=atoi(argv[1]);
srandom(100);
printf("Random Datas are as
follows\n");
for(index=0;index<N;index++)
{
data[index]=random()%100;
printf("index=%d\t
Data=%d\n",index,data[index]);
}
}
MPI_Bcast(&index,1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(data,index, MPI_INT, 0,
MPI_COMM_WORLD);
if ((index % num_procs)==0)
{
chunk=index/num_procs;
low=my_rank*chunk;
high=low+chunk-1;
}
else
{
chunk=(index/num_procs)+1;
low=my_rank*chunk;
if (my_rank!=num_procs-1)
high=low+chunk-1;
else
high=index-1;
}
printf(" process %d
chunk %d :::low %d :::high %d \n", my_rank,
chunk, low, high);
for ( i = low; i<= high ; i++)
my_result +=
data[i];
printf(" The result from process %d is %d \n", my_rank, my_result);
MPI_Finalize( );
return 0;
}
Program 2c.maximum.c
#include
<stdio.h>
#include
"mpi.h"
#define
DATASIZE 1000
int main(int
argc, char *argv[] )
{
int my_rank,
num_procs, index, chunk, low, high, i, my_result;
int
max=0;
int
data[DATASIZE];
FILE *fp;
double stime, etime;
MPI_Init( &argc, &argv );
index = 0;
my_result = 0;
MPI_Comm_rank( MPI_COMM_WORLD, &my_rank);
MPI_Comm_size( MPI_COMM_WORLD, &num_procs);
stime
= MPI_Wtick();
if (my_rank == 0)
{
fp
= fopen("data.dat","r");
if (fp == NULL)
{
printf("Cannot open
file");
exit(0);
}
printf("Datas are as follows\n");
while (!feof(fp))
{
fscanf(fp,"%d",&data[index]);
printf("%d\n",data[index]);
index++;
}
}
MPI_Bcast(&index,1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(data,index, MPI_INT, 0,
MPI_COMM_WORLD);
if ((index % num_procs)==0)
{
chunk=index/num_procs;
low= my_rank * chunk;
high = low +
chunk-1;
}
else{
chunk=(index/num_procs)+1;
low = my_rank * chunk;
if (my_rank!=num_procs-1)
{
high=low+chunk-1;
}
else
high=index-1;
}
printf(" process %d
chunk %d low %d high %d \n", my_rank,
chunk, low, high);
int
j;
for ( i = low; i<= high ; i++)
{
max=data[i];
for(j=i+1;j<=high;j++){
if
(data[j]> max)
max=data[j];
}
}
MPI_Reduce(&max,&my_result,1,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD);
//printf(" The result from process %d is %d \n", my_rank, my_result);
if (my_rank==0)
{
printf("\n\nmax=%d\n\n",my_result);
}
MPI_Finalize( );
return 0;
}
Program 2d.addtiming.c
#include
<sys/time.h>
#include
<stdio.h>
#include
"mpi.h"
#define
DATASIZE 1050
long Time_In_MicroSec(void)
{
static struct timeval tp;
gettimeofday(&tp,(struct
timezone*)0);
return (tp.tv_sec*1000000+tp.tv_usec);
}
int main(int
argc, char *argv[] )
{
int
my_rank, num_procs, index,
chunk, low, high, i, my_result;
int
data[DATASIZE];
FILE *fp;
long stime, etime;
MPI_Init( &argc, &argv );
index = 0;
my_result = 0;
MPI_Comm_rank( MPI_COMM_WORLD, &my_rank);
MPI_Comm_size( MPI_COMM_WORLD, &num_procs);
stime
= Time_In_MicroSec();
if (my_rank == 0)
{
fp
= fopen("data.dat","r");
if (fp == NULL)
{
printf("Cannot open
file");
exit(0);
}
while (!feof(fp))
{
fscanf(fp,"%d",&data[index]);
index++;
}
}
MPI_Bcast(&index,1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(data,index, MPI_INT, 0,
MPI_COMM_WORLD);
chunk = index / num_procs;
low = my_rank * chunk;
high = low +
chunk;
printf(" process %d
chunk %d low %d high %d \n", my_rank,
chunk, low, high);
for ( i = low; i< high ; i++)
my_result +=
data[i];
printf(" The result from process %d is %d \n", my_rank, my_result);
etime=Time_In_MicroSec();
long elaptime=etime-stime;
long avg;
printf("\n\nRank=%d elapsed
time=%d\n\n",my_rank,elaptime);
MPI_Reduce(&elaptime,&avg,1,MPI_LONG,MPI_SUM,0,MPI_COMM_WORLD);
if (my_rank==0)
printf("\nSUM=%d\n",avg);
MPI_Finalize( );
return 0;
}
Program 3ab.summaximum.c
#include
<stdio.h>
#include
"mpi.h"
#define
DATASIZE 1000
int main(int
argc, char *argv[] )
{
int
my_rank, num_procs, index,
chunk, low, high, i, my_result;
int
data[DATASIZE];
int
rdata[DATASIZE];
FILE *fp;
double stime, etime;
int
sum,max;
MPI_Init( &argc, &argv );
index = 0;
my_result = 0;
MPI_Comm_rank( MPI_COMM_WORLD, &my_rank);
MPI_Comm_size( MPI_COMM_WORLD, &num_procs);
stime
= MPI_Wtick();
if (my_rank == 0)
{
fp
= fopen("data.dat","r");
if (fp == NULL)
{
printf("Cannot open
file");
exit(0);
}
while (!feof(fp))
{
fscanf(fp,"%d",&data[index]);
index++;
}
}
MPI_Bcast(&index,1, MPI_INT,0, MPI_COMM_WORLD);
chunk = index / num_procs;
if ((index % num_procs)==0)
{
chunk = index
/ num_procs;
}
else {
chunk =
(index / num_procs) + 1;
}
MPI_Scatter((void *)&data,chunk,MPI_INT,
(void *)&rdata,chunk,MPI_INT,0,MPI_COMM_WORLD);
for(i=0;i<chunk;i++)
{
sum+=rdata[i];
printf("rank=%d\trdata=%d\n",my_rank,rdata[i]);
}
printf("Rank=%d\tSUM=%d\n",my_rank,sum);
MPI_Reduce(&sum,&max,1,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD);
if (my_rank==0)
printf("MAXIMUM=%d\n",max);
/*printf(" process %d
chunk %d low %d high %d \n", my_rank,
chunk, low, high);
for ( i = low; i< high ; i++)
my_result +=
data[i];
printf(" The result from process %d is %d \n", my_rank, my_result);*/
MPI_Finalize( );
return 0;
}
3c.summaximumtiming
#include
<stdio.h>
#include
<sys/time.h>
#include
"mpi.h"
#define
DATASIZE 1000
long Time_In_MicroSec(void)
{
static struct timeval tp;
gettimeofday(&tp,(struct
timezone*)0);
return (tp.tv_sec*1000000+tp.tv_usec);
}
int main(int
argc, char *argv[] )
{
int
my_rank, num_procs, index,
chunk, low, high, i, my_result;
int
data[DATASIZE];
int
rdata[DATASIZE];
FILE *fp;
long stime, etime;
int
sum,max;
MPI_Init( &argc, &argv );
index = 0;
my_result = 0;
MPI_Comm_rank( MPI_COMM_WORLD, &my_rank);
MPI_Comm_size( MPI_COMM_WORLD, &num_procs);
stime
= MPI_Wtick();
stime=Time_In_MicroSec();
if (my_rank == 0)
{
fp
= fopen("data.dat","r");
if (fp == NULL)
{
printf("Cannot open
file");
exit(0);
}
while (!feof(fp))
{
fscanf(fp,"%d",&data[index]);
index++;
}
}
//printf("index %d",index);
MPI_Bcast(&index,1, MPI_INT,0, MPI_COMM_WORLD);
chunk = index / num_procs;
if ((index % num_procs)==0)
{
chunk = index
/ num_procs;
}
else {
chunk =
(index / num_procs) + 1;
}
MPI_Scatter((void *)&data,chunk,MPI_INT,
(void *)&rdata,chunk,MPI_INT,0,MPI_COMM_WORLD);
for(i=0;i<chunk;i++)
{
sum+=rdata[i];
//printf("rank=%d\trdata=%d\n",my_rank,rdata[i]);
}
//printf("Rank=%d\tSUM=%d\n",my_rank,sum);
MPI_Reduce(&sum,&max,1,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD);
//if (my_rank==0)
// printf("MAXIMUM=%d\n",max);
/*printf(" process %d
chunk %d low %d high %d \n", my_rank,
chunk, low, high);
for ( i = low; i< high ; i++)
my_result +=
data[i];
//printf(" The result from process %d is %d \n", my_rank, my_result);*/
etime=Time_In_MicroSec();
long elapsedtime=etime-stime;
long sumtime;
MPI_Reduce(&elapsedtime,&sumtime,1,MPI_LONG,MPI_SUM,0,MPI_COMM_WORLD);
if (my_rank==0)
{
printf("SumTime=%d\n",sumtime);
}
MPI_Finalize( );
return 0;
}
/*low = my_rank *
chunk;
high = low +
chunk;
MPI_Bcast(&index,1, MPI_INT, 0, MPI_COMM_WORLD);
//MPI_Bcast(data,index, MPI_INT, 0,
MPI_COMM_WORLD);*/
4a) Matrix multiplication by Sayed Ahmed 6794063
#include
<stdio.h>
#include
<math.h>
#include
"mpi.h"
#include
<malloc.h>
#define
MAXSIZE 100
main(int argc,
char **argv)
{
int A[MAXSIZE];
int B[MAXSIZE];
int C[MAXSIZE][MAXSIZE];
int size;
int rank;
int m;
int sum=0;
int rowstart,colstart;
int x;
int
row,col,k=0;
int
i,j;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
m=atoi(argv[1]);
if (rank==0)
{
printf("\n\n\n\nA array row
wise\t*****\t B Array Column wise");
for (i=0;i<m*m;i++)
{
A[i]=random()%(m*5)+1;
B[i]=random()%(m*5)+1;
printf("\n%d\t\t*****\t\t%d\n",A[i],B[i]);
}
}
//printf("m=%d\n",m);
MPI_Bcast((void *) &m,1,MPI_INT,0,MPI_COMM_WORLD);if (m==0) return
0;
MPI_Bcast((void *) &A,m*m,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast((void*)&B,m*m,MPI_INT,0,MPI_COMM_WORLD);
/*printf("rank=%d\t%d\t%d\t%d\t%d\n",rank,A[0],A[1],A[2],A[3]);
printf("rank=%d\t%d\t%d\t%d\t%d\n",rank,B[0],B[1],B[2],B[3]);*/
//MPI_Barrier(MPI_COMM_WORLD);
rowstart=rank/m;
rowstart=rowstart*m;
colstart=rank%m;
colstart=colstart*m;
x=colstart;
for(i=rowstart;i<rowstart+m;i++)
{
sum+=A[i]*B[x];
x++;
}
MPI_Barrier(MPI_COMM_WORLD);
printf("rank=%d\tsum=%d\n",rank,sum);
MPI_Finalize();
}
4b.matrixalternate.c
#include
<stdio.h>
#include
"mpi.h"
#define
MAXSIZE 50
main(int argc,
char *argv[])
{
int
A[MAXSIZE][MAXSIZE];
int
B[MAXSIZE][MAXSIZE];
int
C[MAXSIZE][MAXSIZE];
int
rA[MAXSIZE], rB[MAXSIZE];
int
size,rank,sum=0;
int
i,j,m;
int
row,col;
MPI_Status
status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
m=atoi(argv[1]);
if (rank==0)
{
printf("A array row wise\t\t\tB
array colwise shown\n");
printf("%d*%d Array\n",m,m);
for (i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
A[i][j]=random()%(m*5)+1;
B[i][j]=random()%(m*10)+1;
printf("%d\t\t\t\t\t%d\n",A[i][j],B[i][j]);
}
}
for (i=0;i<m*m;i++)
{
int r=i/m;
int c=i%m;
MPI_Send(A[r],m,MPI_INT,i,0,MPI_COMM_WORLD);
MPI_Send(B[c],m,MPI_INT,i,1,MPI_COMM_WORLD);
}
}
MPI_Recv(rA,m,MPI_INT,0,0,MPI_COMM_WORLD,&status);
MPI_Recv(rB,m,MPI_INT,0,1,MPI_COMM_WORLD,&status);
for(i=0;i<m;i++)
{
sum+=rA[i]*rB[i];
}
MPI_Barrier(MPI_COMM_WORLD);
printf("rank=%d\t row=%d col=%d
component=%d\n",rank,rank/m,rank%m,sum);
MPI_Finalize();
}