zoukankan      html  css  js  c++  java
  • Parallel Python 并行开发多核心并行程序(例3)

    #-*- coding: UTF-8 -*-
    #-------------------------------------------------------------------------------
    # Name:        
    # Purpose:     
    #
    # Author:      ankier
    #
    # Created:     05-02-2013
    # Copyright:   (c) ankier 2013
    # Licence:     <your licence>
    #-------------------------------------------------------------------------------
    
    import math, sys, md5, time
    import pp
    
    def part_sum(start, end):
        """Calculates partial sum"""
        sum = 0
        for x in xrange(start, end):
            if x % 2 == 0:
               sum -= 1.0 / x 
            else:
               sum += 1.0 / x 
        return sum
    
    start = 1
    end = 20000000
    
    # Divide the task into 64 subtasks
    parts = 64
    step = (end - start) / parts + 1
    
    # Create jobserver
    job_server = pp.Server()
    
    # Execute the same task with different amount of active workers and measure the time
    for ncpus in (1, 2, 4, 8, 16, 1):
        job_server.set_ncpus(ncpus)
        jobs = []
        start_time = time.time()
        print "Starting ", job_server.get_ncpus(), " workers"
        for index in xrange(parts):
            starti = start+index*step
            endi = min(start+(index+1)*step, end)
            jobs.append(job_server.submit(part_sum, (starti, endi)))
       
        # Retrieve all the results and calculate their sum
        part_sum1 = sum([job() for job in jobs])
        # Print the partial sum
        print "Partial sum is", part_sum1, "| diff =", math.log(2) - part_sum1
    
        print "Time elapsed: ", time.time() - start_time, "s"
        print
    job_server.print_stats()

    运行结果:

    Starting  1  workers
    Partial sum is 0.69314720556 | diff = -2.50000427027e-08
    Time elapsed:  3.40600013733 s
    
    Starting  2  workers
    Partial sum is 0.69314720556 | diff = -2.50000427027e-08
    Time elapsed:  2.04699993134 s
    
    Starting  4  workers
    Partial sum is 0.69314720556 | diff = -2.50000427027e-08
    Time elapsed:  1.6740000248 s
    
    Starting  8  workers
    Partial sum is 0.69314720556 | diff = -2.50000427027e-08
    Time elapsed:  1.66700005531 s
    
    Starting  16  workers
    Partial sum is 0.69314720556 | diff = -2.50000427027e-08
    Time elapsed:  1.67000007629 s
    
    Starting  1  workers
    Partial sum is 0.69314720556 | diff = -2.50000427027e-08
    Time elapsed:  3.36299991608 s
    
    Job execution statistics:
     job count | % of all jobs | job time sum | time per job | job server
           384 |        100.00 |      29.3340 |     0.076391 | local
    Time elapsed since server creation 15.2060000896
    0 active tasks, 1 cores
  • 相关阅读:
    C++各种进制的转换
    C++获取当前目录
    【转】Caffe初试(十)命令行解析
    libsvm下的windows版本中的工具的使用
    libsvm的数据格式及制作
    【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优
    【转】Caffe初试(九)solver及其设置
    【转】Caffe初试(八)Blob,Layer和Net以及对应配置文件的编写
    【转】Caffe初试(七)其它常用层及参数
    Ubuntu 14.04 安装 Sublime Text 3
  • 原文地址:https://www.cnblogs.com/ankier/p/2893408.html
Copyright © 2011-2022 走看看