简体   繁体   中英

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. I try to use py-videocore6 or achieve my result, i write this on my example code:

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)

using normal raspberry resources, and not when i call:

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

using gpu. Why my code is executed at drv.program call and not 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?

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). Thus, in your program, the library functions (sha256, hexdigest, etc.) will run on 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. So you must implement the hash functions by your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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