0Pricing
Mojo Academy · レッスン

parallelize関数

ループの反復処理を複数のスレッドで実行します

「parallelize関数」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

One Core Is Not Enough

A plain loop runs on a single core while the rest sit idle. For heavy work, that wastes most of your CPU.

for i in range(n):
    out[i] = heavy(i)

Spread the Work

Parallelism means handing different loop iterations to different cores so they all work at the same time. 🧵

Meet parallelize

Mojo's parallelize helper runs your loop body across many threads for you, so you skip the manual thread plumbing.

from algorithm import parallelize

The Work Closure

You write a small function that does the work for one index. Mojo calls it many times, once per iteration.

fn work(i: Int):
    out[i] = heavy(i)

Pass the Closure as a Parameter

The closure goes in square brackets as a compile-time parameter, not as a normal call argument. Mojo specializes on it.

parallelize[work](n)

The Count Is Runtime

The number in the parentheses is how many work items to run. That count is an ordinary runtime value.

parallelize[work](num_items)

Ask the Machine for Cores

Use num_physical_cores from the sys module to learn how many real cores you have to spread work across.

from sys import num_physical_cores

Cap the Worker Count

An optional second argument sets how many workers run at once. Matching it to your cores often works best.

parallelize[work](n, num_physical_cores())

Each Call Is Independent

parallelize assumes work items do not depend on each other's order. Each call should stand on its own.

Best for Heavy Loops

Parallelism shines when each item does real work. For tiny loops, the thread overhead can cost more than it saves.

Measure the Speedup

Time the loop before and after going parallel. A real benchmark tells you whether the extra threads actually helped.

Quick Check

You want to run a loop body across several CPU cores in Mojo.

Recap

You import parallelize, pass a per-item closure as a bracket parameter and the count in parentheses, then size workers to your cores for a real speedup. 🚀

よくある質問

「parallelize関数」レッスンは無料ですか?

はい。「parallelize関数」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「parallelize関数」で何を学びますか?

ループの反復処理を複数のスレッドで実行します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「parallelize関数」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. parallelize関数
  2. 処理のチャンク分割
  3. 並列処理とベクトル処理の組み合わせ
  4. データ競合の回避
← Mojo Academyに戻る