copyto! test

This page examines copyto! speed of the lazy grids in the Julia package LazyGrids.

This page was generated from a single Julia file: 2-copyto.jl.

In any such Julia documentation, you can access the source code using the "Edit on GitHub" link in the top right.

The corresponding notebook can be viewed in nbviewer here: 2-copyto.ipynb, and opened in binder here: 2-copyto.ipynb.

Setup

Packages needed here.

using LazyGrids: ndgrid, ndgrid_array
using LazyGrids: btime, @timeo # not exported; just for timing tests here
using BenchmarkTools: @benchmark
using InteractiveUtils: versioninfo

Overview

There are several sub-types of AbstractGrids. Here we focus on the simplest (using OneTo) and the most general (AbstractVector).

OneTo

dims = (2^7,2^8,2^9)

(xl, _, _) = ndgrid(dims...) # lazy version
xa = Array(xl) # regular dense Array
out = zeros(Int, dims)
sizeof.((xl, xa))
(24, 134217728)
ta = @benchmark copyto!(out, xa) # 12.6ms
btime(ta)
"time=26.6ms mem=0 alloc=0"
tl = @benchmark copyto!(out, xl) # 27.3ms
btime(tl)
"time=34.7ms mem=0 alloc=0"

AbstractVector

x,y,z = map(rand, dims)

(xl, _, _) = ndgrid(dims...)
xa = Array(xl)
out = zeros(eltype(x), dims)
sizeof.((xl, xa))
(24, 134217728)
ta = @benchmark copyto!(out, xa) # 15.7ms
btime(ta)
"time=21.0ms mem=0 alloc=0"
tl = @benchmark copyto!(out, xl) # 21.7ms
btime(tl)
"time=29.4ms mem=0 alloc=0"

These results suggest that copyto! is somewhat slower for a lazy grid than for an Array. This drawback could be reduced or possibly even eliminated by adding a dedicated copyto! method for lazy grids. Submit an issue or PR if there is a use case that needs faster copyto!.

See broadcasting.

Reproducibility

This page was generated with the following version of Julia:

io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\n')
10-element Vector{SubString{String}}:
 "Julia Version 1.8.3"
 "Commit 0434deb161e (2022-11-14 20:14 UTC)"
 "Platform Info:"
 "  OS: Linux (x86_64-linux-gnu)"
 "  CPU: 2 × Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz"
 "  WORD_SIZE: 64"
 "  LIBM: libopenlibm"
 "  LLVM: libLLVM-13.0.1 (ORCJIT, skylake-avx512)"
 "  Threads: 1 on 2 virtual cores"
 ""

And with the following package versions

import Pkg; Pkg.status()
Status `~/work/LazyGrids.jl/LazyGrids.jl/docs/Project.toml`
  [6e4b80f9] BenchmarkTools v1.3.2
  [e30172f5] Documenter v0.27.23
  [7031d0ef] LazyGrids v0.5.0 `~/work/LazyGrids.jl/LazyGrids.jl`
  [98b081ad] Literate v2.14.0
  [b77e0a4c] InteractiveUtils

This page was generated using Literate.jl.