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

ฟังก์ชัน __device__ และ __host__

ดูว่าฟังก์ชันแต่ละชนิดสามารถทำงานที่ใด

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

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

Beyond the Kernel

Kernels are entry points, but most code lives in helpers. CUDA gives two more qualifiers to say where a plain function may run. Let us meet them.

The __device__ Qualifier

A __device__ function runs on the GPU and is callable only from other GPU code, like a kernel or another device function.

__device__ int square(int x) {
    return x * x;
}

Helpers for Kernels

Think of __device__ functions as your GPU toolbox. They keep kernels tidy by holding logic the device threads reuse.

The __host__ Qualifier

The __host__ qualifier means ordinary CPU code. It is the default, so plain C++ functions are already host functions.

__host__ void setup() {
    // runs on the CPU
}

Default Is Host

Leave off all qualifiers and your function is host-only by default. That is why normal C++ still compiles inside a .cu file.

Both at Once

Combine the two and a function compiles for both sides. nvcc builds one version for the CPU and one for the GPU.

__host__ __device__ int dbl(int x) {
    return x + x;
}

Why Dual-Compile?

Marking a helper __host__ __device__ lets the same math run in CPU reference code and inside kernels, with zero duplication. ✨

Calling Rules

Host code may not call a __device__ function, and device code may not call a host-only one. The compiler enforces the boundary.

No __global__ from Device

A __device__ function is not launchable. Only __global__ kernels accept the triple-angle-bracket launch from the host.

Inlining for Speed

The compiler often inlines small __device__ functions, so wrapping logic in helpers usually costs nothing at runtime.

Pick the Right Tag

Choose by where it runs: __device__ for GPU helpers, __host__ for CPU code, and both when the logic is shared.

Quick Check

Time to test the device and host qualifiers.

Recap

You learned __device__ runs on the GPU, __host__ runs on the CPU, the default is host, and combining both shares one function across worlds. 🎯

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

บทเรียน “ฟังก์ชัน __device__ และ __host__” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ฟังก์ชัน __device__ และ __host__”

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

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

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

บทเรียน “ฟังก์ชัน __device__ และ __host__” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ตัวระบุคุณสมบัติฟังก์ชัน __global__
  2. ฟังก์ชัน __device__ และ __host__
  3. พื้นที่แอดเดรสแยกจากกัน
  4. วงจรชีวิตของโปรแกรม CUDA
← กลับไปที่ CUDA Academy