Created
July 11, 2020 04:34
-
-
Save genkuroki/d2f965cf7de2e5278a1d15109a7d5d01 to your computer and use it in GitHub Desktop.
ShiftedArrays.circshift vs. My.CircShift
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
{ | |
"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.5.0-rc1.0\"" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using BenchmarkTools", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "function Laplacian2D_for_ifelse!(v, u, h⁻²)\n m, n = size(u)\n @fastmath @inbounds @simd for j in 1:n\n for i in 1:m\n let Left = u[ifelse(i == 1, m, i-1), j], \n Right = u[ifelse(i == m, 1, i+1), j],\n Up = u[i, ifelse(j == 1, n, j-1)],\n Down = u[i, ifelse(j == n, 1, j+1)],\n Center = u[i, j]\n v[i, j] = h⁻²*(Left + Right + Up + Down - 4Center)\n end\n end\n end\n v\nend", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "Laplacian2D_for_ifelse! (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using ShiftedArrays\n\nfunction Laplacian2D_ShiftedArrays!(v, u, h⁻²)\n let Left = ShiftedArrays.circshift(u, ( 1, 0)), \n Right = ShiftedArrays.circshift(u, (-1, 0)),\n Up = ShiftedArrays.circshift(u, ( 0, 1)),\n Down = ShiftedArrays.circshift(u, ( 0, -1)),\n Center = u\n @. v = h⁻²*(Left + Right + Up + Down - 4Center)\n end\nend", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "Laplacian2D_ShiftedArrays! (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module My\n\nabstract type CircShift{T} <: AbstractArray{T, 2} end\nBase.size(u::CircShift) = size(u.A)\n\nstruct CircShiftLeft{T, U<:AbstractArray{T, 2}} <: CircShift{T}\n A::U\nend\nBase.getindex(u::CircShiftLeft, i, j) = let m = size(u.A, 1)\n @inbounds u.A[ifelse(i == 1, m, i-1), j]\nend\n\nstruct CircShiftRight{T, U<:AbstractArray{T, 2}} <: CircShift{T}\n A::U\nend\nBase.getindex(u::CircShiftRight, i, j) = let m = size(u.A, 1)\n @inbounds u.A[ifelse(i == m, 1, i+1), j]\nend\n\nstruct CircShiftUp{T, U<:AbstractArray{T, 2}} <: CircShift{T}\n A::U\nend\nBase.getindex(u::CircShiftUp, i, j) = let n = size(u.A, 2)\n @inbounds u.A[i, ifelse(j == 1, n, j-1)]\nend\n\nstruct CircShiftDown{T, U<:AbstractArray{T, 2}} <: CircShift{T}\n A::U\nend\nBase.getindex(u::CircShiftDown, i, j) = let n = size(u.A, 2)\n @inbounds u.A[i, ifelse(j == n, 1, j+1)]\nend\n\nend\n\nfunction Laplacian2D_My!(v, u, h⁻²)\n let Left = My.CircShiftLeft(u), \n Right = My.CircShiftRight(u),\n Up = My.CircShiftUp(u),\n Down = My.CircShiftDown(u),\n Center = u\n @. v = h⁻²*(Left + Right + Up + Down - 4Center)\n end\nend", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "Laplacian2D_My! (generic function with 1 method)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "n = 200\nx = y = range(-1, 1, length=n)\nh⁻² = 1/(x[end] - x[end-1])^2\nu = @. x'^2 - y^2\n\na = similar(u)\nb = similar(u)\nc = similar(u)\n\nLaplacian2D_for_ifelse!(a, u, h⁻²)\nLaplacian2D_ShiftedArrays!(b, u, h⁻²)\nLaplacian2D_My!(c, u, h⁻²)\n\na == b == c", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "true" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark $Laplacian2D_for_ifelse!($a, $u, $h⁻²)", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 7, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 80.709 μs (0.00% GC)\n median time: 83.043 μs (0.00% GC)\n mean time: 88.547 μs (0.00% GC)\n maximum time: 8.106 ms (0.00% GC)\n --------------\n samples: 10000\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark $Laplacian2D_ShiftedArrays!($b, $u, $h⁻²)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 249.127 μs (0.00% GC)\n median time: 257.526 μs (0.00% GC)\n mean time: 291.761 μs (0.00% GC)\n maximum time: 8.892 ms (0.00% GC)\n --------------\n samples: 10000\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@benchmark $Laplacian2D_My!($c, $u, $h⁻²)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 0 bytes\n allocs estimate: 0\n --------------\n minimum time: 86.308 μs (0.00% GC)\n median time: 91.906 μs (0.00% GC)\n mean time: 117.178 μs (0.00% GC)\n maximum time: 7.587 ms (0.00% GC)\n --------------\n samples: 10000\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-1.5", | |
"display_name": "Julia 1.5.0-rc1", | |
"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.5.0" | |
}, | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"gist": { | |
"id": "d2f965cf7de2e5278a1d15109a7d5d01", | |
"data": { | |
"description": "ShiftedArrays.circshift vs. My.CircShift", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/d2f965cf7de2e5278a1d15109a7d5d01" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment