How to precompile a function with many arguments


#1

Dear all,

I have a function that takes many different inputs and returns a function:

function value_N(
    a::Float64,
    ind_z::Int64,
    # Many other arguments, mainly vectors and arrays  
    VFs::ValueFunctions # My own types
    )
    # Function does her thing
    return return_f
end

How can I pre-compile it? (I’ve already tried specifying all the variables types in a ())

Thanks!


#2

What are you trying to do, precisely? Are you trying to use the precompile function in module, in which case you would use precompile(value_N, Tuple{Float64,Int64,...,ValueFunctions})? Note that Julia will end up compiling your function when it is called anyway, so in 99% of cases there is no reason to call precompile.

Also note that there is no performance advantage to declaring concrete types like Float64 and Int64 for all of your arguments. Julia will type-specialize your function when it is compiled even if the arguments have no types. And by declaring overly restrictive types you are probably making your code less generic than it could be.


#3

Thanks.

Basically, I have a function that does a big computation. At the moment it takes 106 minutes to run and uses 17.7GB. I’m guessing there are problems everywhere. I mean, it’s a computationally intensive task, but in Fortran it takes around 7 minutes only. If the function is already compiled before I run it, I save time to check if the performance is improving.

I know that these type declarations do not improve performance but it’s a useful check-up exercise for my code. I want to make sure I’m passing an integer instead of a float in many cases.


#4

Yes, for type safety and debugging, annotation can be useful.

If I understand your situation, it is very doubtful that precompiling will help you debug. The compilation of even hundreds of lines of complex code will only take a small fraction of 106 minutes. And you are trying to improve by a factor of 20.

I advise breaking the task into smaller pieces (eg functions) to help identify the slow parts. You may already know that the compiler performance type inference on functions, and inlines agressively, so you may gain performance by breaking it into smaller pieces. Sometimes, it chooses not to inline when it should, but this can be overridden with @inline. But, you should only use @inline after you have the bigger performance issues solved.

Read the page in the manual on performance (google “julia performance tips”).

See also @code_warntype.


#5

Compilation time should be insignificant compared to such an expensive task, so it’s really not something you need to worry about here. You can use preompile as @stevengj suggested above, but it’s probably not worth the trouble.

The first step in making your function faster would be to read the performance tips from the manual and make sure you’re obeying them. If there’s a specific part of your approach that’s slow, you can post a working example and we can help you make it faster.