当前位置:   article > 正文

python代码由cpu转成gpu运行_python中在cpu和gpu上跑程序

python中在cpu和gpu上跑程序

参考文件章1:https://blog.csdn.net/qq_28023365/article/details/87970505
参考文件章2:https://blog.csdn.net/u014636245/article/details/84404646
看起来是通过Numba模块,通过装饰器@jit实现转换
有一块样例代码:

import numpy as np
from numba import jit
import time


@jit
def sum_jit(arr):
    s_time = time.time()
    m = arr.shape[0]
    result = 0.0
    for i in range(m):
        result += arr[i]
    e_time = time.time()
    return (e_time - s_time)


def sum(arr):
    s_time = time.time()
    m = arr.shape[0]
    result = 0.0
    for i in range(m):
        result += arr[i]
    e_time = time.time()
    return (e_time-s_time)


def main():
    n = int(10.0*1e6)
    array = np.random.random(n)
    t1 = sum_jit(array)
    t2 = sum(array)
    print("Time with JIT:", t1)
    print("Time without JIT:", t2)


if __name__ == '__main__':
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

运行后的截图如下:
在这里插入图片描述
运行前面报了一些错,可能是我机器上是集成显卡的缘故吧,后面还要再细看

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号