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
|
/*
* PCPARC.C - parallel communications routines for PPC for distributed
* - or multitasked configuration
*
* Source Version: 2.0
* Software Release #92-0043
*
*/
#include "cpyright.h"
#include "ppc.h"
#ifdef HAVE_MPI
#include <mpi.h>
static MPI_Request
*_PC_reqs = NULL;
static int
nrq = 0,
nrqx = 0;
#endif
static char
server[MAXLINE];
static int
server_port = 0,
PC_n_nodes = 0;
static PROCESS
*_PC_server_link = NULL;
#ifdef HAVE_MPI
# define HAVE_DPE
#endif
/*--------------------------------------------------------------------------*/
#ifdef HAVE_DPE
/*--------------------------------------------------------------------------*/
/* PC_PUSH_PENDING - push an I/O transaction onto the pending list */
static void PC_push_pending(pp, op, bf, type, ni, vr, req)
PROCESS *pp;
int op;
char *bf, *type;
size_t ni;
byte *vr;
byte *req;
{PC_pending_msg *pm;
pm = FMAKE(PC_pending_msg, "PC_PUSH_PENDING:pm");
pm->bf = bf;
pm->type = SC_strsavef(type, "PC_PUSH_PENDING:type");
pm->nitems = ni;
pm->vr = vr;
pm->oper = op;
pm->req = pp->n_pending++;
pm->nxt = pp->pending;
pp->pending = pm;
#ifdef HAVE_MPI
{MPI_Request requ;
requ = *(MPI_Request *) req;
SC_REMEMBER(MPI_Request, requ, _PC_reqs, nrq, nrqx, 10);};
#endif
return;}
/*--------------------------------------------------------------------------*/
#endif
/*--------------------------------------------------------------------------*/
/* PC_POP_PENDING - pop a completed pending message */
static void PC_pop_pending(pp, po, pbf, pty, pni, pvr)
PROCESS *pp;
int *po;
char **pbf, **pty;
size_t *pni;
byte **pvr;
{PC_pending_msg *pm;
pm = pp->pending;
if (pm != NULL)
{pp->pending = pm->nxt;
pp->n_pending--;
*po = pm->oper;
*pbf = pm->bf;
*pty = pm->type;
*pni = pm->nitems;
*pvr = pm->vr;
SFREE(pm);};
#ifdef HAVE_MPI
_PC_reqs[--nrq] = 0;
#endif
return;}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_OPEN_GROUP - open a copy of the named executable on each available
* - node
*/
int PC_open_group(argv, pn)
char **argv;
int *pn;
{int i, argc, offs;
char **args, s[MAXLINE], *t;
PROCESS *pp;
#ifdef HAVE_PROCESS_CONTROL
PC_n_nodes = 0;
offs = 3;
argc = 0;
while (argv[argc++] != NULL);
args = FMAKE_N(char *, argc + offs, "PCPARC.C:args");
args[0] = SC_strsavef("pcexec", "char*:PC_OPEN_GROUP:args0");
args[1] = SC_strsavef("-r", "char*:PC_OPEN_GROUP:args1");
args[2] = SC_strsavef("1", "char*:PC_OPEN_GROUP:args2");
for (i = 0; i < argc; i++)
args[i + offs] = argv[i];
pp = PC_open(args, NULL, "rb+");
PC_block(pp);
PC_gets(s, MAXLINE, pp);
strcpy(server, SC_strtok(s, ",", t));
server_port = SC_stoi(SC_strtok(NULL, ",\n", t));
PC_n_nodes = SC_stoi(SC_strtok(NULL, ",\n", t));
_PC_debug = SC_stoi(SC_strtok(NULL, ",\n", t));
for (i = 0; i < offs; i++)
SFREE(args[i]);
SFREE(args);
if (pn != NULL)
*pn = PC_n_nodes;
_PC_server_link = pp;
#endif
return(TRUE);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_OPEN_MEMBER - open a copy of the specified executable on this node */
PROCESS *PC_open_member(argv, pnn)
char **argv;
int *pnn;
{PROCESS *pp;
char *tok, t[MAXLINE], srvr[MAXLINE], *s;
int port, argc;
#ifdef HAVE_PROCESS_CONTROL
/* if the server is the parent of this process, argv will have the
* server name and port number just after the first NULL item in argv
*/
srvr[0] = '\0';
port = -1;
for (argc = 0; argv[argc] != NULL; argc++);
strncpy(t, argv[--argc], MAXLINE-1);
t[MAXLINE-1] = '\0';
tok = SC_strtok(t, ":", s);
if (strcmp(tok, "HOST") == 0)
{strcpy(srvr, SC_strtok(NULL, ",\n", s));
port = SC_stoi(SC_strtok(NULL, ",\n", s));
argv[argc] = NULL;};
pp = PC_mk_process(argv, "rb+", PC_CHILD);
# ifdef HAVE_MPI
/* initialize MPI */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &SC_comm_rank);
MPI_Comm_size(MPI_COMM_WORLD, &SC_comm_size);
# else
if (pnn != NULL)
{if ((srvr[0] == '\0') || (port < 0))
{PC_open_group(argv, pnn);
strcpy(srvr, server);
port = server_port;
pp->in = _PC_server_link->in;
pp->out = _PC_server_link->out;}
else
{pp->in = 0;
pp->out = 1;};
PC_block(pp);
PC_gets(t, MAXLINE, pp);
sscanf(t, "%d,%d,%d\n", &pp->acpu, pnn, &_PC_debug);
PC_printf(pp, "%s,%d,%d\n", srvr, port, getpid());
pp->data = PC_init_client(srvr, port);
SC_comm_size = *pnn;};
# endif
/* conditional diagnostic messages */
if (_PC_debug)
{sprintf(t, "PC_clnt_log.%d", (int) getpid());
_PC_diag = fopen(t, "w");
fprintf(_PC_diag, "\n\n Node #%d at %s:%d.%d\n",
pp->acpu, srvr, port, pp->data);
fflush(_PC_diag);};
#else
pp = NULL;
#endif
return(pp);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_CLOSE_MEMBER - close the member process */
void PC_close_member(pp)
PROCESS *pp;
{
#ifdef HAVE_PROCESS_CONTROL
# ifdef HAVE_MPI
MPI_Finalize();
PC_close(pp);
# else
PC_close(pp);
# endif
/* conditional diagnostic messages */
if (_PC_debug)
{fclose(_PC_diag);};
#endif
return;}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _PC_PUT_DATA - put the data out and return the confirmation info */
static int _PC_put_data(pp, bf, type, ni, nb, dn, pni, types, pi)
PROCESS *pp;
char *bf, *type;
size_t ni;
int nb, dn;
long *pni;
char *types;
int *pi;
{char *pbf, reply[MAXLINE];
int nbo, data;
PC_printf(pp, "%c,%s,%ld,%d\n", PC_FWRITE, type, ni, dn);
data = pp->data;
pbf = bf;
while (nb > 0)
{PC_buffer_data_in(pp);
nbo = write(data, pbf, nb);
if (nbo < 0)
continue;
else if (_PC_debug)
{fprintf(_PC_diag, ".%d", nbo);
fflush(_PC_diag);};
nb -= nbo;
pbf += nbo;};
PC_block(pp);
PC_gets(reply, MAXLINE, pp);
sscanf(reply, "%ld,%s,%d\n", pni, types, pi);
return(TRUE);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _PC_EXTRACT_FILTER_INFO - extract the message description information
* - from the message filter array
*/
static void _PC_extract_filter_info(filt, pti, pit, phn, pdn, pdi,
pbs, pbz, pnn, pnl, pnp, ppl)
int *filt, *pti, *pit, *phn, *pdn, *pdi;
int *pbs, *pbz, *pnn, **pnl, *pnp, **ppl;
{int type_index, ityp, host_node, default_node, default_id;
int block_state, buffer_size, more, nn, np;
int *nl, *pl, *p;
type_index = 0;
default_node = -1;
default_id = -1;
host_node = -1;
block_state = TRUE;
buffer_size = 0;
ityp = 0;
nl = NULL;
pl = NULL;
if (filt != NULL)
{p = filt;
more = TRUE;
while (more)
{switch (*p++)
{case PC_MATCH_TYPE :
ityp |= *p++;
ityp |= ((type_index << 16) & PC_TYPE_MASK);
break;
case PC_MATCH_TAG :
ityp |= (*p++ & PC_TAG_MASK);
break;
case PC_MATCH_NODE :
nl = p;
nn = *p++;
p += nn;
break;
case PC_MATCH_PID :
pl = p;
np = *p++;
p += np;
break;
case PC_BLOCK_STATE :
block_state = *p++;
break;
case PC_BUFFER_SIZE :
buffer_size = *p++;
break;
default :
more = FALSE;
break;};};};
*pti = type_index;
*pit = ityp;
*pbs = block_state;
*pbz = buffer_size;
*pnn = nn;
*pnl = nl;
*pnp = np;
*ppl = pl;
if (phn != NULL)
*phn = host_node;
if (phn != NULL)
*phn = default_node;
if (pdi != NULL)
*pdi = default_id;
return;}
/*--------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* PC_SIZE_MESSAGE - return the number of items of the specified type
* - in a message
* - SP - destination processor index
* - TYPE - type of items
* - TAG - message tag
*/
long PC_size_message(sp, type, tag)
int sp;
char *type;
int tag;
{int ni;
#ifdef HAVE_MPI
MPI_Status stat;
MPI_Probe(sp, tag, MPI_COMM_WORLD, &stat);
MPI_Get_count(&stat, MPI_CHAR, &ni);
#else
ni = 0;
#endif
return((long) ni);}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* PC_GLMN_MESSAGE - return the global minimum of some quantity */
REAL PC_glmn_message(vi)
double vi;
{REAL vo;
#ifdef HAVE_MPI
MPI_Allreduce(&vi, &vo, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
#else
{int i, ip, np;
REAL vt;
static int sp[] = {PC_MATCH_NODE, -1,
PC_MATCH_TAG, 0,
0};
ip = PC_get_processor_number();
np = PC_get_number_processors();
sp[1] = -1;
PC_out(&vi, SC_REAL_S, 1, NULL, sp);
vo = vi;
for (i = 0; i < np; i++)
{if (i == ip)
continue;
sp[1] = i;
PC_in(&vt, SC_REAL_S, 1, NULL, sp);
vo = min(vo, vt);};};
#endif
return(vo);}
/*---------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_OUT - write data out to the filtered list of nodes
* - this does a message passing system's SEND command
* - or a LINDA-like PUT
*/
long PC_out(vr, type, ni, pp, filt)
byte *vr;
char *type;
size_t ni;
PROCESS *pp;
int *filt;
{int i, ityp, dn, nb, nbr, is;
int block, buf_siz;
int type_index, default_node, default_id, host_node;
int *nl, *pnl, *pl, nn, np;
long nis, nib;
char reply[MAXLINE], types[MAXLINE], *bf;
PDBfile *vif, *tf;
PC_ERR_TRAP(-1L);
vif = pp->vif;
_PC_extract_filter_info(filt,
&type_index, &ityp,
&host_node, &default_node, &default_id,
&block, &buf_siz,
&nn, &nl, &np, &pl);
if (nl == NULL)
{nn = 1;
pnl = &default_node;}
else
{pnl = nl;
nn = *pnl++;};
/* conditional diagnostic messages */
if (_PC_debug)
{fprintf(_PC_diag, " Write");
fprintf(_PC_diag, " Attempt(%d,%s,%d)", (int) ni, type, pp->acpu);
fflush(_PC_diag);};
/* get the buffer size */
nbr = PD_sizeof(vif, type, ni, vr);
nb = (buf_siz > 0) ? buf_siz : nbr;
if (nb < nbr)
PC_error("SPECIFIED BUFFER SIZE TOO SMALL - PC_OUT");
/* allocate the buffer */
bf = FMAKE_N(char, nb, "PC_OUT:bf");
/* convert the data into a message buffer */
tf = PN_open(vif, bf);
nis = PN_write(tf, type, ni, vr) ? ni : 0;
PN_close(tf);
/* send the message now */
#ifdef HAVE_MPI
{MPI_Request requ;
for (i = 0; i < nn; i++)
{dn = *pnl++;
if (dn == -1)
{dn = PC_get_processor_number();
MPI_Bcast(bf, nb, MPI_CHAR, dn, MPI_COMM_WORLD);
break;}
else if (block)
{MPI_Send(bf, nb, MPI_CHAR, dn, ityp,
MPI_COMM_WORLD);
SFREE(bf);}
else
{MPI_Isend(bf, nb, MPI_CHAR, dn, ityp,
MPI_COMM_WORLD, &requ);
PC_push_pending(pp, PC_WRITE_MSG,
bf, type, ni, vr, &requ);};};};
#else
PC_unblock(pp);
while (PC_gets(reply, MAXLINE, pp) != NULL);
PC_block(pp);
if (nn == -1)
{for (dn = 0; dn < PC_n_nodes; dn++)
_PC_put_data(pp, bf, type, ni, nb, dn, &nib, types, &is);}
else
{for (i = 0; i < nn; i++)
{dn = *pnl++;
if (dn == PC_GROUP_LEADER)
dn = host_node;
_PC_put_data(pp, bf, type, ni, nb, dn, &nib, types, &is);};};
#endif
/* conditional diagnostic messages */
if (_PC_debug)
{fprintf(_PC_diag, " Sent(%ld,%s,%d)\n", nis, types, dn);
fflush(_PC_diag);};
return(nis);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_IN - read data in from the filtered list of nodes
* - this does a message passing system's RECV command
* - or a LINDA-like GET
* - return the number of items successfully read in
*/
long PC_in(vr, type, ni, pp, filt)
byte *vr;
char *type;
size_t ni;
PROCESS *pp;
int *filt;
{long nir, nb, nbt, nbr;
int ip, nis, ityp, nn, np, *nl, *pl;
int type_index, block, buf_siz, bs;
char reply[MAXLINE], types[MAXLINE], *bf, *pbf;
PDBfile *vif, *tf;
vif = pp->vif;
nir = ni;
_PC_extract_filter_info(filt,
&type_index, &ityp,
NULL, NULL, NULL,
&block, &buf_siz,
&nn, &nl, &np, &pl);
if (nl == NULL)
ip = 0;
else
ip = nl[1];
/* conditional diagnostic messages */
if (_PC_debug)
{fprintf(_PC_diag, " Read");
fflush(_PC_diag);};
/* get the buffer size and allocate it */
if (buf_siz > 0)
nb = buf_siz;
else
nb = PC_size_message(ip, type, ityp);
bf = FMAKE_N(char, nb, "PC_IN:bf");
#ifdef HAVE_MPI
if (block)
{MPI_Status stat;
MPI_Recv(bf, nb, MPI_CHAR, ip, ityp,
MPI_COMM_WORLD, &stat);}
else
{MPI_Request requ;
MPI_Irecv(bf, nb, MPI_CHAR, ip, ityp,
MPI_COMM_WORLD, &requ);
PC_push_pending(pp, PC_READ_MSG,
bf, type, ni, vr, &requ);};
#else
strcpy(types, type);
if (block)
PC_block(pp);
else
PC_unblock(pp);
pbf = bf;
nbr = nb;
for (nbr = nb, bs = FALSE; nbr > 0; pbf += nbt, bs = block)
{nbt = PC_buffer_data_out(pp, pbf, nbr, bs);
if (nbt > 0)
{if (_PC_debug)
{fprintf(_PC_diag, " Recv(%ld,%s)", nbt, types);
fflush(_PC_diag);};};
nbr -= nbt;
if (nbr > 0)
{PC_printf(pp, "%c,%s,%ld,%d\n", PC_FREAD,
SC_CHAR_S, nbr, block);
PC_gets(reply, MAXLINE, pp);
sscanf(reply, "%d,%s\n", &nis, types);
if (_PC_debug && block && (nis > 0))
{fprintf(_PC_diag, " Expect(%d,%s)", nis, types);
fflush(_PC_diag);};};
if (!block)
break;};
#endif
/* convert the message to the requested output data */
if (block)
{tf = PN_open(vif, bf);
nir = PN_read(tf, type, ni, vr);
PN_close(tf);
SFREE(bf);};
/* conditional diagnostic messages */
if (_PC_debug)
{if (nir > 0)
fprintf(_PC_diag, "\n");
else
fprintf(_PC_diag, " Nothing\n");
fflush(_PC_diag);};
return(nir);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_WAIT - wait for all pending communications to finish
* - complete the work, free the buffers, and
* - return the number of pendings honored
*/
long PC_wait(pp)
PROCESS *pp;
{int i, np, oper;
char *type, *bf;
byte *vr;
size_t ni;
PDBfile *vif, *tf;
vif = pp->vif;
np = pp->n_pending;
/* conditional diagnostic messages */
if (_PC_debug)
{fprintf(_PC_diag, " Wait");
fflush(_PC_diag);};
#ifdef HAVE_MPI
{MPI_Status *stats;
stats = FMAKE_N(MPI_Status, np, "PC_WAIT:stats");
MPI_Waitall(np, _PC_reqs, stats);
SFREE(stats);}
#endif
/* convert the message to the requested output data */
for (i = 0; i < np; i++)
{PC_pop_pending(pp, &oper, &bf, &type, &ni, &vr);
if (oper == PC_READ_MSG)
{tf = PN_open(vif, bf);
PN_read(tf, type, ni, vr);
PN_close(tf);};
SFREE(type);
SFREE(bf);};
/* conditional diagnostic messages */
if (_PC_debug)
fprintf(_PC_diag, "\n");
return(np);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_SYNC_EXECUTION - synchronize the execution of the tasks */
void PC_sync_execution()
{
return;}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_GET_NUMBER_PROCESSORS - return the number of processors in the
* - current simulation
*/
int PC_get_number_processors()
{int n;
n = SC_comm_size;
return(n);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* PC_GET_PROCESSOR_NUMBER - return the index of the current processor */
int PC_get_processor_number()
{int n;
n = SC_comm_rank;
return(n);}
/*--------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
|