home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / adaptor / dalib / pvm3 / barrier.c < prev    next >
C/C++ Source or Header  |  1993-04-06  |  3KB  |  79 lines

  1. /*******************************************************************
  2. *                                                                  *
  3. *  MODULE : barrier.c                                              *
  4. *                                                                  *
  5. *  Function: Realization of Barrier Synchronization for Nodes      *
  6. *                                                                  *
  7. *******************************************************************/
  8. #include "system.h"
  9.  
  10. /*******************************************************************
  11. *                                                                  *
  12. *  process_barrier: synchronization for all nodes                  *
  13. *                                                                  *
  14. *     - call only by node processes, not host                      *
  15. *                                                                  *
  16. *******************************************************************/
  17.  
  18. void process_barrier ()
  19.  
  20. /* Synchronization of all node processes */
  21.  
  22. /*   Communication Patterns
  23.  
  24.      0    1    2    3    4    5    6    7    8    9   10  
  25.             <-        <-        <-        <-        <-
  26.             <-------            <--------         
  27.             <-----------------
  28.             <-------------------------------------
  29.             ------------------------------------->
  30.             -----------------> 
  31.             ------->            -------->
  32.             ->        ->        ->        ->        <-
  33. */
  34.  
  35. { int steph, distance;
  36.   int i, n;
  37.   unsigned char *dummy_data;
  38.  
  39.   i = pcb.i ;
  40.  
  41.   n = pcb.p ;
  42.  
  43.   if (i==0)   /* host does not synchronize */
  44.     { 
  45.       return;
  46.     }
  47.  
  48.   /* barrier corresponds a reduction without function, and data
  49.      of length = 0                                              */
  50.   
  51.   distance = 1;
  52.   while (distance < n)    /* log n (base 2) loop */
  53.     { steph = 2*distance;
  54.       if ( ((i-1) % steph) == 0)
  55.          { /* if i+distance exists get a result and combine */
  56.            if ( (i+distance) <= n)
  57.              areceive (i,i+distance,dummy_data,0);
  58.          }
  59.       if ( ((i-1) % steph) == distance)
  60.          asend (i,i-distance,dummy_data,0);
  61.       distance = steph;
  62.     }
  63.  
  64.   /* send values back to all processors */
  65.  
  66.   while (distance > 1)
  67.     { steph = distance;
  68.       distance = distance / 2;
  69.       if ( ((i-1) % steph) == 0)
  70.          { /* if i+distance exists send the new result */
  71.            if ( (i+distance) <= n)
  72.              asend (i,i+distance,dummy_data,0);
  73.          }
  74.       if ( ((i-1) % steph) == distance)
  75.          areceive (i,i-distance,dummy_data,0);
  76.     }
  77. }
  78.  
  79.