Atom doesn't run called function correctly

I am trying to run JuliaMatrixBenchmark.jl and JuliaMatrixBenchmark0001.jl from the repository MatlabJuliaMatrixOperationsBenchmark, that I forked from an old repository for comparing MATLAB and Julia, and edited files to update its Julia language.
However, in Atom when I run the master script from the file JuliaMatrixBenchmark.jl, it gives me strange errors such as temp is not defined. However, if I copy paste the code into REPL it works with no issues. Where is the problem?

Edit: if you are reading this post later: I replaced those files with new ones in the repository which is available here: MatlabJuliaMatrixOperationsBenchmark

I think you should try to paste a minimum working example so that helping you is easier. We will not be able to get very far or to see that your function calls are correct.

However, it sounds to me like you hit a scoping problem. Is there any variable called “temp” that you are trying to update inside a for-loop? try writing global temp inside the for but before trying to update that variable.

Edit:

Apparently the problem is that @benchmark cannot see the local scope, it only sees the global (this I don’t know why).
If you add global temp = cRunTimeFunctions[jj] it will work, although I don’t know if that’s the right way to do it. The reason for it working in the REPL is that the REPL works in the global scope. This scoping rules can be a good place to look at when something works at the REPL but not elsewhere.

In line 110 you have trnasposed instead of transposed
In line 117 you need to use the word dims, like mA = sum(mX, dims=1) .+ minimum(mY, dims=2)

1 Like

Not sure what makes you think that this is an issue with Atom running your code and not with your code. Compare e.g.

julia> let tmp = sin
           @benchmark tmp(1.2)
       end
ERROR: UndefVarError: tmp not defined

julia> let tmp = sin
           @benchmark $tmp(1.2)
       end
1 Like

Defining temp, matrixSize, mX, mY as global solved the issue. However, this is very strange, I don’t understand why I should do this! Is it because of @benchmark?

BTW, thank you for finding the typos. Unfortunately, Atom doesn’t have any code analyser (or it doesn’t work for me).
I updated the repository for this file.

Adding $ doesn’t help.
P.S: I have started with Julia for 1 week, and I asked my question in First steps section, so don’t blame me for not being familiar with these strange problems that may occure.

Yeah, this is apparently part of the design choices of BenchmarkTools.

From the documents here:

Normally, you can't use locally scoped variables in @benchmark or @benchmarkable, since all benchmarks are defined at the top-level scope by design. However, you can work around this by interpolating local variables into the benchmark expression:

# will throw UndefVar error for `x`
julia> let x = 1
           @benchmark sin(x)
       end

# will work fine
julia> let x = 1
           @benchmark sin($x)
       end

Also, as your matrices are also defined in the local scope, you might want to call the function as @benchark temp($matrixSize,$mX,$mY) (adding a ‘’$') to make the benchmarks more accurate.

P.s for me it worked with adding global temp before using it, without making matrixSize, mX, mY globals.

1 Like