0Pricing
CUDA Academy · บทเรียน

พอยน์เตอร์ไปยังหน่วยความจำ GPU

พอยน์เตอร์ของอุปกรณ์อยู่ในตัวแปรของโฮสต์

พอยน์เตอร์ไปยังหน่วยความจำ GPU เป็นบทเรียน CUDA Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน CUDA Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

A Pointer That Lives on the CPU

cudaMalloc hands you a normal C++ pointer variable. It sits in host memory, but the address inside it points into GPU memory. 🔗

The Address Means the Device

A device pointer holds a number that is only valid in the GPU's address space. To the CPU it is just an integer, not real RAM.

float* d_data;  // host variable, device address

The d_ Naming Habit

Programmers prefix device pointers with d_ and host pointers with h_. The name alone reminds you which side each one belongs to.

float* h_data;
float* d_data;

Do Not Dereference on the Host

Writing *d_data in CPU code is a trap. The host cannot touch GPU memory directly, so this crashes or reads nonsense.

Kernels Dereference It Freely

Inside a kernel the same device pointer is perfectly valid. There, d_data[i] reads and writes real GPU memory as you expect.

__global__ void k(float* d) {
    d[0] = 1.0f;
}

Pass It Into the Kernel

You hand the device pointer to a kernel as an argument. The GPU then uses that address to find your data.

myKernel<<<grid, block>>>(d_data);

Two Pointers, Two Spaces

Keep host and device pointers strictly apart. A host pointer is meaningless on the GPU, and a device pointer is meaningless on the CPU.

cudaMemcpy Bridges the Gap

To get data between the two, you copy it. cudaMemcpy takes both pointers and moves bytes across the divide for you.

Pointer Arithmetic Still Works

You may offset a device pointer on the host, like d_data + n, to point at a slice. You just must not read through it there.

float* d_tail = d_data + 100;

Mixing Them Up Is Subtle

The compiler will not stop you from confusing the two. Many CUDA bugs are simply a host pointer handed where a device pointer belonged.

Discipline Beats Debugging

Clear naming and a fixed convention save hours. Decide your d_ and h_ scheme early and never break it. 🧹

Quick Check

Let us test where a device pointer can safely be dereferenced.

Recap

You learned a device pointer lives in a host variable but addresses GPU memory: usable inside kernels, off-limits to direct host access. 🎉

คำถามที่พบบ่อย

บทเรียน “พอยน์เตอร์ไปยังหน่วยความจำ GPU” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พอยน์เตอร์ไปยังหน่วยความจำ GPU” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส CUDA Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “พอยน์เตอร์ไปยังหน่วยความจำ GPU”

พอยน์เตอร์ของอุปกรณ์อยู่ในตัวแปรของโฮสต์ คุณปฏิบัติ CUDA Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน CUDA Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน CUDA Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “พอยน์เตอร์ไปยังหน่วยความจำ GPU” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน CUDA Academy นี้ได้ไหม

ได้ บทเรียน CUDA Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. cudaMalloc และ cudaFree
  2. พอยน์เตอร์ไปยังหน่วยความจำ GPU
  3. เริ่มต้นข้อมูลด้วย cudaMemset
  4. หลีกเลี่ยงหน่วยความจำรั่วและการคืนซ้ำ
← กลับไปที่ CUDA Academy