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

เวกเตอร์และการแปลงของ Thrust

การประมวลผลแบบขนานสไตล์ STL บน GPU

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

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

The STL for Your GPU

Thrust is a high-level library that feels like the C++ STL but runs on the GPU. You write almost no kernel code yourself. 🎉

Two Kinds of Vector

Thrust gives you host_vector for CPU memory and device_vector for GPU memory. They look identical but live in different worlds.

thrust::host_vector<int> h(100);
thrust::device_vector<int> d(100);

Copy Across the Boundary

Assigning a host_vector to a device_vector triggers a memory transfer automatically. No cudaMemcpy boilerplate to write by hand.

thrust::device_vector<int> d = h; // host to device

Allocation Is Automatic

A device_vector allocates and frees GPU memory for you using RAII. When it goes out of scope, the memory is released cleanly.

Fill and Sequence

Helpers like thrust::fill and thrust::sequence initialize a whole device_vector in one parallel call instead of a manual loop.

thrust::sequence(d.begin(), d.end()); // 0,1,2,...

Transform Maps Element to Element

thrust::transform applies a function to every element in parallel and writes the result to an output range. This is the classic map pattern.

thrust::transform(d.begin(), d.end(),
  out.begin(), op);

Functors Are the Operation

The operation you pass is a functor: a struct with an operator() marked __host__ __device__ so it can run on the GPU.

struct Square { __host__ __device__
  float operator()(float x){ return x*x; } };

Built-In Operators

For common math, Thrust ships functors like thrust::plus and thrust::multiplies, so you skip writing a functor for simple jobs.

thrust::transform(a.begin(), a.end(),
  b.begin(), c.begin(), thrust::plus<float>());

Two Inputs, One Output

The binary form of transform takes two input ranges and combines them pairwise, perfect for element-wise vector addition.

Get the Raw Pointer

Need to drop into a hand-written kernel? thrust::raw_pointer_cast hands you the underlying device pointer to pass along.

float* p = thrust::raw_pointer_cast(d.data());

Less Code, Fewer Bugs

Thrust hides allocation, copies, and launch configs. You trade a little control for readable, safe GPU code that just works.

Quick Check

Recall how Thrust applies an operation to a whole vector.

Recap

You used device_vector for automatic memory, transferred data by assignment, and ran transform with functors. STL-style GPU power. 💪

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

บทเรียน “เวกเตอร์และการแปลงของ Thrust” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “เวกเตอร์และการแปลงของ Thrust”

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

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

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

บทเรียน “เวกเตอร์และการแปลงของ Thrust” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ใช้ GEMM ของ cuBLAS ให้ถูกต้อง
  2. เวกเตอร์และการแปลงของ Thrust
  3. Reduce, Scan และ Sort ของ Thrust
  4. cuDNN สำหรับการเรียนรู้เชิงลึก
← กลับไปที่ CUDA Academy