-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
add support for userdata and the corresponding tests #39
Conversation
Hi, thanks a lot for your contribution! However I'm a bit nervous about adding a field tagged with |
Hi, I have just made the Thank you for sending me this link. I have some concerns about that solution. Julia's official performance tips have mentioned that a "captured" value could cause type instability, which costs performance issue. See the following link. https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured. Moreover, with the userdata keyword, the user will have more choice how to pass parameters to the integrand. |
Ok, let's do it. Can you please update the documentation, including an example? Thanks! |
Sure, I will work on it. |
Hi, I have just updated index.md to include an example for the userdata keyword. Please take a look and feel free to modify and improve it. English is not my native language. In addition, I need some help from you on the following things:
Thank you very much! |
The random seed is fixed to 0 by default: Line 37 in d6e4736
I'd prefer to keep the doctests, at least they've been consistent for some time in CI. |
I investigated this problem a little bit. I find that both vegas and divonne give the exactly the same results. However, suave has a small descrepency (but within the errorbar). The below is what I got: julia> suave((x, f) -> f[1] = cos(x[1]))
Component:
1: 0.8413748866950277 ± 7.772872640827611e-5 (prob.: 1.0)
Integrand evaluations: 23000
Number of subregions: 23
Note: The desired accuracy was reached while according to the existing doc (the first example): julia> suave((x, f) -> f[1] = cos(x[1]))
Component:
1: 0.8413748866950329 ± 7.772872640815592e-5 (prob.: 1.0)
Integrand evaluations: 23000
Number of subregions: 23
Note: The desired accuracy was reached The results here are calculated with your code in master branch, so tiny discrepency has nothing to do with the new userdata update. |
Anyway, I have added back the doctests. Although I am still getting tiny discrepency on my local computer, the CI doctests work just fine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before going on, in light of #40, would you agree to contribute to this package under the terms of the MIT license, instead of the current LGPL?
Yes, please! But before merging this PR I'd like to fix CI (see #41), and that's already proving to be complicated. |
function generic_integrand_userdata!(ndim::Cint, x_::Ptr{Cdouble}, ncomp::Cint, | ||
f_::Ptr{Cdouble}, func!_and_userdata, nvec::Cint) | ||
func!, userdata = func!_and_userdata | ||
# Get arrays from "x_" and "f_" pointers. | ||
x = unsafe_wrap(Array, x_, (ndim, nvec)) | ||
f = unsafe_wrap(Array, f_, (ncomp, nvec)) | ||
func!(x, f, userdata) | ||
return Cint(0) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to https://codecov.io/gh/giordano/Cuba.jl/compare/3ddf09bf2627745fab70fa90f0285350e5ac79d4...616a7a108c1c9bda05bbc0825aaf435c91a561af/diff this function is untested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the new commit, I have added some tests for vectorized integration with userdata. It should be sufficient to cover the untested function.
Thanks a lot! |
This is included in |
Hi,
Thank you for creating this fantastic package! I use it heavily for calculating Feynman diagrams in condensed matter physics.
For my applications, I need to provide the integral with a lot of userdata (parameters, the structure of diagrams, etc.). Using global constants makes the code much less flexible. So I need to add support of userdata to your package.
The implementation is straightforward: I pass a tuple of (func!, userdata) to the C function, then unpack it in the generic_integrand function which is on the julia side.
To keep the package back-compatible. The new userdata APi will be invoked only when the user provides the userdata keyword explicitly. I also didn't touch the existing core functions to avoid any possible regression.
The usage is shown as below:
An interesting observation of the above examples is that the new API with the userdata keyword significantly reduces the memory allocation. The packing and unpacking the integrand the userdata probably helps the julia compiler to learn more type information. If this is true, it probably makes sense to improve the old API to reduce memory allocation. But this could be for a separate PR.
A couple of new tests are added for the userdata keyword.
If everything looks fine to you, I could update the documentation, too.