-
Notifications
You must be signed in to change notification settings - Fork 367
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 the testapi code to plot vectors #3528
Conversation
OK, this is a good test. What happens is that you tell GMT these are read-only data. Then it turns out that the operation you have selected (psxy lines with arrows) requires resampling. Having already read the data as read-only, now it would be up to the module to change that and allocate space (this is not happening, of course, and we reallocate and then try to free...). It seems inefficient to have to add such checks to all modules that may need to reallocate or resample the data, and it may be difficult for the i/o machinery to know what will be needed by the module calling it. Here are some possible solutions to this issue:
Happy to hear other suggestions, @joa-quim and @seisman, but to me option 2 is cleaner. Yes, this probably means that there are times we duplicate when we did not have to, but we would be safe from running into such crashes. |
I'm not sure if I explained the issue clearly. When
As I understand it,
Does it mean it should also crashes if |
OK when you pass GMT_IS_REFERENCE, GMT flags these so they are not freed. I will step through again, but yes I think if you just give GMT_IN you are saying "gmt may deal with these arrays are reallocate" but that is not true when coming from externals. For instance, arrays like x[3] = [1, 2 ,3} cannot be passed in without GMT_IS_REFERENCE since GMT cannot tell that this is a dynamic address we can reallocate or something static on the stack. |
Looking at your case again. Yes, this is API user error since you are allocating a fixed array on the stack, then pass it to GMT as if it can be modified. As far as I know, when your x and y are passed into GMT, there is no way we can tell if those are dynamic or static arrays. So the onus is on you to use the correct flag. |
Is it OK to say that, for external programs like PyGMT, we should always pass |
I would like to say yes. I think that is always the case since we have no idea what we are being given. |
OF course, most of the time there are no bad consequences since we just read from the array. |
So my point still stands. If you want to pass a 1 Tb array from python into GMT, you would like not to have it duplicated unless it must be so. And thus is should not have to fall on you to remember which modules you should pass GMT_IS_REFERENCE and which ones you dont have to. |
I agree, and it sure is consensual, that:
But the issue is on how to achieve the only when one need to. From what I understood in your preferred solution 2. that would happen all the times for |
Well, it would probably be good to determine the extent of the problem. It can affect lines and polygons (not points) in psxy[z] but depends on options (-A turns off resampling, for instance). THe question is how many modules are there where resampling can even happen. I think mostly in the plotters since the producers usually allocate output. Perhaps the problem is much smaller than I first thought and option 3 could work if it is just psxy[z]. |
External programs like PyGMT can pass dataset/momory to GMT. By default, GMT can read, modify and free the momery, which sometimes can cause crashes. Issue #406 reports an example in which PyGMT crashes. The issue was reported to the upstream (see GenericMappingTools/gmt#3515 and GenericMappingTools/gmt#3528). It turns out to be a API user error (i.e., a PyGMT bug). As per the explanation of Paul, external programs like PyGMT should always use `GMT_IN|GMT_IS_REFERENCE` to tell GMT that the data is read-only, so that GMT won't try to change and free the memory. This PR makes the change from `GMT_IN` to `GMT_IN|GMT_IS_REFERENCE` in the `Session.open_virtual_file()` function, updates a few docstrings, and also adds the script in #406 as a test.
External programs like PyGMT can pass dataset/momory to GMT. By default, GMT can read, modify and free the momery, which sometimes can cause crashes. Issue #406 reports an example in which PyGMT crashes. The issue was reported to the upstream (see GenericMappingTools/gmt#3515 and GenericMappingTools/gmt#3528). It turns out to be a API user error (i.e., a PyGMT bug). As per the explanation of Paul, external programs like PyGMT should always use `GMT_IN|GMT_IS_REFERENCE` to tell GMT that the data is read-only, so that GMT won't try to change and free the memory. This PR makes the change from `GMT_IN` to `GMT_IN|GMT_IS_REFERENCE` in the `Session.open_virtual_file()` function, updates a few docstrings, and also adds the script in #406 as a test.
After merging #3532 I believe you should add GMT_IS_REFERENCE to GMT_IN here and it should be fine. If not then I can check. |
After merging #3532, passing |
OK, will look at this later. Dentist appointment... |
The key problem with this and other psxy, psxyz, sphdstance calls from external environment is that they all may call gmt_fix_up_path which may reallocate the arrays it is given (this also happens in grdmask, psclip, pscoast as well but we already had solutions in place or no external data is used). Now, this problem was partially dealt with but new features (like line trimming used in the above test example) was added later and the solution for the problem (duplicate external segments if resampling is needed) happened too late in the workflow. I have gone through all places that call gmt_fix_up_path, including via gmt_geo_polygons, and made sure that if data are external and resampling will/may be happening then we duplicate the segment. The testapi_vector_plot works for either modes for me now. Please see you are able to use GMT_IN|GMT_IS_REFERENCE from pyGMT. |
Hi @seisman how is this branch working for you now? I merged in the grdimage stuff. |
Yes, now it works with both |
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.
OK, forgot that I need to add review. Looks good!
Do you want me to do the honor, @seisman ? |
I'm adding a test. Will merge it later. |
Description of proposed changes
Fixes #
Reminders
master
for new features,6.1
for bug fixes