Skip to content

Commit

Permalink
make julia go vrrrm
Browse files Browse the repository at this point in the history
  • Loading branch information
jakee417 committed Mar 7, 2024
1 parent 3042f3b commit 1f788ec
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 225 deletions.
8 changes: 4 additions & 4 deletions _includes/ising_model_speed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Although faster than Python, it is still much slower than Cython."
"Although faster than Python, it is still ~2x slower than Cython."
]
},
{
Expand Down Expand Up @@ -1839,7 +1839,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"outputs": [
{
Expand All @@ -1849,7 +1849,7 @@
"[NbConvertApp] Converting notebook ising_model_speed.ipynb to markdown\n",
"/Users/jakeetaylor/opt/anaconda3/lib/python3.9/site-packages/nbconvert/filters/datatypefilter.py:39: UserWarning: Your element with mimetype(s) dict_keys(['application/vnd.plotly.v1+json']) is not able to be represented.\n",
" warn(\"Your element with mimetype(s) {mimetypes}\"\n",
"[NbConvertApp] Writing 23203 bytes to ising_model_speed.md\n"
"[NbConvertApp] Writing 23202 bytes to ising_model_speed.md\n"
]
},
{
Expand All @@ -1858,7 +1858,7 @@
"CompletedProcess(args=['jupyter', 'nbconvert', '--to', 'markdown', 'ising_model_speed.ipynb'], returncode=0)"
]
},
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
2 changes: 1 addition & 1 deletion _includes/ising_model_speed.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ numba_ising_step(field)
1.09 ms ± 7.63 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)


Although faster than Python, it is still much slower than Cython.
Although faster than Python, it is still ~2x slower than Cython.

### @njit(parallel=True) ~ 650µs
Before trying to speed this up, lets first understand a common exploit of the Ising Model. First, lets extract our outer for-loops and simply keep record of the `n`'s, `m`s, and offset.
Expand Down
68 changes: 30 additions & 38 deletions _includes/ising_model_speed_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"fn random_spin_field(N: Int, M: Int) -> Tensor[data_type]:\n",
" var t = rand[data_type](N, M)\n",
" for i in range(N):\n",
" for j in range(M):\n",
" if t[Index(i, j)] < 0.5:\n",
" if t[i, j] < 0.5:\n",
" t[Index(i, j)] = -1\n",
" else:\n",
" t[Index(i, j)] = 1\n",
Expand All @@ -62,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -75,11 +75,11 @@
" for j in range(m - 1, m + 2):\n",
" if i == n and j == m:\n",
" continue\n",
" total += field[Index(i % N, j % M)]\n",
" var dE = 2 * field[Index(n, m)] * total\n",
" total += field[i % N, j % M]\n",
" var dE = 2 * field[n, m] * total\n",
" if dE <= 0:\n",
" field[Index(n, m)] *= -1\n",
" elif exp(-dE * beta) > rand[data_type](1)[Index(0)]:\n",
" elif exp(-dE * beta) > rand[data_type](1)[0]:\n",
" field[Index(n, m)] *= -1"
]
},
Expand All @@ -92,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -116,29 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"@always_inline\n",
"fn bench() -> Report:\n",
" var N = 200\n",
" var M = 200\n",
" var field = random_spin_field(N, M)\n",
"\n",
" @always_inline\n",
" @parameter\n",
" fn ising_step_fn():\n",
" ising_step(field=field)\n",
"\n",
" return benchmark.run[ising_step_fn](max_runtime_secs=10)\n",
"\n",
"var report = bench()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 46,
"metadata": {},
"outputs": [
{
Expand All @@ -148,19 +126,33 @@
"---------------------\n",
"Benchmark Report (ms)\n",
"---------------------\n",
"Mean: 2.915081212121212\n",
"Total: 2404.942\n",
"Mean: 2.9266933333333331\n",
"Total: 2414.5219999999999\n",
"Iters: 825\n",
"Warmup Mean: 2.508\n",
"Warmup Total: 5.016\n",
"Warmup Mean: 2.5630000000000002\n",
"Warmup Total: 5.1260000000000003\n",
"Warmup Iters: 2\n",
"Fastest Mean: 2.915081212121212\n",
"Slowest Mean: 2.915081212121212\n",
"Fastest Mean: 2.9266933333333336\n",
"Slowest Mean: 2.9266933333333336\n",
"\n"
]
}
],
"source": [
"@always_inline\n",
"fn bench() -> Report:\n",
" var N = 200\n",
" var M = 200\n",
" var field = random_spin_field(N, M)\n",
"\n",
" @always_inline\n",
" @parameter\n",
" fn ising_step_fn():\n",
" ising_step(field=field)\n",
"\n",
" return benchmark.run[ising_step_fn](max_runtime_secs=10)\n",
"\n",
"var report = bench()\n",
"# Print a report in Milliseconds\n",
"report.print(\"ms\")"
]
Expand All @@ -174,15 +166,15 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook ising_model_speed_2.ipynb to markdown\n",
"[NbConvertApp] Writing 3067 bytes to ising_model_speed_2.md\n"
"[NbConvertApp] Writing 3064 bytes to ising_model_speed_2.md\n"
]
}
],
Expand Down
24 changes: 10 additions & 14 deletions _includes/ising_model_speed_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn random_spin_field(N: Int, M: Int) -> Tensor[data_type]:
var t = rand[data_type](N, M)
for i in range(N):
for j in range(M):
if t[Index(i, j)] < 0.5:
if t[i, j] < 0.5:
t[Index(i, j)] = -1
else:
t[Index(i, j)] = 1
Expand All @@ -46,11 +46,11 @@ fn _ising_update(inout field: Tensor[data_type], n: Int, m: Int, beta: Float32)
for j in range(m - 1, m + 2):
if i == n and j == m:
continue
total += field[Index(i % N, j % M)]
var dE = 2 * field[Index(n, m)] * total
total += field[i % N, j % M]
var dE = 2 * field[n, m] * total
if dE <= 0:
field[Index(n, m)] *= -1
elif exp(-dE * beta) > rand[data_type](1)[Index(0)]:
elif exp(-dE * beta) > rand[data_type](1)[0]:
field[Index(n, m)] *= -1
```

Expand Down Expand Up @@ -87,25 +87,21 @@ fn bench() -> Report:
return benchmark.run[ising_step_fn](max_runtime_secs=10)

var report = bench()
```


