Lecture 13

Review of Gravitational N-Body Problem (it was rushed last time)

   idea of problem

   sequential program -- transparency of Figure 11.9

   parallelizing the program
      replicate work
      divide up the work -- exploit symmetry of forces

   allocation strategies -- using simple exmaple of 9 bodies and 3 workers
      blocks
      stripes
      reverse stripes

   program for stripes -- transparency of Figure 11.11

   hierarchical methods
      Barnes-Hut idea; see text and Web for details

   hand out programming assignment; read it and come with questions



Big Picture -- Preview

   we have looked at basics of concurrency, synchronization
   using busy waiting, and concepts/applications of parallel programming

   now we will begin examining useful synchronization methods and
   programming techniques for multithreaded programming


Semaphores

   history:  THE system; Dijkstra -- 1968

   basic idea:  come from train semaphores to signal whether track is free

   definition:   sem s := 0   # or some nonnegative value

           P(s):  << await (s>0) s = s-1; >>

           V(s):  << s = s+1; >>

           what P and V stand for

      talked briefly about busy-waiting implementation; kernel implementation to come


Basic Uses of Semaphores -- Section 4.2

   (1) critical sections -- mutual exclusion

           sem mutex = 1;   # initial value 1 to indicate CS is free

           P(mutex);
           critical section
           V(mutex);

   (2) barriers -- signaling

           sem arrive[1:n] = ([n] 0);   # initially zeros to indicate that
                                        # conditions have not yet occurred


           P[i]::  V(arrive[i]);    # signal I am here
                   P(arrive[j]);    # wait for partner

           P[j]:   V(arrive[j]);    # signal I am here
                   P(arrive[i]);    # wait for partner

           combine instances of these to form a dissemination or
           butterfly structure

           don't have to worry about reset or flag synchronization principles;
           the semantics of semaphores take care of these problems for you

   (3) producer/consumer -- split binary semaphore

            recall the problem

            sem empty = 1, full = 0

            Producer:  P(empty); deposit; V(full);

            Consumer:  P(full); fetch; V(empty);

            definition of split binary semaphore
            key property:  if they are used as above (every P is followed
            by a V) then have mutual exclusion between a P and next V

   (4) resource counting -- bounded buffer

          generalize producer/consumer solution

          what changes:  representation of buffer and initial value of empty
          that's all!

          give code; it works for 1 producer and 1 consumer

          what about multiple producers?  multiple consumers?  both?
             just need to add mutex semaphores:  mutexP and/or mutexC