Java Parallel Computing fork/join and Python parallel computing PP

Source: Internet
Author: User

With the advent of the multi-core era, software developers have to pay attention to the field of parallel programming. The fork/join mode that will be added to JDK 7 is a classic method for processing parallel programming. In python, the parallel Python module already supports multi-core parallel computing, erlang and other languages generated for parallel computing are also very red. Next we will calculate the sum of all prime numbers in the given array, to experience the features and performance of Parallel Computing in Java and Python respectively:

Java fork/join implementation, jsr166y download http://g.oswego.edu/dl/concurrency-interest/

Import java. util. Concurrent. timeunit;

Import jsr166y. forkjoinpool;
Import jsr166y. forkjointask;
Import jsr166y. recursiveaction;

Import org. JUnit. test;

Class prime extends recursiveaction {
Final long [] array;
Int Len;
 
Public prime (long [] array, int Len ){
This. array = array;
This. Len = Len;

}
Private Boolean isprime (long num ){
If (Num <2) return false;
If (num = 2) return true;
Long max = (long) math. Ceil (math. SQRT (Num ));
Long I = 2;
While (I <= max ){
If (Num % I = 0)
Return false;
++ I;
}
Return true;
}

Private long sumprime (long num ){
Long sum = 0;
For (INT I = 2; I <num; I ++ ){
If (isprime (I )){
Sum + = I;
}
}
Return sum;
}

Private Boolean haselement (long [] A, long B ){
For (INT I = 0; I <A. length; I ++ ){
If (A [I] = B) Return true;
}
Return false;

}
@ Override
Protected void compute (){
If (LEN> = 0 ){
System. Out. println ("sum of Primes below" + array [Len] + "is" + sumprime (array [Len]);
-- Len;
Invokeall (new prime (array, Len), new prime (array, len-2 ));
}
}
}

Public class testforkjoinsimple {
Long [] array = {1000000,100 0100,1000200, 1000300,100 0400 };
@ Test
Public void testsort () throws exception {
Date date1 = new date ();
Forkjointask sort = new prime (array, 4 );
Forkjoinpool fjpool = new forkjoinpool ();
Fjpool. Submit (SORT );
Fjpool. Shutdown ();
System. Out. println ("Starting prime with" + fjpool. getparallelism () + "Workers ");
Fjpool. awaittermination (4, timeunit. Seconds );
System. Out. println (new date (). gettime ()-date1.gettime ());}
}

Running result:

Starting prime with 2 workers
Sum of Primes below 1000400 is 37582408783
Sum of Primes below 1000300 is 37574405939
Sum of Primes below 1000100 is 37556402315
Sum of Primes below 1000200 is 37566403929
Sum of Primes below 1000000 is 37550402023
4043

 

Python pp implementation, refer to: http://www.parallelpython.com/

Import math, sys, time
Import PP
Def isprime (n ):
If not isinstance (n, INT ):
Raise typeerror ("argument passed to is_prime is not of 'int' type ")
If n <2:
Return false
If n = 2:
Return true
Max = int (math. Ceil (math. SQRT (N )))
I = 2
While I <= MAX:
If n % I = 0:
Return false
I + = 1
Return true
Def sum_primes (n ):
Return sum ([X for X in xrange (2, n) If isprime (x)])
Print "" Usage: Python sum_primes.py [ncpus]
[Ncpus]-the number of workers to run in parallel,
If omitted it will be set to the number of processors in the system
"""

Ppservers = ()
If Len (SYS. argv)> 1:
Ncpus = int (SYS. argv [1])
Job_server = pp. Server (ncpus, ppservers = ppservers)
Else:
Job_server = pp. Server (ppservers = ppservers)
Print "Starting PP with", job_server.get_ncpus (), "Workers"

Job1 = job_server.submit (sum_primes, (100,), (isprime,), ("math ",))
Result = job1 ()
Print "sum of Primes below 100 is", result
Start_time = Time. Time ()
Inputs = (100000,100 100200,100, 100400)
Jobs = [(input, job_server.submit (sum_primes, (input,), (isprime,), ("math",) for input in inputs]
For input, job in jobs:
Print "sum of Primes below", input, "is", JOB ()
Print "time elapsed:", time. Time ()-start_time, "S"
Job_server.print_stats ()

Running result:

Usage: Python sum_primes.py [ncpus]
[Ncpus]-the number of workers to run in parallel,
If omitted it will be set to the number of processors in the system

Starting PP with 2 workers
Sum of Primes below 100 is 1060
Sum of Primes below 100000 is 454396537
Sum of Primes below 100100 is 454996777
Sum of Primes below 100200 is 455898156
Sum of Primes below 100300 is 456700218
Sum of Primes below 100400 is 457603451
Time elapsed: 1.36199998856 s
Job execution statistics:
Job count | % of all jobs | job time sum | time per job | job server
6 | 100.00 | 2.2980 | 0.383000 | Local
Time elapsed since server creation1.36199998856

 

What can we get from the running results?

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.