4

我每年都在计算一些计算量相对较大的数据。我使用 numba(效果很好)来减少运行迭代计算数据所需的时间。但是,鉴于我有 20 年的独立数据,我想将它们分成 5 x 组,每组 4 个,可以运行在 4 个不同的 cpu 内核上。

def compute_matrices(self):
    for year in self.years:
         self.xs[year].compute_matrix()

在上面的代码片段中,函数是类中的一个方法,其中包含属性 year 和 xs。year只是一个整数年,xs是一个包含 xs.data 和 compute_matrix() 方法的横截面对象。

将其拆分为多个核心的最简单方法是什么?

  1. 如果有一个 Numba 风格的装饰器可以自动分解循环并在不同的进程上运行它们并将结果粘合在一起,那就太好了。这存在吗?

  2. 使用 Python.multiprocessing 是我最好的选择吗?

4

2 回答 2

3

因此,您可以查看以下几件事:

NumbaPro:https ://store.continuum.io/cshop/accelerate/ 。这基本上是类固醇上的 Numba,为多核和多核架构提供支持。不幸的是它并不便宜。

numexpr:https ://code.google.com/p/numexpr/ 。这是实现超线程的 numpy 数组的表达式评估器。

Numexpr-Numba(实验性):https ://github.com/gdementen/numexpr-numba 。顾名思义,这是使用 Numba 后端的 Numexpr。

很多答案将取决于您的compute_matrix方法中所做的事情。

最快的(就开发时间而言)解决方案可能是使用multiprocessing库拆分您的计算。应该注意的是,如果您的compute_matrix函数没有副作用,则使用多处理会更容易。

于 2014-04-05T09:10:29.223 回答
1

对于复杂对象,我遇到的最简单的方法是利用 IPython 并行计算引擎。

只需使用以下命令运行 Ipython 集群:ipcluster start -n 4或使用笔记本

然后您可以遍历分配给不同客户端的 xs 对象。

def multicore_compute_matrices(self):
    from IPython.parallel import Client
    c = Client()
    xs_list = []
    years = sorted(self.years)
    # - Ordered List of xs Objects - #
    for year in years
         xs_list.append(self.xs[year])
    # - Compute across Clusters - #
    results = c[:].map_sync(lambda x: x.compute_matrix(), xs_list)
    # - Assign Results to Current Object - #
    year = years[0]
    for result in results:
        self.xs[year].matrix = result
        year += 1

挂时%time结果:

%time A.compute_matrices()
Wall Time: 5.53s

%time A.multicore_compute_matrices():
Wall Time: 2.58s
于 2014-04-07T02:10:23.567 回答