I realize I can start my program like this:
$ julia -t auto test.jl
but I would also want to be able to just click on the file to run it (e.g. in Windows, where a file extension should run it with julia for me) or run like, and get threading:
$ ./test.jl
I can do that in Linux with a shebang, i.e… the first line of the main .jl program file being something like this (or something shorter), and such is ignored on Windows:
#!/home/pharaldsson/.julia/juliaup/bin/julia -t auto
But I can’t nor do I want to have to run it like this:
$ ./test.jl -t auto
If people are worried about changing the default to auto, then it’s in the hands of users to run programs like:
$ julia -t 1 test.jl
or control with the ENV var, or at least on Linux/non-Windows with this first line:
#!/home/pharaldsson/.julia/juliaup/bin/julia -t 1
if you want to have threading control in the programmers hands (where it seems like it should be).
I think I know the pros and cons. At one point the threading API was experimental, so I guess it was considered safer to have it opt-in. Given the potential cons, would people think such a change needs to wait for 2.0? I think that’s overkill, should just be documented in NEWS? There was however notification recently that seems not to apply, since it applies with or without threads (how might it be fixed; in 2.0 only?):
This is not actually a problem with multithreading specifically, but really a concurrency problem, and it can be demonstrated even with a single thread. For example:
$ julia --threads=1
By now it seems like a misfeature to start with just 1 thread. Threads are cheap to make, auto gives me 16 on my computer (same as virtual cores), and a potential 16x speedup, and it will grow larger with time as computers get more cores.
For those that don’t know, threads open the door to race conditions, i.e. bugs too, but only if you actually use them; and by “you”, I mean if your whole program doesn’t use threads i.e. nor any of your dependencies, then you’re safe. On the flip side, if e.g. you don’t use threads in your “program” (e.g. are ignorant of them, computer science), but one or more of your dependencies use, you are missing out on the speed of those (hopefully) correctly codes libraries.
If none of the code uses threads, which is actually opt-in, in the code, then you are just making 15 extra threads, very quickly, that just sit around not wasting any resources to speak of.
$ hyperfine 'julia -e ""'
Benchmark 1: julia -e ""
Time (mean ± σ): 212.9 ms ± 13.7 ms [User: 146.9 ms, System: 86.1 ms]
Range (min … max): 186.2 ms … 225.9 ms 14 runs
$ hyperfine 'julia -t auto -e ""'
Benchmark 1: julia -t auto -e ""
Time (mean ± σ): 212.5 ms ± 12.6 ms [User: 148.7 ms, System: 97.2 ms]
Range (min … max): 189.9 ms … 229.1 ms 13 runs
Now one other issue is that one one computer you might get 4 threads, and on a larger one 16, or 256. Which in the best case means faster, in the worse case slightly different results, non-bit identical, that just comes with the territory of parallelism. Also if you do I/O in a threaded loop, be design, it’s non-deterministic order. Why you shouldn’t be doing that…
I believe all of the package ecosystem should be tested with threads, i.e. where it applies. Why opt into using them in the code if you do not take the responsibility for the code and correctness. If people are really worried, I could see something like (compiled) packages using auto, and regular packages limited to one thread. Or vise versa, or possibly individual packages opting in or out. Most naturally the opt out by default by not using @threads
in their code, but their packages could use, or callers, e.g. regular user code.
For some reason threads can slow startup, but I believe it’s ionly when you oversubscribe, that auto could never do:
$ time julia -t 32 -e ""
real 0m0,258s
user 0m0,435s
sys 0m0,235s
$ time julia -t 320 -e ""
real 0m0,734s
user 0m7,111s
sys 0m0,367s