Files
metabuilder/mojo/examples/life
johndoe6345789 d772201bf1 feat: Replace placeholder examples with official Modular Mojo examples
Added official examples from github.com/modular/modular:
- gpu-intro, gpu-functions, gpu-block-and-warp (GPU programming)
- layout_tensor, layouts (tensor operations)
- life (Conway's Game of Life)
- operators (custom operators)
- process (subprocess handling)
- python-interop (Python integration)
- testing (unit test patterns)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 17:40:19 +00:00
..

Introduction to Mojo tutorial solution

This directory contains a complete solution for the Get started with Mojo tutorial project, which is an implementation of Conway's Game of Life cellular automation.

Files

This directory contains the following files:

  • The source files lifev1.mojo and gridv1.mojo provide an initial version of the project, with a Grid struct representing the grid of cells as a List[List[Int]].

  • The source files lifev2.mojo and gridv2.mojo provide a subsequent version of the project, with a Grid struct representing the grid of cells as a block of memory managed by UnsafePointer.

  • The source files lifev3.mojo and gridv3.mojo provide a subsequent version of the project to parallelize the evolution of rows across available CPU cores.

  • The benchmark.mojo file performs a simple performance benchmark of the three versions by running 1,000 evolutions of each Grid implementation using a 1,024 x 1,024 grid.

  • The test directory contains unit tests for each Grid implementation using the Mojo testing framework.

  • The pixi.toml file is a Pixi project file containing the project dependencies and task definitions.

Run the code

If you have Pixi installed, you can execute version 1 of the program by running the following command:

pixi run lifev1

This displays a window that shows an initial random state for the grid and then automatically updates it with subsequent generations. Quit the program by pressing the q or <Escape> key or by closing the window.

You can execute version 2 or version 3 of the program by running one of the following two commands:

pixi run lifev2
pixi run lifev3

Just like for version 1, this displays a window that shows an initial random state for the grid and then automatically updates it with subsequent generations. Quit the program by pressing the q or <Escape> key or by closing the window.

You can execute the benchmark program by running the following command:

pixi run benchmark

You can run the unit tests by running the following command:

pixi run test

Dependencies

This project includes an example of using a Python package, pygame-ce, from Mojo. Building the program does not embed pygame or a Python runtime in the resulting executable. Therefore, to run this program your environment must have both a compatible Python runtime (Python 3.12) and the pygame package installed.

The easiest way to ensure that the runtime dependencies are met is to run the program with pixi, which manages a virtual environment for the project as defined by the pixi.toml file.