متجهات وتحويلات Thrust
التوازي بأسلوب STL على وحدة GPU
متجهات وتحويلات Thrust درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 deviceAllocation 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.
ماذا ستتعلم في «متجهات وتحويلات Thrust»؟
التوازي بأسلوب STL على وحدة GPU تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟
لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «متجهات وتحويلات Thrust»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟
نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- استخدام cuBLAS GEMM على النحو الصحيح
- متجهات وتحويلات Thrust
- Reduce وScan وSort في Thrust
- cuDNN للتعلّم العميق