Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active September 27, 2020 15:23
Show Gist options
  • Save genkuroki/c3b1cd6c4ddf259d3f86d43b6c88b9f2 to your computer and use it in GitHub Desktop.
Save genkuroki/c3b1cd6c4ddf259d3f86d43b6c88b9f2 to your computer and use it in GitHub Desktop.
simd example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "VERSION",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"text/plain": "v\"1.6.0-DEV.1046\""
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "https://mobile.twitter.com/lpha_z/status/1310221407649816579"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function test()\n a = fill(1e-10, 1000)\n s = 0.0\n for j = 1:10000000\n for i = 1:1000\n s += a[i]\n end \n end\n s\nend\n@time test()",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": " 9.409779 seconds (1 allocation: 7.938 KiB)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "1.0000000694756235"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function test_inbounds_simd()\n a = fill(1e-10, 1000)\n s = 0.0\n for j = 1:10000000\n @inbounds @simd for i = 1:1000\n s += a[i]\n end \n end\n s\nend\n@time test_inbounds_simd()",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": " 0.895148 seconds (1 allocation: 7.938 KiB)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "1.000000004603537"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function test_builtin_sum()\n a = fill(1e-10, 1000)\n s = 0.0\n for j = 1:10000000\n s += sum(a)\n end\n s\nend\n@time test_builtin_sum()",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": " 1.075225 seconds (1 allocation: 7.938 KiB)\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "0.99999999975017"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools\nprint(\"test(): \")\n@btime test()\nprint(\"test_inbounds_simd():\")\n@btime test_inbounds_simd()\nprint(\"test_builtin_sum(): \")\n@btime test_builtin_sum()\n;",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "test(): 9.394 s (1 allocation: 7.94 KiB)\ntest_inbounds_simd(): 891.354 ms (1 allocation: 7.94 KiB)\ntest_builtin_sum(): 1.036 s (1 allocation: 7.94 KiB)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.6-o3-depwarn",
"display_name": "Julia 1.6.0-DEV depwarn -O3",
"language": "julia"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.6.0"
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "c3b1cd6c4ddf259d3f86d43b6c88b9f2",
"data": {
"description": "simd example",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/c3b1cd6c4ddf259d3f86d43b6c88b9f2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment