Exemplo n.º 1
0
static void Hello()
/*
  Everyone says hi to everyone else
*/
{
    char buf[30];
    long lenbuf = sizeof buf;
    long type=19 | MSGCHR;
    long node, kode, nodefrom, lenmes;
    long sync = 1;

    if (NODEID_() == 0) {
        (void) printf("Hello test ... show network integrity\n----------\n\n");
        (void) fflush(stdout);
    }

    for (node = 0; node<NNODES_(); node++) {
        if (node == NODEID_()) {
            for (kode = 0; kode<NNODES_(); kode++) {
                (void) sprintf(buf, "Hello to %ld from %ld", (long)kode, (long)NODEID_());
                if (node != kode)
                    SND_(&type, buf, &lenbuf, &kode, &sync);
            }
        }
        else {
            RCV_(&type, buf, &lenbuf, &lenmes, &node, &nodefrom, &sync);
            (void) printf("me=%ld, from=%ld: %s\n",(long)NODEID_(), (long)node, buf);
            (void) fflush(stdout);
        }
    }

}
Exemplo n.º 2
0
static void TestProbe()
/*
  Process 0 sleeps for 20 seconds and then sends each
  process a message.  The other processes sleep for periods
  of 2 seconds and probes until it gets a message.  All processes
  respond to process 0 which recieves using a wildcard probe.
  */
{
    long type_syn = 32;
    long type_msg = 33;
    long type_ack = 34;
    long me = NODEID_();
    char buf;
    long lenbuf = sizeof buf;
    long sync = 1;


    if (me == 0) {
        (void) printf("Probe test ... processes should sleep for 20s only\n");
        (void) printf("----------\n\n");
        (void) fflush(stdout);
    }

    SYNCH_(&type_syn);

    if (me == 0) {
        long nproc = NNODES_();
        long anyone = -1;
        long ngot = 0;
        long node;

        (void) sleep((unsigned) 20);

        for (node=1; node<nproc; node++) {
            SND_(&type_msg, &buf, &lenbuf, &node, &sync);
            (void) printf("    Sent message to %ld\n", (long)node);
            (void) fflush(stdout);
        }

        while (ngot < (nproc-1))
            if (PROBE_(&type_ack, &anyone)) {
                RCV_(&type_ack, &buf, &lenbuf, &lenbuf, &anyone, &node, &sync);
                (void) printf("    Got response from %ld\n", (long)node);
                (void) fflush(stdout);
                ngot++;
            }
    }
    else {
        long node = 0;
        while (!PROBE_(&type_msg, &node)) {
            (void) printf("    Node %ld sleeping\n", (long)me);
            (void) fflush(stdout);
            (void) sleep((unsigned) 2);
        }
        RCV_(&type_msg, &buf, &lenbuf, &lenbuf, &node, &node, &sync);
        SND_(&type_ack, &buf, &lenbuf, &node, &sync);
    }

    SYNCH_(&type_syn);
}
Exemplo n.º 3
0
void Setup(ArgStruct *p)
{
    long nprocs;

    nprocs      = NNODES_();
    p->prot.nid = NODEID_();
    {
        char s[255];
        gethostname(s,253);
        printf("%d: %s\n",p->prot.nid,s); fflush(stdout);
    }

    if (nprocs < 2)
    {
        printf("Need at least two processes, we have %d\n", nprocs); fflush(stdout);
        exit(-2);
    }

    p->tr = p->rcv = 0;
    if (p->prot.nid == 0) {
        p->tr = 1;
        p->prot.nbor = nprocs-1;
    } else if( p->prot.nid == nprocs-1 ) {
        p->rcv = 1;
        p->prot.nbor = 0;
    }
}
Exemplo n.º 4
0
/**
 * Flush as many messages as possible without blocking
 * from all of the send q's.
 */
void flush_send_q()
{
    long node;
    long nproc = NNODES_();

    for (node=0; node<nproc; node++)
        if (TCGMSG_proc_info[node].sendq)
            flush_send_q_node(node);
}    
Exemplo n.º 5
0
/**
 * Given an id from NextMsgID extract the node
 */
