Skip to content

Commit

Permalink
Update Mojo examples (modular#817)
Browse files Browse the repository at this point in the history
Changes as of modularml/modular@132eea1

Co-authored-by: modularbot <[email protected]>
  • Loading branch information
scottamain and modularbot authored Sep 16, 2023
1 parent 6ecab31 commit 6aaa0e4
Show file tree
Hide file tree
Showing 8 changed files with 1,122 additions and 1,057 deletions.
6 changes: 4 additions & 2 deletions examples/mandelbrot.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn mandelbrot_kernel_SIMD[
for i in range(MAX_ITERS):
if not t.reduce_or():
break
y2 = y*y
y2 = y * y
y = x.fma(y + y, cy)
t = x.fma(x, y2) <= 4
x = x.fma(x, cx - y2)
Expand Down Expand Up @@ -80,7 +80,9 @@ fn main():
fn bench_parallel[simd_width: Int]():
parallelize[worker](rt, height, height)

let parallelized_ms = Benchmark().run[bench_parallel[simd_width]]() / 1e6
let parallelized_ms = Benchmark().run[
bench_parallel[simd_width]
]() / 1e6
print("Parallelized:", parallelized_ms, "ms")
print("Parallel speedup:", vectorized_ms / parallelized_ms)

Expand Down
40 changes: 12 additions & 28 deletions examples/nbody.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/nbody.html

from utils.index import StaticTuple
from algorithm import unroll
from math import sqrt
from benchmark import Benchmark

Expand Down Expand Up @@ -48,8 +47,8 @@ alias NUM_BODIES = 5
fn offset_momentum(inout bodies: StaticTuple[NUM_BODIES, Planet]):
var p = SIMD[DType.float64, 4]()

@parameter
fn _iter[i: Int]():
@unroll
for i in range(NUM_BODIES):
p += bodies[i].velocity * bodies[i].mass

var body = bodies[0]
Expand All @@ -59,10 +58,9 @@ fn offset_momentum(inout bodies: StaticTuple[NUM_BODIES, Planet]):


fn advance(inout bodies: StaticTuple[NUM_BODIES, Planet], dt: Float64):
@parameter
fn _outer[i: Int]():
@parameter
fn _inner[j: Int]():
@unroll
for i in range(NUM_BODIES):
for j in range(NUM_BODIES - i - 1):
var body_i = bodies[i]
var body_j = bodies[j + i + 1]
let diff = body_i.pos - body_j.pos
Expand All @@ -75,42 +73,31 @@ fn advance(inout bodies: StaticTuple[NUM_BODIES, Planet], dt: Float64):
bodies[i] = body_i
bodies[j + i + 1] = body_j

unroll[NUM_BODIES - i - 1, _inner]()

unroll[NUM_BODIES, _outer]()

@parameter
fn _update[i: Int]():
@unroll
for i in range(NUM_BODIES):
var body = bodies[i]
body.pos += dt * body.velocity
bodies[i] = body

unroll[NUM_BODIES, _update]()


fn energy(bodies: StaticTuple[NUM_BODIES, Planet]) -> Float64:
var e: Float64 = 0

@parameter
fn _outer[i: Int]():
@unroll
for i in range(NUM_BODIES):
let body_i = bodies[i]
e += (
0.5
* body_i.mass
* ((body_i.velocity * body_i.velocity).reduce_add())
)

@parameter
fn _inner[j: Int]():
for j in range(NUM_BODIES - i - 1):
let body_j = bodies[j + i + 1]
let diff = body_i.pos - body_j.pos
let distance = sqrt((diff * diff).reduce_add())
e -= (body_i.mass * body_j.mass) / distance

unroll[NUM_BODIES - i - 1, _inner]()

unroll[NUM_BODIES, _outer]()

return e


Expand Down Expand Up @@ -189,15 +176,12 @@ fn run():
)
offset_momentum(system)

let derived_energy: String = "Energy of System: "
print(derived_energy)
print(energy(system))
print("Energy of System:", energy(system))

for i in range(50_000_000):
advance(system, 0.01)

print(derived_energy)
print(energy(system))
print("Energy of System:", energy(system))


fn benchmark():
Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/BoolMLIR.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"source" :
[
"Mojo is a high-level programming language with an extensive set of modern features. But Mojo also provides you, the programmer, access to all of the low-level primitives that you need to write powerful -- yet zero-cost -- abstractions.\n",
"Mojo is a high-level programming language with an extensive set of modern features. Mojo also provides you, the programmer, access to all of the low-level primitives that you need to write powerful -- yet zero-cost -- abstractions.\n",
"\n",
"These primitives are implemented in [MLIR](https://mlir.llvm.org), an extensible intermediate representation (IR) format for compiler design. Many different programming languages and compilers translate their source programs into MLIR, and because Mojo provides direct access to MLIR features, this means Mojo programs can enjoy the benefits of each of these tools.\n",
"\n",
Expand Down
Loading

0 comments on commit 6aaa0e4

Please sign in to comment.