Исследование повышения производительности последовательной программы после преобразования ее алгоритма в параллельный

Автор: Пользователь скрыл имя, 19 Февраля 2013 в 23:53, курсовая работа

Описание работы

В настоящий момент подавляющее большинство современных компьютеров используют принцип параллельности вычислений. Параллельные вычисления представляют собой целый куст смежных вопросов, включающих аппаратную поддержку, анализ структуры алгоритмов с целью выявления параллелизма и алгоритмические языки для программирования параллельных задач. Технологии параллельных вычислений в настоящее время бурно развиваются в связи с требованиями мировой науки и технологий.

Содержание

Введение 5
1 Описание предметной области 6
1.1 Конвейерная обработка 6
1.2 Параллельная обработка 10
1.3 Постановка задачи 12
2 Эскизный проект 14
2.1 Диаграмма вариантов использования 14
2.2 Диаграммы взаимодействий 16
2.2.1 Диаграмма последовательности 16
2.2.2 Диаграмма кооперации 17
2.3 Осуществление передачи данных при виртуальном соединении 18
3 Технический проект 20
4 Полученные результаты 25
Выводы 28
Список использованной литературы 29
Приложение А - Текст программы 30

Работа содержит 1 файл

курсовая_Марченко.doc

— 654.50 Кб (Скачать)

 

t1 = clock();

elapsed_time = static_cast<double> (t1 - t0)/ CLOCKS_PER_SEC;

cout<<"\nClient side, dt1 = \'"<<elapsed_time<<"\' seconds";

 

if(b>=0)

b-=(b-a)/2;

else

b+=(b-a)/2;

 

printf("\nWe have received interval. A is %f , B is %f\nCalculating...",a,b);

 

result=CalculateData(a, b); 

 

printf("\nSending data to server...");

 

t2 = clock();

char msg1[MAX_MSG_SIZE]="";

sprintf(msg1,"%f",result);

if((nbyte = send(sd, (char *) msg1,  strlen(msg1), MSG_DONTROUTE )) == -1)

{

printf("\nCould not send message to server");

scanf("");

return 0;

}

t3 = clock();

elapsed_time = static_cast<double> (t3 - t2)/ CLOCKS_PER_SEC;

cout<<"\nClient side, dt2 = \'"<< elapsed_time<<"\' seconds";

printf("\nSent");

 

 

}

closesocket(sd);

WSACleanup();

 

printf("\nResult is %f",result);

 

finish = clock();

elapsed_time = static_cast<double> (finish - t1)/ CLOCKS_PER_SEC;

cout<<"\nClient side finished in \'"<< elapsed_time<<"\' seconds";

 

return 0;

}

 

/**

* Функция  вычислений для сервера

* @param

*

* @return

*

*/

int ServerFunc ()

{

WORD winsock_version = MAKEWORD(2,0);

WSADATA wsaData;

SOCKET sd,client;

sockaddr_in addrHost,addrClient;

char message[MAX_MSG_SIZE];

int num=0,nbyte=0,temp=0,numOfClient=0;

double a,b,result,resultFromClient;

char msgA[MAX_MSG_SIZE], msgB[MAX_MSG_SIZE];

 

a=0;b=0;result=0;resultFromClient=0;

 

clock_t   start, finish, t0, t1, t2, t3;

double elapsed_time;

 

start = clock();

 

    if(WSAStartup(winsock_version, &wsaData)!=0) {

printf("\nCould not initialize winsock");

        return 0;

    }

   

    sd = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);

 

addrHost.sin_addr.s_addr = htonl(INADDR_ANY);

addrHost.sin_family = AF_INET;

addrHost.sin_port = htons(SERVER_PORT_NUM);

 

if(bind(sd, (sockaddr *)&addrHost,sizeof(addrHost))  == -1 )

    {

        printf("\nFailed to bind socket");

        return 0;

    }

 

listen(sd, SERVER_MAX_CONNECTIONS);

int iAddrSize = sizeof(addrClient);

printf("\nWait accept...");

client = accept(sd, (sockaddr *)&addrClient, &iAddrSize);

if (client == INVALID_SOCKET)

{

printf("\nAccept failed");

}

else

{

printf("\nAccepted Client: %s:%d", inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port));

printf("\nEnter interval (A,B)...\n");

cin>>msgA>>msgB;

 

t0 = clock();

elapsed_time = static_cast<double> (t0 - start)/ CLOCKS_PER_SEC;

cout<<"\nInitialized in \'"<<elapsed_time<<"\' seconds";

printf("\nSending data to client...");

 

int mlen=strlen(msgA);

if((nbyte = send(client, (char *)msgA, mlen, MSG_DONTROUTE )) == SOCKET_ERROR)

{

int er=WSAGetLastError();

printf("\nCould not send message A to server\nerror=%d\nmsg=%s",er,msgA);

scanf("");

return 0;

}

 

mlen=strlen(msgB);

