Created
January 14, 2014 23:12
-
-
Save varvaruc/8427826 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mpi.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
int main( int argc, char *argv[] ) | |
{ | |
int num_errors = 0; | |
int rank, size; | |
char port1[MPI_MAX_PORT_NAME]; | |
MPI_Status status; | |
MPI_Comm comm1; | |
int data = 0; | |
double prag=0.00002;//1 minisecond | |
double ServerCurrentTime,ClientCurrentTime,ClientTimeRequest,ClientTimeResponse; | |
MPI_Init(&argc, &argv); | |
MPI_Comm_size(MPI_COMM_WORLD, &size); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
char* message="Time"; | |
char temp[100]; | |
if (rank == 0) | |
{ | |
printf("Starting server...\n"); | |
ServerCurrentTime=MPI_Wtime(); | |
printf("ServerCurrnentTime is:%lf\n",ServerCurrentTime); | |
printf("Opening ports...\n"); | |
fflush(stdout); | |
MPI_Open_port(MPI_INFO_NULL, port1); | |
printf("Opened port1: <%s>\n", port1); | |
fflush(stdout); | |
MPI_Send(port1, MPI_MAX_PORT_NAME, MPI_CHAR, 1, 0, MPI_COMM_WORLD); | |
printf("Accepting port1.\n");fflush(stdout); | |
MPI_Comm_accept(port1, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm1); | |
MPI_Close_port(port1); | |
int k=10; | |
while(k) | |
{ | |
memset(temp,0,100); | |
MPI_Recv(temp,100,MPI_CHAR,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status); | |
if(strcmp(temp,message)==0) | |
{ | |
MPI_Send(&ServerCurrentTime, 1, MPI_DOUBLE, 0, 0, comm1); | |
} | |
k--; | |
sleep(2); | |
} | |
MPI_Comm_disconnect(&comm1); | |
} | |
else if (rank ==1) | |
{ | |
printf("Starting Client...\n"); | |
ClientCurrentTime=MPI_Wtime(); | |
printf("ClientCurrentTIme is:%lf\n",ClientCurrentTime); | |
MPI_Recv(port1, MPI_MAX_PORT_NAME, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status); | |
MPI_Comm_connect(port1, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm1); | |
double ServerTimeReceived=0; | |
double TimeSync=0; | |
int k=10; | |
while(k) | |
{ | |
memset(temp,0,100); | |
ClientTimeRequest=MPI_Wtime(); | |
MPI_Send(message,strlen(message)+1,MPI_CHAR,0,99,MPI_COMM_WORLD); | |
printf("Sunt client si am trimis o cerere la timpul:%lf\n",ClientTimeRequest); | |
MPI_Recv(&ServerTimeReceived, 1, MPI_DOUBLE, 0, 0, comm1, &status); | |
printf("Sunt client si am primit un raspuns de la server:%lf \n",ServerTimeReceived); | |
ClientTimeResponse=MPI_Wtime(); | |
double TimeTraveled=(ClientTimeResponse-ClientTimeRequest)/2; | |
printf("TimeTraveled is :%lf \n",TimeTraveled); | |
TimeSync=ServerTimeReceived+TimeTraveled; | |
printf("TimeSync is :%lf\n",TimeSync); | |
double checkSync=TimeSync-ClientCurrentTime; | |
if( checkSync<0) | |
checkSync=- checkSync; | |
printf("checkSync:%lf\n",checkSync); | |
if(checkSync>prag) | |
{ | |
printf("URARARARAARAR\n"); | |
printf("Noul timp al clientului este:%lf !!!\n\n",TimeSync); | |
ClientCurrentTime=TimeSync; | |
} | |
else | |
{ | |
printf("Nu necesita sync!!!\n\n"); | |
} | |
k--; | |
sleep(2); | |
} | |
fflush(stdout); | |
MPI_Comm_disconnect(&comm1); | |
} | |
MPI_Barrier(MPI_COMM_WORLD); | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment