In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use GPU in python to greatly improve efficiency. I think it is very practical, so I share it with you. I hope you can get something after reading this article.
I think cupy can be understood as cuda for numpy, installation mode pip install cupy, assuming
Import numpy as npimport cupy as cp
Then for np.XXX, it can be directly replaced by cp.XXX.
In fact, numpy is fast enough, after all, it is written by C, and every time it runs, it will try its best to call system resources. To verify this, we can test it with matrix multiplication: formally compare the speed of numpy and the scheduling of resources by means of multi-thread concurrency, multi-process parallelism, and single-thread. The code is
# th_pr_array.pyfrom threading import Threadfrom multiprocessing import Processfrom time import time as Nowimport numpy as npimport sysN = 3000def MatrixTest: X = np.random.rand (n) x = Xerox print (f "{name} @ {t}: {Now ()-t}") def thTest (): t = Now () for i in range (5): Thread (target=MatrixTest,args= [Nemeral fareth {I}' T]. Start () def prTest (): t = Now () for i in range (5): Process (target=MatrixTest,args= [NMagneft pr {I}') 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)
The running result is
(base) E:\ Documents\ 00\ 1108 > python th_pr_numpy.py th
Th0 @ 1636357422.3703225: 15.23965334892273
Th2 @ 1636357422.3703225: 17.726242780685425
Th3 @ 1636357422.3703225: 19.001763582229614
Th4 @ 1636357422.3703225: 19.06676197052002
Th5 @ 1636357422.3703225: 19.08676195 1446533
(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
So instead of using python's built-in parallelism and concurrency in numpy, it will be called cumbersome. And such a comparison will prove the powerful performance of numpy.
But in front of cupy, this speed will look very pale. Let's create a random matrix of 5000x5000 and multiply it five times in a row.
# np_cp.pyimport numpy as npimport cupy as cpimport sysfrom time import time as NowN = 5000def testNp (t): for i in range (5): X = np.random.rand (NMagazine N) x = x print (f "np: {Now ()-t}") def testCp (t): for i in range (5): X = cp.random.rand (N) N) x = cp print (f "cp: {Now ()-t}") if _ _ name__ = = "_ _ main__": t = Now () if sys.argv [1] = 'np': testNp (t) elif sys.argv [1] =' cp': testCp (t)
The end result is
(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
And what is very domineering is that when the matrix dimension rises from 5000x5000 to 15000x15000, the computing time of cupy does not change. At best, it increases linearly. After all, as long as the cache can eat it, no matter how large the matrix is, the multiplication number is nothing more than increasing by row or column.
The above is how to use GPU in python to greatly improve efficiency. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.