-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mandelbrot Set benchmarks Mojo vs Codon vs Python
- Loading branch information
Showing
11 changed files
with
261 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
38 changes: 38 additions & 0 deletions
38
benchmarks/multibrot_set/multibrot_mojo_parallelize.exe.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"results": [ | ||
{ | ||
"command": "./benchmarks/multibrot_set/multibrot_mojo_parallelize", | ||
"mean": 0.0071393926600000004, | ||
"stddev": 0.000596373347825188, | ||
"median": 0.00717595916, | ||
"user": 0.036535200000000004, | ||
"system": 0.006670119999999999, | ||
"min": 0.0062225636599999995, | ||
"max": 0.00826972966, | ||
"times": [ | ||
0.00666552166, | ||
0.00710289666, | ||
0.00676443866, | ||
0.0062225636599999995, | ||
0.007547188660000001, | ||
0.007657730660000001, | ||
0.00724902166, | ||
0.0066388136599999995, | ||
0.00727602166, | ||
0.00826972966 | ||
], | ||
"exit_codes": [ | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0 | ||
] | ||
} | ||
] | ||
} |
102 changes: 102 additions & 0 deletions
102
benchmarks/multibrot_set/multibrot_mojo_parallelize.mojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from complex import ComplexSIMD | ||
from math import iota | ||
from algorithm import parallelize, vectorize | ||
from tensor import Tensor | ||
from utils.index import Index | ||
|
||
alias float_type = DType.float64 | ||
alias simd_width = 2 * simdwidthof[float_type]() | ||
|
||
alias width = 960 | ||
alias height = 960 | ||
alias MAX_ITERS = 200 | ||
|
||
alias min_x = -2.0 | ||
alias max_x = 0.6 | ||
alias min_y = -1.5 | ||
alias max_y = 1.5 | ||
|
||
|
||
fn mandelbrot_kernel_SIMD[ | ||
simd_width: Int | ||
](c: ComplexSIMD[float_type, simd_width]) -> SIMD[float_type, simd_width]: | ||
"""A vectorized implementation of the inner mandelbrot computation.""" | ||
let cx = c.re | ||
let cy = c.im | ||
var x = SIMD[float_type, simd_width](0) | ||
var y = SIMD[float_type, simd_width](0) | ||
var y2 = SIMD[float_type, simd_width](0) | ||
var iters = SIMD[float_type, simd_width](0) | ||
|
||
var t: SIMD[DType.bool, simd_width] = True | ||
for i in range(MAX_ITERS): | ||
if not t.reduce_or(): | ||
break | ||
y2 = y * y | ||
y = x.fma(y + y, cy) | ||
t = x.fma(x, y2) <= 4 | ||
x = x.fma(x, cx - y2) | ||
iters = t.select(iters + 1, iters) | ||
return iters | ||
|
||
|
||
fn compute_multibrot_parallelized() -> Tensor[float_type]: | ||
let t = Tensor[float_type](height, width) | ||
|
||
@parameter | ||
fn worker(row: Int): | ||
let scale_x = (max_x - min_x) / width | ||
let scale_y = (max_y - min_y) / height | ||
|
||
@parameter | ||
fn compute_vector[simd_width: Int](col: Int): | ||
"""Each time we operate on a `simd_width` vector of pixels.""" | ||
let cx = min_x + (col + iota[float_type, simd_width]()) * scale_x | ||
let cy = min_y + row * scale_y | ||
let c = ComplexSIMD[float_type, simd_width](cx, cy) | ||
t.data().simd_store[simd_width]( | ||
row * width + col, mandelbrot_kernel_SIMD[simd_width](c) | ||
) | ||
|
||
# Vectorize the call to compute_vector where call gets a chunk of pixels. | ||
vectorize[simd_width, compute_vector](width) | ||
|
||
# Parallelized | ||
parallelize[worker](height, height) | ||
return t | ||
|
||
|
||
def main(): | ||
_ = compute_multibrot_parallelized() | ||
|
||
# let multibrot = compute_multibrot_parallelized() | ||
# try: | ||
# _ = show_plot(multibrot) | ||
# except e: | ||
# print("failed to show plot:", e) | ||
|
||
|
||
def show_plot(tensor: Tensor[float_type]): | ||
alias scale = 10 | ||
alias dpi = 64 | ||
|
||
from python import Python | ||
np = Python.import_module("numpy") | ||
plt = Python.import_module("matplotlib.pyplot") | ||
colors = Python.import_module("matplotlib.colors") | ||
|
||
numpy_array = np.zeros((height, width), np.float64) | ||
|
||
for row in range(height): | ||
for col in range(width): | ||
numpy_array.itemset((col, row), tensor[col, row]) | ||
|
||
fig = plt.figure(1, [scale, scale * height // width], dpi) | ||
ax = fig.add_axes((0.0, 0.0, 1.0, 1.0)) | ||
light = colors.LightSource(315, 10, 0, 1, 1, 0) | ||
|
||
image = light.shade(numpy_array, plt.cm.hot, colors.PowerNorm(0.3), "hsv", 0, 0, 1.5) | ||
plt.imshow(image) | ||
plt.axis("off") | ||
plt.savefig("multibrot_mojo_ parallelize.mojo.png") | ||
plt.show() |