static long NodeFromMsgID(long msgid)
{
    long node = msgid >> 20;

    if (node < 0 || node > NNODES_())
        Error("NodeFromMsgID: invalid msgid", msgid);

    return node;
}
Exemplo n.º 6
0
static long NodeFromMsgID(long msgid)
/*
  Given an id from NextMsgID extract the node
*/
{
  long node = msgid >> 20;

  if (node < 0 || node > NNODES_())
    Error("NodeFromMsgID: invalid msgid", msgid);

  return node;
}
Exemplo n.º 7
0
void flush_send_q()
/*
  Flush as many messages as possible without blocking
  from all of the send q's.
*/
{
  long node;
  long nproc = NNODES_();

  for (node=0; node<nproc; node++)
    if (TCGMSG_proc_info[node].sendq)
      flush_send_q_node(node);
}    
Exemplo n.º 8
0
static void TestGlobals()
{
#define MAXLENG 256*1024
    double *dtest;
    long *itest;
    long len;
    long me = NODEID_(), nproc = NNODES_(), from=NNODES_()-1;
    long itype=3+MSGINT, dtype=4+MSGDBL;

    if (me == 0) {
        (void) printf("Global test ... test brodcast, igop and dgop\n----------\n\n");
        (void) fflush(stdout);
    }

    if (!(dtest = (double *) malloc((unsigned) (MAXLENG*sizeof(double)))))
        Error("TestGlobals: failed to allocated dtest", (long) MAXLENG);
    if (!(itest = (long *) malloc((unsigned) (MAXLENG*sizeof(long)))))
        Error("TestGlobals: failed to allocated itest", (long) MAXLENG);

    for (len=1; len<MAXLENG; len*=2) {
        long ilen = len*sizeof(long);
        long dlen = len*sizeof(double);
        long i;

        if (me == 0) {
            printf("Test length = %d ... ", len);
            fflush(stdout);
        }

        /* Test broadcast */

        if (me == (nproc-1)) {
            for (i=0; i<len; i++) {
                itest[i] = i;
                dtest[i] = (double) itest[i];
            }
        }
        else {
            for (i=0; i<len; i++) {
                itest[i] = 0;
                dtest[i] = 0.0;
            }
        }
        BRDCST_(&itype, (char *) itest, &ilen, &from);
        BRDCST_(&dtype, (char *) dtest, &dlen, &from);

        for (i=0; i<len; i++)
            if (itest[i] != i || dtest[i] != (double) i)
                Error("TestGlobal: broadcast failed", (long) i);

        if (me == 0) {
            printf("broadcast OK ...");
            fflush(stdout);
        }

        /* Test global sum */

        for (i=0; i<len; i++) {
            itest[i] = i*me;
            dtest[i] = (double) itest[i];
        }

        IGOP_(&itype, itest, &len, "+");
        DGOP_(&dtype, dtest, &len, "+");

        for (i=0; i<len; i++) {
            long iresult = i*nproc*(nproc-1)/2;
            if (itest[i] != iresult || dtest[i] != (double) iresult) {
                printf(" dt %f it %ld ir %ld \n",dtest[i],itest[i],iresult);
                Error("TestGlobals: global sum failed", (long) i);
            }
        }

        if (me == 0) {
            printf("global sums OK\n");
            fflush(stdout);
        }
    }

    free((char *) itest);
    free((char *) dtest);
}
Exemplo n.º 9
0
void NextValueServer()
/*
  This runs as process SR_n_proc and provides load balancing service.

*/	   
{
  long cnt     = 0;            /* actual counter */
  long lencnt  = sizeof cnt;   /* length of cnt */
  long ndone   = 0;            /* no. finished for this loop */
  long ntermin = 0;            /* no. terminated so far (pend) */
  long node    = -1;           /* select any node */
  long type    = TYPE_NXTVAL;  /* message type */
  long buf[2];                 /* buffer to get values */
  long lenbuf  = sizeof buf;   /* length of buffer */
  long mproc;                  /* no. of processes running loop */
  long nval;                   /* no. of values requested */
  long done_list[MAX_PROCESS]; /* list of processes finished with this loop */
  long sync = 1;               /* all info goes synchronously */
  long on=0;
  long lenmes, nodefrom;

  SR_exit_on_error = FALSE; /* Want to return no matter what */

  if (setjmp(SR_jmp_buf)) {  /* Error should long jump to here */
/*    (void) printf("Error long jumped to NXTVAL ... returning.\n"); */
    SR_exit_on_error = TRUE;
    return;
  }

  SETDBG_(&on);

  while (1) {

    /* Wait for input from any node */
    
    RCV_(&type, (char *) buf, &lenbuf, &lenmes, &node, &nodefrom, &sync);

    if (lenmes != lenbuf) {
      Error("NextValueServer: lenmes != lenbuf", lenmes);
      return;   /* Never actually gets here as does long jump */
    }

    mproc = buf[0];
    nval = buf[1];
    if (DEBUG_)
      (void) printf("NVS: from=%ld, mproc=%ld, ndone=%ld, ntermin=%ld\n",
		    nodefrom, mproc, ndone, ntermin);

    if (mproc == 0) {

      /* Sending process is about to terminate. Send reply and disable
	 sending to him. If all processes have finished return. 

         Modified so that all processes block on waiting for message
         from nxtval server before terminating. nxtval only lets
         everyone go when all have registered termination. 
	 This is so that processes do not close their sockets
	 while another process is doing a RCV from any node (which
	 results in an unavoidable error condition). */

      if (++ntermin == NNODES_()) {
	(void) signal(SIGCHLD, SIG_DFL); /* Will be dying naturally */
	for (node=0; node<NNODES_(); node++) {
	  SND_(&type, (char *) &cnt, &lencnt, &node, &sync);
	  (void) close(SR_proc_info[node].sock);
	  SR_proc_info[node].sock = -1;
	}
	return;
      }
    }
    else if (mproc > 0) {
      
      /* This is what we are here for */

      SND_(&type, (char *) &cnt, &lencnt, &nodefrom, &sync);
      cnt += nval;
    }
    else if (mproc < 0) {

      /* This process has finished the loop. Wait until all mproc
	 processes have finished before releasing it */

      done_list[ndone++] = nodefrom;

      if (ndone == -mproc) {
	while (ndone--) {
	  nodefrom = done_list[ndone];
	  SND_(&type, (char *) &cnt, &lencnt, &nodefrom, &sync);
	}
	cnt = 0;
	ndone = 0;
      }
    }
  }
}
Exemplo n.º 10
0
long tcg_nnodes()
{
    return NNODES_();
}