|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "source": [ |
| 6 | + "!curl -ssL https://magic.modular.com/ | bash" |
| 7 | + ], |
| 8 | + "metadata": { |
| 9 | + "id": "A8X6phvz7ZoQ", |
| 10 | + "outputId": "21c85116-eb45-4bff-fe32-ba59589954be", |
| 11 | + "colab": { |
| 12 | + "base_uri": "https://localhost:8080/" |
| 13 | + } |
| 14 | + }, |
| 15 | + "execution_count": 1, |
| 16 | + "outputs": [ |
| 17 | + { |
| 18 | + "output_type": "stream", |
| 19 | + "name": "stdout", |
| 20 | + "text": [ |
| 21 | + "Installing the latest version of Magic...\n", |
| 22 | + " % Total % Received % Xferd Average Speed Time Time Time Current\n", |
| 23 | + " Dload Upload Total Spent Left Speed\n", |
| 24 | + " 0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0\n", |
| 25 | + "100 49.9M 100 49.9M 0 0 9574k 0 0:00:05 0:00:05 --:--:-- 72.0M\n", |
| 26 | + "Done. The 'magic' binary is in '/root/.modular/bin'\n", |
| 27 | + "\n", |
| 28 | + "Two more steps:\n", |
| 29 | + "1. To use 'magic', run this command so it's in your PATH:\n", |
| 30 | + "source /root/.bashrc\n", |
| 31 | + "2. To build with MAX and Mojo, go to http://modul.ar/get-started\n" |
| 32 | + ] |
| 33 | + } |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "source": [ |
| 39 | + "import os\n", |
| 40 | + "os.environ['PATH'] += ':/root/.modular/bin'" |
| 41 | + ], |
| 42 | + "metadata": { |
| 43 | + "id": "n7zS_6gK7fnB" |
| 44 | + }, |
| 45 | + "execution_count": 2, |
| 46 | + "outputs": [] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "source": [ |
| 51 | + "!magic init gpu_puzzles --format mojoproject" |
| 52 | + ], |
| 53 | + "metadata": { |
| 54 | + "id": "Zlg5BNMn7j64", |
| 55 | + "outputId": "d8c7e15e-af56-4a3e-8340-984d2df2c267", |
| 56 | + "colab": { |
| 57 | + "base_uri": "https://localhost:8080/" |
| 58 | + } |
| 59 | + }, |
| 60 | + "execution_count": 3, |
| 61 | + "outputs": [ |
| 62 | + { |
| 63 | + "output_type": "stream", |
| 64 | + "name": "stdout", |
| 65 | + "text": [ |
| 66 | + "\u001b[32m✔ \u001b[0mCreated /content/gpu_puzzles/mojoproject.toml\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "source": [ |
| 74 | + "%cd gpu_puzzles/" |
| 75 | + ], |
| 76 | + "metadata": { |
| 77 | + "id": "mO77-mj17lsA", |
| 78 | + "outputId": "669fb4cf-44e7-44f7-bb8a-bf7c0ebb2832", |
| 79 | + "colab": { |
| 80 | + "base_uri": "https://localhost:8080/" |
| 81 | + } |
| 82 | + }, |
| 83 | + "execution_count": 4, |
| 84 | + "outputs": [ |
| 85 | + { |
| 86 | + "output_type": "stream", |
| 87 | + "name": "stdout", |
| 88 | + "text": [ |
| 89 | + "/content/gpu_puzzles\n" |
| 90 | + ] |
| 91 | + } |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "source": [ |
| 97 | + "%%writefile add_vectors.mojo\n", |
| 98 | + "\n", |
| 99 | + "### Add vectors\n", |
| 100 | + "### Mojo kernel for adding corresponding elements of vectors a and b, store in out.\n", |
| 101 | + "\n", |
| 102 | + "from gpu.host import DeviceContext\n", |
| 103 | + "from memory import UnsafePointer\n", |
| 104 | + "from gpu import thread_idx, block_idx, block_dim\n", |
| 105 | + "from testing import assert_equal\n", |
| 106 | + "\n", |
| 107 | + "alias SIZE = 4\n", |
| 108 | + "alias BLOCKS_PER_GRID = 1\n", |
| 109 | + "alias THREADS_PER_BLOCK = SIZE\n", |
| 110 | + "alias dtype = DType.float32\n", |
| 111 | + "\n", |
| 112 | + "\n", |
| 113 | + "fn add(\n", |
| 114 | + " out: UnsafePointer[Scalar[dtype]],\n", |
| 115 | + " a: UnsafePointer[Scalar[dtype]],\n", |
| 116 | + " b: UnsafePointer[Scalar[dtype]],\n", |
| 117 | + "):\n", |
| 118 | + " tid = block_idx.x * block_dim.x + thread_idx.x\n", |
| 119 | + " if tid < SIZE:\n", |
| 120 | + " out[tid] = a[tid] + b[tid]\n", |
| 121 | + "\n", |
| 122 | + "\n", |
| 123 | + "fn main() raises:\n", |
| 124 | + " ctx = DeviceContext()\n", |
| 125 | + " d_array_buff_1 = ctx.enqueue_create_buffer[dtype](SIZE)\n", |
| 126 | + " d_array_buff_2 = ctx.enqueue_create_buffer[dtype](SIZE)\n", |
| 127 | + " d_out_buff = ctx.enqueue_create_buffer[dtype](SIZE)\n", |
| 128 | + " expected = ctx.enqueue_create_host_buffer[dtype](SIZE)\n", |
| 129 | + " _ = d_out_buff.enqueue_fill(0)\n", |
| 130 | + " _ = expected.enqueue_fill(SIZE - 1)\n", |
| 131 | + "\n", |
| 132 | + " with d_array_buff_1.map_to_host() as h_array_buff_1:\n", |
| 133 | + " for i in range(SIZE):\n", |
| 134 | + " h_array_buff_1[i] = i\n", |
| 135 | + "\n", |
| 136 | + " with d_array_buff_2.map_to_host() as h_array_buff_2:\n", |
| 137 | + " for i in range(SIZE - 1, -1, -1):\n", |
| 138 | + " h_array_buff_2[SIZE - 1 - i] = i\n", |
| 139 | + "\n", |
| 140 | + " ctx.enqueue_function[add](\n", |
| 141 | + " d_out_buff.unsafe_ptr(),\n", |
| 142 | + " d_array_buff_1.unsafe_ptr(),\n", |
| 143 | + " d_array_buff_2.unsafe_ptr(),\n", |
| 144 | + " grid_dim=BLOCKS_PER_GRID,\n", |
| 145 | + " block_dim=THREADS_PER_BLOCK,\n", |
| 146 | + " )\n", |
| 147 | + "\n", |
| 148 | + " ctx.synchronize()\n", |
| 149 | + "\n", |
| 150 | + " with d_out_buff.map_to_host() as h_out_buff:\n", |
| 151 | + " print(h_out_buff)\n", |
| 152 | + " for i in range(SIZE):\n", |
| 153 | + " assert_equal(h_out_buff[i], expected[i])\n" |
| 154 | + ], |
| 155 | + "metadata": { |
| 156 | + "id": "UT3V1O2M7txw", |
| 157 | + "colab": { |
| 158 | + "base_uri": "https://localhost:8080/" |
| 159 | + }, |
| 160 | + "outputId": "786e9c23-3d97-4238-fcac-117ba6e8555f" |
| 161 | + }, |
| 162 | + "execution_count": 27, |
| 163 | + "outputs": [ |
| 164 | + { |
| 165 | + "output_type": "stream", |
| 166 | + "name": "stdout", |
| 167 | + "text": [ |
| 168 | + "Overwriting add_vectors.mojo\n" |
| 169 | + ] |
| 170 | + } |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "source": [ |
| 176 | + "!magic run mojo add_vectors.mojo" |
| 177 | + ], |
| 178 | + "metadata": { |
| 179 | + "id": "CkjRGISm7y1Q", |
| 180 | + "outputId": "d05fe142-8743-4ecc-9401-48e532b5ef0a", |
| 181 | + "colab": { |
| 182 | + "base_uri": "https://localhost:8080/" |
| 183 | + } |
| 184 | + }, |
| 185 | + "execution_count": 26, |
| 186 | + "outputs": [ |
| 187 | + { |
| 188 | + "output_type": "stream", |
| 189 | + "name": "stdout", |
| 190 | + "text": [ |
| 191 | + "\u001b[32m⠁\u001b[0m \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2KHostBuffer([3.0, 3.0, 3.0, 3.0])\n" |
| 192 | + ] |
| 193 | + } |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "source": [ |
| 199 | + "!magic run mojo format add_vectors.mojo" |
| 200 | + ], |
| 201 | + "metadata": { |
| 202 | + "colab": { |
| 203 | + "base_uri": "https://localhost:8080/" |
| 204 | + }, |
| 205 | + "id": "Cc2XVTrevpy5", |
| 206 | + "outputId": "112f97cd-da96-4c3a-831e-f9f4eae633d9" |
| 207 | + }, |
| 208 | + "execution_count": 24, |
| 209 | + "outputs": [ |
| 210 | + { |
| 211 | + "output_type": "stream", |
| 212 | + "name": "stdout", |
| 213 | + "text": [ |
| 214 | + "\u001b[32m⠁\u001b[0m \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[1mreformatted add_vectors.mojo\u001b[0m\n", |
| 215 | + "\n", |
| 216 | + "\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n", |
| 217 | + "\u001b[34m\u001b[1m1 file \u001b[0m\u001b[1mreformatted\u001b[0m.\n" |
| 218 | + ] |
| 219 | + } |
| 220 | + ] |
| 221 | + } |
| 222 | + ], |
| 223 | + "metadata": { |
| 224 | + "colab": { |
| 225 | + "name": "Welcome To Colab", |
| 226 | + "provenance": [], |
| 227 | + "gpuType": "T4" |
| 228 | + }, |
| 229 | + "kernelspec": { |
| 230 | + "display_name": "Python 3", |
| 231 | + "name": "python3" |
| 232 | + }, |
| 233 | + "accelerator": "GPU" |
| 234 | + }, |
| 235 | + "nbformat": 4, |
| 236 | + "nbformat_minor": 0 |
| 237 | +} |
0 commit comments