Determinant of a square random matrix using juliac

The Julia code is

using LinearAlgebra

function (@main)(args::Vector{String})::Cint
    m = rand(Float64, 3, 3)
	for i in 1:3
		for j in 1:3
				print(Core.stdout, m[i, j], " ")
		end 
		println(Core.stdout)
	end 

	result = -Inf

	try
		result = det(m)
	catch e 
		println(Core.stdout, "Error occured")
		println(Core.stdout, typeof(e))
	end
    println(Core.stdout, "Determinant of matrix m is: ", result)
    return 0
end 

# This file currently throws a TypeError
# due to possibly a type unstability in the LinearAlgebra.det()

This file is located in the repo GitHub - jbytecode/juliac: Examples for Julia's generating small binary executables and libraries feature.

I compile the code using

julia +nightly ./juliac/juliac.jl --experimental --trim  --output-exe determinant determinant.jl

When I run the compiled binary using

> ./determinant

I get

> ./determinant
0.8743122811827831 0.6300620531943388 0.3450695610105955 
0.7027868683889693 0.8758619592106516 0.2880180735794807 
0.6450057839884562 0.3323840835725862 0.8649912996516349 
Error occured
TypeError
Determinant of matrix m is: -Inf

I know juliac is its initial stages, how to get rid of this error?

2 Likes

Strange, I was under the impression that type instabilities would result in compile-time warnings/errors, but that looks very much like a runtime error. What happens when you don’t try-catch, could you let the stacktrace print out and see what calls threw it?

Might be worth making the matrix values constant while you work it out, but those printed values should work fine with det.

julia> using LinearAlgebra # 1.12.0-rc1

julia> [0.8743122811827831 0.6300620531943388 0.3450695610105955
       0.7027868683889693 0.8758619592106516 0.2880180735794807
       0.6450057839884562 0.3323840835725862 0.8649912996516349] |> det
0.19838562158551779
1 Like

On my system, it does compile and provide a numerical result (and no error) using the 1.12 release candidate. I have experienced the same as you have on the nightly releases. I don’t know the difference between the two, but it seems the nightly version is still more unstable than the 1.12 rc.

2 Likes

Yikes, does that happen when you evaluate the same code in the REPL?

The issue on nightly is due to known limitations around LazyLibrary and --trim

This works fine on 1.12, since it doesn’t use LazyLibrary to load BLAS, but on master this will be regressed until this is fixed trimming: no way to `ccall` into a dynamically-computed library name · Issue #57707 · JuliaLang/julia · GitHub.

We’ve got a plan to resolve this before 1.13, but it’s definitely a regression for nightly in the mean time.

4 Likes

@topolarity - Thank you for the response. Whenever I remove the --trim flag, it successfully compiles and runs as expected (but indeed it’s very big). I’m now leaving the Makefile as is with the nightly build and I will be watching the latest news on the juliac.
Thanks again for those who contribute to juliac again.

1 Like