-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi-threading particle swarm #735
Conversation
…icles within bounds. speedup on my particular problem was at least 2x from 1 thread to 6.
I think this is the wrong way to go. Instead, I think the interface should be changed so that the user gives a function |
That sounds nice but it also sounds like a major change. I just wanted to implement something here that can at least get this more on par with solvers in languages like MATLAB where this is already possible. Like I said, I didn't test it on any problems other than mine, but it seems like it should work fine as long as the cost function is relatively standalone. |
Also tricky because most optimizers don't support multiple evaluations at the same time, which would make it sort of weird to change the API for this single particular case. In general there's an explosion in the properties of the objective functions (differentiable, in place, multiple evaluations) that's pretty nasty to handle... Short term I think this feature could be useful, provided it's toggled by a flag (also objective functions that have threaded BLAS calls inside them should not be threaded, at least for now) |
Codecov Report
@@ Coverage Diff @@
## master #735 +/- ##
==========================================
- Coverage 81.69% 81.64% -0.05%
==========================================
Files 43 43
Lines 2414 2414
==========================================
- Hits 1972 1971 -1
- Misses 442 443 +1
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #735 +/- ##
==========================================
- Coverage 81.69% 81.52% -0.17%
==========================================
Files 43 43
Lines 2414 2419 +5
==========================================
Hits 1972 1972
- Misses 442 447 +5
Continue to review full report at Codecov.
|
I can add a user supplied |
I added a user-supplied flag to enable the multi-threading, which when enabled just calls the threaded version of the There is one outstanding issue with this that I've noticed and it is that the tracking of the total number of function calls is off. When run single-threaded, the number of function calls seems to be |
Hm, that's right. The easy way out is to make the add atomic in the counter incrementation. That's in NLSolversBase, in the objective function wrappers. |
We discussed this on slack so I just want to note that I've seen it. I'm still digesting the change and comments. For some things @ChrisRackauckas's approach can be nice, but in other cases what's in here is what you need. I think we may just have different modes if we want to support this. As @antoine-levitt mentions, if it's threading you're after then many times you can take advantage of all your available threads in the objective function yourself. Sometimes the objective function is inherently serial, so it may only be possible to thread at the |
so I have a proto type of this, and it wasn't hard to do at the PSO level, but it won't really play nice with |
What about something like
? Serial is what it is now, Threading is a |
The new PSO will support this. But thanks for bringing this up! :) |
https://discourse.julialang.org/t/optim-jl-particleswarm-multithreading-at-the-function-level/50117 for those who find this issue |
I added
Threads.@threads
in two spots:compute_cost!
andlimit_X!
. The speedup on my particular problem was approximately 3x going from 1 thread to 6, the bulk of which comes from parallel execution of the cost function.The user does not have to do anything additional, just needs to start up julia with multiple threads.
I have not run any further tests, and I am not sure how well threading would work on every problem. If there is IO or something else funky inside the cost function (calls to a RNG?), there could be an issue with multi-threading (from what I read on the recent blog post on the homepage).
I am on julia v1.3.0-alpha.