• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

codon

date: 2023-03-25 excerpt: codonの使い方

tag: codonpython


codonの使い方

概要

  • (静的な型付をした)pythonのコードをLLVMを介してコンパイルする仕組み
  • Cythonに似たものであるが、Cythonより書きやすい
  • アノテーションで並列化することができ、GILではないのか早い

インストール

/bin/bash -c "$(curl -fsSL https://exaloop.io/install.sh)"
  • GPUを利用したい場合はLLVMからコンパイルする必要がある
    • コンパイルは依存がかなり多く面倒

具体例

フィボナッチ数例

def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()
fib(1000)

ビルドして実行

$ codon run test.py
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

ビルドして実行バイナリを作成

$ codon build -release -exe test.py
$ ./test
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

openmpで並列化

from sys import argv

def is_prime(n):
    factors = 0
    for i in range(2, n):
        if n % i == 0:
            factors += 1
    return factors == 0

limit = int(argv[1])
total = 0

@par(schedule='dynamic', chunk_size=100, num_threads=16)
for i in range(2, limit):
    if is_prime(i):
        total += 1

print(total)

参考

  • Welcome to Codon
  • あなたのPythonを100倍高速にする技術 / Codon入門


codonpython Share Tweet