<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>CFGReader</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></span>
<b>Prefix sum</b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is programming assignment of comp346.</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <p ALIGN="LEFT"><b>What is &quot;prefix sum&quot;? Try to imagine that we have n numbers 
  and I want to know the sum of first 1,</b></div>
<div align="left">
  <p ALIGN="LEFT"><b>the sum of first 2, the sum of first 3... the sum of first 
  n. How to calculate?</b></div>
<div align="left">
  <p ALIGN="LEFT">กก</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  <pre><b>A kind of repetition  of previous assignment. Basically you need only calculate the sum AFTER your previous </b></pre>
</div>
<div align="left">
  <pre><b>neighbor finishes it. So, you simply declare n threads and each them just sum up its own value with its previous</b></pre>
</div>
<div align="left">
  <pre><b>neighbor's value. </b></pre>
</div>
<div align="left">
  <pre>And please see the following which is the PA1 model which only requires THREE sum operations. I agree that in multi-
processor-situation PA2 has some advantages such that each thread don't have to wait each other at each LEVEL. But the
total number of sum operation is also a factor when considering the efficiency of algorithms.  
 

          col0       col1                 col2                    col3
level 0     a0         a1                  a2                       a3
level 1     a0       a01=a0+a1             a2                       a3  
level 2     a0       a01               a012=a01+a2                  a3
level 3     a0       a01                 a012                      a0123=a012+a3
 </pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>1<span lang="en-ca">. </span>Cyclic<span lang="en-ca">.</span>java</b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: </b></font></span><font size="3" color="#FF0000"><b>Cyclic.java</b></font></pre>
</div>
<pre>public class Cyclic {
  static int P         = 8;                         // number of workers
 // static int logP      = 3;
 // static int Pplus1    = 9;                         // 0-indexed array
 // static int logPplus1 = 4;                         // lengths

  static Daemon        daemon = new Daemon ();
  static Semaphore     future[] = new Semaphore[P];
  static int    a[][]   = new int[2][P];
//  static int    a[logP]   = new int[P];
//  static ...
//  static ...

  public static void main (String[] args) {
    int col;	
    Worker w[] = new Worker[P];

    for( int j=0; j&lt;P; j++ )
    {
	int temp=0;
	if (j==0)
	{	
		temp=1;
	}
	future[j]  = new Semaphore(temp);
   }	
    for( int j=0; j&lt;P; j++ ) {
      col = daemon.column (j);
      w[col] = new Worker (col);
      w[col].start ();
    }

    for( int j=0; j&lt;P; j++ ) {                      // wait for workers
      try { w[j].join (); }                         // to terminate
      catch (InterruptedException exc) { };
    }
   for( int j=0; j&lt;P; j++ )
   {
       System.out.println (&quot;Sum &quot; + (j+1)+ &quot; = &quot;+a[1][j]);
    }	    
    System.out.println (&quot;System terminates normally.&quot;);
  }
}

class Daemon {
  private static int[] w = new int[] {3, 6, 5, 7, 4, 2, 1, 0};
  private static int[] d = new int[] {0, 4, 4, 4, 4, 4, 4, 4, 4};

  public int column (int j) {
    return w[j];
  }

  public void interrupt (int j) {
    if ( d[j] &gt; 4 )
      Thread.yield ();
  }
}

class Semaphore {
  private int value;
  Semaphore (int value1) {
    value = value1;
  }

  public synchronized void Wait () {
    while( value &lt;= 0 ) {
      try { wait (); }
      catch (InterruptedException e) { };
    }
    value--;
  }

  public synchronized void Signal () {
    ++value;
    notify ();
  }
}

class Worker extends Thread {
  private int j;
  private int sum;
  private int hop = 1;
  Worker (int col) {
    j   = col;
    sum = j+1;
  }

  public void run () {
    System.out.println (&quot;Worker &quot; + (j+1) + &quot; begins execution.&quot;);
    yield ();
    
    for (int level=0; level&lt;2; level++) 
    {
	if (level==0)
	{
      		System.out.println (&quot;Worker &quot; + (j+1) + &quot; defines a[&quot; + level + &quot;,&quot; + j +&quot;].&quot;);
		Cyclic.a[level][j] = sum;
		System.out.println(&quot;a[&quot;+level+&quot;][&quot;+j+&quot;]=&quot;+Cyclic.a[level][j]);
	}
	else
	{	  
		if (j&gt;0)
		{	
			Cyclic.future[j].Wait();		
	        	System.out.println (&quot;Worker &quot; + (j+1) + &quot; uses    a[&quot; + level + &quot;,&quot;
                           + j +&quot;].&quot;);
	        	sum += Cyclic.a[level][j-1];
			//System.out.println(&quot;Worker &quot; +(j+1)+ &quot; has sum of &quot; + sum);
		}
		
		Cyclic.a[level][j]=sum;
		if (j&lt;(Cyclic.P-1))
		{
			Cyclic.future[j+1].Signal();	
		}
      	};  
	Cyclic.daemon.interrupt(j);   	
      
    };					

    System.out.println (&quot;Worker &quot; + (j+1) + &quot; terminates.&quot;);
  }
}


</pre>
<pre>

</pre>
<pre><font color="#0000FF"><b><span lang="en-ca">The </span>running result<span lang="en-ca"> is something like following:</span></b></font></pre>

<p>[qingz_hu@alpha A2] % java Cyclic<br>
Worker 4 begins execution.<br>
Worker 7 begins execution.<br>
Worker 6 begins execution.<br>
Worker 8 begins execution.<br>
Worker 5 begins execution.<br>
Worker 3 begins execution.<br>
Worker 2 begins execution.<br>
Worker 1 begins execution.<br>
Worker 4 defines a[0,3].<br>
a[0][3]=4<br>
Worker 7 defines a[0,6].<br>
a[0][6]=7<br>
Worker 6 defines a[0,5].<br>
a[0][5]=6<br>
Worker 8 defines a[0,7].<br>
a[0][7]=8<br>
Worker 5 defines a[0,4].<br>
a[0][4]=5<br>
Worker 3 defines a[0,2].<br>
a[0][2]=3<br>
Worker 2 defines a[0,1].<br>
a[0][1]=2<br>
Worker 1 defines a[0,0].<br>
a[0][0]=1<br>
Worker 1 terminates.<br>
Worker 2 uses a[1,1].<br>
Worker 2 terminates.<br>
Worker 3 uses a[1,2].<br>
Worker 3 terminates.<br>
Worker 4 uses a[1,3].<br>
Worker 4 terminates.<br>
Worker 5 uses a[1,4].<br>
Worker 5 terminates.<br>
Worker 6 uses a[1,5].<br>
Worker 6 terminates.<br>
Worker 7 uses a[1,6].<br>
Worker 7 terminates.<br>
Worker 8 uses a[1,7].<br>
Worker 8 terminates.<br>
Sum 1 = 1<br>
Sum 2 = 3<br>
Sum 3 = 6<br>
Sum 4 = 10<br>
Sum 5 = 15<br>
Sum 6 = 21<br>
Sum 7 = 28<br>
Sum 8 = 36<br>
System terminates normally.<br>
[qingz_hu@alpha A2] % <br>
กก</p>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>