Commit fd2df844 authored by Gaurav Kukreja's avatar Gaurav Kukreja

Assignment 3 - MPI

 * ping-pong for latency code done by Umbreen - buggy
 * reduction done by Viktor
 * result.c done by Gaurav
Signed-off-by: 's avatarGaurav Kukreja <mailme.gaurav@gmail.com>
parent 7aeae8b9
CC = mpicc
CFLAGS = -O3
all: ping-pong reduction
ping-pong: ping-pong.o result.o
$(CC) $(CFLAGS) -o $@ $+
reduction: reduction.o
$(CC) $(CFLAGS) -o $@ $+
clean:
rm -rf *.o reduction ping-pong
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include "result.h"
#define FNAME_CMDARG 1
extern char *filename;
struct data_latency data_l;
int main (int argc, char **argv)
{
int my_proc,other_proc,i,j, nprocs;
int tag=10;
char a, b;
double tstart, tend;
MPI_Status status;
filename = argv[FNAME_CMDARG];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &my_proc);
for(i=1; i<nprocs; i++)
{
other_proc=i;
MPI_Barrier(MPI_COMM_WORLD);
if (my_proc == 0) {
tstart = MPI_Wtime();
for(j=0; j<5; j++)
{
MPI_Send(&a, 0, MPI_DOUBLE, other_proc, 10, MPI_COMM_WORLD);
MPI_Recv(&b, 0, MPI_DOUBLE, other_proc, 10, MPI_COMM_WORLD, &status);
}
tend = MPI_Wtime();
} else {
for(j=0; j<5; j++)
{
MPI_Recv(&b, 0, MPI_DOUBLE, 0, 10, MPI_COMM_WORLD, &status);
MPI_Send(&b, 0, MPI_DOUBLE, 0, 10, MPI_COMM_WORLD);
}
}
MPI_Barrier(MPI_COMM_WORLD);
data_l.source_thread = 0;
data_l.dest_thread = other_proc;
data_l.latency = (tend - tstart)/10;
submit_latency_data(data_l);
printf("Latency for process %d = %lf\n",other_proc, tend-tstart);
}
MPI_Finalize();
}
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
main (int argc,char *argv[])
{
int myid, np, ierr;
int a, tmp, i,tag;
MPI_Status status;
ierr = MPI_Init (&argc, &argv);
if (ierr != MPI_SUCCESS)
{
printf("Cannot initialize MPI!\n");
MPI_Finalize();
exit(0);
}
MPI_Comm_size(MPI_COMM_WORLD, &np);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
// ToDo: Check if np is a power of 2
a = myid;
i = 1;
tag = 1; // tag can be an arbitrary value
while (i < np)
{
if (myid%(2*i) == 0)
{
MPI_Recv(&tmp, 1, MPI_INT, myid+i, tag, MPI_COMM_WORLD, &status);
a = a + tmp;
}
if (myid%(2*i) == i)
{
MPI_Send(&a, 1, MPI_INT, myid-i, tag, MPI_COMM_WORLD);
}
i = i*2;
MPI_Barrier( MPI_COMM_WORLD); // may not be required
}
if (myid == 0)
{
printf("Number of Threads = %d\n", np);
printf("Result = %d\n", a);
}
MPI_Finalize();
}
#include <stdio.h>
#include "result.h"
char *filename;
int submit_bandwidth_data(struct data_bandwidth data_b)
{
FILE* fp;
fp = fopen(filename, "a");
fprintf(fp, "%d/t%d/t%d/t%lf/t%lf/n", data_b.source_thread,
data_b.dest_thread, data_b.log_size,
data_b.time, data_b.bandwidth);
fclose(fp);
return 1;
}
int submit_latency_data(struct data_latency data_l)
{
FILE* fp;
fp=fopen(filename, "a");
fprintf(fp, "%d/t%d/t%lf/n", data_l.source_thread,
data_l.dest_thread, data_l.latency);
fclose(fp);
return 1;
}
#ifndef RESULT_H
#define RESULT_H
struct data_latency {
int source_thread;
int dest_thread;
double latency;
};
struct data_bandwidth {
int source_thread;
int dest_thread;
int log_size;
double time;
double bandwidth;
};
int submit_latency_data(struct data_latency);
int submit_bandwidth_data(struct data_bandwidth);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment