codon - LLVMを使用した高性能、ゼロオーバーヘッド、拡張可能なPythonコンパイラ

(A high-performance, zero-overhead, extensible Python compiler using LLVM)

Created at: 2021-09-28 01:56:34
Language: C++
License: NOASSERTION

コドン

ドキュメント|FAQ |ブログ|フォーラム|チャット|ベンチマーク

ビルドステータス

コドンとは何ですか?

Codon は高性能な Python コンパイラで、ランタイムのオーバーヘッドなしに Python コードをネイティブマシンコードにコンパイルします。 Pythonの典型的な高速化は、シングルスレッドで10〜100倍以上のオーダーです。コドンの性能は通常、 (時にはC / C ++よりも優れています)。Pythonとは異なり、Codonはネイティブマルチスレッドをサポートしているため、多くの高速化につながる可能性があります さらに高い倍。コドンはSeqプロジェクトから生まれました。

取り付ける

Linux (x86_64) および macOS (x86_64 および arm64) 用のビルド済みバイナリは、各リリースと共に入手できます。 以下を使用してダウンロードしてインストールします。

/bin/bash -c "$(curl -fsSL https://exaloop.io/install.sh)"

または、ソースからビルドすることもできます。

CodonはPython互換言語であり、多くのPythonプログラムは、変更を加えたとしてもほとんど動作しません。

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

コンパイラには、いくつかのオプションとモードがあります。

codon

# compile and run the program
codon run fib.py
# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

# compile and run the program with optimizations enabled
codon run -release fib.py
# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

# compile to executable with optimizations enabled
codon build -release -exe fib.py
./fib
# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

# compile to LLVM IR file with optimizations enabled
codon build -release -llvm fib.py
# outputs file fib.ll

その他のオプションと例については、ドキュメントを参照してください。

この素数のカウント例は、1行を追加することで有効になったCodonのOpenMPサポートを示しています。 注釈は、コンパイラに次の-ループを並列化するように指示します (この場合は動的スケジュールを使用して)、チャンクサイズ 100、および16スレッド。

@par
for

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)

CodonはGPUカーネルの書き込みと実行をサポートしています。マンデルブロ集合を計算する例を次に示します。

import gpu

MAX    = 1000  # maximum Mandelbrot iterations
N      = 4096  # width and height of image
pixels = [0 for _ in range(N * N)]

def scale(x, a, b):
    return a + (x/N)*(b - a)

@gpu.kernel
def mandelbrot(pixels):
    idx = (gpu.block.x * gpu.block.dim.x) + gpu.thread.x
    i, j = divmod(idx, N)
    c = complex(scale(j, -2.00, 0.47), scale(i, -1.12, 1.12))
    z = 0j
    iteration = 0

    while abs(z) <= 2 and iteration < MAX:
        z = z**2 + c
        iteration += 1

    pixels[idx] = int(255 * iteration/MAX)

mandelbrot(pixels, grid=(N*N)//1024, block=1024)

GPU プログラミングは、 の構文を使用して行うこともできます。

@par
@par(gpu=True)

コドンではないものは何ですか?

CodonはPythonの構文のほぼすべてをサポートしていますが、ドロップイン置換ではなく、大規模なコードベースでは変更が必要になる場合があります Codonコンパイラを介して実行されます。たとえば、PythonのモジュールのいくつかはまだCodon内に実装されておらず、Pythonのモジュールのいくつかは 動的機能は許可されません。Codon コンパイラーは、非互換性の識別と解決に役立つ詳細なエラー・メッセージを生成します。

Codon は、@codon.jit デコレータを介して、より大きな Python コードベース内で使用できます。 プレーンなPython関数とライブラリは、Pythonの相互運用性を介してCodon内から呼び出すこともできます。

ドキュメンテーション

詳細なドキュメントについては、docs.exaloop.io を参照してください。