كتابة دالة نواة GPU
عبّر عن تعليمات الجهاز البرمجية في Mojo
كتابة دالة نواة GPU درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What a Kernel Is
A GPU kernel is the function each thread runs. You write the work for one element, and the hardware repeats it across many.
Think One Thread
The trick is to write a kernel from a single thread's view. Each thread does its own small slice of the work.
A Kernel Is Just an fn
In Mojo a kernel is a normal fn. Its strict, typed nature is exactly what device code needs to compile fast.
fn add_kernel():
passPassing in Buffers
A kernel takes pointers to device memory as parameters. These buffers hold the inputs and the output it will fill.
fn add_kernel(a: UnsafePointer[Float32], out: UnsafePointer[Float32]):
passFinding This Thread's Index
Inside the kernel, each thread computes its own position first. That index selects which element it must process.
var i = block_idx.x * block_dim.x + thread_idx.xGuarding the Bounds
Always check the index before touching memory. A bounds check keeps stray threads from reading past the array.
if i < n:
out[i] = a[i] + b[i]Doing the Element's Work
The body is tiny: read inputs, compute, write one result. The whole kernel often fits in a single line of math.
out[i] = a[i] * b[i]Launching the Kernel
You launch it by choosing a grid and block size. The launch fans your one-thread code out across the whole grid.
ctx.enqueue_function[add_kernel](grid_dim=blocks, block_dim=256)No Return Value
Kernels do not return results to the caller. They write into the output buffer, which you read back afterward.
Keep It Branch-Light
Threads run best in lockstep. Heavy branching makes lanes diverge and wait, so keep kernel logic simple and uniform.
Same Idea, Massive Scale
One short kernel plus a big grid equals millions of results. The scale comes from the launch, not from longer code.
Quick Check
You are writing the body of a GPU kernel for one thread.
Recap
A kernel is an fn for one thread: find your index, guard the bounds, do one element's math, then launch over a grid. ⚡
الأسئلة الشائعة
هل درس «كتابة دالة نواة GPU» مجاني؟
نعم — نص درس «كتابة دالة نواة GPU» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «كتابة دالة نواة GPU»؟
عبّر عن تعليمات الجهاز البرمجية في Mojo تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «كتابة دالة نواة GPU»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا نستخدم GPU لأعباء الذكاء الاصطناعي
- الخيوط والكتل والشبكات
- كتابة دالة نواة GPU
- نقل البيانات من الجهاز وإليه