0Pricing
Mojo Academy · درس

تحسين الجداء الداخلي

وجّه جداء النقاط ووسّع حلقته

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

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

The Dot Product Core

The inner product multiplies two sequences element by element and adds the results. Speeding it up speeds your whole matmul.

var acc: Float32 = 0.0
for k in range(K):
    acc += a[k] * b[k]

One Lane at a Time Is Slow

A plain loop handles a single pair per step. Modern CPUs can do many at once, so scalar code leaves lanes idle.

Enter SIMD

A SIMD value packs several numbers and applies one operation to all of them together, doing real work in parallel per instruction.

var v = SIMD[DType.float32, 4](1, 2, 3, 4)

Load a Chunk at Once

Instead of one float, load a whole SIMD-width slice of each array in a single move, ready for vector math.

var va = a.load[width=4](k)
var vb = b.load[width=4](k)

Multiply Whole Vectors

Multiplying two SIMD values yields a vector of products in one step. The element-wise work happens across all lanes together.

var prod = va * vb  # 4 products at once

Accumulate Into a Vector

Keep a SIMD accumulator and add each product vector into it. You delay the final sum until the loop is done.

var acc = SIMD[DType.float32, 4](0)
acc += va * vb

Step by the Width

The loop now advances by the SIMD width, not by one. Four lanes wide means a quarter as many iterations.

for k in range(0, K, 4):
    acc += a.load[width=4](k) * b.load[width=4](k)

Reduce at the End

After the loop, collapse the lane accumulator into one number with a horizontal reduce, giving the final dot product.

var total = acc.reduce_add()

Unrolling the Loop

Unrolling processes several widths per iteration, cutting loop overhead and exposing more independent work to the CPU.

Handle the Remainder

If K is not a multiple of the width, a few elements are left over. A small tail loop finishes them one at a time.

for k in range(K - K % 4, K):
    total += a[k] * b[k]

Let Mojo Help

Mojo offers the vectorize helper to apply a width-parameterized body across a range, handling stepping and the tail for you.

Quick Check

After a SIMD-width dot product loop, why do you call reduce_add at the end?

Recap

Vectorize the inner product by loading SIMD chunks, multiplying lanes together, accumulating, then reduce; unroll and clean up the tail. ⚡

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

هل درس «تحسين الجداء الداخلي» مجاني؟

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

ماذا ستتعلم في «تحسين الجداء الداخلي»؟

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

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

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

كم من الوقت يستغرق درس «تحسين الجداء الداخلي»؟

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

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

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

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

  1. نمذجة موتر في Mojo
  2. بناء Matmul خطوة بخطوة
  3. تحسين الجداء الداخلي
  4. التحقق من الصحة العددية
← العودة إلى Mojo Academy