首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Raspberry pi4 py-视频核心6使用GPU执行程序

Raspberry pi4 py-视频核心6使用GPU执行程序
EN

Stack Overflow用户
提问于 2021-05-04 17:46:51
回答 1查看 162关注 0票数 2

在我的项目中,我将使用GPU而不是CPU在Raspberry pi4上运行python程序。我尝试使用py-视频核心6或实现我的结果,我在我的示例代码中写了以下代码:

代码语言:javascript
复制
from videocore6.driver import Driver
from videocore6.assembler import qpu
from time import monotonic
from hashlib import sha256

@qpu
def mine(asm, message, difficulty=1):
    assert difficulty >= 1
    prefix = '1' * difficulty
    for i in range(10000000):
        digest = sha256(str(hash(message+ str(i))).encode('utf-8'))
        if digest.hexdigest().startswith(prefix):
            print ("after " + str(i) + " iterations found nonce: "+ digest.hexdigest())

    # This synchronization is needed between the last TMU operation and the
    # program end with the thread switch just before the loop above.
    barrierid(syncb, sig=thrsw)
    nop()
    nop()

    nop(sig=thrsw)
    nop(sig=thrsw)
    nop()
    nop()
    nop(sig=thrsw)
    nop()
    nop()
    nop()


def branch_rel_label():

    with Driver() as drv:

        start = monotonic()
        code = drv.program(mine, "test message", 4)
        X = drv.alloc((16, ), dtype = 'uint32')
        Y = drv.alloc((16,), dtype = 'uint32')
        unif = drv.alloc(2, dtype = 'uint32')

        X[:] = np.arange(16)
        Y[:] = 0.0

        unif[0] = X.addresses()[0]
        unif[1] = Y.addresses()[0]

        drv.execute(code, unif.addresses()[0], thread=8)
        end = monotonic()

        print("Time elpsed: ", (end-start))


if __name__ == "__main__":
    branch_rel_label()

如果我测试我的代码,它会运行,但是代码会在我调用时执行。

drv.program(我的,“测试消息”,4)

使用正常的覆盆子资源,而不是当我调用:

drv.execute(代码,unif.addresses(),thread=8)

使用gpu。为什么我的代码是在drv.program调用而不是drv.execute时执行的?在这种行为中,我的代码在完全相同的时间内执行函数,在正常的python中,我可以使用GPU来执行我的函数吗?

我提前感谢你

EN

回答 1

Stack Overflow用户

发布于 2021-05-10 14:16:18

正如我在源代码中快速看到的那样,带有@qpu装饰符的函数中的所有代码都是在编译时执行的(drv.program)。因此,在您的程序中,库函数(sha256、十六进制等)会在CPU上运行。py-视频核心6功能(nop、add等)也不例外,但它们将QPU指令附加到asm中,生成的指令由QPU在drv.execute上执行。因此,您必须自己实现哈希函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67389709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档