diff --git a/example-julia/LICENSE b/example-julia/LICENSE new file mode 100644 index 0000000..0f5674c --- /dev/null +++ b/example-julia/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2020, Samuel Omlin +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/example-julia/Manifest.toml b/example-julia/Manifest.toml new file mode 100644 index 0000000..039b7ee --- /dev/null +++ b/example-julia/Manifest.toml @@ -0,0 +1,226 @@ +# This file is machine-generated - editing it directly is not advised + +[[ADIOS2]] +deps = ["ADIOS2_jll", "MPI", "Printf", "Test"] +git-tree-sha1 = "6ceb028e69332b994b9b032252cda77e260af357" +repo-rev = "main" +repo-url = "/Users/eschnett/src/jl/ADIOS2.jl" +uuid = "e0ce9d3b-0dbd-416f-8264-ccca772f60ec" +version = "0.4.0" + +[[ADIOS2_jll]] +deps = ["Artifacts", "Blosc_jll", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "MPICH_jll", "MicrosoftMPI_jll", "Pkg", "ZeroMQ_jll", "libpng_jll", "zfp_jll"] +git-tree-sha1 = "779737d5a0e559c5e0943d8c3005c36485d7a36c" +uuid = "44b1415a-b224-5c99-9168-ff4febb5f37f" +version = "2.7.1+6" + +[[ArgTools]] +uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" + +[[Artifacts]] +uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" + +[[Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[Blosc_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Lz4_jll", "Pkg", "Zlib_jll", "Zstd_jll"] +git-tree-sha1 = "e747dac84f39c62aff6956651ec359686490134e" +uuid = "0b7ba130-8d10-5ba8-a3d6-c5182647fed9" +version = "1.21.0+0" + +[[CompilerSupportLibraries_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" + +[[Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + +[[DocStringExtensions]] +deps = ["LibGit2"] +git-tree-sha1 = "a32185f5428d3986f47c2ab78b1f216d5e6cc96f" +uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" +version = "0.8.5" + +[[Downloads]] +deps = ["ArgTools", "LibCURL", "NetworkOptions"] +uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" + +[[InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[JLLWrappers]] +deps = ["Preferences"] +git-tree-sha1 = "642a199af8b68253517b80bd3bfd17eb4e84df6e" +uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" +version = "1.3.0" + +[[LibCURL]] +deps = ["LibCURL_jll", "MozillaCACerts_jll"] +uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" + +[[LibCURL_jll]] +deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] +uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" + +[[LibGit2]] +deps = ["Base64", "NetworkOptions", "Printf", "SHA"] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[LibSSH2_jll]] +deps = ["Artifacts", "Libdl", "MbedTLS_jll"] +uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" + +[[Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[Lz4_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "5d494bc6e85c4c9b626ee0cab05daa4085486ab1" +uuid = "5ced341a-0733-55b8-9ab6-a4889d929147" +version = "1.9.3+0" + +[[MPI]] +deps = ["Distributed", "DocStringExtensions", "Libdl", "MPICH_jll", "MicrosoftMPI_jll", "OpenMPI_jll", "Pkg", "Random", "Requires", "Serialization", "Sockets"] +git-tree-sha1 = "e4549a5ced642a73bd8ba2dd1d3b1d30b3530d94" +uuid = "da04e1cc-30fd-572f-bb4f-1f8673147195" +version = "0.19.0" + +[[MPICH_jll]] +deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "c6cafe3f9747c0a0740611e2dffc4d37248fb691" +uuid = "7cb0a576-ebde-5e09-9194-50597f1243b4" +version = "3.4.2+0" + +[[Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[MbedTLS_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" + +[[MicrosoftMPI_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "e5c90234b3967684c9c6f87b4a54549b4ce21836" +uuid = "9237b28f-5490-5468-be7b-bb81f5f5e6cf" +version = "10.1.3+0" + +[[MozillaCACerts_jll]] +uuid = "14a3606d-f60d-562e-9121-12d972cd8159" + +[[NetworkOptions]] +uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" + +[[OpenMPI_jll]] +deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "a784e5133fc7e204c900f2cf38ed37a92ff9248d" +uuid = "fe0851c0-eecd-5654-98d4-656369965a5c" +version = "4.1.1+2" + +[[Pkg]] +deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" + +[[Preferences]] +deps = ["TOML"] +git-tree-sha1 = "00cfd92944ca9c760982747e9a1d0d5d86ab1e5a" +uuid = "21216c6a-2e73-6563-6e65-726566657250" +version = "1.2.2" + +[[Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[Random]] +deps = ["Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[Requires]] +deps = ["UUIDs"] +git-tree-sha1 = "4036a3bd08ac7e968e27c203d45f5fff15020621" +uuid = "ae029012-a4dd-5104-9daa-d747884805df" +version = "1.1.3" + +[[SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" + +[[Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[TOML]] +deps = ["Dates"] +uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" + +[[Tar]] +deps = ["ArgTools", "SHA"] +uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" + +[[Test]] +deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[ZeroMQ_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "libsodium_jll"] +git-tree-sha1 = "fe5c65a526f066fb3000da137d5785d9649a8a47" +uuid = "8f1865be-045e-5c20-9c9f-bfbfb0764568" +version = "4.3.4+0" + +[[Zlib_jll]] +deps = ["Libdl"] +uuid = "83775a58-1f1d-513f-b197-d71354ab007a" + +[[Zstd_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "cc4bf3fdde8b7e3e9fa0351bdeedba1cf3b7f6e6" +uuid = "3161d3a3-bdf6-5164-811a-617609db77b4" +version = "1.5.0+0" + +[[libpng_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"] +git-tree-sha1 = "94d180a6d2b5e55e447e2d27a29ed04fe79eb30c" +uuid = "b53b4c65-9356-5827-b1ea-8c7a1a84506f" +version = "1.6.38+0" + +[[libsodium_jll]] +deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] +git-tree-sha1 = "848ab3d00fe39d6fbc2a8641048f8f272af1c51e" +uuid = "a9144af2-ca23-56d9-984f-0d03f7b5ccf8" +version = "1.0.20+0" + +[[nghttp2_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" + +[[p7zip_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" + +[[zfp_jll]] +deps = ["CompilerSupportLibraries_jll", "Libdl", "Pkg"] +git-tree-sha1 = "ab161f72edcde0d437e35946a3fdf5dac84cc0f3" +uuid = "007be453-5351-5f08-b8c7-bf95923b8de2" +version = "0.5.5+0" diff --git a/example-julia/Project.toml b/example-julia/Project.toml new file mode 100644 index 0000000..caa002f --- /dev/null +++ b/example-julia/Project.toml @@ -0,0 +1,9 @@ +name = "ADIOS2Tutorial" +uuid = "7bb01ee6-02dc-4e7e-8c69-90a439f2b3b1" +authors = ["Erik Schnetter "] +version = "0.1.0" + +[deps] +ADIOS2 = "e0ce9d3b-0dbd-416f-8264-ccca772f60ec" +MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/example-julia/README.md b/example-julia/README.md new file mode 100644 index 0000000..4587630 --- /dev/null +++ b/example-julia/README.md @@ -0,0 +1,15 @@ +# ADIOS2Tutorial.jl + +Adapted from the Python code [here](https://github.com/omlins/adios2-tutorial). + +Run these two commands simultaneoulsy (in different terminals): + +```sh +cd example-julia +julia --project=@. -e 'using ADIOS2Tutorial; ADIOS2Tutorial.write()' +``` + +```sh +cd example-julia +julia --project=@. -e 'using ADIOS2Tutorial; ADIOS2Tutorial.read()' +``` diff --git a/example-julia/adios2.xml b/example-julia/adios2.xml new file mode 100644 index 0000000..fdeddf8 --- /dev/null +++ b/example-julia/adios2.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example-julia/src/ADIOS2Tutorial.jl b/example-julia/src/ADIOS2Tutorial.jl new file mode 100644 index 0000000..6d8081a --- /dev/null +++ b/example-julia/src/ADIOS2Tutorial.jl @@ -0,0 +1,164 @@ +module ADIOS2Tutorial + +using ADIOS2 +using MPI + +function MPI_Dims_create(nprocs, ndims) + dims = zeros(Cint, ndims) + MPI.Dims_create!(nprocs, ndims, dims) + return dims +end + +function update_halo(A::AbstractArray{Float64,2}, neighbors_x::NTuple{2,Int}, neighbors_y::NTuple{2,Int}, comm::MPI.Comm) + if neighbors_x[1] >= 0 # MPI_PROC_NULL? + sendbuf = np[2, :] + recvbuf = zeros(size(A, 1)) + Sendrecv!(sendbuf, neighbors_x[1], 0, recvbuf, neighbors_x[1], 1, comm) + A[1, :] .= recvbuf + end + if neighbors_x[2] >= 0 # MPI_PROC_NULL? + sendbuf = np[end - 1, :] + recvbuf = zeros(size(A, 1)) + Sendrecv!(sendbuf, neighbors_x[2], 1, recvbuf, neighbors_x[2], 0, comm) + A[end, :] .= recvbuf + end + if neighbors_y[1] >= 0 # MPI_PROC_NULL? + sendbuf = np[:, 2] + recvbuf = zeros(size(A, 2)) + Sendrecv!(sendbuf, neighbors_y[1], 2, recvbuf, neighbors_y[1], 3, comm) + A[:, 1] .= recvbuf + end + if neighbors_y[2] >= 0 # MPI_PROC_NULL? + sendbuf = np[:, end - 1] + recvbuf = zeros(size(A, 2)) + Sendrecv!(sendbuf, neighbors_y[2], 3, recvbuf, neighbors_y[2], 2, comm) + A[:, end] .= recvbuf + end +end + +function write() + MPI.Init() + + # MPI + nprocs = MPI.Comm_size(MPI.COMM_WORLD) + dims = MPI_Dims_create(nprocs, 2) + comm = MPI.Cart_create(MPI.COMM_WORLD, length(dims), dims, Cint[0, 0], true) + me = MPI.Comm_rank(comm) + coords = MPI.Cart_coords(comm) + neighbors_x = MPI.Cart_shift(comm, 0, 1) + neighbors_y = MPI.Cart_shift(comm, 1, 1) + + # Physics + lam = 1.0 # Thermal conductivity + cp_min = 1.0 # Minimal heat capacity + lx, ly = 10.0, 10.0 # Length of computational domain in dimension x and y + + # Numerics + nx, ny = 128, 128 # Number of gridpoints in dimensions x and y + nt = 10000 # Number of time steps + nx_g = dims[1] * (nx - 2) + 2 # Number of gridpoints of the global problem in dimension x + ny_g = dims[2] * (ny - 2) + 2 # ... in dimension y + dx = lx / (nx_g - 1) # Space step in dimension x + dy = ly / (ny_g - 1) # ... in dimension y + + # Array initializations + T = zeros(nx, ny) + Cp = zeros(nx, ny) + # dTedt = zeros(nx - 2, ny - 2) + # qTx = zeros(nx - 1, ny - 2) + # qTy = zeros(nx - 2, ny - 1) + dTedt = zeros(nx, ny) + + # Initial conditions (heat capacity and temperature with two Gaussian anomalies each) + x0 = coords[1] * (nx - 2) * dx + y0 = coords[2] * (ny - 2) * dy + Cp = [cp_min + + 5 * exp(-((x0 + ix * dx - lx / 1.5) / 1.0)^2 - ((y0 + iy * dy - ly / 1.5) / 1.0)^2) + + 5 * exp(-((x0 + ix * dx - lx / 1.5) / 1.0)^2 - ((y0 + iy * dy - ly / 3.0) / 1.0)^2) for ix in 1:nx, iy in 1:ny] + T = [100 * exp(-((x0 + ix * dx - lx / 3.0) / 2.0)^2 - ((y0 + iy * dy - ly / 2.0) / 2.0)^2) + + 50 * exp(-((x0 + ix * dx - lx / 1.5) / 2.0)^2 - ((y0 + iy * dy - ly / 2.0) / 2.0)^2) for ix in 1:nx, iy in 1:ny] + + # ADIOS2 + # (size and start of the local and global problem) + nxy_nohalo = (nx - 2, ny - 2) # In ADIOS2 slang: count + nxy_g_nohalo = (nx_g - 2, ny_g - 2) # ... shape + start = Tuple(coords) .* nxy_nohalo # ... start + T_nohalo = zeros(nxy_nohalo) # Prealocate array for writing temperature + # (intialize ADIOS2, io, engine and define the variable temperature) + adios = adios_init_mpi("adios2.xml", comm) # Use the configurations defined in "adios2.xml"... + io = declare_io(adios, "writerIO") # ... in the section "writerIO" + T_id = define_variable(io, "temperature", eltype(T), nxy_g_nohalo, start, nxy_nohalo; constant_dims=true) # Define the variable "temperature" + engine = open(io, "diffusion2D.bp", mode_write) + + # Time loop + nsteps = 50 # Number of times data is written during the simulation + dt = min(dx, dy)^2 * cp_min / lam / 4.1 # Time step for the 2D Heat diffusion + t = 0 # Initialize physical time + tic = time() # Start measuring wall time + for it in 1:nt + if it % (nt ÷ nsteps) == 0 # Write data only nsteps times + T_nohalo = T[2:(end - 1), 2:(end - 1)] # Copy data removing the halo + begin_step(engine) # Begin ADIOS2 write step + put!(engine, T_id, T_nohalo) # Add T (without halo) to variables for writing + end_step(engine) # End ADIOS2 write step (includes normally the actual writing of data) + println("Time step $it...") + end + + for j in 2:(ny - 1), i in 2:(nx - 1) + qTxm = -lam * (T[i, j] - T[i - 1, j]) / dx # Fourier's law of heat conduction: q_x = -λ δT/δx + qTxp = -lam * (T[i + 1, j] - T[i, j]) / dx + qTym = -lam * (T[i, j] - T[i, j - 1]) / dy # ... q_y = -λ δT/δy + qTyp = -lam * (T[i, j + 1] - T[i, j]) / dy + + dTedt[i, j] = 1.0 / Cp[i, j] * (-(qTxp - qTxm) / dx - (qTyp - qTym) / dy)# Conservation of energy: δT/δt = 1/cₚ(-δq_x/δx - δq_y/dy) + end + for j in 2:(ny - 1), i in 2:(nx - 1) + T[i, j] += dt * dTedt[i, j] # Update of temperature T_new = T_old + δT/δt + end + + t += dt # Elapsed physical time + update_halo(T, neighbors_x, neighbors_y, comm) # Update the halo of T + end + + close(engine) + println("time: $(time() - tic)") + println("Min. temperature $(minimum(T))") + println("Max. temperature $(maximum(T))") + return +end + +function read() + adios = adios_init_serial("adios2.xml") + io = declare_io(adios, "readerIO") + @show engine_type(io) + engine = open(io, "diffusion2D.bp", mode_read) + @show type(engine) + @show name.(inquire_all_variables(io)) + + T = nothing + nprocessed = 0 + while begin_step(engine, step_mode_read, 100.0) != step_status_end_of_stream + T_id = inquire_variable(io, "temperature") + if nprocessed == 0 + nxy_global = shape(T_id) + nxy = count(T_id) + T_type = type(T_id) + T = zeros(T_type, nxy) + println(nxy_global, nxy, T_type) + sleep(4) + end + get(engine, T_id, T) + end_step(engine) + println(nprocessed, minimum(T), maximum(T)) + # IPython.display.clear_output(wait=True) + # plt.title('Temperature at step ' + str(engine.CurrentStep())) + # plt.contourf(T, 256, cmap=plt.cm.jet) + # plt.colorbar() + # plt.show() + nprocessed += 1 + end + + return +end + +end diff --git a/example-julia/test/runtests.jl b/example-julia/test/runtests.jl new file mode 100644 index 0000000..84f1dfb --- /dev/null +++ b/example-julia/test/runtests.jl @@ -0,0 +1,2 @@ +using ADIOS2Tutorial +using Test