Python 分布式计算模块 Parallel

GPL
Python
跨平台
2013-11-02
欢哥

Parallel Python是Python进行分布式计算的开源模块,能够将计算压力分布到多核CPU或集群的多台计算机上,能够非常方便的在内网中搭建一个自组织的分布式计算平台。先从多核计算开始,普通的Python应用程序只能够使用一个CPU进程,而通过Parallel Python能够很方便的将计算扩展到多个CPU进程中

示例代码:

#!/usr/bin/python
# File: sum_primes.py
# Author: VItalii Vanovschi
# Desc: This program demonstrates parallel computations with pp module
# It calculates the sum of prime numbers below a given integer in parallel
# Parallel Python Software: http://www.parallelpython.com

import math, sys, time
import pp

def isprime(n):
    """Returns True if n is prime and False otherwise"""
    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):
    """Calculates sum of all primes below given integer 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
"""

# tuple of all parallel python servers to connect with
ppservers = ()
#ppservers = ("10.0.0.1",)

if len(sys.argv) > 1:
    ncpus = int(sys.argv[1])
    # Creates jobserver with ncpus workers
    job_server = pp.Server(ncpus, ppservers=ppservers)
else:
    # Creates jobserver with automatically detected number of workers
    job_server = pp.Server(ppservers=ppservers)

print "Starting pp with", job_server.get_ncpus(), "workers"

# Submit a job of calulating sum_primes(100) for execution. 
# sum_primes - the function
# (100,) - tuple with arguments for sum_primes
# (isprime,) - tuple with functions on which function sum_primes depends
# ("math",) - tuple with module names which must be imported before sum_primes execution
# Execution starts as soon as one of the workers will become available
job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",))

# Retrieves the result calculated by job1
# The value of job1() is the same as sum_primes(100)
# If the job has not been finished yet, execution will wait here until result is available
result = job1()

print "Sum of primes below 100 is", result

start_time = time.time()

# The following submits 8 jobs and then retrieves the results
inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)
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()

# Parallel Python Software: http://www.parallelpython.com
加载中
此软件有 1 条评论,请先登录后再查看。

暂无资讯

暂无问答

OpenACC parallel

▶ 使用 kernels 导语并行化 for 循环 ● 同一段代码,使用 kernels,parallel 和 parallel + loop 进行对比 1 #include <stdio.h> 2 #include <time.h> 3 #include <openacc.h> 4 ...

2018/08/03 13:59
25
0
GNU Parallel指南

GNU Parallel是一个shell工具,为了在一台或多台计算机上并行的执行计算任务。本文简要介绍GNU Parallel的使用,文章主要翻译自:http://www.gnu.org/software/parallel/parallel_tutorial....

2014/06/01 22:05
3W
3
Parallel 编程资料

[Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4](https://www.microsoft.com/en-us/download/details.aspx?id=19222)...

2015/10/23 14:34
29
0
The Parallel Challenge Ballgame

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Description Before the ACM/ICPC world final 2005, there is a competition called “The P...

2013/08/28 14:12
163
0
.net多线程 Parallel

1.主线程参与计算 1 //用Task完成这个 你们会吗 不在意那个主线程参与计算的事儿 2 //Parallel.Invoke(() => this.Coding("爱书客", "Client") 3 // ...

2019/06/07 22:47
18
0
Concurrent and Parallel Programming

Concurrent and Parallel Programming

2015/10/27 15:37
118
0
Introduction to Parallel Computing

Copied From:https://computing.llnl.gov/tutorials/parallel_comp/ Author: Blaise Barney, Lawrence Livermore National Laboratory UCRL-MI-133316 Table of Contents Abstract Overvie...

2018/11/16 21:22
10
0
intel parallel studio

最近开始做一些profile工作,出了使用内部开发的profile信息搜集之外,也开始安装使用parallel studio xe 2011. 首先使用界面比较好看 amplifier,属于vtune的延续或者进入parallel studio的...

2012/03/09 14:07
203
0
Parallel并行之乱用

  关于Parallel我也不细说了,一则微软封装的很好用,二来介绍这个的遍地都是。   我要说的是,要想成为一个优秀的标题党,一定要把重点放到别的地方,为了节省大家阅读时间,我先把结论...

2016/12/28 11:25
19
0

没有更多内容

加载失败,请刷新页面

返回顶部
顶部