cudaMemcpyAsync ในสตรีม
ถ่ายโอนแบบไม่บล็อกซึ่งทำงานซ้อนกันได้
cudaMemcpyAsync ในสตรีม เป็นบทเรียน CUDA Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน CUDA Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Blocking by Default
Plain cudaMemcpy stops your CPU until the copy finishes. Your host thread just waits, doing nothing useful while bytes move.
The Async Cousin
Meet cudaMemcpyAsync. It queues the copy and returns to your CPU immediately, so the host can keep working while the transfer happens.
It Needs a Stream
The async copy takes an extra argument: a stream. The stream is the ordered queue where the transfer waits its turn to run.
cudaMemcpyAsync(d_data, h_data, bytes, cudaMemcpyHostToDevice, stream);Pinned or Bust
Here is the catch: async copies only run truly non-blocking from pinned host memory. Pass a pageable buffer and CUDA silently falls back to a blocking copy.
Returns, Does Not Finish
When the call returns, the copy has only been scheduled, not completed. The data may still be in flight, so do not read it yet.
Order Within a Stream
Work in one stream runs in order, one item after another. So a copy issued before a kernel in the same stream is guaranteed to finish first.
Wait When You Must
Before touching the results on the CPU, make sure the queue drained. Call cudaStreamSynchronize to block until that stream's work is done.
cudaStreamSynchronize(stream);The Whole Point: Overlap
Async copies let a transfer in one stream run while a kernel in another stream computes. That concurrency is how you hide transfer time.
Beware the Default Stream
If you pass stream 0, the legacy default, it can serialize against everything else. Use your own created streams to actually get overlap.
Keep the Buffer Alive
Since the copy finishes later, the host buffer must stay valid until then. Free it too early and the in-flight transfer reads garbage.
A Typical Pattern
The classic flow is async copy in, launch the kernel, then async copy out, all in one stream. Sync only at the very end.
cudaMemcpyAsync(d_in, h_in, bytes, cudaMemcpyHostToDevice, stream);
kernel<<<grid, block, 0, stream>>>(d_in, d_out);Quick Check
What does cudaMemcpyAsync require to be genuinely non-blocking?
Recap
cudaMemcpyAsync queues a copy in a stream and returns at once, but needs pinned memory to overlap. Sync the stream before reading results. ⚡
คำถามที่พบบ่อย
บทเรียน “cudaMemcpyAsync ในสตรีม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “cudaMemcpyAsync ในสตรีม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส CUDA Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “cudaMemcpyAsync ในสตรีม”
ถ่ายโอนแบบไม่บล็อกซึ่งทำงานซ้อนกันได้ คุณปฏิบัติ CUDA Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน CUDA Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน CUDA Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “cudaMemcpyAsync ในสตรีม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน CUDA Academy นี้ได้ไหม
ได้ บทเรียน CUDA Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำไมหน่วยความจำแบบแบ่งหน้าได้จึงช้า
- หน่วยความจำตรึงด้วย cudaMallocHost
- cudaMemcpyAsync ในสตรีม
- ไปป์ไลน์แบบบัฟเฟอร์คู่