简体   繁体   English

树莓派 py-videocore6 使用 GPU 执行程序

[英]Raspberry pi4 py-videocore6 execute a program using GPU

in my project i would run a python program on my Raspberry pi4 using GPU instead CPU.在我的项目中,我将使用 GPU 而不是 CPU 在我的 Raspberry pi4 上运行 python 程序。 I try to use py-videocore6 or achieve my result, i write this on my example code:我尝试使用 py-videocore6 或达到我的结果,我在我的示例代码上写了这个:

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()

if i test my code it runs but code are executed when i call如果我测试我的代码它会运行但代码在我调用时执行

drv.program(mine, "test message", 4) drv.program(我的,“测试消息”,4)

using normal raspberry resources, and not when i call:使用普通的覆盆子资源,而不是当我打电话时:

drv.execute(code, unif.addresses()[0], thread=8) drv.execute(代码,unif.addresses()[0],线程=8)

using gpu.使用 gpu。 Why my code is executed at drv.program call and not drv.execute?为什么我的代码在 drv.program 调用而不是 drv.execute 中执行? in this kind of behavior my code execute function in the exactly same time a in normal python shell..how can i use GPU for execute my funcs? in this kind of behavior my code execute function in the exactly same time a in normal python shell..how can i use GPU for execute my funcs?

So many thanks in advance非常感谢提前

As i could fastly see in source project code, all the codes in the function with @qpu decorator is executed on compilation (drv.program).正如我在源项目代码中快速看到的那样,带有@qpu 装饰器的 function 中的所有代码都在编译时执行(drv.program)。 Thus, in your program, the library functions (sha256, hexdigest, etc.) will run on CPU.因此,在您的程序中,库函数(sha256、hexdigest 等)将在 CPU 上运行。 The py-videocore6 functions (nop, add, etc.) are not exceptions, but they append QPU instructions to asm, and the generated instructions are executed by QPU on drv.execute. py-videocore6函数(nop、add等)也不例外,它们是append QPU指令到asm,生成的指令由QPU在drv.execute上执行。 So you must implement the hash functions by your own.所以必须自己实现 hash 函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM