コアの並列化とチューニング
スレッドを追加し、マシンに合わせて自動調整します
「コアの並列化とチューニング」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
From Fast to Faster
Your kernel already beats Python on one core. Now spread it across all your cores and let Mojo tune it for the machine. ⚡
Why Go Parallel
Modern CPUs have many cores sitting idle. Parallelism lets each core work on part of the data at the same time for a big speedup.
Meet parallelize
Mojo's parallelize runs loop chunks across threads for you. You give it the work and how many pieces to split it into.
from algorithm import parallelizeWrap Work in a Closure
Express one chunk of work as a small function. parallelize calls it once per chunk, each on its own core.
fn work(chunk: Int):
process(chunk)
parallelize[work](num_chunks)Split the Range
Divide the data into roughly equal chunks, one per core. Balanced pieces keep every thread busy instead of one lagging behind.
var per_chunk = n // num_chunksStack SIMD on Threads
Keep your SIMD math inside each chunk. Now you get two wins at once: many cores, each crunching vectors per instruction.
Avoid Data Races
Two threads writing the same spot causes a data race. Give each chunk its own output slice so writes never collide.
Expose Tunable Knobs
Turn tile size and SIMD width into parameters. With knobs in place, you can try many settings without rewriting the kernel.
fn kernel[tile: Int, width: Int]():
passLet Autotuning Search
The best settings differ per machine. Autotuning tries candidate parameter values, times each, and keeps the fastest combination.
Lock In the Winner
Once you know the best tile and width, fix them as the chosen config. The kernel is now specialized for your hardware.
Verify Under Parallelism
Run the tuned, threaded kernel and compare to the baseline. Correctness must still hold once many cores write at once.
Quick Check
Pick the real risk when parallelizing the core.
Recap
You used parallelize over balanced chunks, kept SIMD inside each, avoided data races, then autotuned the knobs. The core is now multi-core fast. 🎯
よくある質問
「コアの並列化とチューニング」レッスンは無料ですか?
はい。「コアの並列化とチューニング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「コアの並列化とチューニング」で何を学びますか?
スレッドを追加し、マシンに合わせて自動調整します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「コアの並列化とチューニング」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Pythonベースラインのプロファイリング
- Mojoでホットパスを書き直す
- コアの並列化とチューニング
- 高速化したプロジェクトのリリース