About pi Everyone is familiar with: we learn from the textbook to as early as more than 1000 years ago, zu Pi to 3.1415926 to 3.1415927 ... After the birth of the computer, calculate pi is used to detect computer hardware performance, day and night burning CPU to see if there is a problem ... Others also want to see if there is a rule behind this mysterious figure of infinite extension, to discover some cosmic secrets ...
mention Pi, cannot mention Fabrice Bellard, he is considered a computer genius, in the industry has an important impact. In 1996 he wrote a concise but complete C compiler and a Java Virtual machine harissa. Fabrice Bellard invented TINYCC is the GNU environment of the smallest ANSI C language compiler, is currently known as the fastest compiler C compiler. Fabrice Bellard masterpiece Numerous and involves extensive, 1998 years to write a concise OpenGL implementation tinygl,2003 year developed the Emacs clone qemacs,2005 year also designed a LOW-COST digital TV system.
Fabrice Bellard, using a common desktop computer, has done a great feat of pounding the PI records of the supercomputer, using the desktop to compute PI to 2.7 trillion digits after the decimal point, surpassing the 47th place in the world now ranked T2K The open supercomputer created a 2.5 trillion-digit decimal point record last August.
Bellard uses a computer that is based on a 2.93GHz Core i7 processor, the computer's memory capacity is 6GB, and the hard drive uses five RAID-0 configured 1.5TB capacity Seagate 7200.11, which runs 64 red Hat Fedora 10 operating system, the file system uses Linux EXT4.
This calculated pi data accounted for 1137GB of hard disk capacity, Bellard spent 103 days to calculate this result.
There are many ways to calculate pi
:
Calculus Cutting Circle Method:
or using a Chudnovsky formula that is convenient for computing:
but these computational methods are more complex, it is difficult to let the reader understand and use parallel computing to ask, fortunately the Taylor series is a good thing, it will be the calculus of Things to use infinite series to express, so it is easy to parallel computing decomposition:
π=4*∑ ( -1) ^n+1/(2n-1) or written as: π=4* (1-1/3+1/5-1/7+ ...)
can also be obtained: Πn =πn-1+ ( -1) ^n+1/(2n-1), which means that the current π value can be obtained by the π value at the front of the iteration.
we first write a stand-alone program according to the formula above:
public class pitest{public static void main (string] args) {double pi=0.0; for (double i=1.0;i<1000000001d;i++) {pi + + MATH.POW ( -1,i+1)/(2*I-1); } System.out.println (4*PI); }}
run the above program and control the PI standard value: 3.141592653589793238462643383279 ...
if i<10000, get pi = 3.1416926635905345 (since the red ones are inaccurate)
if i<1000000, get pi = 3.1415936535907742 (since the red ones are inaccurate)
if i<1000000000, get pi = 3.1415926525880504 (since the red ones are inaccurate)
......
can see that the greater the number of rotations in an iteration, the more accurate the PI value is.
because it is infinite additive, we can easily change to parallel program solution, such as i=4n, can be divided into 4-segment parallel solution, and then 4 parts and merged to get the final pi value. Assuming we have 4 computers, the parallel computing design is as follows:
We are here through the Fourinone provides a variety of parallel computing models to design, the first use can refer to the Distributed Computing hands-on Demo guide, development package download address: http://code.google.com/p/fourinone/
Program implementation:
Piworker: is a PI compute worker implementation, we can see it through the command line to enter a calculated pi value of the starting and ending values, we also started 4 Piworker instances, at the start of the specified different start end parameters.
Pictor: is a PI calculation of the contractor implementation, its implementation is very simple, access to online workers, through the dotaskbatch phase of the calculation, waiting for each worker to complete the calculation, the workers return the results of the PI calculation combined.
Run steps:
1, Boot parkserverdemo (its IP port is already specified in the configuration file)
JAVA-CP Fourinone.jar; Parkserverdemo
2, run 4 Piworker, the iteration of the 100,000,000-wheel calculation split to 4 workers in parallel to complete, here to facilitate the demonstration is on the same machine, real-world applications can be done on multiple computers.
can see that 4 workers in the same machine in parallel to complete the calculation of pi value of the time is 29 seconds, if it is running a stand-alone program pitest completion of the time in 45 seconds, precision is to the decimal point after 8 "3.14159265", but there is a significant gap in time-consuming, if more than one instance of the machine, Efficiency will be further improved, parallel computing performance improvement analysis can be referred to "using parallel computing greatly enhance the efficiency of recursive algorithm."
Full Demo source code is as follows:
//Parkserverdemo
import Com.fourinone.beancontext;public class parkserverdemo{public static void main (string] args) { Beancontext.startpark (); }}
//Piworker
Import Com.fourinone.migrantworker;import Com.fourinone.warehouse;public class Piworker extends migrantworker{public Double m=0.0,n=0.0; Public Piworker (double m, double n) {this.m = M. THIS.N = n;} public WareHouse Dotask (WareHouse inhouse) {double pi=0.0; Double i=m;i<n;i++) {pi + math.pow ( -1,i+1)/(2*i-1);} System.out.println (4*PI); Inhouse.setobj ("PI", 4*PI); return inhouse; public static void Main (string] args) {piworker MW = new Piworker (double.parsedouble (args[2)), double.parsedouble (args [3])); Mw.waitworking (Args[0],integer.parseint (args[1)), "Piworker"); }}
//Pictor
Import Com.fourinone.contractor;import Com.fourinone.warehouse;import Com.fourinone.workerlocal;import Java.util.date;public class Pictor extends contractor{public WareHouse givetask (WareHouse inhouse) {workerlocal[] wks = Getwaitingworkers ("Piworker"); System.out.println ("Wks.length:" +wks.length); warehouse[] Hmarr = Dotaskbatch (wks, inhouse); Double pi=0.0; for (WareHouse result:hmarr) {pi = pi + (Double) result.getobj ("PI");} System.out.println ("PI:" +pi); return inhouse; public static void Main (string] args) {Pictor a = new Pictor (); Long begin = (new Date ()). GetTime (); A.givetask (new WareHouse ()); Long end = (new Date ()). GetTime (); System.out.println ("Time:" + (End-begin)/1000+ "s"); A.exit (); }}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.