Julia: writing matrix with NaN values to binary file

Viewed 202

I have a matrix containing Float64's and possibly some NaN entries. I would like to save it to file as a file in binary (as this is fastest and speed is important here) e.g.,

io = open(string(saveto_dir,"/arr"),"w")
@time write(io,arr) 
close(io)

However Julia gives the error message:

`write` is not supported on non-isbits arrays

Is there a workaround?

1 Answers

It sounds like this is due to (perhaps inadvertently) using an Array of type Any (i.e., Array{Any}), but if your data is as described you can use an Array{Float64} instead, in which case you will not have this problem.

To give a concrete example, writing

arr = Any[1.0, NaN]
io = open("./arr","w")
write(io,arr) 
close(io)

gives exactly the error you describe, but if you just change that to

arr = Float64[1.0, NaN]
io = open("./arr","w")
write(io,arr) 
close(io)

there is no error.

In general, one should vehemently avoid Array{Any} (or really anything involving Any) when there is any other practicable option, effectively due to type instability. For example:

julia> A = rand(10000); # Array{Float64}

julia> B = Any[A...]; # Array{Any}

julia> using BenchmarkTools

julia> @benchmark sum($A)
BechmarkTools.Trial: 10000 samples with 10 evaluations.
 Range (min … max):  1.318 μs …  12.705 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     1.347 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.400 μs ± 269.376 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▇█▅▂▂▂      ▃       ▂▂                                    ▁ ▂
  ██████▇▆▁▅▅███▇▁▃▃▁▁██▆▅▃▄▃▃▁▁▁▁▁▁▁▁▁▁▁▁▃▁▃▃▁▃▁▁▃▁▁▁▃▁▁▃▁▇█ █
  1.32 μs      Histogram: log(frequency) by time      2.62 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark sum($B)
BechmarkTools.Trial: 10000 samples with 1 evaluations.
 Range (min … max):  185.967 μs …  1.664 ms  ┊ GC (min … max): 0.00% … 75.01%
 Time  (median):     195.609 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   213.615 μs ± 62.394 μs  ┊ GC (mean ± σ):  1.31% ±  4.62%

  ▅██▅▄▅▃▃▂▂▄▃▁▂▁▁▂▄▁▁    ▃▃                                 ▂ ▂
  ███████████████████████▇████▆▆▇▆▄▄▄▃▄▃▅▄▄▅▄▁▅▃▄▅▅▃▄▃▃▄▄▅▁▄▅█ █
  186 μs        Histogram: log(frequency) by time       413 μs <

 Memory estimate: 156.23 KiB, allocs estimate: 9999.

About a 100x performance difference is fairly typical (notice also the difference in number of allocations).

Related