Can print affect the behavior of a script?

Hi everyone,

I have a really strange problem. I have a simulation software that I’m developing. When I run my tests, the results may change depending on what I print. An example will be clearer I suppose.

I run my tests using Julia run_tests.jl. Consider this function

function convert_to_left_site(mat::AbstractMatrix{Float64}, n_p_edges::Int, p_size::Int, l_size::Int)
    println(length(mat), "  ", p_size, "  ", n_p_edges, "  ", l_size)
    r_size = div(length(mat), (p_size^n_p_edges) * l_size)
    return Site(reshape(mat, fill(p_size, n_p_edges)..., l_size, r_size))

I have different results if I comment (or remove) different lines. If I comment nothing I get

0  3  1  0

If I comment only the first println I get

9  3  1  1

And if I comment only the second println I get

3  3  1  1

The rest of the code may work or not depending on those values. Also, this function is always call by the same function before using the same inputs.

Here is the whole code if you want more info. The function is defined at line 82 of the file site.jl.



This is the kind of behavior I’m used to seeing in C++ when I accidentally invoke undefined behavior (like reading or writing off the end of a vector, for example). Such things are also possible in Julia (albeit much less common because Julia’s default behaviors are safer), but it’s hard to know without more information. What inputs do you use to invoke that function to get the varying outputs?

1 Like

Technically, the inputs should be
mat is a 3 by 3 matrix of Float64
n_p_edges is 1
p_size is 3
l_size is 1.

If I print anything in the function that invoke convert_to_left_site, the input are always corrects. If I don’t print before, I get a 0 by 0 matrix as input instead.

I’ve tried to get to the top of the call stack to see where the errors appear first, but every time I tried to print something or change a part of the code, it works. Thus, I don’t know where is the source of the errors.

Finally, I observed the same behavior if I add a random line of code such as x = 2. If I comment this line, some errors appears, if not, the result is good.

Also, here is the run_test.jl file

using Test

x = 2

Everything work fine this way and I get

julia Tests/run_tests.jl
Test Summary:   | Pass  Total
MatrixProbState |    6      6

But, if I comment the x = 2 line. I get some errors

julia Tests/run_tests.jl
MatrixProbState: Error During Test at /Users/maximetremblay/Git/Doctorat/TNPolarCodes/Tests/matrix_prob_state.jl:1
  Got exception outside of a @test
  DivideError: integer division error
   [1] div at ./int.jl:232 [inlined]
   [2] convert_to_left_site(::Array{Float64,2}, ::Int64, ::Int64, ::Int64) at /Users/maximetremblay/Git/Doctorat/TNPolarCodes/Sources/site.jl:86
   [3] left_truncate(::Site{3}, ::Site{3}, ::Int64) at /Users/maximetremblay/Git/Doctorat/TNPolarCodes/Sources/site.jl:126
   [4] left_canonical_form!(::MatrixProbState) at /Users/maximetremblay/Git/Doctorat/TNPolarCodes/Sources/matrix_prob_state.jl:39
   [5] top-level scope at /Users/maximetremblay/Git/Doctorat/TNPolarCodes/Tests/matrix_prob_state.jl:5
   [6] top-level scope at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/Test/src/Test.jl:1083
   [7] top-level scope at /Users/maximetremblay/Git/Doctorat/TNPolarCodes/Tests/matrix_prob_state.jl:2
   [8] include at ./boot.jl:326 [inlined]
   [9] include_relative(::Module, ::String) at ./loading.jl:1038
   [10] include(::Module, ::String) at ./sysimg.jl:29
   [11] include(::String) at ./client.jl:403
   [12] top-level scope at none:0
   [13] include at ./boot.jl:326 [inlined]
   [14] include_relative(::Module, ::String) at ./loading.jl:1038
   [15] include(::Module, ::String) at ./sysimg.jl:29
   [16] exec_options(::Base.JLOptions) at ./client.jl:267
   [17] _start() at ./client.jl:436
Test Summary:   | Error  Total
MatrixProbState |     1      1
ERROR: LoadError: LoadError: Some tests did not pass: 0 passed, 0 failed, 1 errored, 0 broken.