if((nbyte = send(client, (char *)msgB,  mlen, MSG_DONTROUTE )) == SOCKET_ERROR)

{

printf("\nCould not send message B to server");

scanf("");

return 0;

}

 

t1 = clock();

elapsed_time = static_cast<double> (t1 - t0)/ CLOCKS_PER_SEC;

cout<<"\nServer side, dt1 = \'"<<elapsed_time<<"\' seconds";

 

a=atof(msgA);

b=atof(msgB);

 

if(b>=0)

a+=(b-a)/2;

else

a-=(b-a)/2;

 

result=CalculateData(a, b); 

 

t2 = clock();

 

printf("\nResult of integration on [%f;%f] is %f",a,b,result);

 

printf("\nReceiving data from client...");

 

int nbyte=recv(client, (char *)message, MAX_MSG_SIZE, MSG_PEEK);

if (nbyte != SOCKET_ERROR) {

resultFromClient=atof(message);

}

else if (nbyte==0 || nbyte<0)

{

printf("\n RECV: Error");

scanf("");

return 0;

}  

 

t3 = clock();

elapsed_time = static_cast<double> (t3 - t2)/ CLOCKS_PER_SEC;

cout<<"\nServer side, dt2 = \'"<<elapsed_time<<"\' seconds";

 

printf("\nResult from client is %f\nAnd result of integral on interval [%f,%f] is %f ",resultFromClient,a,b,result+resultFromClient);

}

closesocket(sd);

closesocket(client);

WSACleanup();

 

finish = clock();

elapsed_time = static_cast<double> (finish - t0)/ CLOCKS_PER_SEC;

cout<<"\nServer side finished in  = \'"<<elapsed_time<<"\' seconds";

 

return 0;

}

 

int ClientOnlyCalculate ()

{

WORD winsock_version = MAKEWORD(2,0);

WSADATA wsaData;

SOCKET sd;

sockaddr_in serverAddr;

double a,b;

double result=0;

char messageA[MAX_MSG_SIZE]="";

char messageB[MAX_MSG_SIZE]="";

    int num=0,nbyte;

a=0;b=0;

clock_t   start, finish, t0, t1, t2, t3;

double elapsed_time;

 

start = clock();

 

    if(WSAStartup(winsock_version, &wsaData)!=0) {

printf("Could not initialize winsock\n");

        return 0;

    }

   

    sd = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);

 

    serverAddr.sin_family = AF_INET;

    serverAddr.sin_port = htons(SERVER_PORT_NUM);

   serverAddr.sin_addr.s_addr = inet_addr(dtmp);

 

if(connect(sd, (sockaddr *)&serverAddr, sizeof(serverAddr)) == -1)

{

   printf("\nCould not initialize winsock");

   return 0;

}

else {

t0 = clock();

elapsed_time = static_cast<double> (t0 - start)/ CLOCKS_PER_SEC;

cout<<"\nInitialized in \'"<<elapsed_time<<"\' seconds";

 

printf("\nHave connected to the server\nWaiting for messages:");

int recvA = recv(sd, (char *)messageA, MAX_MSG_SIZE, 0);

if (recvA != SOCKET_ERROR) {

printf("\nmsgA = %s",messageA);

a=atof(messageA);   

}

else if (recvA==0 || recvA<0)

{

printf("\nRECV: Error");

scanf("");

return 0;

}

 

int recvB=recv(sd, (char *)messageB, MAX_MSG_SIZE, 0);

if (recvB != SOCKET_ERROR) {

printf("\nmsgB = %s",messageB);

b=atof(messageB);

}

else if (recvB==0 || recvB<0)

{

printf("\nRECV: Error");

scanf("");

return 0;

}

 

t1 = clock();

elapsed_time = static_cast<double> (t1 - t0)/ CLOCKS_PER_SEC;

cout<<"\nClient side, dt1 = \'"<<elapsed_time<<"\' seconds";

 

printf("\nWe receive interval. A is %f , B is %f\nCalculate...",a,b);

 

result=CalculateData(a, b); 

 

printf("\nSending data to server...");

 

t2 = clock();

 

char msg1[MAX_MSG_SIZE]="";

sprintf(msg1,"%f",result);

if((nbyte = send(sd, (char *) msg1,  strlen(msg1), MSG_DONTROUTE )) == -1)

{

printf("\nCould not send message to server");

scanf("");

return 0;

}

 

t3 = clock();

elapsed_time = static_cast<double> (t3 - t2)/ CLOCKS_PER_SEC;

cout<<"\nClient side, dt2 = \'"<< elapsed_time<<"\' seconds";

printf("\nSent");

}

closesocket(sd);

WSACleanup();

 

printf("\nResult is %f",result);

 

finish = clock();

elapsed_time = static_cast<double> (finish - t1)/ CLOCKS_PER_SEC;

cout<<"\nClient side finished in \'"<< elapsed_time<<"\' seconds";

 

return 0;

}

 

