⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aspglobal.java

📁 collective processing environment for mobile devices. It is an environment for mobile device to solv
💻 JAVA
字号:

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.Serializable;

import com.ibm.dthreads.DThread;
import com.ibm.dthreads.Logger;
import com.ibm.dthreads.MP.ObjectMP;


//Application class has to extend DThread or implement DRunnable
public class ASPGlobal extends DThread{
	
	int[][] data;
	int low;
	int high;
	int size;
	int extra;
	int id;
	int total;
	int noofvertices;
	
//Constructor that initializes the array
	ASPGlobal(int noofvertices)
	{
		this.noofvertices = noofvertices;
		data = new int[noofvertices][noofvertices];
		for(int i=0;i<noofvertices;i++)
		{
		  for(int j=0;j<noofvertices;j++)
		  {
			data[i][j]=9999;
			if(i==j) data[i][j]=0;
		  }
		} 
	}

	
//Extends the run() method of the DThread class
	
	public void run()
	{
	  try
	  {
		long start,end;
		int totalthreads;
				
		start=System.currentTimeMillis();
		int rank=DThread.getContext().getIdentity();
		totalthreads =DThread.getContext().getNumberOfThreads();
		ObjectMP messagepasser = DThread.getContext().getMP();
		id=rank;
		int size=noofvertices/totalthreads;
		int extra=(noofvertices%totalthreads);			
		low=id*size;
		high=low+size;
		if(id==totalthreads-1) 
		high=noofvertices;
		System.out.println("Full data length is  " + data.length);
		System.out.println("My ID is "+id+" \n My Data Size is "+size+" \nLower Limit is "+low+" & higher limit  is "+high);
		
//DThread with rank 0 is considered as the Root node for this application
          if(rank==0)
          {
          	System.out.println("Rank 0 started");
		//Computation Part	
             for(int k=0;k<data.length;k++)
			{
			  for(int i=low;i<high;i++)
			  {
				 for(int j=0;j<data.length;j++)
				 {
			    	 data[i][j]=Math.min(data[i][j],(data[i][k]+data[k][j]));
				 }
			  }
			}

            //Creating output file
			File output=new File("outputDthread.dat");
			PrintWriter  out=new PrintWriter(new FileWriter(output));


                  //Get the partial result from all the Other Dthreads and compute the final result matrix
			if(totalthreads!=1)
			{
			  System.out.println("SUMMARY OF ALL NODES EXECUTION TIMES \n");
			  System.out.println(" Node 0 Finished Computation "+((System.currentTimeMillis()-start)+"milliseconds"));
			  messagepasser.barrier();
        	        for (int i=1;i<totalthreads;i++)
        	        {
			   Partial_Result iobj = (Partial_Result) messagepasser.get(i,"0");
			   int rowIndex=0;
			   System.out.println("Node "+i+" finished computation in "+iobj.timeTaken+"milliseconds");
			   for(int j=iobj.low;j<iobj.high;j++)
			     data[j]=iobj.data[rowIndex++];
		         }
			}
		    System.out.println(" \nNode 0 Finished getting Partial result "+((System.currentTimeMillis()-start)+"milliseconds"));
		    

  //Write the result matrix into the output file

               for(int i=0;i<data.length;i++)
		    {
		      for(int j=0;j<data.length;j++)
			  {
			    out.println(new Integer(data[i][j]).toString());
			  }
	 	    }
		    out.close();
			System.out.println("Please see outputDthread.dat for the result");
		 }
		

        //If the DThread is not root
	 
         else
          {

              //Computation Part
		  for(int k=0;k<data.length;k++)
		  {
			for(int i=low;i<high;i++)
			{
			   for(int j=0;j<data.length;j++)
			   {
			   	   data[i][j]=Math.min(data[i][j],(data[i][k]+data[k][j]));
			   }
			}
		  }

              //Build the Partial result and send to the root node ( in my case it is DThread with rank 0)

		  Partial_Result presult=new Partial_Result(data,low,high,(System.currentTimeMillis()-start));
		  messagepasser.put(presult,rank,0,"0");
		  System.out.println("Finished sending Partial result "+((System.currentTimeMillis()-start)+"milliseconds"));
		  messagepasser.barrier();
          }         
		  System.out.println(" I with id "+id+"Completed");
		  end=System.currentTimeMillis();
		  System.out.println("Total Time Taken is "+(end-start)+" milliseconds");
}

catch(Exception e)
{
  System.out.println("Exception ::" + e.getMessage());
  e.printStackTrace();
}

	
}
}


//Class which will hold the partial result

class Partial_Result implements Serializable
{
	int[][] data;
	int row,col;
	int low;
	int high;
	long timeTaken;
	Partial_Result(int[][] data,int low,int high,long time)
	{
		row=high-low;
		col=data.length;
		this.data=new int[row][col];
		this.timeTaken=time;
		for(int i=low,m=0;i<high;i++,m++)
		{
		  for(int j=0,n=0;j<col;j++,n++)
		  { 
		  	 this.data[m][n]=data[i][j];
		  }
		}
		this.low=low;
		this.high=high;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -