การเขียนฟังก์ชันเคอร์เนล GPU
เขียนโค้ดอุปกรณ์ใน Mojo
การเขียนฟังก์ชันเคอร์เนล GPU เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What a Kernel Is
A GPU kernel is the function each thread runs. You write the work for one element, and the hardware repeats it across many.
Think One Thread
The trick is to write a kernel from a single thread's view. Each thread does its own small slice of the work.
A Kernel Is Just an fn
In Mojo a kernel is a normal fn. Its strict, typed nature is exactly what device code needs to compile fast.
fn add_kernel():
passPassing in Buffers
A kernel takes pointers to device memory as parameters. These buffers hold the inputs and the output it will fill.
fn add_kernel(a: UnsafePointer[Float32], out: UnsafePointer[Float32]):
passFinding This Thread's Index
Inside the kernel, each thread computes its own position first. That index selects which element it must process.
var i = block_idx.x * block_dim.x + thread_idx.xGuarding the Bounds
Always check the index before touching memory. A bounds check keeps stray threads from reading past the array.
if i < n:
out[i] = a[i] + b[i]Doing the Element's Work
The body is tiny: read inputs, compute, write one result. The whole kernel often fits in a single line of math.
out[i] = a[i] * b[i]Launching the Kernel
You launch it by choosing a grid and block size. The launch fans your one-thread code out across the whole grid.
ctx.enqueue_function[add_kernel](grid_dim=blocks, block_dim=256)No Return Value
Kernels do not return results to the caller. They write into the output buffer, which you read back afterward.
Keep It Branch-Light
Threads run best in lockstep. Heavy branching makes lanes diverge and wait, so keep kernel logic simple and uniform.
Same Idea, Massive Scale
One short kernel plus a big grid equals millions of results. The scale comes from the launch, not from longer code.
Quick Check
You are writing the body of a GPU kernel for one thread.
Recap
A kernel is an fn for one thread: find your index, guard the bounds, do one element's math, then launch over a grid. ⚡
คำถามที่พบบ่อย
บทเรียน “การเขียนฟังก์ชันเคอร์เนล GPU” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเขียนฟังก์ชันเคอร์เนล GPU” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเขียนฟังก์ชันเคอร์เนล GPU”
เขียนโค้ดอุปกรณ์ใน Mojo คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การเขียนฟังก์ชันเคอร์เนล GPU” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดภาระงานปัญญาประดิษฐ์จึงใช้ GPU
- เธรด บล็อก และกริด
- การเขียนฟังก์ชันเคอร์เนล GPU
- การย้ายข้อมูลไปยังและออกจากอุปกรณ์