Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataContainers only stores a deep copy of the data when !data_are_columns #114

Closed
ilopezgp opened this issue Mar 2, 2022 · 1 comment · Fixed by #116
Closed

DataContainers only stores a deep copy of the data when !data_are_columns #114

ilopezgp opened this issue Mar 2, 2022 · 1 comment · Fixed by #116
Assignees

Comments

@ilopezgp
Copy link
Contributor

ilopezgp commented Mar 2, 2022

We currently have

struct DataContainer{FT <: Real}
     #stored data, each piece of data is a column [data dimension × number samples]
     stored_data::AbstractMatrix{FT}
     #constructor with 2D arrays
     function DataContainer(stored_data::AbstractMatrix{FT}; data_are_columns = true) where {FT <: Real}

         if data_are_columns
             new{FT}(stored_data)
         else
             new{FT}(permutedims(stored_data, (2, 1)))
         end
     end
 end

The behavior for this struct is as follows,

julia > u = rand(5,3)

output > 5×3 Matrix{Float64}:
 0.553978   0.100353  0.455385
 0.393232   0.138405  0.74255
 0.0660367  0.174017  0.401449
 0.669909   0.231632  0.249801
 0.113858   0.4794    0.315958

julia > data1 = DataContainer(u)
julia> data2 = DataContainer(u, data_are_columns = false)

julia> get_data(data1)

output > 5×3 Matrix{Float64}:
 0.273102   0.24338   0.728426
 0.0066842  0.320747  0.562981
 0.420169   0.521042  0.620609
 0.431041   0.177357  0.631251
 0.284572   0.120433  0.876362

julia> u[1,1] = -5

julia> get_data(data1)

output > 5×3 Matrix{Float64}:
 -5.0        0.24338   0.728426
  0.0066842  0.320747  0.562981
  0.420169   0.521042  0.620609
  0.431041   0.177357  0.631251
  0.284572   0.120433  0.876362

# With data_are_columns, the memory is shared.

julia> get_data(data2)

3×5 Matrix{Float64}:
  0.273102  0.0066842  0.420169  0.431041  0.284572
  0.24338   0.320747   0.521042  0.177357  0.120433
  0.728426  0.562981   0.620609  0.631251  0.876362

# With !data_are_columns, the memory is not shared.

@odunbar
Copy link
Collaborator

odunbar commented Mar 2, 2022

Yeah this is unexpected... let's always make a copy on setting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants