Created
October 24, 2020 13:04
-
-
Save antimon2/45ef86512440de90a9855cba7838338e to your computer and use it in GitHub Desktop.
access_array_sb2.ipynb
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": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:01:01.434Z", | |
"end_time": "2020-10-24T22:01:02.243000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "versioninfo()", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Julia Version 1.5.2\nCommit 539f3ce943 (2020-09-23 23:17 UTC)\nPlatform Info:\n OS: Linux (x86_64-pc-linux-gnu)\n CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\n WORD_SIZE: 64\n LIBM: libopenlibm\n LLVM: libLLVM-9.0.1 (ORCJIT, skylake)\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:01:03.681Z", | |
"end_time": "2020-10-24T22:01:03.378000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module A\n# using Parameters\n######################\nlinspace(xstart, xend, n) = collect(range(xstart, xend, length=n))\nV(x) = -2 / sqrt(x^2 + 1)\ne(x) = exp(-x^2)\n######################\n# @with_kw struct Param{TF <: AbstractFloat,TI}\nstruct Param{TF <: AbstractFloat,TI}\n # grid [-L/2, L/2]\n L::TF # = 160.0\n N::TI # = 4001\n dx::TF # = L / (N - 1)\n x # = linspace(-L / 2, L / 2, N)\nend\nParam(;L = 160.0, N = 4001) = Param(L, N, L / (N - 1), linspace(-L / 2, L / 2, N))\n######################\nfunction main(param::Param)\n # @unpack x, N = param\n x = param.x\n N = param.N\n f = e.(x)\n # println(typeof(f))\n g = zeros(eltype(f), N)\n access!(x, f, g)\n return g\nend\n######################\nfunction access!(x::T, f::T, g::T) where T\n @. g += V(x) * f\nend\n######################\nend", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": "Main.A" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T12:58:00.138Z", | |
"end_time": "2020-10-24T21:57:59.453000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# module B: 実質 A と違いがないので省略", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:01:05.477Z", | |
"end_time": "2020-10-24T22:01:04.782000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module C\n# using Parameters\n######################\nlinspace(xstart, xend, n) = collect(range(xstart, xend, length=n))\nV(x) = -2 / sqrt(x^2 + 1)\ne(x) = exp(-x^2)\n######################\n# @with_kw struct Param{TF <: AbstractFloat,TI}\nstruct Param{TF <: AbstractFloat,TI}\n # grid [-L/2, L/2]\n L::TF # = 160.0\n N::TI # = 4001\n dx::TF # = L / (N - 1)\n x # = linspace(-L / 2, L / 2, N)\nend\nParam(;L = 160.0, N = 4001) = Param(L, N, L / (N - 1), linspace(-L / 2, L / 2, N))\n######################\nfunction main(param::Param)\n x = param.x\n N = param.N\n f = e.(x)\n # println(typeof(f))\n g = zeros(eltype(f), N)\n access!(param, f, g)\n return g\nend\n######################\nfunction access!(param::Param, f::T, g::T) where T\n # @unpack x = param\n x = param.x\n @. g += V(x) * f\nend\n######################\nend", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "Main.C" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:01:06.638Z", | |
"end_time": "2020-10-24T22:01:05.984000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using BenchmarkTools", | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:01:09.646Z", | |
"end_time": "2020-10-24T22:01:20.688000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "pa = A.Param(L=50.0, N=100001)\n@benchmark resa = A.main(pa)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 1.53 MiB\n allocs estimate: 8\n --------------\n minimum time: 829.768 μs (0.00% GC)\n median time: 856.727 μs (0.00% GC)\n mean time: 889.352 μs (1.35% GC)\n maximum time: 2.320 ms (22.55% GC)\n --------------\n samples: 5618\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:01:30.877Z", | |
"end_time": "2020-10-24T22:01:40.840000+09:00" | |
} | |
}, | |
"cell_type": "code", | |
"source": "pc = C.Param(L=50.0, N=100001)\n@benchmark resc = C.main(pc)", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 1.53 MiB\n allocs estimate: 15\n --------------\n minimum time: 966.954 μs (0.00% GC)\n median time: 985.705 μs (0.00% GC)\n mean time: 1.046 ms (1.61% GC)\n maximum time: 2.436 ms (0.00% GC)\n --------------\n samples: 4779\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:02:27.874Z", | |
"end_time": "2020-10-24T22:02:28.266000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "fa = A.e.(pa.x)\nga = zeros(eltype(fa), pa.N)\n@code_warntype A.access!(pa.x, fa, ga)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Variables\n #self#\u001b[36m::Core.Compiler.Const(Main.A.access!, false)\u001b[39m\n x\u001b[36m::Array{Float64,1}\u001b[39m\n f\u001b[36m::Array{Float64,1}\u001b[39m\n g\u001b[36m::Array{Float64,1}\u001b[39m\n\nBody\u001b[36m::Array{Float64,1}\u001b[39m\n\u001b[90m1 ─\u001b[39m %1 = Base.broadcasted(Main.A.V, x)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Main.A.V),Tuple{Array{Float64,1}}}\u001b[39m\n\u001b[90m│ \u001b[39m %2 = Base.broadcasted(Main.A.:*, %1, f)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(*),Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Main.A.V),Tuple{Array{Float64,1}}},Array{Float64,1}}}\u001b[39m\n\u001b[90m│ \u001b[39m %3 = Base.broadcasted(Main.A.:+, g, %2)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(+),Tuple{Array{Float64,1},Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(*),Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Main.A.V),Tuple{Array{Float64,1}}},Array{Float64,1}}}}}\u001b[39m\n\u001b[90m│ \u001b[39m %4 = Base.materialize!(g, %3)\u001b[36m::Array{Float64,1}\u001b[39m\n\u001b[90m└──\u001b[39m return %4\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:02:47.963Z", | |
"end_time": "2020-10-24T22:02:47.359000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "fc = C.e.(pc.x)\ngc = zeros(eltype(fc), pc.N)\n@code_warntype C.access!(pc, fc, gc)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Variables\n #self#\u001b[36m::Core.Compiler.Const(Main.C.access!, false)\u001b[39m\n param\u001b[36m::Main.C.Param{Float64,Int64}\u001b[39m\n f\u001b[36m::Array{Float64,1}\u001b[39m\n g\u001b[36m::Array{Float64,1}\u001b[39m\n x\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\nBody\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m1 ─\u001b[39m (x = Base.getproperty(param, :x))\n\u001b[90m│ \u001b[39m %2 = Base.broadcasted(Main.C.V, x)\u001b[91m\u001b[1m::Base.Broadcast.Broadcasted{_A,Nothing,typeof(Main.C.V),_B} where _B<:Tuple where _A<:Union{Nothing, Base.Broadcast.BroadcastStyle}\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m %3 = Base.broadcasted(Main.C.:*, %2, f)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m %4 = Base.broadcasted(Main.C.:+, g, %3)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m %5 = Base.materialize!(g, %4)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m└──\u001b[39m return %5\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:03:20.133Z", | |
"end_time": "2020-10-24T22:03:19.599000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "typeof(pc.x)", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 10, | |
"data": { | |
"text/plain": "Array{Float64,1}" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:03:21.294Z", | |
"end_time": "2020-10-24T22:03:21.468000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "collect(zip(fieldnames(typeof(pc)), fieldtypes(typeof(pc))))", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": "4-element Array{Tuple{Symbol,DataType},1}:\n (:L, Float64)\n (:N, Int64)\n (:dx, Float64)\n (:x, Any)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:03:25.394Z", | |
"end_time": "2020-10-24T22:03:24.699000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "module D\n# using Parameters\n######################\nlinspace(xstart, xend, n) = collect(range(xstart, xend, length=n))\nV(x) = -2 / sqrt(x^2 + 1)\ne(x) = exp(-x^2)\n######################\n# @with_kw struct Param{TF <: AbstractFloat,TI}\nstruct Param{TF <: AbstractFloat,TI,TA <: AbstractVector{TF}}\n # grid [-L/2, L/2]\n L::TF # = 160.0\n N::TI # = 4001\n dx::TF # = L / (N - 1)\n x::TA # = linspace(-L / 2, L / 2, N)\nend\nParam(;L = 160.0, N = 4001) = Param(L, N, L / (N - 1), linspace(-L / 2, L / 2, N))\n######################\nfunction main(param::Param)\n x = param.x\n N = param.N\n f = e.(x)\n # println(typeof(f))\n g = zeros(eltype(f), N)\n access!(param, f, g)\n return g\nend\n######################\nfunction access!(param::Param, f::T, g::T) where T\n x = param.x\n @. g += V(x) * f\nend\n######################\nend", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 12, | |
"data": { | |
"text/plain": "Main.D" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:03:34.217Z", | |
"end_time": "2020-10-24T22:03:44.276000+09:00" | |
} | |
}, | |
"cell_type": "code", | |
"source": "pd = D.Param(L=50.0, N=100001)\n@benchmark resd = D.main(pd)", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 13, | |
"data": { | |
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 1.53 MiB\n allocs estimate: 4\n --------------\n minimum time: 826.247 μs (0.00% GC)\n median time: 857.967 μs (0.00% GC)\n mean time: 963.590 μs (2.92% GC)\n maximum time: 2.537 ms (64.51% GC)\n --------------\n samples: 5186\n evals/sample: 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:03:47.612Z", | |
"end_time": "2020-10-24T22:03:46.970000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "fd = D.e.(pd.x)\ngd = zeros(eltype(fd), pd.N)\n@code_warntype D.access!(pd, fd, gd)", | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Variables\n #self#\u001b[36m::Core.Compiler.Const(Main.D.access!, false)\u001b[39m\n param\u001b[36m::Main.D.Param{Float64,Int64,Array{Float64,1}}\u001b[39m\n f\u001b[36m::Array{Float64,1}\u001b[39m\n g\u001b[36m::Array{Float64,1}\u001b[39m\n x\u001b[36m::Array{Float64,1}\u001b[39m\n\nBody\u001b[36m::Array{Float64,1}\u001b[39m\n\u001b[90m1 ─\u001b[39m (x = Base.getproperty(param, :x))\n\u001b[90m│ \u001b[39m %2 = Base.broadcasted(Main.D.V, x)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Main.D.V),Tuple{Array{Float64,1}}}\u001b[39m\n\u001b[90m│ \u001b[39m %3 = Base.broadcasted(Main.D.:*, %2, f)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(*),Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Main.D.V),Tuple{Array{Float64,1}}},Array{Float64,1}}}\u001b[39m\n\u001b[90m│ \u001b[39m %4 = Base.broadcasted(Main.D.:+, g, %3)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(+),Tuple{Array{Float64,1},Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(*),Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Main.D.V),Tuple{Array{Float64,1}}},Array{Float64,1}}}}}\u001b[39m\n\u001b[90m│ \u001b[39m %5 = Base.materialize!(g, %4)\u001b[36m::Array{Float64,1}\u001b[39m\n\u001b[90m└──\u001b[39m return %5\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:04:03.047Z", | |
"end_time": "2020-10-24T22:04:02.340000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "typeof(pd.x)", | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 15, | |
"data": { | |
"text/plain": "Array{Float64,1}" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2020-10-24T13:04:09.678Z", | |
"end_time": "2020-10-24T22:04:08.973000+09:00" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "collect(zip(fieldnames(typeof(pd)), fieldtypes(typeof(pd))))", | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 16, | |
"data": { | |
"text/plain": "4-element Array{Tuple{Symbol,DataType},1}:\n (:L, Float64)\n (:N, Int64)\n (:dx, Float64)\n (:x, Array{Float64,1})" | |
}, | |
"metadata": {} | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-1.5", | |
"display_name": "Julia 1.5.2", | |
"language": "julia" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.5.2" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "access_array_sb2.ipynb", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment