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的计算时间并没有什么变化,充其量是线性增长,毕竟只要缓存吃得下,无论多么大的矩阵,乘法数也无非是按行或者按列增加而已。

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

来源:https://blog.csdn.net/m0_37816922/article/details/121223407

标签:Python,GPU提高效率,GPU
0
投稿

猜你喜欢

  • 使用documentElement正确取得当前可见区域的大小

    2024-04-18 09:34:06
  • 深入了解Python 中线程和进程区别

    2021-09-01 20:51:38
  • Dreamweaver技巧十二招

    2009-07-05 18:53:00
  • Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】

    2022-02-12 01:25:05
  • pytorch dataset实战案例之读取数据集的代码

    2023-10-06 23:51:01
  • Python实现好友全头像的拼接实例(推荐)

    2021-08-21 17:29:35
  • python绘制双柱形图代码实例

    2022-02-28 19:42:27
  • Python-GUI wxPython之自动化数据生成器的项目实战

    2021-06-08 14:43:18
  • tornado框架blog模块分析与使用

    2023-01-29 10:39:27
  • python爬虫之selenium库的安装及使用教程

    2021-04-22 20:20:46
  • python实现打砖块游戏

    2023-11-09 21:27:15
  • sqlserver2005使用row_number() over分页的实现方法

    2024-01-18 20:01:02
  • Flask 入门系列 Cookie与session的介绍

    2022-06-21 00:45:44
  • 深入理解Python爬虫代理池服务

    2022-11-05 17:20:29
  • 深入讲解Go语言中函数new与make的使用和区别

    2023-06-16 17:52:29
  • numpy:找到指定元素的索引示例

    2022-05-18 22:29:37
  • Mysql安装 Navicat 出现1044/1045错误的解决方法

    2024-01-15 18:13:30
  • MySQL备份与恢复之冷备(1)

    2024-01-26 13:37:50
  • Vue开发之封装上传文件组件与用法示例

    2024-05-10 14:09:44
  • Python如何使用pymongo连接MongoDB数据库并进行相关操作

    2023-05-12 01:21:49
  • asp之家 网络编程 m.aspxhome.com