python 详解如何使用GPU大幅提高效率
作者:微小冷 时间:2023-08-24 19:45:46
cupy我觉得可以理解为cuda for numpy,安装方式pip install cupy
,假设
import numpy as np
import cupy as cp
那么对于np.XXX
一般可以直接替代为cp.XXX
。
其实numpy
已经够快了,毕竟是C写的,每次运行的时候都会尽其所能地调用系统资源。为了验证这一点,我们可以用矩阵乘法来测试一下:在形式上通过多线程并发、多进程并行以及单线程的方式,来比较一下numpy
的速度和对资源的调度情况,代码为
# 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)
运行结果为
(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
所以说在numpy中就别用python内置的并行和并发了,反而会称为累赘。而且这么一比更会印证numpy的强大性能。
但在cupy
面前,这个速度会显得十分苍白,下面连续5次创建5000x5000的随机矩阵并进行矩阵乘法,
#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)
最后的结果是
(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
而且非常霸道的是,当矩阵维度从5000x5000升到15000x15000后,cupy的计算时间并没有什么变化,充其量是线性增长,毕竟只要缓存吃得下,无论多么大的矩阵,乘法数也无非是按行或者按列增加而已。
来源:https://blog.csdn.net/m0_37816922/article/details/121223407
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用documentElement正确取得当前可见区域的大小
深入了解Python 中线程和进程区别
Dreamweaver技巧十二招
Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】
![](https://img.aspxhome.com/file/2023/7/84957_0s.png)
pytorch dataset实战案例之读取数据集的代码
![](https://img.aspxhome.com/file/2023/4/99204_0s.jpg)
Python实现好友全头像的拼接实例(推荐)
![](https://img.aspxhome.com/file/2023/1/75911_0s.jpg)
python绘制双柱形图代码实例
![](https://img.aspxhome.com/file/2023/4/97334_0s.png)
Python-GUI wxPython之自动化数据生成器的项目实战
![](https://img.aspxhome.com/file/2023/2/93802_0s.png)
tornado框架blog模块分析与使用
python爬虫之selenium库的安装及使用教程
![](https://img.aspxhome.com/file/2023/8/78678_0s.png)
python实现打砖块游戏
sqlserver2005使用row_number() over分页的实现方法
Flask 入门系列 Cookie与session的介绍
![](https://img.aspxhome.com/file/2023/9/103769_0s.png)
深入理解Python爬虫代理池服务
![](https://img.aspxhome.com/file/2023/9/100819_0s.png)
深入讲解Go语言中函数new与make的使用和区别
numpy:找到指定元素的索引示例
Mysql安装 Navicat 出现1044/1045错误的解决方法
MySQL备份与恢复之冷备(1)
![](https://img.aspxhome.com/file/2023/3/104123_0s.jpg)
Vue开发之封装上传文件组件与用法示例
![](https://img.aspxhome.com/file/2023/4/126574_0s.png)
Python如何使用pymongo连接MongoDB数据库并进行相关操作
![](https://img.aspxhome.com/file/2023/7/72017_0s.png)