-
-
Notifications
You must be signed in to change notification settings - Fork 392
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
Remove RNG_BEGIN()
and RNG_END()
#2583
Comments
Why is it difficult to control nesting with a flag in thread-local storage? |
It could be, but this would still need to be implemented on the R side, where But it seems to me that this would only be a temporary workaround and wouldn't address the core issue, which is that micro-managing this is difficult. Besides nesting, sometimes we forget to add the RNG_BEGIN/END, and sometimes we add too many potentially performance-killing saves/restores. The clean solution here is to do a single save/restore at the beginning/end of functions in the generated wrappers. In the long term this is the least amount of work, and only requires a fairly small adaptation for the R interface generator. |
In other projects, we've seen that having these wrappers everywhere also harms performance. I need a better understanding of the issue at hand, and I can't spend the time it needs right now. |
This is why we can make it opt-out for the interface generator, and in the longer term disable it where it's not needed. EDIT: Let's discuss it at a meeting then. If this is not done for 1.0, we'd need to wait for 2.0, which may be quite some time away ... It's not a lot of work on the R side, and will significantly simplify things. It'd be very good to do it. |
Note that with this we would simply be trading the micro-management of My biggest worry right now is that we have absolutely no way to validate whether the RNG is actually used outside an |
What we have now only doesn't seem to be more of a burden because issues go unnoticed (no symptom unless R is involved, and difficult to notice symptoms even with R), and it's too easy to ignore issues. I'm sure that igraph is full of these kinds of bugs at the moment. Here's just the last one I fixed: 3cbf7be The test suite cannot detect these bugs, so we're back to having to pay extra special attention. There's many instances of RNG_BEGIN being called in a loop which invalidates any performance concerns about just adding these in the R interface generator. Just now as trying to make my point I found this: igraph/src/community/label_propagation.c Lines 264 to 284 in c6606ae
How would you fix this? Move it outside the loop? Don't miss that As you can see Having to pay attention to all this is a much bigger burden to me than marking functions as needing the RNG. BUT! I never wanted to make this opt-in, but opt-out. Functions are assumed to need the RNG by default, unless it is indicated that they don't. If there's reason to believe that the unnecessary call is a performance issue for some function, only then one can put in the effort to investigate if marking it as non-RNG is possible. I expect only a few simple functions, like |
Ah okay, this seems to make more sense to me. So, basically, all functions would be assumed to call the RNG at one point or another, unless explicitly specified otherwise in How shall we call the new flag in There remains the question of initializing the RNG. Most higher-level interfaces initialize the RNG anyway as far as I know so we can safely assume that higher-level interfaces need not provide an RNG binding that needs to take care of initializaiton. The C core would need to auto-initialize the RNG to keep the current behaviour. We could either say that in low-level C one needs to seed the RNG explicitly, or we could provide a C implementation of the RNG that auto-seeds. In order to avoid performance problems by checking whether the RNG is seeded before every call to a RNG routine, we would actually need to provide two RNG implementations in C: a private one that assumes that the RNG is already seeded, and a public one that assumes that the RNG is not seeded, seeds it when needed, and sneakily replaces the RNG function table with the private one to avoid performance problems in future calls. |
This issue is to discuss the removal of
RNG_BEGIN()
andRNG_END()
for igraph 1.0, which were primarily motivated by R's requirement to save/restore the random state. This is the last major change needed to fully decouple the C core from the R interface and remove theUSING_R
macro. It will pave the way to linking the R interface to the C library in the same way as it'd link to any other library.Nesting these can cause issues, but avoiding any nesting is very difficult, almost impossible to manage. The current setup requires micro-managing the placement of
RNG_BEGIN/END
pairs, which is bug-prone, and likely has several existing bugs.These macros serve two purposes:
CC @krlmlr
Ref: igraph/rigraph#1131
The text was updated successfully, but these errors were encountered: