next up previous contents
Next: Structured Dagger Up: Intra-component Coordination Previous: Motivating Example   Contents


Threaded Coordination

{CodeTwo}
void compute_thread(int idx1, int idx2)
{
  getPatch(idx1);
  getPatch(idx2);
  threadId[0] = createThread(recvFirst);
  threadId[1] = createThread(recvSecond);
  threadJoin(2, threadId);
  computeInteractions(first, second);
}
void recvFirst(void)
{
  recvPatch(first, FIRST_TAG);
  filter(first);
}
void recvSecond(void)
{
  recvPatch(second, SECOND_TAG);
  filter(second);
}

Figure 4.5: Multi-threaded Implementation of ``Compute-Object'' in Molecular dynamics
\begin{figure}\centering \fbox{\BUseVerbatim{CodeTwo}} \end{figure}

Contrast the compute-object example in Figure 4.3 with a thread-based implementation of the same scheme in Figure 4.5. Functions getFirst, and getSecond send messages asynchronously to the PatchManager, requesting that the specified patches be sent to them, and return immediately. Since these messages with patches could arrive in any order, two threads recvFirst, and recvSecond are created. These threads block waiting for messages to arrive. After each message arrives, each thread performs the filtering operation. The main thread waits for these two threads to complete, and then computes the pairwise interactions. The threaded implementation eliminates the programming complexity of buffering the messages and maintaining the counters. However, this implementation has added costs of thread creation, context switching, and synchronization.



Milind Bhandarkar 2002-06-12