设为首页 收藏本站
查看: 711|回复: 0

[经验分享] java并行计算Fork/Join和python并行计算pp

[复制链接]

尚未签到

发表于 2017-5-5 11:53:47 | 显示全部楼层 |阅读模式
  多核时代的已经来临,软件开发人员不得不开始关注并行编程领域。而 JDK 7 中将会加入的 Fork/Join 模式是处理并行编程的一个经典的方法,python中也早已有了Parallel Python模块支持多核并行计算,erlang等为并行计算而生的语言也大红大紫,下面我们通过计算给定数组中数据所有素数之和这样一个实例,来分别体验一下java和python并行计算的特点和性能:
  java的Fork/Join实现,需要jsr166y的下载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== 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, 1000100,1000200, 1000300, 1000400};
@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()); }
}
  运行结果:
DSC0000.gif

  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实现,参考: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, 100100, 100200, 100300, 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()
  运行结果:

  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 creation 1.36199998856
  从运行结果我们可以得出点什么?

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373450-1-1.html 上篇帖子: 通过比较学python(5):IronPython引用.Net类 下篇帖子: 用python爬虫抓站的一些技巧总结 (转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表