home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / prospero / PRM / src / testprog / ringsim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-02  |  2.9 KB  |  107 lines

  1. /*
  2.  * Copyright (c) 1992, 1993 by the University of Southern California
  3.  *
  4.  * For copying and distribution information, please see the files
  5.  * <prm-copyr.h>.
  6.  *
  7.  * Written by srao 6/92
  8.  *
  9.  */
  10.  
  11. #include <prm-copyr.h>
  12.  
  13.  
  14. #include <stdio.h>
  15.  
  16. #define  MAIN_PROG
  17. #include <comm.h>   /*     This file defines certain constants, and declares
  18.             some global variables used by the message passing
  19.             routines.  */
  20.  
  21. #ifdef HPUX
  22. #   define srandom srand
  23. #   define random rand
  24. #endif
  25.  
  26. #ifndef RNEIGH
  27. #   define RNEIGH(x,tot)    (x<tot)?x+1:1 /* right neighbor of x in the ring */
  28. #endif
  29.  
  30. #define MAXTIME 25                        /* Maximum computation time in sec.*/
  31. #define INT_SZ sizeof(int)
  32. #define SENDTO_PORT 1                     /* Port_id on destination task */
  33. #define RCVON_PORT  1                     /* Port on which to rcv messages */
  34.  
  35. char *progname;
  36.  
  37. main(argc, argv)
  38. int argc;
  39. char **argv;
  40. {
  41.   int ntasks, my_tid;
  42.   int time_dcrmt, timeleft, iter_cnt;
  43.   int sendto_task;
  44.  
  45.   init_task(argv);    /* Initialization is required for all tasks in every
  46.              application */
  47.   pfs_debug=0;
  48.   my_tid = gettid(); 
  49.   if (my_tid == -1) {
  50.     io_printf(" task could not get its tid!", (char *)0);
  51.     exit(1);
  52.   }
  53.   
  54.   iter_cnt = 1; 
  55.   timeleft = MAXTIME;
  56.   ntasks = numtasks();   /* Total number of tasks in this job */
  57.   
  58.   sendto_task = RNEIGH(my_tid,ntasks);   /* My right neighbor in the ring */
  59.  
  60.   srandom(_my_jobid);
  61.  
  62.   time_dcrmt = random()&7+4;   /* Number of seconds for which computation is 
  63.                   performed */
  64.  
  65.   if (my_tid == 1) {           /* Task 1 begins sending message */
  66.     
  67.     sleep(time_dcrmt);         /* Simulate computation by sleeping */
  68.     
  69.     io_printf("**** completed iteraton %d.  ...sending message to task %d...", 
  70.         iter_cnt++, sendto_task, (char *)0);
  71.     
  72.     /* Send message to right neighbor. */ 
  73.     while (vsend (sendto_task, SENDTO_PORT, ANY_TAG, &time_dcrmt, INT_SZ) != SUCCESS)  
  74.       io_printf("vsend to task %d timed out!\n", sendto_task, (char *)0 );
  75.  
  76.     timeleft = timeleft - time_dcrmt;
  77.   }
  78.  
  79.   while(timeleft > 0) {
  80.     /* Receive message from left neighbor */
  81.     if ( vrecv(ANY_TASK, RCVON_PORT, ANY_TAG, &time_dcrmt, INT_SZ) == -1)
  82.       io_printf("%s", p_err_string);
  83.  
  84.     if(my_tid == 1) 
  85.       time_dcrmt = random()&7+4;   /* task 1 calculates the computation time */
  86.     
  87.     io_printf("received message from task %d", (my_tid==1 ? ntasks:(my_tid-1)), 
  88.         (char *)0 );
  89.     sleep(time_dcrmt);
  90.     io_printf("**** completed iteraton %d.  ....sending message to task %d...", 
  91.         iter_cnt++, sendto_task, 0 );
  92.     
  93.     while ( vsend(sendto_task, SENDTO_PORT, ANY_TAG, &time_dcrmt, INT_SZ) != SUCCESS) 
  94.       io_printf("vsend to task %d timed out!\n", sendto_task, (char *)0 );
  95.     
  96.     timeleft = timeleft - time_dcrmt;
  97.   } /* while */
  98.   
  99.   if (my_tid == 1)    /* Task 1 terminates after receiving last message */
  100.     vrecv(ANY_TASK, RCVON_PORT, ANY_TAG, &time_dcrmt, INT_SZ);
  101.  
  102.   io_printf(" done.\n", my_tid, (char *)0 );
  103.   
  104.   exit(0);
  105. }
  106.  
  107.