Skip to content

Serialization decouples view memory location on load #50815

Open
@Matthew-Whisenant

Description

I posted a question on the discourse here but didn't get any traction on the topic. I believe it may be a bug in base serialization package.

I have a large matrix that I would like to manipulate via views to make it easier. Ideally, I would like to be able to save this matrix with the views so that I don't have to reload and parse data. But when I save and load, it appears that the matrix views are no longer tied memory-wise.

The julia 1.9 was installed on a Linux server by me using the 64-bit (glibc) generic Linux tar.

Here is a minimum example:

# julia> versioninfo()
# Julia Version 1.9.0
# Commit 8e630552924 (2023-05-07 11:25 UTC)
# Platform Info:
#   OS: Linux (x86_64-linux-gnu)
#   CPU: 56 × Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
#   WORD_SIZE: 64
#   LIBM: libopenlibm
#   LLVM: libLLVM-14.0.6 (ORCJIT, broadwell)
#   Threads: 16 on 56 virtual cores
# Environment:
#   JULIA_EDITOR = code
#   JULIA_NUM_THREADS = 16

# Make matrix and views
x = rand(100, 100)
xv = [view(x, i, :) for i in axes(x, 1)]

# Struct to hold views and data together
struct test
    x
    xv
end
mytest = test(x, xv)

# Example of updating, works!
setindex!(mytest.xv[1], 1:100, :)
# julia> mytest.x
# 100×100 Matrix{Float64}:
#  1.0         2.0       …  99.0        100.0
#  0.00710095  0.305477      0.928174     0.117497
#  0.344555    0.181217      0.145659     0.874601
#  0.0893118   0.519935      0.619816     0.50697
#  0.420268    0.984494      0.409384     0.0129134
#  0.248145    0.850273  …   0.954395     0.856448
#  ⋮                     ⋱              
#  0.118925    0.885483  …   0.474676     0.944833
#  0.928362    0.132294      0.678013     0.0946481
#  0.798483    0.490875      0.663746     0.963795
#  0.345317    0.296794      0.0500642    0.320534
#  0.348782    0.45757       0.177782     0.796724

using Serialization

# Save and load to new struct
serialize("myteststruct", mytest)
myload = deserialize("myteststruct")

# Example of updating, decoupled!
setindex!(myload.xv[1], 101:200, :)
# julia> myload.x
# 100×100 Matrix{Float64}:
#  1.0         2.0       …  99.0        100.0
#  0.00710095  0.305477      0.928174     0.117497
#  0.344555    0.181217      0.145659     0.874601
#  0.0893118   0.519935      0.619816     0.50697
#  0.420268    0.984494      0.409384     0.0129134
#  0.248145    0.850273  …   0.954395     0.856448
#  ⋮                     ⋱              
#  0.118925    0.885483  …   0.474676     0.944833
#  0.928362    0.132294      0.678013     0.0946481
#  0.798483    0.490875      0.663746     0.963795
#  0.345317    0.296794      0.0500642    0.320534
#  0.348782    0.45757       0.177782     0.796724

using JLD2

# Save and load to new struct
save_object("mytest.jld2", mytest)
myload2 = load_object("mytest.jld2")

# Example of updating, works!
setindex!(myload2.xv[1], 101:200, :)
# julia> myload2.x
# 100×100 Matrix{Float64}:
#  101.0         102.0       …  199.0        200.0
#    0.00710095    0.305477       0.928174     0.117497
#    0.344555      0.181217       0.145659     0.874601
#    0.0893118     0.519935       0.619816     0.50697
#    0.420268      0.984494       0.409384     0.0129134
#    0.248145      0.850273  …    0.954395     0.856448
#    ⋮                       ⋱               
#    0.118925      0.885483  …    0.474676     0.944833
#    0.928362      0.132294       0.678013     0.0946481
#    0.798483      0.490875       0.663746     0.963795
#    0.345317      0.296794       0.0500642    0.320534
#    0.348782      0.45757        0.177782     0.796724

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    stdlibJulia's standard library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions