numba的协程 [英] Coroutines in numba

查看:186
本文介绍了numba的协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究需要快速协程的东西,我相信numba可以加快我的代码的速度。

I'm working on something that requires fast coroutines and I believe numba could speed up my code.

这是一个愚蠢的例子:一个将输入平方的函数,

Here's a silly example: a function that squares its input, and adds to it the number of times its been called.

def make_square_plus_count():
    i = 0
    def square_plus_count(x):
        nonlocal i
        i += 1
        return x**2 + i
    return square_plus_count

您甚至不能 nopython = False 这样做,大概是由于非本地关键字。

You can't even nopython=False JIT this, presumably due to the nonlocal keyword.

但是如果您使用课程,则不需要非本地相反:

But you don't need nonlocal if you use a class instead:

def make_square_plus_count():
    @numba.jitclass({'i': numba.uint64})
    class State:
        def __init__(self):
            self.i = 0

    state = State()

    @numba.jit()
    def square_plus_count(x):
        state.i += 1
        return x**2 + state.i
    return square_plus_count

这至少有效,但是如果您会执行 nopython = True

This at least works, but it breaks if you do nopython=True.

是否存在可以使用编译的解决方案nopython = True

推荐答案

如果您要使用状态类,则可以还使用方法而不是闭包(应该是非python编译的):

If you're going to use a state-class anyway you could also use methods instead of a closure (should be no-python compiled):

import numba

@numba.jitclass({'i': numba.uint64})
class State(object):
    def __init__(self):
        self.i = 0

    def square_plus_count(self, x):
        self.i += 1
        return x**2 + self.i

square_with_call_count = State().square_plus_count  # using the method
print([square_with_call_count(i) for i in range(10)])
# [1, 3, 7, 13, 21, 31, 43, 57, 73, 91]

但是时间显示,这实际上比纯python闭包实现要慢tion。我希望只要您不使用 nonlocal numpy-arrays或在您的方法(或闭包)中对数组进行操作,效率就会降低!

However timings show that this is actually slower than a pure python closure implementation. I expect that as long as you don't use nonlocal numpy-arrays or do operations on arrays in your method (or closure) this will be less efficient!

这篇关于numba的协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