-
Notifications
You must be signed in to change notification settings - Fork 0
/
MacMPI_S.c
6180 lines (6082 loc) · 193 KB
/
MacMPI_S.c
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
/* Partial MPI library based on Sockets in Macintosh OS X,
using TCP/IP protocol.
No local buffering of messages is implemented, so that all messages
must be received in the order sent, and receives with wildcard
sources are not supported. the following subroutines are implemented:
MPI_Init, MPI_Finalize, MPI_Send, MPI_Recv, MPI_Isend, MPI_Irecv
MPI_Test, MPI_Wait, MPI_Sendrecv, MPI_Ssend, MPI_Issend, MPI_Waitall
MPI_Waitany, MPI_Get_count, MPI_Initialized, MPI_Comm_size
MPI_Comm_rank, MPI_Comm_dup, MPI_Comm_split, MPI_Comm_free
MPI_Cart_create, MPI_Cart_coords, MPI_Cart_get, MPI_Cart_shift
MPI_Cart_rank, MPI_Cart_sub, MPI_Dims_create
MPI_Bcast, MPI_Barrier, MPI_Reduce, MPI_Scan
MPI_Allreduce, MPI_Gather, MPI_Allgather, MPI_Scatter, MPI_Alltoall
MPI_Gatherv, MPI_Allgatherv, MPI_Scatterv, MPI_Alltoallv
MPI_Reduce_scatter, MPI_Abort, MPI_Wtime, MPI_Wtick, MPI_Type_extent
MPI_Request_free, MPI_Get_processor_name, MPI_Errhandler_set
Sockets are described in Unix: Network Programming, vol. 1, by
W. Richards Stevens [Prentice Hall PTR, Upper Saddle River, NJ, 1998].
The Message Passing Interface (MPI) is described in the reference,
M. Snir, S. Otto, S. Huss-Lederman, D. Walker, and J. Dongarra,
MPI: The Complete Reference [MIT Press, Cambridge, MA,1996].
The file MPIerrs is used throughout for error messages
written by viktor k. decyk, ucla
copyright 2002, regents of the university of california.
all rights reserved.
no warranty for proper operation of this software is given or implied.
software or information may be copied, distributed, and used at own
risk; it may not be distributed without this notice included verbatim
with each file.
update: august 16, 2005 */
#define TARGET_API_MAC_CARBON 1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "mpi.h"
#include <sys/types.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/utsname.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <net/if.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#define __OPENTRANSPORTPROVIDERS__ /* Prevent OT from inserting constants */
#include <Gestalt.h>
#include <MacMemory.h>
#include <Events.h>
#include <MacWindows.h>
#undef TCP_NODELAY
#define TCP_NODELAY 0x01 /* Redefine in case of bad definition */
/* MAXS = maximum number of nodes connected */
#define MAXS 64
/* MAXM = maximum number of outstanding messages on a node */
#define MAXM (2*MAXS)
/* MAXC = maximum number of communicators */
#define MAXC 10
/* MAXD = maximum number of topology dimensions */
#define MAXD 6
/* internal mpi common block
nproc = number of real or virtual processors obtained
idproc = processor id
epref = array of socket endpoints for each participating node
ioc = array of context pointers for notifier function
fstime = first time stamp if MPI_Init successful
mapcomm = communicator map
maxfds = maximum descriptor value
fdsset = fd_set reprsenting all endpoints
sset = signal mask */
static int nproc = -1, idproc;
static int epref[MAXS+2];
static int ioc[MAXS+2][4], mapcomm[MAXC][MAXS+MAXD+3];
static struct timeval fstime;
static int maxfds;
static fd_set fdsset;
static sigset_t sset;
/* internal common block for non-blocking messages
monitor = (0,1,2) = (suppress,display,display & log) monitor messages
curreq = request record for transmission parameters
header = message envelope
rwrec = read/write record for asynchronous messages
trash = trash bin for unwanted data
mqueue = message request queue */
struct RWRec {
int ref;
int ioflag;
void *buf;
size_t nbytes;
int flags;
void *sbuf;
int sln;
int len;
struct timeval ts[2];
int nextm;
int nfatal;
};
struct ipdata {
struct iovec iov[2];
int hdata[4];
};
static int monitor = 1, curreq[MAXM][5], mqueue[MAXS+2][2];
static struct ipdata header[MAXM];
static struct RWRec rwrec[MAXM];
static unsigned char trash[1024];
/* internal common block for message window
cpptr = pointer to window structure
crect = current drag region
nsp = amount of space between boxes
nbx = size of box
nds = number of message sizes monitored
mbs = maximum maximum bandwidth in MB/sec expected */
static WindowPtr cpptr = 0;
static Rect crect;
static short nsp = 8, nbx = 16, nds = 24, mbs = 10;
/* internal common block for error handler
errh = error handler */
static int errh[MAXC];
static FILE *unit2;
/* prototypes for internal procedures */
static void alarmf(int signo);
static void notifier(int signo);
long otpinit(int np, struct sockaddr_in *addrn, int alen);
long etmsec(struct timeval *ptime);
long sbtusec(struct timeval *pstime, struct timeval *crtime);
int ioresult(int *pblock);
int checkesc(long stk);
void sndmsgf(MPI_Request request, int dest);
void rcvmsgf(MPI_Request request, int source);
static int imax(int val1, int val2);
static int imin(int val1, int val2);
static float flmax(float val1, float val2);
static float flmin(float val1, float val2);
static double dmax(double val1, double val2);
static double dmin(double val1, double val2);
static void iredux(int *recvbuf, int *sendbuf, int offset, int count, MPI_Op op);
static void fredux(float *recvbuf, float *sendbuf, int offset, int count, MPI_Op op);
static void dredux(double *recvbuf, double *sendbuf, int offset, int count, MPI_Op op);
void writerrs(char *source, int ierror);
void rwstat(int request, FILE *unit);
void wqueue(FILE *unit);
void messwin(int nvp);
void logmess(int idp, int lstat, int lsize, int mticks, int tag);
void showmess(int idp, int istat, int istyle);
void showdism(int ibin, int nbin, int mbin, int lmax, int istyle);
void shospeed(float atime, float ctime, float arate, float crate);
void Logname(char *name);
void Set_Mon(int monval);
int Get_Mon();
void delmess();
/* function definitions */
int MPI_Init(int *argc, char ***argv) {
/* initialize the MPI execution environment
input: argc, argv, output: none
local data */
int ierror, nerr, nv, i, alen;
int portnum = 0, pshift = 0, pnumid[3], epnum[8];
long oss, response;
char *cnerr = 0;
struct utsname uinfo;
struct hostent *info;
struct in_addr address, oldadd;
char ename[36], myself[36], location[18], fname[18];
struct sockaddr_in addrn, addrb, addrl;
struct timeval ptime;
struct sigaction act;
MPI_Status stat;
FILE *unit3;
/* internal mpi common block
nproc = number of real or virtual processors obtained
idproc = processor id
epref = array of socket endpoints for each participating node
ioc = array of context pointers for notifier function
ioc[i][0] = endpoint reference for notifier for endpoint i
ioc[i][1] = processor id for listener for endpoint i
ioc[i][2] = handle for current receive from endpoint i
ioc[i][3] = handle for current send to endpoint i
fstime = first time stamp if MPI_Init successful
maxfds = maximum descriptor value
fdsset = fd_set reprsenting all endpoints
sset = signal mask
mapcomm = communicator map
mapcomm[i][0:nproc-1] = actual proc id for given rank in communicator i
mapcomm[i][nproc:MAXS-1] = MPI_UNDEFINED
mapcomm[i][MAXS] = number of processes in comm i
mapcomm[i][MAXS+1] = rank for this node in comm i
mapcomm[i][MAXS+2] = ndims = number of dimensions in topology in comm i
mapcomm[i][iMAXS+3:MAXS+2+ndims] = size of dimension in comm i,
negative if non-periodic
mapcomm[i][MAXS+3+ndims:MAXS+2+MAXD] = 0 */
/* internal common block for non-blocking messages
monitor = (0,1,2) = (suppress,display,display & log) monitor messages
curreq = request record for transmission parameters, see rwstat
header = message envelope
hdata[i][0] = communicator, hdata[i][1] = tag, hdata[i][2] = datatype
hdata[i][3] = length of data (in bytes) for message handle i
rwrec = read/write record for asynchronous messages, see rwstat
mqueue = message request queue
mqueue[i][0] = end of message queue for receives from endpoint i
mqueue[i][1] = end of message queue for sends to endpoint i */
/* internal common block for adsp
epref0 = endpoint reference used by tcp and adsp providers */
/* Initialize common block data */
for (i = 0; i < MAXS+2; i++) {
epref[i] = 0;
for (nv = 0; nv < 2; nv++)
mqueue[i][nv] = 0;
}
for (i = 0; i < MAXC; i++) {
for (nv = 0; nv < MAXS+MAXD+3; nv++)
mapcomm[i][nv] = 0;
}
for (i = 0; i < MAXM; i++) {
for (nv = 0; nv < 5; nv++)
curreq[i][nv] = 0;
}
/* Set MPI_COMM_WORLD and MPI_COMM_SELF mapping */
for (i = 0; i < MAXS; i++) {
mapcomm[0][i] = i;
mapcomm[1][i] = MPI_UNDEFINED;
}
/* make all errors fatal */
for (i = 0; i < MAXC; i++) {
errh[i] = MPI_ERRORS_ARE_FATAL;
}
/* Zero out descriptor array */
FD_ZERO(&fdsset);
/* Open error file */
unit2 = fopen("MPIerrs","w");
if (monitor==2)
fprintf(unit2,"MPI_Init started\n");
/* Get information about the operating environment */
Gestalt(gestaltSystemVersion,&response);
/* Check if MacOS 10.0 or higher is installed */
if (response < 4096) {
fprintf(unit2,"MacMPI_S requires MacOS 10.0 or higher\n");
nv = response/256;
response = response - 256*nv;
nerr = response/16;
response = response - 16*nerr;
sprintf(ename,"%d.%d%d\n",nv,nerr,response);
fprintf(unit2,"MacOS detected: %s\n",ename);
ierror = -7;
return ierror;
}
/* Obtain the current time stamp */
oss = gettimeofday(&fstime,NULL);
if (oss < 0) {
oss = errno;
fprintf(unit2,"Gettimeofday Error = %d, %s\n",oss,strerror(oss));
}
/* Obtain information about the Internet environment */
oss = uname(&uinfo);
if (oss < 0) {
ierror = errno;
fprintf(unit2,"Uname Error = %d, %s\n",ierror,strerror(ierror));
return ierror;
}
strcpy(myself,uinfo.nodename);
/* debug */
if (monitor==2) {
fprintf(unit2,"local host name=%s\n",myself);
}
info = gethostbyname(uinfo.nodename);
if (info==NULL) {
oss = h_errno;
fprintf(unit2,"Gethostbyname Error = %d for %s\n",oss,myself);
fprintf(unit2,"%s, Trying INADDR_ANY\n",hstrerror(oss));
address.s_addr = INADDR_ANY;
}
else {
/* debug */
if (monitor==2) {
i = 1;
cnerr = ((info->h_addr_list)[i]);
while (cnerr) {
address.s_addr = *(in_addr_t *) cnerr;
cnerr = inet_ntoa(address);
strcpy(myself,cnerr);
fprintf(unit2,"other local addresses=%d,%s\n",i,myself);
i =+ 1;
cnerr = ((info->h_addr_list)[i]);
}
}
address.s_addr = *(in_addr_t *) ((info->h_addr_list)[0]);
}
cnerr = inet_ntoa(address);
strcpy(myself,cnerr);
/* debug */
if (monitor==2) {
fprintf(unit2,"local address=%s\n",myself);
}
oldadd = address;
/*
Everyone opens a port
*/
/* Open file containing portnum (and possibly participating nodes)
first line in nodelist file on all nodes contains common portnum
if the file is missing or empty, a default number of 5013 is used */
unit3 = fopen("nodelist_ip","r");
if (!unit3)
unit3 = fopen("nodelist","r");
if (unit3) {
cnerr = fgets(location,11,unit3);
if (!cnerr)
strcpy(location,"5013");
/* Replace trailing newlines with nulls */
cnerr = strchr(location,'\n');
if (cnerr)
cnerr[0] = '\0';
if (location[0]=='\0')
strcpy(location,"5013");
}
else
strcpy(location,"5013");
/* Convert string to integer */
nerr = sscanf(location,"%d",&portnum);
if ((!nerr) || (portnum < 5000) || (portnum > 49152))
portnum = 5013;
/* Construct sockaddr_in of listener */
addrn.sin_family = AF_INET;
addrn.sin_port = portnum;
addrn.sin_addr.s_addr = INADDR_ANY;
/* addrn.sin_addr = address; */
alen = sizeof(addrn);
for (i = 0; i < 8; i++)
addrn.sin_zero[i] = '\0';
strcpy(ename,myself);
nproc = 0;
/* Initialize synchronous listener endpoint provider */
oss = otpinit(MAXS+1,&addrn,alen);
if (oss) {
ierror = oss;
nerr = MPI_Finalize();
return ierror;
}
/* Save copy of portnum */
pnumid[0] = addrn.sin_port;
/* Set struct sockaddr_in for address which is returned */
nerr = alen;
oss = getsockname(epref[MAXS+1],(struct sockaddr *)&addrb,&nerr);
if (oss < 0) {
ierror = errno;
fprintf(unit2,"Getsockname Error = %d, %s\n",ierror,strerror(ierror));
nerr = MPI_Finalize();
return ierror;
}
/* Pass processor id to listener notifier */
ioc[MAXS+1][1] = nproc + 1;
/* debug */
if (monitor==2)
fprintf(unit2,"listener=%s,port=%d,socket=%d\n",myself,portnum,
epref[MAXS+1]);
/*
Determine if node is master (idproc=0) or slave (idproc>0).
on the master node, the second and subsequent lines of nodelist file
contain IP addresses of the nodes participating, in dotted-decimal
format (for example, "12.13.14.15").
if this list of nodes is missing, then the node is a slave.
every node also makes a connection to itself.
*/
if (unit3) {
cnerr = fgets(location,17,unit3);
}
/* Must be slave */
if (!unit3 || !cnerr)
idproc = 1;
else {
/* Replace trailing newlines with nulls */
cnerr = strchr(location,'\n');
if (cnerr)
cnerr[0] = '\0';
/* Must be slave */
if (location[0]=='\0')
idproc = 1;
/* May be master */
else {
if (((!strcmp(location,"self")) || (!strcmp(location,myself)))
&& (pnumid[0]==portnum)) {
idproc = 0;
/* Delete file if empty */
if (!fseek(unit2,0,SEEK_END)) {
i = ftell(unit2);
fclose(unit2);
if (!i)
remove("MPIerrs");
}
sprintf(fname,"MPIerrs%.2d",idproc);
unit2 = fopen(fname,"w");
}
else
/* Must be slave */
idproc = 1;
}
}
/* Update port number */
portnum = pnumid[0];
/*
* * * begin main iteration loop * * *
Prepare to accept connection
*/
/* Establish connection end */
/* Listen for an incoming connection request */
L10: oss = listen(epref[MAXS+1],1);
if (oss < 0) {
ierror = errno;
fprintf(unit2,"Listen Error = %d, %s\n",ierror,strerror(ierror));
nerr = MPI_Finalize();
return ierror;
}
/* Prepare alarm */
act.sa_handler = alarmf;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
oss = sigaction(SIGALRM,&act,NULL);
if (oss < 0) {
ierror = errno;
fprintf(unit2,"Sigaction SIGALRM Error = %d, %s\n",ierror,
strerror(ierror));
nerr = MPI_Finalize();
return ierror;
}
/* For connections to oneself, jump to connect */
if (idproc==nproc) {
ioc[MAXS+1][1] = MAXS+2;
goto L70;
}
/* Set alarm */
alarm(60);
/* Accept an incoming connection request */
nerr = alen;
epref[nproc] = accept(epref[MAXS+1],(struct sockaddr *)&addrl,&nerr);
/* Clear alarm */
alarm(0);
if (epref[nproc] < 0) {
ierror = errno;
if (ierror==EINTR)
ierror = ETIMEDOUT;
fprintf(unit2,"Accept Error = %d, %s\n",ierror,strerror(ierror));
return ierror;
}
ioc[nproc][0] = nproc;
ioc[nproc][1] = 0;
/* debug */
if (monitor==2) {
cnerr = inet_ntoa(addrl.sin_addr);
fprintf(unit2,"socket=%d accepted connection with=%s,%d\n",
epref[nproc],cnerr,addrl.sin_port);
}
ioc[MAXS+1][1] = 0;
/* Receive portnum for verification and processor id from remote node */
nproc += 1;
mapcomm[0][MAXS] = nproc;
/* Reset iocompletion flag to notifier */
ioc[MAXS+1][1] = nproc + 1;
ierror = MPI_Recv(epnum,3,MPI_INT,nproc-1,3,0,&stat);
/* Extract processor id on first connection */
if (nproc==1) {
idproc = epnum[1];
pshift = portnum - epnum[2];
portnum = epnum[2];
mapcomm[0][MAXS+1] = idproc;
/* Delete file if empty */
if (!fseek(unit2,0,SEEK_END)) {
i = ftell(unit2);
fclose(unit2);
if (!i)
remove("MPIerrs");
}
sprintf(fname,"MPIerrs%.2d",idproc);
unit2 = fopen(fname,"w");
}
/* Check if remote portnum agrees with local portnum */
nerr = pnumid[0] - epnum[0];
/* Send reject flag to remote node */
ierror = MPI_Send(&nerr,1,MPI_INT,nproc-1,4,0);
/* Reject if portnums disagree */
if (nerr) {
fprintf(unit2,"Session rejected, idproc = %d\n",idproc);
fprintf(unit2,"Portnums do not agree\n");
ierror = 5;
fprintf(unit2,"remote portnum = %d\n",epnum[0]);
nerr = MPI_Finalize();
return ierror;
}
/* Check for processor number overflow */
if (nproc > MAXS) {
fprintf(unit2,"processor number overflow, nproc = %d\n",nproc);
ierror = 6;
nerr = MPI_Finalize();
return ierror;
}
/* debug */
if (monitor==2)
fprintf(unit2,"connection accepted with idproc=%d\n",nproc-1);
/* Accept more connections */
if (idproc >= nproc)
goto L10;
/*
Master prepares to start connection
*/
/* Find internet address of requested node */
/* Convert a character string into an address */
L40: oss = inet_aton(location,&address);
if (!oss) {
fprintf(unit2,"inet_aton failed, location = %s\n",location);
fprintf(unit2,"Invalid nodelist file possibly being used\n");
ierror = oss;
nerr = MPI_Finalize();
return ierror;
}
/* Second cpu on a node uses incremented port number */
if (address.s_addr==oldadd.s_addr)
pshift = pshift + 1;
else {
pshift = 0;
oldadd = address;
}
pnumid[0] = portnum + pshift;
addrn.sin_family = AF_INET;
addrn.sin_port = pnumid[0];
/* addrn.sin_addr.s_addr = INADDR_ANY; */
addrn.sin_addr = address;
for (i = 0; i < 8; i++)
addrn.sin_zero[i] = '\0';
/* Convert an address into a character string */
cnerr = inet_ntoa(address);
strcpy(ename,cnerr);
/* Establish connection end */
/* Create a new socket for this endpoint */
L70: addrb.sin_port = 0;
/* Initialize synchronous endpoint provider */
oss = otpinit(nproc,&addrb,alen);
if (oss) {
ierror = oss;
nerr = MPI_Finalize();
return ierror;
}
/* debug */
if (monitor==2)
fprintf(unit2,"socket=%d requesting connection with=%s,%d\n",
epref[nproc],ename,pnumid[0]);
/* Pause to let OS get some time */
if (checkesc(1)) {
ierror = -9;
writerrs("MPI_Init: ",ierror);
return ierror;
}
/* Obtain the current time stamp */
oss = gettimeofday(&ptime,NULL);
/* Set alarm */
alarm(60);
/* Request a connection to a remote peer */
oss = connect(epref[nproc],(struct sockaddr *)&addrn,alen);
/* Clear alarm */
alarm(0);
if (oss < 0) {
ierror = errno;
if (ierror==EINTR)
ierror = ETIMEDOUT;
fprintf(unit2,"Connect Error = %d, %s\n",ierror,strerror(ierror));
return ierror;
}
/* Accept an incoming connection request to oneself */
if (idproc==nproc) {
nerr = alen;
epref[MAXS] = accept(epref[MAXS+1],(struct sockaddr *)&addrl,&nerr);
if (epref[MAXS] < 0) {
ierror = errno;
fprintf(unit2,"Accept Error = %d, %s\n",ierror,strerror(ierror));
return ierror;
}
ioc[MAXS][0] = MAXS;
ioc[MAXS][1] = 0;
/* debug */
if (monitor==2) {
cnerr = inet_ntoa(addrl.sin_addr);
fprintf(unit2,"self socket=%d accepted connection with=%s,%d\n",
epref[MAXS],cnerr,addrl.sin_port);
}
}
ioc[nproc][1] = 0;
/* debug */
if (monitor==2) {
nerr = alen;
oss = getpeername(epref[nproc],(struct sockaddr *)&addrl,&nerr);
if (oss < 0) {
oss = errno;
fprintf(unit2,"Getpeername Error = %d, %s\n",oss,strerror(oss));
addrl.sin_addr.s_addr = 0;
}
cnerr = inet_ntoa(addrl.sin_addr);
fprintf(unit2,"tentative connection started with=%s,%d\n",cnerr,
addrl.sin_port);
}
/* Send portnum for verification and processor id to remote node */
nerr = nproc;
nproc += 1;
mapcomm[0][MAXS] = nproc;
if (nerr > idproc) {
/* Set processor id */
pnumid[1] = nerr;
pnumid[2] = portnum;
ierror = MPI_Send(pnumid,3,MPI_INT,nproc-1,3,0);
/* Read and check reject flag */
ierror = MPI_Recv(&nerr,1,MPI_INT,nproc-1,4,0,&stat);
if (nerr) {
fprintf(unit2,"Connection rejected, reject info = %d\n",nerr);
fprintf(unit2,"Portnums do not agree, idproc = %d\n",nproc-1);
ierror = 12;
nerr = MPI_Finalize();
return ierror;
}
}
/* Check for processor number overflow */
if (nproc > MAXS) {
fprintf(unit2,"processor number overflow, nproc = %d\n",nproc);
ierror = 6;
nerr = MPI_Finalize();
return ierror;
}
/* debug */
if (monitor==2)
fprintf(unit2,"connection confirmed with idproc=%d\n",nproc-1);
/* Pass current location to next node */
if (nproc > (idproc+2))
nerr = MPI_Send(location,4,MPI_INT,idproc+1,1,0);
/* Read location of next node from file */
if (idproc==0) {
if ((nproc >= 2) || (!strcmp(location,"self")) || (!strcmp(location,myself))) {
if (!unit3)
goto L90;
cnerr = fgets(location,17,unit3);
if (!cnerr)
goto L90;
/* Replace trailing newlines with nulls */
cnerr = strchr(location,'\n');
if (cnerr)
cnerr[0] = '\0';
if (location[0]=='\0')
goto L90;
}
}
/* Receive location of next node from another processor */
else {
nerr = MPI_Recv(location,4,MPI_INT,idproc-1,1,0,&stat);
/* End of file marker received */
if (stat.len==0)
goto L90;
}
/* Start another connection */
goto L40;
/*
* * * end main iteration loop * * *
*/
/* All expected nodes activated */
L90: nv = nproc - 1;
/* debug */
if (monitor==2)
fprintf(unit2,"all nodes activated, idproc, nproc=%d,%d\n",
idproc,nproc);
/* Send null record to next processor */
if (idproc < nv)
nerr = MPI_Send(location,0,MPI_INT,idproc+1,1,0);
if (unit3)
fclose(unit3);
/* Check number of processors */
if (idproc==nv) {
for (i = 1; i <= nv; i++) {
nerr = MPI_Send(&nproc,1,MPI_INT,nv-i,2,0);
}
}
else {
nerr = MPI_Recv(&response,1,MPI_INT,nv,2,0,&stat);
/* Local processor does not agree with last processor on total number */
if (response != nproc) {
fprintf(unit2,"processor number error, local/remote nproc = %d,%d\n",
nproc,response);
ierror = 7;
nerr = MPI_Finalize();
return ierror;
}
}
/* Clear unused MPI_COMM_WORLD mapping */
for (i = nproc; i < MAXS; i++) {
mapcomm[0][i] = MPI_UNDEFINED;
}
mapcomm[0][MAXS+2] = 0;
/* Set MPI_COMM_SELF */
mapcomm[1][0] = idproc;
mapcomm[1][MAXS] = 1;
mapcomm[1][MAXS+1] = 0;
mapcomm[1][MAXS+2] = 0;
/* Set descriptor array for notifier */
maxfds = -1;
for (i = 0; i < MAXS+1; i++) {
if (epref[i]) {
maxfds = imax(epref[i],maxfds);
FD_SET(epref[i],&fdsset);
}
}
maxfds = maxfds + 1;
/* Prepare signal mask */
oss = sigemptyset(&sset);
oss = sigaddset(&sset,SIGIO);
/* Prepare signal action */
act.sa_handler = notifier;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
oss = sigaction(SIGIO,&act,NULL);
if (oss < 0) {
ierror = errno;
fprintf(unit2,"Sigaction SIGIO Error = %d, %s\n",ierror,
strerror(ierror));
nerr = MPI_Finalize();
return ierror;
}
/* Set providers to asynchronous mode */
nv = 1;
nerr = getpid();
for (i = 0; i < MAXS+1; i++) {
if (epref[i]) {
oss = fcntl(epref[i],F_SETOWN,nerr);
if (oss < 0) {
oss = errno;
fprintf(unit2,"F_SETOWN fcntl Error, oss = %d, %s\n",oss,
strerror(oss));
return oss;
}
oss = ioctl(epref[i],FIOASYNC,&nv);
if (oss < 0) {
oss = errno;
fprintf(unit2,"FIOASYNC ioctl Error, oss = %d, %s\n",oss,
strerror(oss));
return oss;
}
oss = ioctl(epref[i],FIONBIO,&nv);
if (oss < 0) {
oss = errno;
fprintf(unit2,"FIONBIO ioctl Error, oss = %d, %s\n",oss,
strerror(oss));
return oss;
}
}
}
/* Create window for showing MPI message status */
if (monitor > 0) {
messwin(nproc);
checkesc(1);
if (monitor==2)
fprintf(unit2,"MPI_Init complete\n\n");
}
/* Set error code to success */
ierror = 0;
return ierror;
}
static void alarmf(int signo) {
/* callback function to handle connect/accept alarms */
return;
}
static void notifier(int signo) {
/* notifier function for asynchronous and completion events
local data */
int i, j, l, n, m;
long num, oss;
struct timeval timeout = {0,0};
fd_set lfdsset;
/* internal mpi common block
epref = array of endpoint references for each participating node
maxfds = maximum descriptor value
fdsset = fd_set reprsenting all endpoints */
/* internal common block for non-blocking messages
rwrec = read/write record for asynchronous messages
trash = trash bin for unwanted data
mqueue = message request queue */
/* Check if any descriptors are ready for reading */
lfdsset = fdsset;
num = select(maxfds,&lfdsset,NULL,NULL,&timeout);
/* Check for error in select */
if (num <= 0) {
num = 0;
goto L20;
}
l = 0;
/* Find which descriptor has a read event */
for (i = 0; i < nproc; i++) {
if (FD_ISSET(epref[i],&lfdsset)) {
/* Get reference to endpoint */
n = ioc[i][0];
/* Read pointer to current read record */
m = ioc[i][2] - 1;
/* Read data sent from a remote peer */
if ((m >= 0) && (m < MAXM)) {
/* Obtain the current time stamp */
oss = gettimeofday(&rwrec[m].ts[1],NULL);
L10: oss = recv(rwrec[m].ref,rwrec[m].buf,rwrec[m].nbytes,
rwrec[m].flags);
if (oss < 0)
oss = errno - 1024;
/* Process data which arrived */
if (oss > 0) {
/* Clear non-fatal error code */
rwrec[m].nfatal = 0;
/* Set actual length received */
rwrec[m].len += oss;
/* Check if all the data has arrived */
if (rwrec[m].len < header[m].hdata[3]) {
/* Incomplete message */
if (rwrec[m].nbytes > oss) {
/* Readjust buffer pointer */
rwrec[m].buf = (void *)(((unsigned char *)rwrec[m].buf) + oss);
rwrec[m].nbytes -= oss;
if (rwrec[m].len >= 0)
rwrec[m].ioflag += 1;
}
/* Header is received, readjust parameters to receive data */
else if (rwrec[m].ioflag==1) {
rwrec[m].buf = rwrec[m].sbuf;
rwrec[m].nbytes = imin(header[m].hdata[3],rwrec[m].sln);
rwrec[m].ioflag = 2;
}
/* Data is received and buffer is full */
else {
rwrec[m].buf = &trash;
rwrec[m].nbytes = 1024;
}
goto L10;
}
/* Message complete */
else {
/* Obtain the current time stamp */
oss = gettimeofday(&rwrec[m].ts[1],NULL);
/* Set iocompletion flag */
rwrec[m].ioflag = 0;
/* Get next message if messages are queued */
if (rwrec[m].nextm > 0) {
m = rwrec[m].nextm;
if (m==mqueue[n][0])
mqueue[n][0] = 0;
ioc[i][2] = m;
m -= 1;
goto L10;
}
/* Clear pointer to current send record */
else
ioc[i][2] = 0;
}
}
/* Check for errors */
else {
/* Check for EOF */
if (oss==0)
oss = -1;
/* Quit if no data is available */
if ((oss+1024) != EWOULDBLOCK) {
/* Set iocompletion flag to error */
rwrec[m].ioflag = oss;
ioc[i][2] = 0;
}
}
}
/* All receive events processed */
l += 1;
if (l==num)
goto L20;
}
}
/* Check if any descriptors are ready for writing */
L20: lfdsset = fdsset;
oss = select(maxfds,NULL,&lfdsset,NULL,&timeout);
if (oss > 0)
num += oss;
/* Check for error in select */
if (oss <= 0)
return;
/* Find which descriptor has a write event */
for (j = 0; j < nproc+1; j++) {
i = j;
if (j==nproc)
i = MAXS;
if (FD_ISSET(epref[i],&lfdsset)) {
/* Get reference to endpoint */
n = ioc[i][0];
/* Read pointer to current send record */
m = ioc[i][3] - 1;
/* Send data to a remote peer */
if ((m >= 0) && (m < MAXM)) {
/* Obtain the current time stamp */
oss = gettimeofday(&rwrec[m].ts[1],NULL);
L30: if (rwrec[m].ioflag==1)
oss = writev(rwrec[m].ref,rwrec[m].buf,rwrec[m].nbytes);
else
oss = send(rwrec[m].ref,rwrec[m].buf,rwrec[m].nbytes,
rwrec[m].flags);
if (oss < 0)
oss = errno - 1024;
/* Process data which has been sent */
if (oss > 0) {
/* Clear non-fatal error code */
rwrec[m].nfatal = 0;
/* Set actual length sent */
rwrec[m].len += oss;
/* Check for incomplete header */
if (rwrec[m].len < 0) {
header[m].iov[0].iov_base =
(void *)(((unsigned char *)header[m].iov[0].iov_base) + oss);
header[m].iov[0].iov_len -= oss;
goto L30;
}
/* Check for incomplete data */
else if (rwrec[m].sln > rwrec[m].len) {
/* Header is sent, readjust parameters to send data */
if (rwrec[m].ioflag==1) {
rwrec[m].buf = rwrec[m].sbuf;
rwrec[m].nbytes = rwrec[m].sln;
oss -= header[m].iov[0].iov_len;
}
/* Readjust buffer pointer */
rwrec[m].buf = (void *)(((unsigned char *)rwrec[m].buf) + oss);
rwrec[m].nbytes -= oss;
rwrec[m].ioflag += 1;
goto L30;
}
/* Data is sent */
else {
/* Obtain the current time stamp */
oss = gettimeofday(&rwrec[m].ts[1],NULL);
/* Set iocompletion flag */
rwrec[m].ioflag = 0;
/* Get next message if messages are queued */
if (rwrec[m].nextm > 0) {
m = rwrec[m].nextm;
if (m==mqueue[n][1])
mqueue[n][1] = 0;
ioc[i][3] = m;
m -= 1;
goto L30;
}
/* Clear pointer to current send record */
else
ioc[i][3] = 0;
}
}
/* Check for errors */
else {
/* Check for EOF */
if (oss==0)
oss = -1;
/* Quit if no data can be sent */
if ((oss+1024) != EWOULDBLOCK) {
/* Set iocompletion flag to error */
rwrec[m].ioflag = oss;
ioc[i][3] = 0;
}
}
}
/* All send events processed */
l += 1;
if (l==num)
return;
}
}
return;
}
long otpinit(int np, struct sockaddr_in *addrn, int alen) {
/* this subroutine initializes a Socket provider for
index np, using address specified in addrn.
provider is left in asynchronous, blocking mode
np = index to endpoint reference array
addrn = address to which endpoint is to be bound
alen = length of addrn structure