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.mojoandgridv1.mojoprovide an initial version of the project, with aGridstruct representing the grid of cells as aList[List[Int]]. -
The source files
lifev2.mojoandgridv2.mojoprovide a subsequent version of the project, with aGridstruct representing the grid of cells as a block of memory managed byUnsafePointer. -
The source files
lifev3.mojoandgridv3.mojoprovide a subsequent version of the project to parallelize the evolution of rows across available CPU cores. -
The
benchmark.mojofile performs a simple performance benchmark of the three versions by running 1,000 evolutions of eachGridimplementation using a 1,024 x 1,024 grid. -
The
testdirectory contains unit tests for eachGridimplementation using the Mojo testing framework. -
The
pixi.tomlfile 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.