Skip to content

Commit

Permalink
Cleaned up Mandelbrot example.
Browse files Browse the repository at this point in the history
  • Loading branch information
janpfeifer committed Jul 5, 2024
1 parent 9c05b14 commit 40c228a
Showing 1 changed file with 48 additions and 114 deletions.
162 changes: 48 additions & 114 deletions examples/mandelbrot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"id": "ca704995",
"metadata": {},
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using 24 workers.\n",
"\tElapsed: 2.646615ms\n"
"\tElapsed: 2.448217ms\n"
]
},
{
Expand Down Expand Up @@ -80,7 +82,6 @@
"\n",
" // Set up worker pool of runtime.NumCPU() to process rows.\n",
" // We assign a whole row as a task because pixel would be to fine-grained.\n",
" fmt.Printf(\"Using %d workers.\\n\", runtime.NumCPU())\n",
" var wg sync.WaitGroup\n",
" for _ = range runtime.NumCPU() {\n",
" wg.Add(1)\n",
Expand All @@ -104,10 +105,11 @@
"}\n",
"\n",
"%%\n",
"fmt.Printf(\"Using %d workers.\\n\", runtime.NumCPU())\n",
"start := time.Now()\n",
"img := mandelbrotGo(480, 480, 128, -2, -2, 2, 2)\n",
"fmt.Printf(\"\\tElapsed: %s\\n\", time.Since(start))\n",
"must.M(gonbui.DisplayImage(img))\n"
"must.M(gonbui.DisplayImage(img))"
]
},
{
Expand All @@ -132,15 +134,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\t- Added replace rule for module \"github.com/gomlx/gopjrt\" to local directory \"/home/janpf/Projects/gopjrt\".\n",
"Set: LD_LIBRARY_PATH=\"/usr/local/lib\"\n"
"\t- Added replace rule for module \"github.com/gomlx/gopjrt\" to local directory \"/home/janpf/Projects/gopjrt\".\n"
]
}
],
"source": [
"!*rm -f go.work && go work init && go work use . ${HOME}/Projects/gopjrt\n",
"%goworkfix\n",
"%env LD_LIBRARY_PATH=/usr/local/lib"
"%goworkfix"
]
},
{
Expand All @@ -153,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 13,
"id": "5e54861d",
"metadata": {},
"outputs": [
Expand All @@ -162,9 +162,9 @@
"output_type": "stream",
"text": [
"Compiling XLA program:\n",
"\tElapsed: 4.093068988s\n",
"\tElapsed: 3.966579204s\n",
"Generating image:\n",
"\tElapsed: 96.028904ms\n"
"\tElapsed: 88.594484ms\n"
]
},
{
Expand Down Expand Up @@ -291,7 +291,7 @@
" }\n",
"\n",
" // This finishes the building of the computation, which we then immediately compile and leave it ready to\n",
" // execute.\n",
" // be executed.\n",
" computation := must.M1(builder.Build(result))\n",
" builder.Destroy()\n",
" m.exec = must.M1(m.client.Compile().WithComputation(computation).Done())\n",
Expand All @@ -312,6 +312,7 @@
" // of a flat slice, and the dimensions (width x height). Since we know the dimensions, we just\n",
" // discard it here.\n",
" flat, _ := must.M2(pjrt.BufferToArray[float32](outputBuffer))\n",
" outputBuffer.Destroy()\n",
"\n",
" // Create the image from the returned matrix, with the number of iterations.\n",
" // Notice we take the `log(#iterations + 1)` as the color to decrease the contrast.\n",
Expand Down Expand Up @@ -362,96 +363,26 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 14,
"id": "fb197cde-685d-4093-a009-9d9a03c2a98b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<style>\n",
".gonb-err-location {\n",
"\tbackground: var(--jp-err-color2); \n",
"\tborder-radius: 3px;\n",
"\tborder-style: dotted;\n",
"\tborder-width: 1px;\n",
"\tborder-color: var(--jp-border-color2);\n",
"}\n",
".gonb-err-location:hover {\n",
"\tborder-width: 2px;\n",
"\tborder-style: solid;\n",
"\tborder-color: var(--jp-border-color2);\n",
"}\n",
".gonb-err-context {\n",
"\tdisplay: none;\n",
"}\n",
".gonb-err-location:hover + .gonb-err-context {\n",
"\tbackground: var(--jp-dialog-background); \n",
"\tborder-radius: 3px;\n",
"\tborder-style: solid;\n",
"\tborder-width: 1px;\n",
"\tborder-color: var(--jp-border-color2);\n",
"\tdisplay: block;\n",
"\twhite-space: pre;\n",
"\tfont-family: monospace;\n",
"}\n",
".gonb-err-line {\n",
"\tborder-radius: 3px;\n",
"\tborder-style: dotted;\n",
"\tborder-width: 1px;\t\n",
"\tborder-color: var(--jp-border-color2);\n",
"\tbackground-color: var(--jp-rendermime-err-background);\n",
"\tfont-weight: bold;\n",
"}\n",
".gonb-cell-line-info {\n",
"\tbackground: var(--jp-layout-color2);\n",
"\tcolor: #999;\n",
"\tmargin: 0.1em;\n",
"\tborder: 1px solid var(--jp-border-color1);\n",
"\tpadding-left: 0.2em;\n",
"\tpadding-right: 0.2em;\n",
"}\n",
"</style>\n",
"<div class=\"lm-Widget p-Widget lm-Panel p-Panel jp-OutputArea-child\">\n",
"<div class=\"lm-Widget p-Widget jp-RenderedText jp-mod-trusted jp-OutputArea-output\" data-mime-type=\"application/vnd.jupyter.stderr\" style=\"font-family: monospace;\">\n",
"\n",
"\n",
"<span style=\"white-space: pre;\"> # gonb_3e22db50 [gonb_3e22db50.test]</span>\n",
"\n",
"<br/>\n",
"\n",
"<span class=\"gonb-cell-line-info\">Cell[6]: Line 10</span>\n",
"<span class=\"gonb-err-location\">./main_test.go:33:9: </span> undefined: mandelbrotGo\n",
"<div class=\"gonb-err-context\">\n",
")\n",
"\n",
"func BenchmarkMandelbrotGo(b *testing.B) {\n",
"<div class=\"gonb-err-line\"> _ = mandelbrotGo(benchmarkWidth, benchmarkHeight, benchmarkIterations, -2, -2, 2, 2)\n",
"</div>}\n",
"\n",
"\n",
"</div>\n",
"\n",
"<br/>\n",
"\n",
"\n",
"<span style=\"white-space: pre;\"> </span>\n",
"\n",
"<br/>\n",
"\n",
"</div>\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"ename": "ERROR",
"evalue": "failed to run \"/home/janpf/src/golang/go/bin/go test -c -o /tmp/gonb_3e22db50/gonb_3e22db50\": exit status 1",
"output_type": "error",
"traceback": [
"failed to run \"/home/janpf/src/golang/go/bin/go test -c -o /tmp/gonb_3e22db50/gonb_3e22db50\": exit status 1"
"name": "stdout",
"output_type": "stream",
"text": [
"goos: linux\n",
"goarch: amd64\n",
"pkg: gonb_aed47ed4\n",
"cpu: 12th Gen Intel(R) Core(TM) i9-12900K\n",
"BenchmarkMandelbrotGo\n",
"BenchmarkMandelbrotGo-24 \t 2\t 695240989 ns/op\n",
"BenchmarkMandelbrotPJRT\n",
"BenchmarkMandelbrotPJRT/compile\n",
"BenchmarkMandelbrotPJRT/compile-24 \t 7\t 154958414 ns/op\n",
"BenchmarkMandelbrotPJRT/exec\n",
"BenchmarkMandelbrotPJRT/exec-24 \t 1\t2574761008 ns/op\n",
"PASS\n"
]
}
],
Expand All @@ -461,20 +392,22 @@
"const (\n",
" benchmarkWidth = 10000\n",
" benchmarkHeight = 10000\n",
" benchmarkIterations = 256\n",
" benchmarkIterations = 128 // 256\n",
")\n",
"\n",
"func BenchmarkMandelbrotGo(b *testing.B) {\n",
" _ = mandelbrotGo(benchmarkWidth, benchmarkHeight, benchmarkIterations, -2, -2, 2, 2)\n",
" for i := 0; i < b.N; i++ {\n",
" _ = mandelbrotGo(benchmarkWidth, benchmarkHeight, benchmarkIterations, -2, -2, 2, 2)\n",
" }\n",
"}\n",
"\n",
"func BenchmarkMandelbrotPJRT(b *testing.B) {\n",
" var m *MandelbrotGenerator\n",
" b.Run(\"compile\", func (b *testing.B) {\n",
" if m != nil {\n",
" m.Finalize()\n",
" }\n",
" for i := 0; i < b.N; i++ {\n",
" if m != nil {\n",
" m.Finalize()\n",
" }\n",
" m = NewMandelbrotGenerator(benchmarkWidth, benchmarkHeight, benchmarkIterations)\n",
" }\n",
" })\n",
Expand All @@ -483,6 +416,7 @@
" _ = m.Exec(-2, -2, 2, 2) \n",
" }\n",
" })\n",
" m.Finalize()\n",
"}"
]
},
Expand All @@ -500,7 +434,7 @@
"\n",
"| **Configuration** | **Go CPU** | **PJRT CPU** | **PJRT GPU** |\n",
"|:---:|:---:|:---:|:---:|\n",
"| Intel i9-12900K+Nvidia2080Ti GPU | 7.2s | 3.2s | 2.4 |\n"
"| Intel i9-12900K+Nvidia2080Ti GPU | 7.2s | 3.3s | 2.4 |\n"
]
},
{
Expand Down Expand Up @@ -533,7 +467,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 17,
"id": "86baba0f-0709-419c-8a9b-b8473e6547db",
"metadata": {},
"outputs": [
Expand All @@ -542,12 +476,11 @@
"output_type": "stream",
"text": [
"Go implementation:\n",
"Using 24 workers.\n",
"\tElapsed: 3.749589181s\n",
"\tElapsed: 7.222290479s\n",
"Compiling XLA program:\n",
"\tElapsed: 3.148206608s\n",
"\tElapsed: 443.354499ms\n",
"Generating image:\n",
"\tElapsed: 2.21864421s\n"
"\tElapsed: 3.278511284s\n"
]
}
],
Expand All @@ -556,10 +489,10 @@
"const (\n",
" benchmarkWidth = 10000\n",
" benchmarkHeight = 10000\n",
" benchmarkIterations = 128\n",
" benchmarkIterations = 256\n",
")\n",
"\n",
"%% --plugin=cuda\n",
"%% --plugin=cpu\n",
"iters := benchmarkIterations\n",
"fmt.Println(\"Go implementation:\")\n",
"start := time.Now()\n",
Expand All @@ -574,13 +507,14 @@
"fmt.Println(\"Generating image:\")\n",
"start = time.Now()\n",
"_ = m.Exec(minX, minY, maxX, maxY)\n",
"fmt.Printf(\"\\tElapsed: %s\\n\", time.Since(start))"
"fmt.Printf(\"\\tElapsed: %s\\n\", time.Since(start))\n",
"m.Finalize()"
]
}
],
"metadata": {
"jupytext": {
"formats": "ipynb,auto:percent"
"formats": "ipynb,go:percent"
},
"kernelspec": {
"display_name": "Go (gonb)",
Expand Down

0 comments on commit 40c228a

Please sign in to comment.