int ServerOnlyCalculate ()

{

WORD winsock_version = MAKEWORD(2,0);

WSADATA wsaData;

SOCKET sd,client;

sockaddr_in addrHost,addrClient;

char message[MAX_MSG_SIZE];

int num=0,nbyte=0,temp=0,numOfClient=0;

double a,b,result,resultFromClient;

char msgA[MAX_MSG_SIZE], msgB[MAX_MSG_SIZE];

 

a=0;b=0;result=0;resultFromClient=0;

 

clock_t   start, finish, t0, t1, t2, t3;

double elapsed_time;

 

start = clock();

 

    if(WSAStartup(winsock_version, &wsaData)!=0) {

printf("\nCould not initialize winsock");

        return 0;

    }

   

    sd = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);

 

addrHost.sin_addr.s_addr = htonl(INADDR_ANY);

addrHost.sin_family = AF_INET;

addrHost.sin_port = htons(SERVER_PORT_NUM);

 

if(bind(sd, (sockaddr *)&addrHost,sizeof(addrHost))  == -1 )

    {

        printf("\nFailed to bind socket");

        return 0;

    }

 

listen(sd, SERVER_MAX_CONNECTIONS);

int iAddrSize = sizeof(addrClient);

printf("\nWait accept...");

client = accept(sd, (sockaddr *)&addrClient, &iAddrSize);

if (client == INVALID_SOCKET)

{

printf("\nAccept failed");

}

else

{

printf("\nAccepted Client: %s:%d", inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port));

printf("\nEnter interval (A,B)...\n");

cin>>msgA>>msgB;

 

t0 = clock();

elapsed_time = static_cast<double> (t0 - start)/ CLOCKS_PER_SEC;

cout<<"\nInitialized in \'"<<elapsed_time<<"\' seconds";

printf("\nSending data to client...");

 

int mlen=strlen(msgA);

if((nbyte = send(client, (char *)msgA, mlen, MSG_DONTROUTE )) == SOCKET_ERROR)

{

int er=WSAGetLastError();

printf("\nCould not send message A to server\nerror=%d\nmsg=%s",er,msgA);

scanf("");

return 0;

}

 

mlen=strlen(msgB);

if((nbyte = send(client, (char *)msgB,  mlen, MSG_DONTROUTE )) == SOCKET_ERROR)

{

printf("\nCould not send message B to server");

scanf("");

return 0;

}

 

t1 = clock();

elapsed_time = static_cast<double> (t1 - t0)/ CLOCKS_PER_SEC;

cout<<"\nServer side, dt1 = \'"<<elapsed_time<<"\' seconds";

 

a=atof(msgA);

b=atof(msgB);

 

t2 = clock();

printf("\nReceiving data from client...");

 

int nbyte=recv(client, (char *)message, MAX_MSG_SIZE, MSG_PEEK);

if (nbyte != SOCKET_ERROR) {

resultFromClient=atof(message);

}

else if (nbyte==0 || nbyte<0)

{

printf("\n RECV: Error\n");

scanf("");

return 0;

}  

 

t3 = clock();

elapsed_time = static_cast<double> (t3 - t2)/ CLOCKS_PER_SEC;

cout<<"\nServer side, dt2 = \'"<<elapsed_time<<"\' seconds";

 

printf("\nResult of integral on interval [%f,%f] is %f",a,b,resultFromClient);

}

closesocket(sd);

closesocket(client);

WSACleanup();

 

finish = clock();

elapsed_time = static_cast<double> (finish - t0)/ CLOCKS_PER_SEC;

cout<<"\nServer side finished in  = \'"<<elapsed_time<<"\' seconds";

 

return 0;

}

 

void OneCampCalculate()

{

double a,b,result;

 

printf("\nEnter interval (A,B)...\n");

cin>>a>>b;

 

result=CalculateData(a, b); 

 

/**

* main-function

* @param

*

* @return

*

*/

int main(int argc, char * argv[])

{

int choice=1;

if(argc>2)

{

dtmp=argv[1];

printf("\nServer IP is %s",dtmp);

NUM_OF_REPEATS=atof(argv[2]);

printf("\nNum of repeats is %d",NUM_OF_REPEATS);

while(choice!=0)

{

printf("\n\n\n\n  Menu\n1.Server side\n2.Client side\n3.One camp\n4.Server side (client only)\n5.Client side (client only)\nEnter number(0 - for exit): ");

scanf("%d",&choice);

switch (choice)

{

case 1:

ServerFunc();

break;

case 2:

ClientFunc();

break;

case 3:

OneCampCalculate();

break;

case 4:

ServerOnlyCalculate();

break;

case 5:

ClientOnlyCalculate();

break;

default:

printf("\nEnter number from 0 to 5");

}

}

}

else

{

printf("\nNot enough parameters!");

scanf("enter");

}

return 0;

}

 


Информация о работе Исследование повышения производительности последовательной программы после преобразования ее алгоритма в параллельный