欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python代码由cpu转成gpu运行

程序员文章站 2022-07-13 15:02:33
...

参考文件章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()

运行后的截图如下:
python代码由cpu转成gpu运行
运行前面报了一些错,可能是我机器上是集成显卡的缘故吧,后面还要再细看

相关标签: 量化交易