0

I'm now trying to precompile a Julia module so that it may run faster. However, it seems strange to me that writing __precompile__() before the module declaration doesn't seem to have produced any cache file in the folder.

Then, I tried to call the function Base.compilecache with the following steps:

  1. Launch the REPL in the project folder
  2. include("M.jl")
  3. Base.compilecache("M")

However, even though the first two steps finish without problems and that I can actually run the functions defined within the module, the third step complains "ERROR: ArgumentError: M not found in path", and I still can't seem to generate any cache for the module.

What did I do wrong here?

xji
  • 7,341
  • 4
  • 40
  • 61

1 Answers1

1

Compiled modules are produced in a user-defined place, in linux typically ~/.julia/lib/v0.6/. I'm sure if you look in there you will find .ji files corresponding to precompiled versions of your modules. Remove one and try importing again in a julia session to confirm that it will attempt to precompile all over again; if it does, it means your __precompile__() directive is working.

Furthermore, be careful to push! the path of your custom module (i.e. the source code .jl part, not the path for the precompiled .ji files) to the LOAD_PATH (i.e. push!(LOAD_PATH, "./") ) if this is not 'installed' in the default place (typically ~/.julia/v0.6/). Julia needs access to both when you're about to import the module.

(PS. you should also see a Precompilation occuring if you update your module)

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
  • I just looked into it but I don't see my module, only some other libraries. What could be the cause of this? Might there be something that makes a module uncompilable? – xji May 31 '18 at 19:33
  • ah, sorry, just spotted the exact wording in your question. Do not _include_ the module. `import` it. Precompilation checks, I believe, only take place when you attempt to `import` or `using` a module. If you simply `include` it, essentially you're dumping code into your current environment, so no attempt to precompile the module is made as far as I understand it. – Tasos Papastylianou May 31 '18 at 19:50
  • this is also the point about "having to load your current path to the LOAD_PATH variable". Julia only looks for modules in the directories defined in the `LOAD_PATH`. To import a module that is sitting in your current directory you need to add this directory in the `LOAD_PATH` first, so that julia can find it. – Tasos Papastylianou May 31 '18 at 19:52
  • Thanks. Using `import` finally let Julia precompile the module. However, does that mean anybody who wants to run this program will have to first alter the `LOAD_PATH` of their `julia` instance and then `import` the module? I guess the alternative would be to actually distribute the program as a Pkg and then ask the other users to install the Pkg. However, if the program is just a collection of scripts so far, then there seems to be no other way to do it? – xji Jun 06 '18 at 20:30
  • Not at all. Yes, the best way is to create a package, but alternatively you can distribute your module along with a script (as per my answer to your other recent question), and prepare any `LOAD_PATH` instructions there, along with any other environmental variable initialisation, or parsing of inputs etc. – Tasos Papastylianou Jun 06 '18 at 21:04
  • @xji also, you may also find [this answer to another question](https://stackoverflow.com/a/49405645/4183191) somewhat relevant too. – Tasos Papastylianou Jun 06 '18 at 21:06