การย้ายข้อมูลไปยังและออกจากอุปกรณ์
จัดการการถ่ายโอนหน่วยความจำระหว่างโฮสต์กับอุปกรณ์
การย้ายข้อมูลไปยังและออกจากอุปกรณ์ เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Two Separate Memories
The CPU and GPU each have their own memory. The GPU cannot read your host arrays until the data lives on the device.
Host and Device
We call the CPU side the host and the GPU side the device. Moving data between them is a core part of GPU work.
Allocating on the Device
First you reserve space on the GPU for your data. This device buffer is where inputs and outputs will live.
var d_a = ctx.enqueue_create_buffer[DType.float32](n)Copying Inputs Over
Next you copy your host arrays into those device buffers. This upload gets the GPU the numbers it needs.
ctx.enqueue_copy(d_a, h_a)Running the Kernel
With data on the device, you launch the kernel. It reads and writes the device buffers, never the host ones.
ctx.enqueue_function[kernel](grid_dim=blocks, block_dim=256)Copying Results Back
The output sits on the GPU until you fetch it. A download copies the result buffer back into host memory.
ctx.enqueue_copy(h_out, d_out)Work Is Queued, Not Instant
These calls are enqueued on the GPU, not finished at once. You must synchronize before trusting the results on the host.
ctx.synchronize()The Round Trip
The full cycle is upload, compute, download. Picture data making a round trip from host to device and back.
Transfers Cost Time
Copying over the bus is slow compared to compute. Each transfer can erase your speedup if you do it too often.
Keep Data on the Device
Chain several kernels on the same buffers before copying back. Minimizing round trips keeps the GPU win intact.
Free What You Allocate
Device memory is limited, so release buffers you no longer need. Mojo's ownership rules help free them at the right time.
Quick Check
You want a GPU program to actually run faster end to end.
Recap
Upload inputs to the device, run the kernel, then download results and synchronize, while keeping costly transfers to a minimum. 🔁
คำถามที่พบบ่อย
บทเรียน “การย้ายข้อมูลไปยังและออกจากอุปกรณ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การย้ายข้อมูลไปยังและออกจากอุปกรณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การย้ายข้อมูลไปยังและออกจากอุปกรณ์”
จัดการการถ่ายโอนหน่วยความจำระหว่างโฮสต์กับอุปกรณ์ คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การย้ายข้อมูลไปยังและออกจากอุปกรณ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดภาระงานปัญญาประดิษฐ์จึงใช้ GPU
- เธรด บล็อก และกริด
- การเขียนฟังก์ชันเคอร์เนล GPU
- การย้ายข้อมูลไปยังและออกจากอุปกรณ์