parallelize 함수
반복 횟수를 여러 스레드에서 실행합니다.
parallelize 함수은(는) CoddyKit의 무료 Mojo Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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 parallelizeThe 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_coresCap 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Mojo Academy 강의 전체를 잠금 해제할 수 있습니다. Mojo Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“parallelize 함수”에서 뭘 배우나요?
반복 횟수를 여러 스레드에서 실행합니다. 브라우저에서 직접 실행하는 실습 코드로 Mojo Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Mojo Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Mojo Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“parallelize 함수” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Mojo Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Mojo Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- parallelize 함수
- 작업을 청크로 나누기
- 병렬 처리와 벡터화 결합하기
- 데이터 경합 피하기