atomicAdd وما شابهها
عمليات القراءة والتعديل والكتابة العتادية.
atomicAdd وما شابهها درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
One Indivisible Step
An atomic operation reads, modifies, and writes a value as a single step that no other thread can interrupt. That kills the race. ⚛️
Meet atomicAdd
atomicAdd takes a pointer and a value, adds safely, and returns the old value. Many threads can hit the same address with no lost updates.
atomicAdd(total, 1);Fixing the Racy Counter
Swap the plain increment for atomicAdd and the count becomes correct every single time, no matter how many threads run.
__global__ void count(int* total) {
atomicAdd(total, 1);
}The Return Value Is Useful
atomicAdd returns the value held before your add. That old value is a unique slot you can use as an index or a ticket number.
int slot = atomicAdd(counter, 1);A Whole Family
atomicAdd has many friends: atomicSub, atomicMax, atomicMin, atomicExch, atomicAnd, atomicOr, and atomicXor all work the same atomic way.
Swapping a Value
atomicExch stores a new value and returns the old one in one step. It is handy for claiming flags or grabbing a previous state safely.
int prev = atomicExch(flag, 1);Atomic Max and Min
atomicMax updates the target only if your value is larger. It is perfect for finding a global maximum across all threads without races.
atomicMax(best, myValue);Supported Types
Atomics cover int, unsigned, and on newer hardware float and double too. Always check that your type and arch support the call you want.
Atomics Have a Cost
Atomics serialize threads that target the same address. Heavy contention on one spot becomes a bottleneck, so use them only where needed.
Spread the Load
Reduce contention by having threads update different addresses, then combine results. Fewer collisions means atomics stay fast.
Atomics vs Reductions
For summing huge arrays, a tree reduction often beats a flood of atomics. Reach for atomics when updates are sparse or irregular.
Quick Check
Test your grip on atomicAdd's behavior.
Recap: atomicAdd and Friends
You met the atomic family, fixed the racy counter, and learned atomics return the old value but cost time under contention. ✅
الأسئلة الشائعة
هل درس «atomicAdd وما شابهها» مجاني؟
نعم — نص درس «atomicAdd وما شابهها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.
ماذا ستتعلم في «atomicAdd وما شابهها»؟
عمليات القراءة والتعديل والكتابة العتادية. تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟
لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «atomicAdd وما شابهها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟
نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- حالات التسابق على GPU
- atomicAdd وما شابهها
- بناء Histogram
- عمليات ذرية مخصصة باستخدام atomicCAS