0Pricing
Mojo Academy · Lección

Combinar paralelismo y vectorización

Añada hilos sobre SIMD.

Combinar paralelismo y vectorización es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Two Kinds of Speed

SIMD packs many values into one core's instruction. Threads spread work across cores. Stacking both gives you the most. ⚡

Outer Layer: Threads

Use parallelize on the outer level so each core owns a chunk of the data. That is the coarse split across cores.

parallelize[do_chunk](workers)

Inner Layer: Vectors

Inside each chunk, use vectorize so one core chews through its slice in SIMD packs. That is the fine split per core.

from algorithm import vectorize

Import Both Helpers

Both tools live in the algorithm module, so you can bring them in together and combine them in one function.

from algorithm import parallelize, vectorize

The Chunk Worker

Each chunk worker computes its start and end, then hands its own slice to vectorize for the SIMD sweep.

fn do_chunk(c: Int):
    var start = c * chunk

Vectorize the Inner Range

Within the worker, call vectorize on just this chunk's length. The SIMD width sets how many lanes run at once.

vectorize[step, width](end - start)

Offset Into the Data

The inner step gets a local index, so add the chunk start to reach the right spot in the full array.

fn step[w: Int](i: Int):
    out.store[width=w](start + i, ...)

Pick the SIMD Width

Match the vector width to your hardware lanes with simdwidthof so each core uses its registers fully.

alias width = simdwidthof[DType.float32]()

Cores Times Lanes

The win multiplies: many cores, each doing many lanes per step. That product is why combined code is so fast.

Keep Chunks Independent

This stacking only works because chunks never touch each other's data. Independence keeps the two layers safe.

Measure the Combined Gain

Benchmark scalar, vector-only, and combined versions. The numbers reveal how much each layer contributes.

Quick Check

You combine threads and SIMD on the same workload.

Recap

You wrap parallelize over chunks and call vectorize inside each, offsetting by the chunk start, so cores times lanes multiply your throughput. 🚀

Preguntas frecuentes

¿La lección «Combinar paralelismo y vectorización» es gratis?

Sí — el texto completo de «Combinar paralelismo y vectorización» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Combinar paralelismo y vectorización»?

Añada hilos sobre SIMD. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.

¿Cuánto tiempo toma la lección «Combinar paralelismo y vectorización»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. La función parallelize
  2. Dividir el trabajo en fragmentos
  3. Combinar paralelismo y vectorización
  4. Evitar condiciones de carrera
← Volver a Mojo Academy