```python
# Print a report in Milliseconds
report.print("ms")
```

---------------------
Benchmark Report (ms)
---------------------
Mean: 2.915081212121212
Total: 2404.942
Mean: 2.9266933333333331
Total: 2414.5219999999999
Iters: 825
Warmup Mean: 2.508
Warmup Total: 5.016
Warmup Mean: 2.5630000000000002
Warmup Total: 5.1260000000000003
Warmup Iters: 2
Fastest Mean: 2.915081212121212
Slowest Mean: 2.915081212121212
Fastest Mean: 2.9266933333333336
Slowest Mean: 2.9266933333333336



Expand Down
24 changes: 10 additions & 14 deletions _includes/ising_model_speed_2.md-e
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn random_spin_field(N: Int, M: Int) -> Tensor[data_type]:
var t = rand[data_type](N, M)
for i in range(N):
for j in range(M):
if t[Index(i, j)] < 0.5:
if t[i, j] < 0.5:
t[Index(i, j)] = -1
else:
t[Index(i, j)] = 1
Expand All @@ -46,11 +46,11 @@ fn _ising_update(inout field: Tensor[data_type], n: Int, m: Int, beta: Float32)
for j in range(m - 1, m + 2):
if i == n and j == m:
continue
total += field[Index(i % N, j % M)]
var dE = 2 * field[Index(n, m)] * total
total += field[i % N, j % M]
var dE = 2 * field[n, m] * total
if dE <= 0:
field[Index(n, m)] *= -1
elif exp(-dE * beta) > rand[data_type](1)[Index(0)]:
elif exp(-dE * beta) > rand[data_type](1)[0]:
field[Index(n, m)] *= -1
```

Expand Down Expand Up @@ -87,25 +87,21 @@ fn bench() -> Report:
return benchmark.run[ising_step_fn](max_runtime_secs=10)

var report = bench()
```


```mojo
# Print a report in Milliseconds
report.print("ms")
```

---------------------
Benchmark Report (ms)
---------------------
Mean: 2.915081212121212
Total: 2404.942
Mean: 2.9266933333333331
Total: 2414.5219999999999
Iters: 825
Warmup Mean: 2.508
Warmup Total: 5.016
Warmup Mean: 2.5630000000000002
Warmup Total: 5.1260000000000003
Warmup Iters: 2
Fastest Mean: 2.915081212121212
Slowest Mean: 2.915081212121212
Fastest Mean: 2.9266933333333336
Slowest Mean: 2.9266933333333336



Expand Down
Loading

0 comments on commit 1f788ec

Please sign in to comment.