I wrote a C++ plugin for the statistical software Stata to give it access to Julia. Stata is a GUI program, so in macOS, it is normally not launched from a shell. The problem I have is giving the user the option to launch it with nthreads > 1.
This is hard because on the Mac, I believe that graphical programs normally have no access to environment variables. So setting JULIA_NUM_THREADS is normally useless.
And the jl_init() function that launches an embedded copy of Julia takes no arguments. So I see no way to pass the equivalent of an “-t 8” command line option at launch.
Opening a terminal window and typing open -a StataMP does work, for then the graphical program is launched in a context with environment variables defined by .zshenv. But I would rather the user didn’t have to learn such tricks.
Is there a way to pass the equivalent of command line arguments to Julia when embedding it?
You probably should be using Jluna (since you’re calling from C++, it improves on the C API of Julia for C++, maybe even you should use this if your code were C code?):
I find the installation process for that overwhelmingly complicated, especially if I have to do it in three platforms. I’m developing for Windows, Linux, and macOS, and I didn’t know all of them well.
I wouldn’t know, I’ve not even used Jluna for one platform! I just wanted to help, if the C API directly (or some other wrapper helps) is enough for you then great! It seems you have good help already, reading the thread. It just seemed to me that Jluna is the go-to-solution for C++, though it may be overkill for you, or just for enabling multi-threading.
It seems sensible that GUI apps on macOS do not have access to ENV vars, and I believe that’s also done by now on Windows, at least for some types of GUI apps.
But in either case, it seems like ENV vars may work, in the sense that setting them from within your app, for further processing from it, for its benefit, seems still sensible, and I don’t see a reason why it shouldn’t work or be blocked.
It seems if I’m reading the right code, that’s what Jluna does:
so if it doesn’t work it will be surprising for users, and I and others would like to know! About Jluna being “overwhelmingly complicated” (to install), it’s at least meant to be a better C++ API, and the only one claiming that. Whatever you do, I would like to know how it goes, if you or other can embed into a larger (cross-platform) GUI app.
If you were just making a cross-platform (GUI) app, with Julia as the only language, or the main one, I would point you to AppBundler.jl. I’m not sure it helps you since you do not start with Julia (or how well it would fit with Jluna, probably not going to hurt, nor help much), and call e.g. C++/Stata (could you?). I’m not sure, maybe it can also help if Julia is being called? It’s a rather new project, I would like to know more about app building across languages (and operating systems), people claim difficult for Python alone, harder for Julia alone, and even more combining (on recent Reddit thread). Tools are likely not ready for that (by default), but might be improved…
I’m sure someone has embedded Julia before, for any of the platforms (should be supported on all), even with threading, not sure anyone has done for all three yet, or a GUI app. It seems like necessarily more complex, or at repeated compilation 3x. Do you think Jluna could do something to make it better? About changing code, or just adding docs?
Thank you @mkitti. This is tremendously helpful. The key thing I learned is “first argument is the name of the executable”. I was putting “–threads=7” in argv[0].
In my case I am compiling to a shared library, not an executable, so technically the caller is Stata. But I found I can just set argv[0] to 0 in your example and it still works.
It is now possible for a Stata user to type jl start, threads(7) and launch Julia with 7 threads.