0Pricing
CUDA Academy · درس

حصر الأجهزة واختيارها

‏cudaSetDevice وسياقات كل وحدة GPU

حصر الأجهزة واختيارها درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

More Than One GPU

A single machine can hold several GPUs. To use them all, your program first needs to discover how many devices are present before it sends any work.

Counting the Devices

One call tells you how many GPUs the runtime can see. cudaGetDeviceCount writes that number into an int you hand it.

int count;
cudaGetDeviceCount(&count);

Devices Are Numbered

Each GPU gets an integer id from 0 up to count minus one. That device id is how you point the runtime at one specific card.

Picking the Active Device

You choose which GPU your next calls target with cudaSetDevice. After this, allocations and launches go to that card.

cudaSetDevice(1);

There Is a Current Device

At any moment exactly one GPU is the current device for the calling thread. Every cudaMalloc or kernel launch lands on whatever device is current.

Inspecting a Device

Before trusting a GPU you can read its specs. cudaGetDeviceProperties fills a struct with name, memory size, and compute capability.

cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);

Reading the Properties

The properties struct is rich. Fields like name and totalGlobalMem help you pick the best card or skip one that is too small.

Each Device Owns Its Memory

A pointer from cudaMalloc belongs to whichever GPU was current then. Using it while another device is current is an error waiting to happen.

Per-Device Contexts

Behind each GPU sits a context that holds its allocations and streams. Switching the current device switches you into that device's context.

Looping Over All GPUs

A common pattern is a loop that calls cudaSetDevice for each id, then does setup on that card. This is how you spread work across every device.

for (int d = 0; d < count; d++) {
  cudaSetDevice(d);
}

Restore Before You Leave

If a helper changes the current device, switch it back when done. Leaving the current device changed can surprise the rest of your code.

Quick Check

Recall which call chooses the GPU your next allocations and launches will use.

Recap

You count GPUs, pick one with cudaSetDevice, and inspect it with properties. Each device owns its own memory and context. Next: splitting work across them. ✨

الأسئلة الشائعة

هل درس «حصر الأجهزة واختيارها» مجاني؟

نعم — نص درس «حصر الأجهزة واختيارها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.

ماذا ستتعلم في «حصر الأجهزة واختيارها»؟

‏cudaSetDevice وسياقات كل وحدة GPU تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟

لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «حصر الأجهزة واختيارها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟

نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. حصر الأجهزة واختيارها
  2. تقسيم العمل بين وحدات GPU
  3. الوصول إلى الذاكرة من نظير إلى نظير
  4. استخدام وحدات GPU متعددة مع NCCL
← العودة إلى CUDA Academy