Removing globals from minpack wrapper
![](https://secure.gravatar.com/avatar/1d3f40b8fb201f5a6a2e62b9ba09b025.jpg?s=120&d=mm&r=g)
I was having my fun with optimize.fsolve() at work, when I realized that both the Python side and the C/FORTRAN side allocate work spaces, and then copy between them, which seemed wasteful. I started playing around with the code to try and create a version that allows the same buffer to move around between Python and minpack. I couldn't do that (and I doubt now that it's possible), but on the way I found a method to remove the global variables that are used to store the data needed by the multipack functions but not passed in by the FORTRAN code which doesn't even know about it. The trick was to allocate extra space on the work buffers and use it as a pointer to a structure that contains the information. This may seem just as ugly, but it has all the advantages of not using globals, including the prospects of someday making it thread-safe. The attached patch implements the trick in minpack_hybrj, and the Python code tests that it works. Do you think it's worth doing to the rest of the minpack functions? -- http://yosefm.imagekind.com/Eclectic
![](https://secure.gravatar.com/avatar/40489da22d2dc0cc12596420bb810463.jpg?s=120&d=mm&r=g)
Yosef Meller wrote:
I was having my fun with optimize.fsolve() at work, when I realized that both the Python side and the C/FORTRAN side allocate work spaces, and then copy between them, which seemed wasteful. I started playing around with the code to try and create a version that allows the same buffer to move around between Python and minpack.
I couldn't do that (and I doubt now that it's possible), but on the way I found a method to remove the global variables that are used to store the data needed by the multipack functions but not passed in by the FORTRAN code which doesn't even know about it.
The trick was to allocate extra space on the work buffers and use it as a pointer to a structure that contains the information. This may seem just as ugly, but it has all the advantages of not using globals, including the prospects of someday making it thread-safe.
The attached patch implements the trick in minpack_hybrj, and the Python code tests that it works.
Do you think it's worth doing to the rest of the minpack functions?
That is pretty clever. Amy I correct that the only time cost is an extra array allocation (for ap_fvec) per complete calla? I think it is worth it. Using globals requires a bit of rigging to support re-entrance of the optimizers (so you could have a python function that itself calls optimize). Thanks for looking at the code. -Travis
![](https://secure.gravatar.com/avatar/1d3f40b8fb201f5a6a2e62b9ba09b025.jpg?s=120&d=mm&r=g)
Travis E. Oliphant wrote:
Yosef Meller wrote: [snip]
The attached patch implements the trick in minpack_hybrj, and the Python code tests that it works.
Do you think it's worth doing to the rest of the minpack functions?
That is pretty clever. Amy I correct that the only time cost is an extra array allocation (for ap_fvec) per complete calla?
That's about right. Maybe there's extra O(evals) pointer retrievals, but that's much smaller than O(n) if I understand this correctly.
I think it is worth it. Using globals requires a bit of rigging to support re-entrance of the optimizers (so you could have a python function that itself calls optimize).
I'll try to keep the patches coming. -- http://yosefm.imagekind.com/Eclectic
participants (2)
-
Travis E. Oliphant
-
Yosef Meller