欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 詳解如何使用GPU大幅提高效率

 更新時(shí)間:2021年11月09日 16:44:44   作者:微小冷  
CuPy是一個(gè)開(kāi)源矩陣庫(kù),使用NVIDIA CUDA加速。CuPy使用Python提供GPU加速計(jì)算。CUPY使用CUDA相關(guān)庫(kù),包括 CuBLAS、CUDNN、Curand、CuoSver、CuPaSeSE、Cufft和NCCL,以充分利用GPU架構(gòu)

cupy我覺(jué)得可以理解為cuda for numpy,安裝方式pip install cupy,假設(shè)

import numpy as np
import cupy as cp

那么對(duì)于np.XXX一般可以直接替代為cp.XXX。

其實(shí)numpy已經(jīng)夠快了,畢竟是C寫的,每次運(yùn)行的時(shí)候都會(huì)盡其所能地調(diào)用系統(tǒng)資源。為了驗(yàn)證這一點(diǎn),我們可以用矩陣乘法來(lái)測(cè)試一下:在形式上通過(guò)多線程并發(fā)、多進(jìn)程并行以及單線程的方式,來(lái)比較一下numpy的速度和對(duì)資源的調(diào)度情況,代碼為

# th_pr_array.py
from threading import Thread
from multiprocessing import Process
from time import time as Now
import numpy as np
import sys

N = 3000

def MatrixTest(n,name,t):
    x = np.random.rand(n,n)
    x = x@x
    print(f"{name} @ {t} : {Now()-t}")

def thTest():
    t = Now()
    for i in range(5):
        Thread(target=MatrixTest,args=[N,f'th{i}',t]).start()

def prTest():
    t = Now()
    for i in range(5):
        Process(target=MatrixTest,args=[N,f'pr{i}',t]).start()

if __name__=="__main__":
    if sys.argv[1]=="th":
        thTest()
    elif sys.argv[1]=="pr":
        prTest()
    else:
        t = Now()
        for i in range(5):
            MatrixTest(N,"single",t)

運(yùn)行結(jié)果為

(base) E:\Documents\00\1108>python th_pr_numpy.py th
th0 @ 1636357422.3703225 : 15.23965334892273
th1 @ 1636357422.3703225 : 17.726242780685425
th2 @ 1636357422.3703225 : 19.001763582229614
th3 @ 1636357422.3703225 : 19.06676197052002
th4 @ 1636357422.3703225 : 19.086761951446533

(base) E:\Documents\00\1108>python th_pr_numpy.py pr
pr3 @ 1636357462.4170427 : 4.031360864639282
pr0 @ 1636357462.4170427 : 4.55387806892395
pr1 @ 1636357462.4170427 : 4.590881824493408
pr4 @ 1636357462.4170427 : 4.674877643585205
pr2 @ 1636357462.4170427 : 4.702877759933472

(base) E:\Documents\00\1108>python th_pr_numpy.py single
single @ 1636357567.8899782 : 0.36359524726867676
single @ 1636357567.8899782 : 0.8137514591217041
single @ 1636357567.8899782 : 1.237830400466919
single @ 1636357567.8899782 : 1.683635950088501
single @ 1636357567.8899782 : 2.098794937133789

所以說(shuō)在numpy中就別用python內(nèi)置的并行和并發(fā)了,反而會(huì)稱為累贅。而且這么一比更會(huì)印證numpy的強(qiáng)大性能。

但在cupy面前,這個(gè)速度會(huì)顯得十分蒼白,下面連續(xù)5次創(chuàng)建5000x5000的隨機(jī)矩陣并進(jìn)行矩陣乘法,

#np_cp.py
import numpy as np
import cupy as cp
import sys
from time import time as Now

N = 5000

def testNp(t):
    for i in range(5):
        x = np.random.rand(N,N)
        x = x@x
    print(f"np:{Now()-t}")

def testCp(t):
    for i in range(5):
        x = cp.random.rand(N,N)
        x = x@x
    print(f"cp:{Now()-t}")


if __name__ == "__main__":
    t = Now()
    if sys.argv[1] == 'np':
        testNp(t)
    elif sys.argv[1]=='cp':
        testCp(t)

最后的結(jié)果是

(base) E:\Documents\00\1108>python np_cp.py np
np:8.914457082748413

(base) E:\Documents\00\1108>python np_cp.py cp
cp:0.545649528503418

而且非常霸道的是,當(dāng)矩陣維度從5000x5000升到15000x15000后,cupy的計(jì)算時(shí)間并沒(méi)有什么變化,充其量是線性增長(zhǎng),畢竟只要緩存吃得下,無(wú)論多么大的矩陣,乘法數(shù)也無(wú)非是按行或者按列增加而已。

在這里插入圖片描述

以上就是python 詳解如何使用GPU大幅提高效率的詳細(xì)內(nèi)容,更多關(guān)于Python GPU提高效率的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論