?? reduction.java.bak
字號:
/* Where there are no comments below, insert them Such comments should explain the purpose of important aspects of the code, and document classes, methods etc. They should communicate useful information, not merely state the obvious (e.g." i:=i+1 // add 1 to i" will lose marks).*/public class Reduction{ CubbyHole [] storage; // data parallel "machine" "pvar" int inArrayLength; // length of intput array public BarrierSynch waiting; // object for barrier synchronisation. NodeThread 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 Reduce(ApplyObj applic,Object[] inArray){ inArrayLength = inArray.length; // get the length from input array this.applic = applic; storage = new CubbyHole[inArrayLength]; initializeStorage(inArray); number_of_iters = (int) Math.ceil(Math.log( (double)inArrayLength)/ Math.log(2.0));// how many times the iterations . for example , 6 element shuold hace 3 iteration // 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++) { System.out.println( "reduction result " + storage[j].get()); }// display the differernt vaule of the array for each step System.out.println("************************"+"STEP " + i); startThreads(); // start DP step waiting.iveArrived(999); // Reduce enters barrier }; // end for/* QUESTION 1: Reduce, and all the threads, participate in the barrier. The program may behave incorrectly if only the threads use the barrier. Explain why. Write your answer here:ANSWER:In this code, Reduce is a supervisor thread and the startThread () creates the threads.In the first step, this method creates the old threads. These threads compute andupdate the data. After that the supervisor (Reduce) should make sure that all threadsalready finished their work. Then, new threads can use the data that already updatedby old threads for next computation. Thus, we must ensure that both threads andReduce must participate the barrier. Just threats participating the barrier not supervisorwill cause problems.For instance, some old threads do not finished their work and thenew threads try to access the data that are still processing by the old threads. Thus,the data are corrupted and the result may be wrong.*/ System.out.println("Finished -- what a relief"); return storage[0].get();// display the finall result } /* ========================================================== 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]); } }/* QUESTION 2: What would be the effect on the behaviour of your data parallel emulation if the methods of class CubbyHole were not synchronized? Explain briefly in a few lines inserted here:ANSWER:Class CubbyHole must employ synchronized. Synchronized just like asemaphore can avoid the race condition and protect the critical section.For example, if a thread needs to "get" an object in CubbyHole, then theobject will be locked. No other thread can access this object until thisthread finish to use this object. If class CubbyHole does not invoke thesynchronized mechanism, then every thread can access the critical sectionin same time, which is a problem just like writer and reader problem.For example, one thread read a value of an object is 100, at the same timeanother thread write this value to 0. So, to avoid this situation, the class CubbyHolemust employ the synchronized mechanism.*/ /* ============================================================== 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 NodeThread(this,i); nthread.start(); } }}class NodeThread extends Thread{ int myId; BarrierSynch myBarrier; CubbyHole myCubby; Reduction myParent; ApplyObj myApplic; NodeThread(Reduction 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 reduction 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> // this is the code executed by individual "processors" in a // data parallel step: remember to determine active/inactive threads // The code below this line needs to be modified... // at the moment it does almost nothing. int step; step=(int)Math.pow((double)2,((double)myParent.i-1));// myParent.i is the number of step // if(((myId%((int)Math.pow((double)2,(double)myParent.i))))==0){ if((myId+step)<myParent.inArrayLength){ myCubby.put(myApplic.f(myCubby.get(),myParent.storage[myId+step].get())); } } System.out.println("Hello there from thread: " + myId); myBarrier.iveArrived(myId); // Theads entry the barrier System.out.println("Thread finished " + myId); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -