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

📄 asprunnable.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.DRunnable;
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 ASPRunnable implements DRunnable{
	
	int[][] data;
	int low;
	int high;
	int size;
	int extra;
	int id;
	int total;
	int noofvertices;
	int rank;
	int totalthreads=0;
	ObjectMP messagepasser;

 //Constructor that initializes the array

	
	ASPRunnable(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;
		  }
		} 
				System.out.println("data length" + data.length);
				System.out.println("I'm inside Floyd 2nd constructor"); 
	}
	
	
	
	
	
	
	
	
	
	//Extends the run() method of the DThread class
	public void run()
	{
	  try
	  {
		long start,end;
		
				
		start=System.currentTimeMillis();
		rank=DThread.getContext().getIdentity();
		totalthreads = DThread.getContext().getNumberOfThreads();
		ObjectMP messagepasser = DThread.getContext().getMP();
		System.out.println("Rank is "+rank+" Tot threads is "+totalthreads+"Message passer is "+messagepasser);
		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
			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]));
				 }
			  }
			}

                   //Create the Output file in the current path
			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)
			{
			  messagepasser.barrier();
			  for (int i=1;i<totalthreads;i++)
			  {
			   PartialResult11 iobj = (PartialResult11) messagepasser.get(i,"0");
			   int rowIndex=0;
			   for(int j=iobj.low;j<iobj.high;j++)
				 data[j]=iobj.data[rowIndex++];
			  }
			}
			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();
		 }
			 
		 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)

		  PartialResult11 presult=new PartialResult11(data,low,high);
		  messagepasser.put(presult,rank,0,"0");
		  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 PartialResult11 implements Serializable
{
	int[][] data;
	int row,col;
	int low;
	int high;
	PartialResult11(int[][] data,int low,int high)
	{
		row=high-low;
		col=data.length;
		this.data=new int[row][col];
		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;
		/* for(int i=0;i<row;i++)
		{
		  for(int j=0;j<col;j++)
		  { 
			 System.out.println(this.data[i][j]);
		  }
		}*/
	}
}

/* class GlobalData implements Serializable
{
	 int[][] data;
	 
	 Globaldata(int[][] data)
	 {
		this.data = new int[data.length][data.length];
		this.data = data;
	 }
}*/

⌨️ 快捷键说明

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