-
Notifications
You must be signed in to change notification settings - Fork 39
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
Release GIL #25
Comments
It should be very easy to implement since it is literally two lines of code per API function. However, the OSQP interface is a bit more complicated than the SCS one since we divide the python commands exactly as in the C API with It might be useful to write some unittests to make sure the internal OSQP memory does not get corrupted in multi-threading if we disable the GIL. Just a curiosity, for which application do you need to disable the GIL? EDIT: After thinking about it, I am not 100% sure simply disabling the GIL would work since we are using the workspace structure which is shared between different functions calls. |
I think releasing the GIL would be most important for the The application for disabling the GIL is solving multiple OSQP in parallel, in python, using threads. If the GIL is never released, then the calls will actually be concurrent. Using threads is preferred over processes because you don't have all the overhead of forking a new process, copying memory, etc... |
The problem is that if we release the GIL, the workspace is shared between threads that will probably end up overwriting the ADMM iterates and other variables. OSQP Python wrapper defines the workspace inside a python object and stores it so that we can reuse it in subsequent solves. SCS does not do that. Instead, it wraps the function scs which calls One solution is to write a similar function for OSQP that executes the However, if you called OSQP with many threads in this way, I suspect you would find similar bottlenecks as with multiprocessing. This is because when we run Let me know this solution would work for you anyway. |
The main motivation would be for here: It seems like setup and solve are called together here. |
PR #29 should fix this issue. It adds a function In the osqpth repo you mentioned setup and solve are called together. However, I added some comments like this one where I believe it would be better to split setup and solve. I am not sure what would be the most efficient way to do so (using setup + solve separately VS having a unique function that performs them together disabling the GIL). |
This looks great to me. I will give it a try.
I do believe that calling setup and solve separately could definitely be
beneficial. Maybe we can release the GIL for each separately?
…On Fri, Aug 16, 2019 at 11:26 AM Bartolomeo Stellato < ***@***.***> wrote:
PR #29 <#29> should fix
this issue. It adds a function osqp.solve that does not need any object.
It performs setup solve and cleanup immediately and it disables the GIL.
@sbarratt <https://github.com/sbarratt> could you please try that branch?
In the osqpth repo you mentioned setup and solve are called together.
However, I added some comments like this one
<https://github.com/oxfordcontrol/osqpth/blob/c001ec76857af60027337969f9c0e62c7f0a41e0/osqpth/osqpth.py#L25>
where I believe it would be better to split setup and solve.
I am not sure what would be the most efficient way to do so (using setup +
solve separately VS having a unique function that performs them together
disabling the GIL).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#25>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB7LUGI3OVJAGNNN4O5FDYLQE3WMTANCNFSM4H4LGVJA>
.
|
Unfortunately not. If you run setup once and then solve in parallel using threads with no GIL, they will change the workspace variables at the same time. This can create many troubles. The exact same issue applies to the SCS C library by the way. |
The use case im referring to is for entirely separate problems, with their
own workspaces. It would presumably work here, right?
I’m assuming the user won’t be multithreading multiple calls using the same
OSQP object, since well warn them that it isn’t thread safe.
…On Mon, Aug 19, 2019 at 10:53 AM Bartolomeo Stellato < ***@***.***> wrote:
Unfortunately not. If you run setup once and then solve in parallel using
threads with no GIL, they will change the workspace variables at the same
time. This can create many troubles. The exact same issue applies to the
SCS C library by the way.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#25>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB7LUGLYTDOLDQFR7LJ73MTQFLM25ANCNFSM4H4LGVJA>
.
|
I need to point the osqp source to the develop branch, right?
…On Mon, Aug 19, 2019 at 11:28 AM Shane Barratt ***@***.***> wrote:
The use case im referring to is for entirely separate problems, with their
own workspaces. It would presumably work here, right?
I’m assuming the user won’t be multithreading multiple calls using the
same OSQP object, since well warn them that it isn’t thread safe.
On Mon, Aug 19, 2019 at 10:53 AM Bartolomeo Stellato <
***@***.***> wrote:
> Unfortunately not. If you run setup once and then solve in parallel using
> threads with no GIL, they will change the workspace variables at the same
> time. This can create many troubles. The exact same issue applies to the
> SCS C library by the way.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#25>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AB7LUGLYTDOLDQFR7LJ73MTQFLM25ANCNFSM4H4LGVJA>
> .
>
|
Tried this a few days ago and got a seg fault. I’ll try again this weekend.
Shane
…On Mon, Aug 19, 2019 at 1:35 PM Shane Barratt ***@***.***> wrote:
I need to point the osqp source to the develop branch, right?
On Mon, Aug 19, 2019 at 11:28 AM Shane Barratt ***@***.***>
wrote:
> The use case im referring to is for entirely separate problems, with
> their own workspaces. It would presumably work here, right?
>
> I’m assuming the user won’t be multithreading multiple calls using the
> same OSQP object, since well warn them that it isn’t thread safe.
>
> On Mon, Aug 19, 2019 at 10:53 AM Bartolomeo Stellato <
> ***@***.***> wrote:
>
>> Unfortunately not. If you run setup once and then solve in parallel
>> using threads with no GIL, they will change the workspace variables at the
>> same time. This can create many troubles. The exact same issue applies to
>> the SCS C library by the way.
>>
>> —
>> You are receiving this because you were mentioned.
>> Reply to this email directly, view it on GitHub
>> <#25>,
>> or mute the thread
>> <https://github.com/notifications/unsubscribe-auth/AB7LUGLYTDOLDQFR7LJ73MTQFLM25ANCNFSM4H4LGVJA>
>> .
>>
>
|
Still getting a seg fault. Here's the code I'm running:
|
There is an issue with printing enabled if that function is called. Let's continue in #33 to avoid duplicates. Without printing this function should be fine. |
Would it be possible to release the GIL before calling into
osqp
, with the macroPy_BEGIN_ALLOW_THREADS
, or are there possible complications?Similar to how this is done in scs.
The text was updated successfully, but these errors were encountered: