ชนิดแจกแจงทิศทางการคัดลอก
ทำความเข้าใจ cudaMemcpyKind และเลือกใช้ให้ถูกต้อง
ชนิดแจกแจงทิศทางการคัดลอก เป็นบทเรียน CUDA Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน CUDA Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Fourth Argument
Every cudaMemcpy ends with a direction flag of type cudaMemcpyKind. It tells the runtime which way the bytes travel. 🧭
cudaMemcpy(dst, src, bytes, kind);Host to Device
Use cudaMemcpyHostToDevice to upload inputs from CPU RAM into GPU memory before a kernel runs.
cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);Device to Host
Use cudaMemcpyDeviceToHost to download results from GPU memory back into CPU RAM after computing.
cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);Device to Device
To copy between two GPU buffers without touching the CPU, reach for cudaMemcpyDeviceToDevice.
cudaMemcpy(d_b, d_a, bytes, cudaMemcpyDeviceToDevice);Host to Host
There is even cudaMemcpyHostToHost, a plain CPU copy. You rarely need it since standard memcpy already does the job.
Let the Driver Decide
With cudaMemcpyDefault, the runtime infers direction from the pointers. It needs unified addressing to work safely.
cudaMemcpy(dst, src, bytes, cudaMemcpyDefault);The Direction Must Match
The flag must agree with your real pointers. A device pointer with a HostToHost kind triggers an error or a crash.
A Common Mix-Up
Swapping HostToDevice and DeviceToHost is a frequent bug. The compiler stays silent, so read each call carefully. 🐛
Read It Like a Sentence
The enum name spells the data flow: HostToDevice means from host to device. Match it to your source and destination.
Pair It With the Pointers
Look at which buffer is the source and which is the destination, then pick the kind that names that exact path.
When in Doubt, Be Explicit
Prefer the named direction over cudaMemcpyDefault. Being explicit makes your intent obvious and catches mistakes sooner.
Quick Check
You are copying one device buffer into another device buffer, never touching the CPU. Which kind fits?
Recap
You met every cudaMemcpyKind: HostToDevice, DeviceToHost, DeviceToDevice, HostToHost, and Default. Match the flag to your pointers every time. 🎉
คำถามที่พบบ่อย
บทเรียน “ชนิดแจกแจงทิศทางการคัดลอก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ชนิดแจกแจงทิศทางการคัดลอก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส CUDA Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ชนิดแจกแจงทิศทางการคัดลอก”
ทำความเข้าใจ cudaMemcpyKind และเลือกใช้ให้ถูกต้อง คุณปฏิบัติ CUDA Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน CUDA Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน CUDA Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ชนิดแจกแจงทิศทางการคัดลอก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน CUDA Academy นี้ได้ไหม
ได้ บทเรียน CUDA Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ถ่ายโอนข้อมูลจากโฮสต์ไปอุปกรณ์
- ถ่ายโอนข้อมูลจากอุปกรณ์ไปโฮสต์
- ชนิดแจกแจงทิศทางการคัดลอก
- คอขวดการถ่ายโอน PCIe