0Pricing
Mojo Academy · درس

حساب SIMD عنصرًا بعنصر

اجمع المتجهات واضربها دفعة واحدة

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

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

Math on Whole Vectors

The best part of SIMD is that arithmetic works on the entire vector at once. One operation touches every lane together.

Adding Two Vectors

Add two SIMD values and each lane is added to its matching lane. This is called element-wise addition.

var a = SIMD[DType.int32, 4](1, 2, 3, 4)
var b = SIMD[DType.int32, 4](10, 20, 30, 40)
var c = a + b

Lane Lines Up with Lane

In a + b, lane 0 adds to lane 0 and lane 1 adds to lane 1. The lanes never mix, they stay aligned.

print(c[0])

Multiplying Vectors

Multiplication is element-wise too. Each lane of one vector is multiplied by the same lane of the other vector.

var p = a * b

Subtract and Divide

Subtraction and division follow the same rule. Every lane is handled independently and in parallel across the pack.

var d = b - a

Scalar Meets Vector

Add a single number to a vector and Mojo applies it to every lane. The scalar spreads across all positions at once.

var bumped = a + 100

Widths Must Match

To combine two SIMD values they need the same DType and the same width. Mismatched lane counts will not line up.

Comparisons Go Lane by Lane

Comparing two vectors gives a SIMD of Bools, one True or False per lane. Each lane reports its own result.

var mask = a > SIMD[DType.int32, 4](2)

Reducing to One Number

Sometimes you want a single answer. reduce_add sums all lanes into one scalar, perfect for totals.

var total = a.reduce_add()
print(total)

Built-in Math Functions

Standard math like sqrt also works lane-wise on SIMD, computing the result for every element in one call.

from math import sqrt
var roots = sqrt(a.cast[DType.float32]())

Same Code, Many Results

Writing a + b looks like scalar math but produces many results at once. That readability with speed is Mojo's promise.

Quick Check

You compute a + b where both are SIMD[DType.int32, 4]. How is the addition done?

Recap

Element-wise SIMD math runs +, -, *, and / on every lane at once, scalars spread to all lanes, and reduce_add folds a vector into one number. ✨

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

هل درس «حساب SIMD عنصرًا بعنصر» مجاني؟

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

ماذا ستتعلم في «حساب SIMD عنصرًا بعنصر»؟

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

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

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

كم من الوقت يستغرق درس «حساب SIMD عنصرًا بعنصر»؟

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

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

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

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

  1. التعرّف على نوع SIMD
  2. حساب SIMD عنصرًا بعنصر
  3. اختيار عرض SIMD
  4. توجيه حلقة باستخدام المتجهات
← العودة إلى Mojo Academy