0Pricing
CUDA Academy · درس

cudaMalloc وcudaFree

حجز الذاكرة العامة وتحريرها.

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

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

The GPU Has Its Own RAM

Your GPU does not borrow the computer's main memory. It owns a separate pool called global memory that kernels read and write. 🧠

You Must Ask for It

Before a kernel can use device memory, you reserve a chunk yourself. The function that does this reserving is cudaMalloc.

The cudaMalloc Signature

cudaMalloc takes the address of a pointer and a byte count. It fills that pointer with a fresh device address.

float* d_data;
cudaMalloc(&d_data, n * sizeof(float));

Why a Pointer to a Pointer

You pass &d_data so cudaMalloc can write the new address back into your variable. The return value is reserved for an error code.

Always Count in Bytes

cudaMalloc thinks in raw bytes, not elements. Multiply your element count by sizeof(type) so the buffer is the right size.

cudaMalloc(&d_arr, count * sizeof(int));

Give It Back with cudaFree

When you finish with a buffer, return it to the GPU using cudaFree. This releases the global memory for other work.

cudaFree(d_data);

Pairs, Like Lock and Key

Treat cudaMalloc and cudaFree as a pair. Every allocation you make should have exactly one matching free.

Memory Is Uninitialized

Fresh memory from cudaMalloc holds garbage values. Reading it before you write or copy data gives unpredictable results.

Allocation Can Fail

If the GPU is out of memory, cudaMalloc returns an error instead of crashing. Always check that return code in real code.

Global Memory Is Plentiful but Slow

Buffers from cudaMalloc live in global memory: gigabytes of space, yet the slowest tier on the chip. Use it generously but mind access patterns.

The Rhythm of Every Program

Nearly every CUDA program follows the same beat: cudaMalloc, copy data in, launch a kernel, copy results out, then cudaFree.

Quick Check

Let us confirm how cudaMalloc receives its target pointer.

Recap

You learned to reserve GPU memory with cudaMalloc, sized in bytes, and to release it with cudaFree in matched pairs. 🎉

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

هل درس «cudaMalloc وcudaFree» مجاني؟

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

ماذا ستتعلم في «cudaMalloc وcudaFree»؟

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

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

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

كم من الوقت يستغرق درس «cudaMalloc وcudaFree»؟

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

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

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

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

  1. cudaMalloc وcudaFree
  2. مؤشرات إلى ذاكرة GPU
  3. تهيئة الذاكرة باستخدام cudaMemset
  4. تجنّب تسريبات الذاكرة والتحرير المزدوج
← العودة إلى CUDA Academy