What method did Julia use?
Asked Answered
M

1

10

I have defined the following variable

julia> X = (1:10) * ones(1,10)

which defines a matrix with each row equals to the same number and the numbers in the column increasing from 1 to 10 by 1. I want to know which method used Julia for the function *. How can I ask that to Julia?

Matrilineage answered 1/2, 2016 at 1:52 Comment(3)
That's matrix multiplicationDottydoty
I know it @Daniel, just I want that Julia tells me that I am using the method that is defined like *(UnitRange{Int64}, Array{Float64,2})Matrilineage
I'm not very good at interpreting the output, but @code_warntype (1:10) * ones(1,10) might tell you what you need to know. As near as I can tell (and I emphasize this is not something I know much about), ultimately * appears to be calling Base.LinAlg.generic_matmatmul! on two input Array{Float64, 2}. Hopefully someone a bit more knowledgeable can chip in.Proclivity
I
12

@which is what you are looking for:

@which (1:10) * ones(1, 10)
# *(A::AbstractArray{T,1}, B::AbstractArray{T,2}) at linalg/matmul.jl:89

in Jupyter it will also hyperlink to the corresponding line of code where the method is defined in Julia's GitHub.

Incendiarism answered 1/2, 2016 at 3:13 Comment(7)
Interesting. Is there a way to iteratively dig deeper? ie, the line referenced in your answer calls * again, but on two abstract matrices. Is there a way to get @which to also tell us which method was called next? (presumably in this case it was * for two Matrix{Float64}?)Proclivity
@colin No that I know of but I'm sure there is a metaprogramming way of doing it.Incendiarism
@ColinTBowers if you got that line of code, you can check the next-* by which(*, (Matrix{Float64},Matrix{Float64})) manually.Topsoil
is this tracing nightmare a drawback of multiple dispatch? :pIncendiarism
@Incendiarism i guess what Colin want is just the Jump to Definition functionality in most of modern IDEs which can be used to dig out more info about * iteratively in this specific case.Topsoil
@GnimucK. "which(*, (Matrix{Float64},Matrix{Float64}))" -> yep, that does it. As amrods suggests, there's probably a neat little function that can be written here to iterate up the chain. Thanks for responding (both of you).Proclivity
When tracing code this way, I assign new names to my variables that match the arguments of the function found by @which. Then I copy/paste lines of code from that function into the REPL until I get to the next line I want to trace, and use @which again. Saves you from having to be sure about the types.Ilarrold

© 2022 - 2024 — McMap. All rights reserved.