-
Notifications
You must be signed in to change notification settings - Fork 4
/
WAVE.cpp
2013 lines (1866 loc) · 58.8 KB
/
WAVE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Author: cos
* Date: 2015.8
* 参照MPI实现的分布式计算框架(实现原理:通信双方双向认证。使用了socket编程,引入了epoll来实现IO的多路复用,
* 打破传统socket编程的保存的客户端数目上限FD_SETSIZE宏(2048)的限制。
* 常用的打破FD_SETSIZE宏限制的方法有:修改宏的大小,会带来网络效率的下降;Apache服务器的PPC多进程连接的方案,但是进程间需要同步操作;
* 多线程方案,线程同步虽然比进程同步开销小,但是浪费效率;selec/poll方案依然还有限制最大连接数,调用次数多会导致性能线性下降;
* epoll则打破这个限制,用空间换时间,IO多路复用,相对比来说效率较高)
* 实现了几个简单的通信函数(send/recv,barrier,broadcast,reduction)
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <string.h>
#include <ctype.h>
#include "WAVE.h"
//打开输出调试信息
//#define DEBUG
//打开输出节点IP和端口信息
//#define INFO
//检查系统位数,__LP64__是linux预定义的64位宏
#ifdef __LP64__
#define WAVE_WORD_64
#else
#define WAVE_WORD_32
#endif
#define WAVE_BAG_SIZE 16384 //IP头所能表示的窗口大小2^16/4(实验结果)
//保存每一个节点绑定的IP和端口信息
struct WAVE_node_info{
char *ip;
unsigned int port;
};
//自定义的epoll_data
struct WAVE_epoll_data{
int fd; //客户端套接字标示符
int state; //客户端状态标示符
};
//进程全局编号
int WAVE_RANK=-1;
//所有进程数目
int WAVE_SIZE=-1;
//所有进程的IP和端口信息
struct WAVE_node_info *WAVE_NODES = NULL;
//认证消息长度
int WAVE_ACMESSAGE_LENGTH = 60;
//等待时钟
int WAVE_CLOCK = 1;
//服务器套接口
int WAVE_SERVER_SOCKET;
//服务器监听队列长度
int WAVE_SERVER_SOCKET_LISTEN_QUEUE_LENGTH = 1000;
//服务器epoll events数量
int WAVE_EPOLL_EVENTS_NUM = 1000;
//服务器网络地址
struct sockaddr_in WAVE_SERVER_IN;
//返回数组中某一值所对应的下标
int WAVE_IndexOfValue(int *array, int length, int value){
int i;
for(i=0; i<length; i++){
if(array[i] == value)
return i;
}
return -1;
}
//将字符串恢复成其他种类的信息
int WAVE_CharToOther(char *message, size_t length, int type, void *newmessage){
size_t i;
char *message1;
char *message2;
switch (type)
{
//有符号整形
case WAVE_INT:
{
int *temp = (int *)newmessage;
message1 = message;
for(i=0; i<length; i++){
temp[i] = (int)strtol(message1, &message2, 10);
message1 = (char *)(message2 + 1);
}
break;
}
//无符号整形
case WAVE_UINT:
{
unsigned int *temp = (unsigned int *)newmessage;
message1 = message;
for(i=0; i<length; i++){
temp[i] = (unsigned int)strtoul(message1, &message2, 10);
message1 = (char *)(message2 + 1);
}
break;
}
//有符号长整形
case WAVE_LONG:
{
long *temp = (long *)newmessage;
message1 = message;
for(i=0; i<length; i++){
temp[i] = strtol(message1, &message2, 10);
message1 = (char *)(message2 + 1);
}
break;
}
//无符号长整形
case WAVE_ULONG:
{
unsigned long *temp = (unsigned long *)newmessage;
message1 = message;
for(i=0; i<length; i++){
temp[i] = strtoul(message1, &message2, 10);
message1 = (char *)(message2 + 1);
}
break;
}
//单精度浮点数
case WAVE_FLOAT:
{
float *temp = (float *)newmessage;
message1 = message;
for(i=0; i<length; i++){
temp[i] = (float)strtod(message1, &message2);
message1 = (char *)(message2 + 1);
}
break;
}
//双精度浮点数
case WAVE_DOUBLE:
{
double *temp = (double *)newmessage;
message1 = message;
for(i=0; i<length; i++){
temp[i] = strtod(message1, &message2);
message1 = (char *)(message2 + 1);
}
break;
}
default :
{
return 0;
}
}
return 1;
}
//设置端口连接方式是非阻塞
int WAVE_SetNonBlocking(int sockid)
{
int opts;
opts = fcntl(sockid,F_GETFL);
if(opts < 0)
{
perror("fcntl(sockid, GETFL)");
return 0;
}
opts = opts|O_NONBLOCK;
if(fcntl(sockid, F_SETFL, opts)<0)
{
perror("fcntl(sockid,SETFL, opts)");
return 0;
}
return 1;
}
//关闭连接的客户端端口和epoll事件
void WAVE_CloseAndDisable(int sockid, struct epoll_event event)
{
struct WAVE_epoll_data *temp = (struct WAVE_epoll_data *)(event.data.ptr);
close(sockid);
close((*temp).fd);
(*temp).fd = -1;
(*temp).state = WAVE_CLIENT_END;
}
//检查数据类型是否合法
int WAVE_IsTypeLegal(int type){
if(type==WAVE_CHAR||type==WAVE_INT||type==WAVE_UINT||type==WAVE_LONG||type==WAVE_ULONG||type==WAVE_FLOAT||type==WAVE_DOUBLE)
return 1;
else
return 0;
}
//检查运算符是否合法
int WAVE_IsOpLegal(int op){
if(op==WAVE_ADD||op==WAVE_MUL||op==WAVE_AND||op==WAVE_OR)
return 1;
else
return 0;
}
//自定义的归约运算符
int WAVE_Operation(void *base, void *message, size_t length, int type, int op){
size_t i;
switch (type)
{
case WAVE_CHAR :
{
char *temp1 = (char *)base;
char *temp2 = (char *)message;
switch (op)
{
case WAVE_ADD:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] + temp2[i];
}
break;
}
case WAVE_MUL:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] * temp2[i];
}
break;
}
case WAVE_AND:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] & temp2[i];
}
break;
}
case WAVE_OR:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] | temp2[i];
}
break;
}
}
break;
}
//有符号整形
case WAVE_INT:
{
int *temp1 = (int *)base;
int *temp2 = (int *)message;
switch (op)
{
case WAVE_ADD:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] + temp2[i];
}
break;
}
case WAVE_MUL:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] * temp2[i];
}
break;
}
case WAVE_AND:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] & temp2[i];
}
break;
}
case WAVE_OR:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] | temp2[i];
}
break;
}
}
break;
}
//无符号整形
case WAVE_UINT:
{
unsigned int *temp1 = (unsigned int *)base;
unsigned int *temp2 = (unsigned int *)message;
switch (op)
{
case WAVE_ADD:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] + temp2[i];
}
break;
}
case WAVE_MUL:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] * temp2[i];
}
break;
}
case WAVE_AND:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] & temp2[i];
}
break;
}
case WAVE_OR:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] | temp2[i];
}
break;
}
}
break;
}
//有符号长整形
case WAVE_LONG:
{
long *temp1 = (long *)base;
long *temp2 = (long *)message;
switch (op)
{
case WAVE_ADD:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] + temp2[i];
}
break;
}
case WAVE_MUL:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] * temp2[i];
}
break;
}
case WAVE_AND:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] & temp2[i];
}
break;
}
case WAVE_OR:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] | temp2[i];
}
break;
}
}
break;
}
//无符号长整形
case WAVE_ULONG:
{
unsigned long *temp1 = (unsigned long *)base;
unsigned long *temp2 = (unsigned long *)message;
switch (op)
{
case WAVE_ADD:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] + temp2[i];
}
break;
}
case WAVE_MUL:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] * temp2[i];
}
break;
}
case WAVE_AND:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] & temp2[i];
}
break;
}
case WAVE_OR:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] | temp2[i];
}
break;
}
}
break;
}
//单精度浮点数
case WAVE_FLOAT:
{
float *temp1 = (float *)base;
float *temp2 = (float *)message;
switch (op)
{
case WAVE_ADD:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] + temp2[i];
}
break;
}
case WAVE_MUL:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] * temp2[i];
}
break;
}
default :
{
return 0;
}
}
break;
}
//双精度浮点数
case WAVE_DOUBLE:
{
double *temp1 = (double *)base;
double *temp2 = (double *)message;
switch (op)
{
case WAVE_ADD:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] + temp2[i];
}
break;
}
case WAVE_MUL:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] * temp2[i];
}
break;
}
default :
{
return 0;
}
}
break;
}
default :
{
char *temp1 = (char *)base;
char *temp2 = (char *)message;
switch (op)
{
case WAVE_ADD:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] + temp2[i];
}
break;
}
case WAVE_MUL:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] * temp2[i];
}
break;
}
case WAVE_AND:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] & temp2[i];
}
break;
}
case WAVE_OR:
{
for(i=0; i<length; i++){
temp1[i] = temp1[i] | temp2[i];
}
break;
}
}
return 0;
}
}
return 1;
}
//返回数据类型一个所占内存大小
int WAVE_SizeOf(int type){
switch(type)
{
case WAVE_CHAR:
{
return sizeof(char);
}
case WAVE_INT:
{
return sizeof(int);
}
case WAVE_UINT:
{
return sizeof(unsigned int);
}
case WAVE_LONG:
{
return sizeof(long);
}
case WAVE_ULONG:
{
return sizeof(unsigned long);
}
case WAVE_FLOAT:
{
return sizeof(float);
}
case WAVE_DOUBLE:
{
return sizeof(double);
}
}
return sizeof(size_t);
}
//将一条消息分成多个WAVE_BAG_SIZE发送
size_t WAVE_SendMessage(int socket, void *buf, size_t length){
size_t i = 0, size;
while(i < length){
size = length - i;
if(size > WAVE_BAG_SIZE)
size = WAVE_BAG_SIZE;
if(send(socket, (void *)(buf + i), size, 0) == -1){
return 0;
}
i += WAVE_BAG_SIZE;
}
return length;
}
//将一条消息分成多个WAVE_BAG_SIZE接收
size_t WAVE_RecvMessage(int socket, void *buf, size_t length){
size_t i = 0, size;
while(i < length){
size = length - i;
if(size > WAVE_BAG_SIZE)
size = WAVE_BAG_SIZE;
if(recv(socket, (void *)(buf + i), size, MSG_WAITALL) == -1){
//if(recv(socket, (void *)(buf + i), size, 0) == -1){
return 0;
}
i += WAVE_BAG_SIZE;
}
return length;
}
//同步函数
int WAVE_Barrier(){
//使用0号进程充当server,其余进程向它发消息协议如何同步
int root = 0;
//假设发送的消息是int类型
int type = WAVE_INT;
//假设发送的消息长度是0
size_t length = 0;
//发送进程
if(WAVE_RANK == root){
int barrierSize = WAVE_SIZE - 1; //除root进程之外同步进程数目
int client_socket = -1; //客户端套接口
socklen_t client_size = sizeof(struct sockaddr); //客户端网络地址长度
struct sockaddr_in client_in; //客户端网络地址
char *buf1 = (char *)malloc(sizeof(char)*WAVE_ACMESSAGE_LENGTH); //收发认证信息缓冲区长度
char *buf2;
char *buf3;
size_t i, j, k, l, m;
int nfds;
size_t client_num=0; //已连接的客户端数
size_t client_end=0; //已结束广播的客户端数
size_t client_accept = 0; //建立连接的客户端数目
int error = 0;
//监听网络请求
if(listen(WAVE_SERVER_SOCKET, WAVE_SERVER_SOCKET_LISTEN_QUEUE_LENGTH) == -1){
printf("root %d Barrier call to listen fail\n", WAVE_RANK);
exit(0);
}
//声明epoll_event结构体的变量,ev用于注册事件,数组用于回传要处理的事件
struct epoll_event ev;
struct epoll_event *events =(struct epoll_event *)malloc(sizeof(struct epoll_event)*WAVE_EPOLL_EVENTS_NUM);
//生成用于处理accept的epoll专用的文件描述符
int epfd = epoll_create(WAVE_SIZE);
//设置与要处理的事件相关的文件描述符
ev.data.fd = WAVE_SERVER_SOCKET;
//设置要处理的事件类型
ev.events = EPOLLIN;
//注册epoll事件
epoll_ctl(epfd, EPOLL_CTL_ADD, WAVE_SERVER_SOCKET, &ev);
#ifdef DEBUG
printf("node %d Barrier accepting other node connections ...\n", WAVE_RANK);
#endif
while(client_end < barrierSize){
//等待epoll事件的发生
nfds = epoll_wait(epfd, events, WAVE_EPOLL_EVENTS_NUM, -1);
//处理所发生的所有事件
for(i=0; i<nfds; ++i)
{
//如果新监测到一个SOCKET用户连接到了绑定的SOCKET端口,建立新的连接。
if(client_accept < barrierSize && events[i].data.fd == WAVE_SERVER_SOCKET)
{
//接收客户请求
if((client_socket = accept(WAVE_SERVER_SOCKET, (struct sockaddr*)&client_in, &client_size))==-1){
printf("root %d Barrier call to accept fail \n", WAVE_RANK);
exit(0);
}
#ifdef DEBUG
char *str = inet_ntoa(client_in.sin_addr);
printf("root %d Barrier call to accept a connection from %s\n", WAVE_RANK, str);
#endif
//非阻塞方式
WAVE_SetNonBlocking(client_socket);
//设置用于读操作的文件描述符
ev.data.fd = client_socket;
//设置用于注册的读操作事件
ev.events=EPOLLIN;
//注册ev
epoll_ctl(epfd, EPOLL_CTL_ADD, client_socket, &ev);
client_accept++;
}
//如果是已经连接的用户,读入认证消息
else if(events[i].events & EPOLLIN)
{
client_socket = events[i].data.fd;
//获取客户发送的数据
if(recv(client_socket, buf1, WAVE_ACMESSAGE_LENGTH, 0) == -1){
printf("root %d Barrier call to recv ac message fail \n", WAVE_RANK);
exit(0);
}
j = strtol(buf1, &buf2, 10); //连接进程的rank
k = strtol((char *)(buf2+1), &buf3, 10); //连接进程想要的消息tag
l = strtol((char *)(buf3+1), &buf2, 10); //连接进程想要的消息类型
m = strtol((char *)(buf2+1), NULL, 10); //连接进程想要的消息长度
#ifdef DEBUG
printf("root %d Barrier received ac message from client %d: %s\n", WAVE_RANK, j, buf1);
#endif
//信息类型和长度不匹配说明程序有错
if(k != WAVE_TAG_BARRIER || l != type || m != length){
error = 1;
}
//设置用于注测的写操作事件
ev.data.fd = client_socket;
//修改client_socket上要处理的事件为EPOLLOUT
ev.events = EPOLLOUT;
epoll_ctl(epfd, EPOLL_CTL_MOD, client_socket, &ev);
client_num++;
}
else if(client_num == barrierSize && (events[i].events & EPOLLOUT))// 如果有数据发送
{
//发送对认证消息的确认
client_socket = events[i].data.fd;
//程序员的程序有错
if(error){
memcpy(buf1, "Fail_barrier", 13);
//将信息类型和长度不匹配警告广播给所有进程
if(send(client_socket, buf1, WAVE_ACMESSAGE_LENGTH, 0) == -1){
printf("root %d Barrier call to send ac fail message fail\n", WAVE_RANK);
exit(0);
}
#ifdef DEBUG
printf("root %d Barrier call to send ac fail message: %s\n", WAVE_RANK, buf1);
#endif
}
else{
memcpy(buf1, "Success_barrier", 16);
//将认证成功广播给所有进程
if(send(client_socket, buf1, WAVE_ACMESSAGE_LENGTH, 0) == -1){
printf("root %d Barrier call to send ac success message fail\n", WAVE_RANK);
exit(0);
}
#ifdef DEBUG
printf("root %d Barrier call to send ac success message: %s\n", WAVE_RANK, buf1);
#endif
}
close(client_socket);
//close(events[i].data.fd);
client_end++;
}//end if out
}//end for
}//end while(1)
//释放申请的认证信息缓冲区
#ifdef DEBUG
printf("node %d reach Barrier\n", WAVE_RANK);
#endif
close(epfd);
free(buf1);
free(events);
if(error){
printf("root %d Barrier fail because error in your program(not all node can reach this Barrier).\n", WAVE_RANK);
exit(0);
}
else
return 1;
}
else{
int server_socket; //服务器套接口
struct sockaddr_in server_in; //服务器网络地址
struct hostent *server_name;
char *buf1 = (char *)malloc(sizeof(char)*WAVE_ACMESSAGE_LENGTH); //收发认证信息缓冲区长度
//转换服务器域名或 IP 地址为 IP 结构体
if((server_name = gethostbyname(WAVE_NODES[root].ip)) == 0){
printf("node %d barrier. error resolving host %s\n", WAVE_RANK, WAVE_NODES[root].ip);
exit(0);
}
//初始化 IP 地址结构
bzero(&server_in,sizeof(server_in));
server_in.sin_family = AF_INET;
server_in.sin_addr.s_addr = htonl(INADDR_ANY);
server_in.sin_addr.s_addr = ((struct in_addr*)(server_name->h_addr))->s_addr;
server_in.sin_port = htons(WAVE_NODES[root].port);
while(1){
//获取远程服务器套接口描述符
if((server_socket = socket(AF_INET,SOCK_STREAM,0)) == -1){
printf("node %d barrier call to socket fail\n", WAVE_RANK);
exit(0);
}
#ifdef DEBUG
printf("node %d barrier start connect node %d(ip %s port %d)\n", WAVE_RANK, root, WAVE_NODES[root].ip, WAVE_NODES[root].port);
#endif
//发送连接请求到服务器
while(connect(server_socket, (struct sockaddr *)&server_in, sizeof(server_in)) == -1){
usleep(WAVE_CLOCK);
}
#ifdef DEBUG
printf("node %d barrier start connect node %d success\n", WAVE_RANK, root);
#endif
//创建认证信息
#ifdef WAVE_WORD_32
sprintf(buf1, "%d %d %d %d", WAVE_RANK, WAVE_TAG_BARRIER, type, length);
#else
sprintf(buf1, "%d %ld %d %ld", WAVE_RANK, WAVE_TAG_BARRIER, type, length);
#endif
//发送认证消息给同步方
if(send(server_socket, buf1, WAVE_ACMESSAGE_LENGTH, 0) == -1){
printf("node %d barrier(认证步骤1:发送自己的rank,tag,type,length失败)\n", WAVE_RANK);
exit(0);
}
//从同步方接收认证消息的确认
if(recv(server_socket, buf1, WAVE_ACMESSAGE_LENGTH, 0) == -1){
printf("node %d barrier(认证步骤2:接收提示消息失败)\n", WAVE_RANK);
exit(0);
}
//同步成功
if(strcmp(buf1, "Success_barrier") == 0){
#ifdef DEBUG
printf("node %d reach Barrier\n", WAVE_RANK);
#endif
//断开连接
close(server_socket);
break;
}
else{//同步认证失败,身份不匹配
//同步进程此时不是在同步,而在做一对一通信,此时还不想给自己发信息,需要不断再请求
if(strcmp(buf1, "Fail_conflict") == 0){
//断开连接
close(server_socket);
usleep(WAVE_CLOCK);
continue;
}
//同步不匹配,说明程序有错
if(strcmp(buf1, "Fail_barrier") == 0){
printf("root %d crash.error in your program(call barrier)\n", root);
//断开连接
close(server_socket);
//释放申请的认证信息缓冲区
free(buf1);
exit(0);
return 0;
}
}
}
free(buf1);
return 1;
}
}
//普通归约函数
int WAVE_Reduce(void *orimessage, void *recvbuf, size_t length, int type, int op, int root){
size_t reducMessageSize = WAVE_SizeOf(type)*length; //归约信息所占内存大小
//root进程
if(WAVE_RANK == root){
int reducSize = WAVE_SIZE - 1; //归约的目标进程数
int client_socket; //客户端套接口
struct WAVE_epoll_data *clients = (struct WAVE_epoll_data *)malloc(sizeof(struct WAVE_epoll_data)*reducSize); //连接的客户端状态
socklen_t client_size = sizeof(struct sockaddr); //客户端网络地址长度
struct sockaddr_in client_in; //客户端网络地址
char *buf1 = (char *)malloc(sizeof(char)*WAVE_ACMESSAGE_LENGTH); //收发认证信息缓冲区长度
char *buf2;
char *buf3;
void *temp = (void *)malloc(reducMessageSize);
size_t i, j, k, l, m;
int nfds;
size_t client_num=0; //已连接的客户端数
size_t client_end=0; //已结束广播的客户端数
size_t client_accept = 0; //已建立的连接数
int error = 0;
//初始化归约结果
memcpy(recvbuf, orimessage, reducMessageSize);
//监听网络请求
if(listen(WAVE_SERVER_SOCKET, WAVE_SERVER_SOCKET_LISTEN_QUEUE_LENGTH) == -1){
printf("root %d Reduc call to listen fail\n", WAVE_RANK);
exit(0);
}
//声明epoll_event结构体的变量,ev用于注册事件,数组用于回传要处理的事件
struct epoll_event ev;
struct epoll_event *events =(struct epoll_event *)malloc(sizeof(struct epoll_event)*WAVE_EPOLL_EVENTS_NUM);
//生成用于处理accept的epoll专用的文件描述符
int epfd = epoll_create(WAVE_SIZE);
//设置与要处理的事件相关的文件描述符
struct WAVE_epoll_data epoll_data_temp0;
epoll_data_temp0.fd = WAVE_SERVER_SOCKET;
epoll_data_temp0.state = WAVE_CLIENT_ORI;
ev.data.ptr = &epoll_data_temp0;
//设置要处理的事件类型
ev.events = EPOLLIN;
//注册epoll事件
epoll_ctl(epfd, EPOLL_CTL_ADD, WAVE_SERVER_SOCKET, &ev);
#ifdef DEBUG
printf("root %d Reduc accepting other node connections ...\n", WAVE_RANK);
#endif
while(client_end < reducSize){
//等待epoll事件的发生
nfds = epoll_wait(epfd, events, WAVE_EPOLL_EVENTS_NUM, -1);
//处理所发生的所有事件
for(i=0; i<nfds; ++i)
{
struct WAVE_epoll_data *epoll_data_temp1 = (struct WAVE_epoll_data *)(events[i].data.ptr);
//如果新监测到一个SOCKET用户连接到了绑定的SOCKET端口,建立新的连接。
if(client_accept<reducSize && (*epoll_data_temp1).fd == WAVE_SERVER_SOCKET)
{
//接收客户请求
if((client_socket = accept(WAVE_SERVER_SOCKET, (struct sockaddr*)&client_in, &client_size))==-1){
printf("root %d Reduc call to accept fail \n", WAVE_RANK);
exit(0);
}
#ifdef DEBUG
char *str = inet_ntoa(client_in.sin_addr);
printf("root %d Reduc accapt a connection from %s\n", WAVE_RANK, str);
#endif
//非阻塞方式
WAVE_SetNonBlocking(client_socket);
//设置用于读操作的文件描述符
struct WAVE_epoll_data *epoll_data_temp2 = (struct WAVE_epoll_data *)(clients + client_accept);
(*epoll_data_temp2).fd = client_socket;
(*epoll_data_temp2).state = WAVE_CLIENT_START;
ev.data.ptr = epoll_data_temp2;
//设置用于注册的读操作事件
ev.events=EPOLLIN;
//注册ev
epoll_ctl(epfd, EPOLL_CTL_ADD, client_socket, &ev);
client_accept++;
}
//如果是已经连接的用户,读入认证消息
else if(events[i].events & EPOLLIN)
{
client_socket = (*epoll_data_temp1).fd;
if((*epoll_data_temp1).state == WAVE_CLIENT_START){
//获取客户发送的数据
if(recv(client_socket, buf1, WAVE_ACMESSAGE_LENGTH, 0) == -1){
printf("root %d Reduc call to recv ac message fail \n", WAVE_RANK);
exit(0);
}
j = strtol(buf1, &buf2, 10); //连接进程的rank
k = strtol((char *)(buf2+1), &buf3, 10); //连接进程想要的消息tag
l = strtol((char *)(buf3+1), &buf2, 10); //连接进程想要的消息类型
m = strtol((char *)(buf2+1), &buf3, 10); //连接进程想要的消息长度
#ifdef DEBUG
printf("root %d reduc received ac message from client%d: %s\n", WAVE_RANK, j, buf1);
#endif
//信息类型和长度不匹配说明程序有错
if(k != WAVE_TAG_REDUC || l != type || m != length){
error = 1;
}
//设置用于注测的写操作事件
(*epoll_data_temp1).state = WAVE_CLIENT_WRITE1;
ev.data.ptr = epoll_data_temp1;
//修改client_socket上要处理的事件为EPOLLOUT
ev.events = EPOLLOUT;
epoll_ctl(epfd, EPOLL_CTL_MOD, client_socket, &ev);
client_num++;
continue;
}
if(client_num == reducSize && (*epoll_data_temp1).state == WAVE_CLIENT_READ1){
//获取客户发送的数据
if(WAVE_RecvMessage(client_socket, temp, reducMessageSize) == 0){
printf("root %d Reduc call to recv message fail \n", WAVE_RANK);
exit(0);
}
//做运算,recvbuf= recvbuf (op) temp;
WAVE_Operation(recvbuf, temp, length, type, op);
//WAVE_CloseAndDisable(client_socket, events[i]);
close(client_socket);
client_end++;
}
}
else if(client_num == reducSize && (events[i].events & EPOLLOUT)) // 如果有数据发送
{
client_socket = (*epoll_data_temp1).fd;
//程序员的程序有错
if(error){
memcpy(buf1, "Fail_reduc", 11);
//将信息类型和长度不匹配警告广播给所有进程
if(send(client_socket, buf1, WAVE_ACMESSAGE_LENGTH, 0) == -1){
printf("root %d Reduc call to send ac fail message fail\n", WAVE_RANK);
exit(0);
}
//WAVE_CloseAndDisable(client_socket, events[i]);
close(client_socket);
client_end++;
#ifdef DEBUG
printf("root %d Reduc call to send ac fail message: %s\n", WAVE_RANK, buf1);
#endif
}
else{
memcpy(buf1, "Success_reduc", 14);
//将认证成功广播给所有进程
if(send(client_socket, buf1, WAVE_ACMESSAGE_LENGTH, 0) == -1){
printf("root %d Reduc call to send ac success message fail\n", WAVE_RANK);
exit(0);
}
#ifdef DEBUG
printf("root %d Reduc call to send ac success message: %s\n", WAVE_RANK, buf1);
#endif
//设置用于读操作的文件描述符
(*epoll_data_temp1).state = WAVE_CLIENT_READ1;
ev.data.ptr = epoll_data_temp1;
//设置用于注测的读操作事件
ev.events=EPOLLIN;
epoll_ctl(epfd, EPOLL_CTL_MOD, client_socket, &ev);
}
}//end if out
}//end for
}//end while(1)
#ifdef DEBUG
printf("root %d Reduc end\n",WAVE_RANK);
#endif
close(epfd);
//释放申请的认证信息缓冲区
free(clients);
free(buf1);
free(events);
free(temp);
if(error){
printf("root %d Reduc fail because error in your program(not all node can take part in Reduc).\n", WAVE_RANK);
exit(0);
}
else