简体   繁体   English

如何让我的 python 脚本在我的 GPU 上运行

[英]How to make my python script run on my GPU

I created a script to find a number inside pi:我创建了一个脚本来在 pi 中查找一个数字:

from math import pi
from mpmath import mp
from time import sleep as sleep


def loop(find):

    #Breaks the find string into a list

    findList = []


    print('Finding ' + str(find))

    num = 1000

    while True:

        mp.dps = num

        string = str(mp.pi)

        result = string.find(str(find))

        if result == -1:

            print("Couldn't find " + str(find) + " within the first " + str(num) + " of Pi. Looking moving into the first " + str(num * 10) + " digits instead")

            num = num * 10

            continue

            pass

        else:

            print(str(find )+ ' was found at character: ' + str(result))
            break

        pass

    pass

def main():

    find = input("What do you want to find: ")


    find = int(find)
    character = loop(find)

    
    


if True:

    main()

    input = ()

When a long number is inputed it takes a long time to process for obvious reasons.当输入一个长数字时,由于显而易见的原因,它需要很长时间来处理。 I'm running an Intel i5-9300h and a GTX 1650. I am wondering if 1) I can make this code run on my GPU instead of my CPU 2) If so, how do I do this?我正在运行 Intel i5-9300h 和 GTX 1650。我想知道是否 1) 我可以让这段代码在我的 GPU 而不是我的 CPU 上运行 2) 如果是这样,我该怎么做? 3) Would it even benefit performance? 3)它甚至会有益于性能吗?

Any help is much appreciated.任何帮助深表感谢。

I think numba will help with what you're looking for.我认为numba会帮助你找到你想要的东西。 It can run python code with CUDA support (ie your graphics card).它可以运行 python 代码并支持 CUDA (即您的显卡)。 Using CUDA for math is often faster than using a CPU because of better multithreading, however, whether you would get any benefit from it will depend on implementation.由于更好的多线程,使用 CUDA 进行数学运算通常比使用 CPU 更快,但是,您是否会从中获得任何好处将取决于实现。 In your example I think you'll have to write a function that can approximate pi in a multithreaded way, in order to take advantage of what CUDA has to offer.在您的示例中,我认为您必须编写一个可以以多线程方式近似 pi 的 function,以便利用 CUDA 必须提供的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM