由于PyOpenCL和Parallel Python都是专用于并行处理的Python模块,有人可以提供一个程序员为什么会使用其中一个的例子吗?
答案 0 :(得分:0)
pyopencl - 访问OpenCL并行计算API的Python模块
pp - 用于Python的并行和分布式编程工具包
程序员使用pp
创建“作业”和“作业服务器”,以在多核,多处理器和/或群集计算环境中分配工作。
OpenCL就像CUDA的超集语言(特别是应用程序编程接口(API)),不是语法,而是用法。 OpenCL是用于编程连接到计算机的许多不同类型设备的接口。这些设备包括不同的图形卡架构,其中CUDA仅适用于NVIDIA芯片组卡。对于可能只在具有NVIDA GPU的系统上运行的程序,还有pycuda
。
这些模块的使用取决于所访问的硬件和要解决的问题。它们可以根据需要一起使用或单独使用。
这是ParallelPython程序的example。
#!/usr/bin/python
# File: dynamic_ncpus.py
# Author: Vitalii Vanovschi
# Desc: This program demonstrates parallel computations with pp module
# and dynamic cpu allocation feature.
# Program calculates the partial sum 1-1/2+1/3-1/4+1/5-1/6+... (in the limit it is ln(2))
# Parallel Python Software: http://www.parallelpython.com
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
print """Usage: python dynamic_ncpus.py"""
print
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)
# Submit a job which will calculate partial sum
# part_sum - the function
# (starti, endi) - tuple with arguments for part_sum
# () - tuple with functions on which function part_sum depends
# () - tuple with module names which must be imported before part_sum execution
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()
# Parallel Python Software: http://www.parallelpython.com