CSC/ECE 506 Spring 2012/ch5a ja: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 155: Line 155:
== Graphs ==
== Graphs ==
=== Graph Intro ===
=== Graph Intro ===
A graph data structure is another type of linked-list structure that focuses on data that requires linking according to various types of relationships.  For example, in a networking application, one network node may have connections to a variety of other network nodes.  These nodes then also link to a variety of other nodes in the network.  Using this connection of nodes, it would be possible to then find a path from one specific node to another in the chain.  This could be accomplished by having each node contain a linked list of pointers to all other reachable nodes.
=== Serial Code Example ===
=== Serial Code Example ===
=== Parallel Code Solution ===
=== Parallel Code Solution ===
Line 161: Line 164:
http://oreilly.com/catalog/masteralgoc/chapter/ch08.pdf
http://oreilly.com/catalog/masteralgoc/chapter/ch08.pdf
http://www.devjavasoft.org/code/classhashtable.html
http://www.devjavasoft.org/code/classhashtable.html
http://osr600doc.sco.com/en/SDK_c++/_Intro_graph.html

Revision as of 00:29, 27 February 2012

Chapter 5a CSC/ECE 506 Spring 2012 / ch5a

An exploration and summary of concurrency issues as it relates to linked-list based data structures such as hash tables, trees, and graphs. This topic examines concurrency problems related to each type and possible solutions to allow for parallelization.

Introduction to Linked-List Parallel Programming

In determining opportunities for parallel programming in your software design, many programmers will focus exclusively on loop or array structures, rather than also considering the possibilities that linked-list pointer-based structures could also provide.

For example, one parallel technique used in array processing is the copy-scan technique. Take the following grid:

The basic process for copy-scan would be to:

 a) Copy row 1 to row 2.
 b) Copy row 1 to row 3, row 2 to row 4, etc on the next run.
 c) Continue in this manner until all rows have been copied in a log(n) fashion.

Similarly, in the linked list world, there is the concept of pointer doubling.

 a) Each processor will make a copy of the pointer it holds to it's neighbor.
 b) Next, each processor will make a pointer to the processor 2 steps away.
 c) This continues in logarithmic fashion until each processor has a pointer to the end of the chain.

In this chapter, we will explore 3 linked-list based data structures and the concurrency issues they present: hash tables, trees, and graphs.

Linked Data Structure Conflicts

Insertions

Deletions

Search

Linked Data Structures

Trees

Tree Intro

Serial Code Example

Parallel Code Solution

Hash Tables

Hash Table Intro

Hash tables are very efficient data structures often used in searching algorithms for fast lookup operations. They consist essentially of an array that can be accessed by a special key value. Behind the scenes, a hash table is a mapping tied to a special hash function that is responsible for mapping these key values to positions in the array.

The major advantage of a hash table is that lookup times are essentially a constant value since lookups are done using the key value. With a proper hashing function in place, it should be fairly rare that 2 keys would generate the same values in the hash function.

In the case that 2 keys do map to the same position, there is a conflict that must be dealt with in some fashion to obtain the correct value. One way that is relevant to linked list structures is to have a chained hash table in which a linked list is created with all values that have been placed in that particular bucket.

Serial Code Example

 /**
  *  Put a key/value pair into the table by hashing the key to get the array
  *  index of the chain that should hold the node containing the pair.
  *  If an existing node with the same key is present then overwrite its value,
  *  otherwise add a new node.
  *
  *@param  key  key object reference
  *@param  val  data object reference
  */
 public void put(Object key, Object val)
 {
   int index = getIndex(key);
   Node node = find(key, data[index]);
   // Use helper method to determine if node with same key is already in
   // chain. If not, add a new node to head of chain.
   if (node == null)
   {
     data[index] = new Node(data[index], key, val);
   }
   else
   {
     // Otherwise update data value of existing node.
     node.val = val;
   }
 }
 /**
  *  Given a key object, return a data object from the table or
  *  null if it is not found.
  *
  *@param  key  key object reference
  *@return      data object reference or null if no object found
  */
 public Object get(Object key)
 {
   Node temp = find(key, data[getIndex(key)]);
   if (temp != null)
   {
     return temp.val;
   }
   else
   {
     return null;
   }
 }
 /**
  * Remove a key/value pair from table if present, otherwise make no change.
  *
  *@param  key  key object reference
  */
 public void remove(Object key)
 {
   int index = getIndex(key);
   Node ref = data[index];
   Node previous = null;
   while (ref != null)
   {
     if ((ref.key).equals(key))
     {
       if (previous == null)
       {
         data[index] = ref.next;
       }
       else
       {
         previous.next = ref.next;
       }
       return;
     }
     previous = ref;
     ref = ref.next;
   }
 }
 /**
  *  Given a key, use a hash function to obtain the array index of the chain
  *  corresponding to the key.
  *  The hashCode method inherited (and possibly overridden)
  *  from class Object is called to do the hashing, with the returned
  *  value constrained to the hash table array bounds.
  *  The distribution of objects in the hash table will depend
  *  on the quality of the hash function implemented by hashCode.
  *
  *@param  key  reference to object to be hashed
  *@return      array index of chain corresponding to key
  */
 private int getIndex(Object key)
 {
   return Math.abs(key.hashCode() % data.length);
 }
 /**
  *  Find node given a key and a chain to search.
  *
  *@param  key    key object reference
  *@param  ref    node object reference where search will start
  *@return        node object holding key or null if not found
  */
 private Node find(Object key, Node ref)
 {
   while (ref != null)
   {
     if ((ref.key).equals(key))
     {
       return ref;
     }
     ref = ref.next;
   }
   return null;
 }

Parallel Code Solution

Graphs

Graph Intro

A graph data structure is another type of linked-list structure that focuses on data that requires linking according to various types of relationships. For example, in a networking application, one network node may have connections to a variety of other network nodes. These nodes then also link to a variety of other nodes in the network. Using this connection of nodes, it would be possible to then find a path from one specific node to another in the chain. This could be accomplished by having each node contain a linked list of pointers to all other reachable nodes.

Serial Code Example

Parallel Code Solution

Quiz

References

http://oreilly.com/catalog/masteralgoc/chapter/ch08.pdf http://www.devjavasoft.org/code/classhashtable.html http://osr600doc.sco.com/en/SDK_c++/_Intro_graph.html