?? scanner.java
字號:
/** * <p>Title: Scanner * <p>Description: mulit reduction * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author :Tong Yuxin * @Student No. a1103993 */public class Scanner { CubbyHole [] storage; // data parallel "machine" "pvar" CubbyHole [] temp; int inArrayLength; // length of intput array public BarrierSynch waiting; // object for barrier synchronisation. ScanThread nthread; // temp var for starting threads ApplyObj applic; // the function passed in as a parameter int number_of_iters; // total number of iterations int i,j; /* ========================================================== Main Method.. initialize; start up threads for DP step ========================================================== */ public Object[] Scan(ApplyObj applic,Object[] inArray){ inArrayLength = inArray.length; this.applic = applic; storage = new CubbyHole[inArrayLength]; temp = new CubbyHole[inArrayLength]; initializeStorage(inArray); number_of_iters = (int) Math.ceil(Math.log( (double)inArrayLength)/ Math.log(2.0)); // sample tracing code -- outputs "storage" for (j=0; j< inArrayLength; j++) { System.out.println( "^^ " + storage[j].get()); };/* DATA PARALLEL STEPS -- make sure code for a step is complete*/ // this does only one DP step -- edit the loop control appropriately for (i=1; i <= number_of_iters; i++){ waiting = new BarrierSynch(inArrayLength+1 ); // create barrier System.out.println("STEP " + i); // trace: which DP step? for (j=0; j< inArrayLength; j++) { temp[j].put(storage[j].get()); }; System.out.println( "*********************************************"); for (j=0; j< inArrayLength; j++) { System.out.println( "temp " + temp[j].get()+" "+"storage " + storage[j].get()); }// display the differernt vaule of the array for each step System.out.println( "******************************************"); startThreads(); // start DP step waiting.iveArrived(999); // Scan enters barrier }; // end for // sample tracing code -- outputs "storage" Object[] outArray = new Object[inArrayLength]; for (i=0; i< inArrayLength; i++) { outArray[i] = (Object) storage[i].get(); //System.out.println( "^^" + outArray[i]); } return outArray; //return storage[inArrayLength-1].get(); } /* ========================================================== Fill up CubbyHoles with data from inArray ========================================================== */ private void initializeStorage(Object[] inArray){ int i; for (i=0;i<inArrayLength;i++){ storage[i] = new CubbyHole(); (storage[i]).put(inArray[i]); temp[i] = new CubbyHole(); (temp[i]).put(inArray[i]); } } /* ============================================================== Start up threads with a pointer to this object plus a thread number. ============================================================== */ private void startThreads(){ int i = 0; for (i=0;i < inArrayLength; i++){ nthread = new ScanThread(this,i); nthread.start(); } }class ScanThread extends Thread{ int myId; BarrierSynch myBarrier; CubbyHole myCubby; Scanner myParent; ApplyObj myApplic; ScanThread(Scanner parent,int id){ myId = id; myBarrier = parent.waiting; myCubby = parent.storage[id]; myParent = parent; myApplic = parent.applic; } /* ================================================================== The run method is invoked by running start on each thread. Defining the code for this method is much of the work in your project. In this project run will implement the code for the part each thread plays in the partial multiplications process. This will generally involve using Myapplic and writing to myCubby and other CubbyHoles. Barrier synchronizations is needed to provide end-of-step synch. Each thread will have a different role according to its thread number. ================================================================== */ public void run(){ // <insert code here> //create a interval step for reduce threads int step=(int)Math.pow((double)2,((double)myParent.i-1));// myParent.i is the number of step //decide whether or not the reduce threads have been already finished if(myId<myParent.inArrayLength-step){ { myParent.storage[myId+step].put(myApplic.f(myParent.temp[myId].get(),myParent.temp[myId+step].get())); } } System.out.println("Hello there from thread: " + myId); System.out.println("Thread finished " + myId); myBarrier.iveArrived(myId);// Theads entry the barrier } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -