V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wizardyhnr
V2EX  ›  Python

Taichi 计算加速

  •  
  •   wizardyhnr · 2023-04-25 02:06:31 +08:00 · 1509 次点击
    这是一个创建于 369 天前的主题,其中的信息可能已经有所发展或是发生改变。

    无意间发现 Taichi 计算加速还挺快的。官网例子如下。不知道更复杂的案例效果怎么样。

    #!/usr/bin/env python
    #prime_taichi.py
    import taichi as ti
    ti.init(arch=ti.gpu)
    @ti.func
    def is_prime(n: int):
        result = True
        for k in range(2, int(n ** 0.5) + 1):
            if n % k == 0:
                result = False
                break
        return result
    
    @ti.kernel
    def count_primes(n: int) -> int:
        count = 0
        for k in range(2, n):
            if is_prime(k):
                count += 1
    
        return count
    
    #!/usr/bin/env python
    #test.py
    """Count the prime numbers in the range [1, n]
    """
    from prime_taichi import count_primes
    import time
    
    start: int = time.time()
    print(count_primes(10000000))
    print(time.time()-start)
    

    #Taichi 版本,笔记本 2070S

    [Taichi] version 1.5.0, llvm 15.0.4, commit 7b885c28, linux, python 3.10.6
    [Taichi] Starting on arch=cuda
    664579
    0.20201754570007324
    

    #纯 Python 版本

    664579
    83.8845808506012
    
    5 条回复    2023-04-25 14:23:14 +08:00
    dayeye2006199
        1
    dayeye2006199  
       2023-04-25 02:20:26 +08:00
    这个和 numba 之类的是否有相似之处?
    wizardyhnr
        2
    wizardyhnr  
    OP
       2023-04-25 02:38:19 +08:00
    都是 JIT 加速,性能好像比 numba 好不少。
    file:///dev/shm/performance-comparisons-for-python-libraries-in-parallel-computing-and-physical-simulation-1.pdf
    https://docs.taichi-lang.org/blog/taichi-compared-to-cub-cupy-numba
    huoshen
        3
    huoshen  
       2023-04-25 02:46:06 +08:00
    Taichi 是能加速, 因为运行的时候不是 python 了, 是他编译的静态语言, 但限制挺多的, 有些不支持递归, 有的不支持嵌套, 有的并行运行, 会导致额外的非原子内存操作导致数据不一致. 想额外吐槽一下 Taichi, 这学期上的一个 CG 课, 教授指定用的就是 Taichi, 官网上文档都不全, 然后各种 Machine dependent bug 搞得我心态都快炸了.
    t133
        4
    t133  
       2023-04-25 08:42:07 +08:00
    taichi 跟 jax 哪个快有没有都用过的
    oldshensheep
        5
    oldshensheep  
       2023-04-25 14:23:14 +08:00
    怎么不试试神奇的 GraalVM ,用 GraalVM 直接运行 Python 文件:
    664579
    8.049000024795532
    用 CPython 运行:
    664579
    115.89525604248047
    GraalVM 不能使用 GPU 加速,实验在甲骨文的 ARM 服务器上运行,快了十几倍
    优点就是不用改代码,不过不能使用 GPU
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   882 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:28 · PVG 05:28 · LAX 14:28 · JFK 17:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.