โหลดแบบเวกเตอร์ด้วย float4
ธุรกรรมที่กว้างขึ้นเพื่อแบนด์วิดท์ที่มากขึ้น
โหลดแบบเวกเตอร์ด้วย float4 เป็นบทเรียน CUDA Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน CUDA Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Move More Per Instruction
A normal load fetches one value at a time. A vectorized load grabs several adjacent values in a single wider instruction, doing more work per request.
Meet float4
CUDA ships built-in vector types. A float4 packs four floats into one 16-byte bundle you can load or store together.
float4 v = make_float4(1, 2, 3, 4);
float first = v.x; // also .y .z .wOne Load, Four Floats
Reading a float4 from memory pulls four floats in a single transaction. That cuts the number of memory instructions a thread must issue by four.
float4 a = reinterpret_cast<float4*>(in)[i];Reinterpret the Pointer
To use vector loads on a plain float array, you cast the pointer. reinterpret_cast lets you read the same bytes as float4 elements.
float4* in4 = reinterpret_cast<float4*>(in);
float4 chunk = in4[idx];Alignment Is Required
A float4 must sit on a 16-byte boundary. If the data is not properly aligned, the load is illegal and your kernel can crash or read garbage.
cudaMalloc Aligns for You
Good news: buffers from cudaMalloc are aligned to at least 256 bytes. So freshly allocated device arrays are safe to read as float4 from the start.
Fewer Requests, More Bandwidth
Wider loads mean fewer outstanding requests for the same data. That can raise achieved bandwidth when a kernel is limited by memory, not math.
Index by Vectors, Not Floats
With float4 each thread now covers four elements. Your global index steps through float4 slots, so the total thread count drops by four.
int i = blockIdx.x * blockDim.x + threadIdx.x;
float4 d = in4[i]; // covers 4 floatsOther Vector Widths
float4 is not the only choice. Types like float2 and int4 give you 8- or 16-byte loads, so you can pick a width that fits your data.
Watch the Leftovers
If your array length is not a multiple of four, handle the extra remainder elements with a normal scalar loop after the vectorized part.
It Costs Registers
A float4 holds four values, so it uses more registers per thread. As always, confirm the bandwidth gain outweighs any drop in occupancy.
Quick Check
You reinterpret a float array as float4 but the kernel crashes. Why?
Recap: Wider Is Faster
You learned that float4 loads four floats at once, cutting instructions and lifting bandwidth. Keep data aligned and handle leftover elements. 📦
คำถามที่พบบ่อย
บทเรียน “โหลดแบบเวกเตอร์ด้วย float4” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “โหลดแบบเวกเตอร์ด้วย float4” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส CUDA Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โหลดแบบเวกเตอร์ด้วย float4”
ธุรกรรมที่กว้างขึ้นเพื่อแบนด์วิดท์ที่มากขึ้น คุณปฏิบัติ CUDA Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน CUDA Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน CUDA Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “โหลดแบบเวกเตอร์ด้วย float4” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน CUDA Academy นี้ได้ไหม
ได้ บทเรียน CUDA Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การขนานระดับคำสั่ง
- คลี่ลูปด้วย #pragma unroll
- โหลดแบบเวกเตอร์ด้วย float4
- แรงกดดันของรีจิสเตอร์และการสปิล