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

📄 pi.java

📁 JAMPACK Grid programming
💻 JAVA
字号:
/**
 *
 * Program         : Pi_Impl.java (Example Application Program)
 *
 * Author          : Vijayakrishnan Menon
 *
 * Date            : 20th Feb 2006
 *
 * Organization    : Centre for Excellence in 
 *						Computational Engineering and Networking (CEN),
 *                   		Amrita Viswa Vidyapeetham
 *
 **/



import JAMPack.JComm;
import JAMPack.JScheduler;
import JAMPack.JGrid;
import JAMPack.Task;
import JAMPack.Comm;
import JAMPack.JBootStrap;


/** This is how an ideal user task should be written. The purpose of this 
  * program is to demostrate the use of the frame work by a common end user. 
  * Though the package is not full fledged, it will under go additions in future
  * and better versions will be released. The basic template will remain same. 
  * Additions will mainly be in the IPC services offered, and also in the 
  * network abstraction modules, that facilitate communications through the 
  * concept of Virtual organizations. Extragrids can also be realised in the 
  * upcoming versions through SOAP-XML services and HTTP-tunelling.  
  **/

public class Pi implements Task {
	private JGrid _grid;
	
	public void runTask(int rank, int size) {	
		System.out.println("*********************************************************************");
		System.out.println("Sample Pi program to demostrate JAMPack Grid Runtime 1.5");
		double sum=0,U,L,x,range =0.0000001,buff=0;
		Comm comm = new JComm();
		
		L = (double)rank/(double)size;
		U = (double)(rank + 1)/(double)size;
		
		for(x = L;x<=U;x+=range) sum += (4.00/(1.00+x*x))*range;
		
		System.out.println("Node "+rank+" calculated value :"+sum);		
		try {
			if(rank==0) 
			{
				if(size>1) {
					for(int i=1;i<size;i++) {
						buff = comm.receive(buff,i,_grid,1);
						sum += buff;
					}
				}
				System.out.println("Value of Pi: "+sum);
			}
			else 
			{
				comm.send(sum,0,_grid,1);
				System.out.println("Sending to node zero....");
			}
		}catch(Exception e) {e.printStackTrace();}	
		System.out.println("*********************************************************************");
	}	 	
	
	public void setGrid(JGrid grid) {
 		_grid = grid;
 	} 

	public static void main(String args[]) throws Exception
	{
		JScheduler scheduler = JScheduler.getInstance("Hosts.txt");
		Task task = JBootStrap.getBootStrapedTask("Pi");
		JScheduler.post(task,3);			
	}	
}


/*
 *int []x = new int[3];
			x[0] = i*2;
			x[1] = 256-i*2;
			x[2] = 128-i ;
			
 */

⌨️ 快捷键说明

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