-
Notifications
You must be signed in to change notification settings - Fork 131
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
Use new H3MemoryManager system #261
Conversation
Codecov Report
@@ Coverage Diff @@
## dev_v4 #261 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 17 17
Lines 425 427 +2
=========================================
+ Hits 425 427 +2
Continue to review full report at Codecov.
|
`polyfill_polygon`. some memory error. not sure what's happening just yet.
src/h3/_cy/memory.pyx
Outdated
return (<H3int[:]>a)[:0] | ||
|
||
|
||
cdef class H3MemoryManager: |
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.
I'd expect a memory manager to have broader responsibilities for managing allocations (have more verbs essentially); this seems to be more like a wrapped buffer or a block of managed memory?
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.
Yeah, basically just an object to be responsible for deallocating the memory before that responsibility is handed off to a memoryview
object.
Open to different names :)
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.
Can we make this a context manager? Seems like a good fit unless for some reason a context manager is hard/slow in Cython
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.
I was also thinking about using a context manager here. I forget the specific examples, but it seemed like not every situation naturally fell into a context manager pattern.
An object that guarantees memory clean up would, at least, be an improvement over what we have now. I wouldn't be opposed to refactoring that into a context manager in the future.
Either way, I'll keep it in mind as I play with this. And if a context manager does end up working out, maybe I'll do that.
return i | ||
|
||
|
||
cdef H3int[:] empty_memory_view(): |
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.
I know this is just copying an existing function, but can this have a docstring of what this function is used for? A memoryview isn't growable iirc, so not clear to me what the point of an empty memory view is.
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.
Ah, ok. I can try to add one.
The point of an empty memory view is that we could have situations where functions return an empty set.
IIRC, this was the cleanest way to do that. The syntax for creating one was pretty ugly otherwise, as far as I could figure it out, so I banished it to this function.
It'd be amazing if someone figured out a cleaner way to do this!
src/h3/_cy/memory.pyx
Outdated
return (<H3int[:]>a)[:0] | ||
|
||
|
||
cdef class H3MemoryManager: |
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.
Can we make this a context manager? Seems like a good fit unless for some reason a context manager is hard/slow in Cython
* remove scripts/examples/apps * BUILD_FUZZERS OFF * ENABLE_FORMAT off * turn off all the things (and sort options) * mac attack * see if there's a build time difference * back to the future
faces = {f for f in faces if f >= 0} | ||
stdlib.free(ptr) | ||
# todo: wait? do faces start from 0 or 1? | ||
# we could do this check/processing in the int_mv object |
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.
This doesn't seem like something you would want in the int_mv object itself, as it's very application specific
@@ -63,6 +71,7 @@ cdef h3lib.GeoLoop make_geoloop(geos, bool lnglat_order=False) except *: | |||
|
|||
gl.numVerts = len(geos) | |||
|
|||
# todo: need for memory management |
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.
I'm not sure what this means.
src/h3/_cy/cells.pyx
Outdated
# todo: mystery: why does this break in the tests when you move it before the error check? | ||
# ideally, the create_mv method would be robust to borked data... | ||
mv = hmm.create_mv() |
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.
What exactly breaks? Does this cause an exception to get swallowed?
src/h3/_cy/h3lib.pxd
Outdated
@@ -10,7 +9,7 @@ cdef extern from "h3api.h": | |||
cdef int H3_VERSION_MINOR | |||
cdef int H3_VERSION_PATCH | |||
|
|||
ctypedef uint64_t H3Index | |||
ctypedef uint64_t H3int "H3Index" |
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.
Why is this getting renamed?
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.
So that the Cython code only has one name for this concept. I'm using H3int
here since the Python library deals with both integer and string representations of an H3Index
, so and the names H3int
and H3str
help make it clear which one it is.
src/h3/_cy/memory.pxd
Outdated
todo: should i be allocating with python memory functions? | ||
https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html |
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.
I would expect this is preferred
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.
It turns out there are some interesting trade-offs we might consider around acquiring the GIL and Python/Cython versions. Checkout the changes here: bf5fb8a
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.
cc: @kylebarron
src/h3/_cy/memory.pyx
Outdated
self.n = n | ||
self.ptr = <H3int*> stdlib.calloc(self.n, sizeof(H3int)) | ||
|
||
if (self.n > 0) and (not self.ptr): |
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.
nit: are the parenthesis needed?
src/h3/_cy/memory.pyx
Outdated
return (<H3int[:]>a)[:0] | ||
|
||
|
||
cdef class H3MemoryManager: |
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.
Please add a docstring as to what this class does. It's not immediately obvious it's specific to H3 indexes, although I think that's the case.
src/h3/_cy/memory.pyx
Outdated
# should be NULL, and deallocing on NULL is fine. | ||
# If the pointer is *not* NULL, then this means the MemoryManager | ||
# has is still responsible for the memory (it hasn't given the memory away to another object). | ||
stdlib.free(self.ptr) |
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.
Set self.ptr to NULL?
…ure there's a way to make them private
Co-authored-by: AJ Friend <[email protected]>
Got history got weird. Moving these changes over to #268 |
* Squash #261: Use new H3MemoryManager system Co-authored-by: AJ Friend <[email protected]> * add some error checks * comments * comments * clearer logical flow * arrays are different from memoryviews! * little cleaner * Revert "comments" This reverts commit 80c3f4d. * Revert "Revert "comments"" This reverts commit a9d6590. * Revert "comments" This reverts commit 24904bd. * comments * arrays are different from memoryviews! * try _copy_to_mv * i don't get enough opportunities to consider using the word "penultimate" * ugh, and there i go using it incorrectly... * hmmm. was this really all it was? * That's what it was! This reverts commit 7e72adb. * clarify branches * remove simple_mv * moving around * clean up * comments * comments * comments Co-authored-by: Isaac Brodsky <[email protected]>
* WIP: Starting work on v4, The Sequel (#238) * cimport for h3fake2 doesn't seem to work * back to where we were * meh, close to what i was hoping for * h3fake2 in github actions * comment out current bindings that consume the h3c v3 library * don't need the coveragerc file at the moment; clear out c files in /tests * sdist needs h3fake2 * try CIBW_BEFORE_BUILD to get h3fake2 * need h3fake2 for cibuildwheel tests * update h3 C library * comment out most of h3lib.pxd * calling from v4 c lib works! (tests dont' work yet tho) * tests shall pass * starting to move over first few functions * little steps * util submodule fully moved over; some hackery with h3fake2 errors * getting even trickier with h3fake2 errors * few more simple functions * geo_to_h3 and h3_to_geo * grid_distance function * grid_disk * parents and kids * compact/uncompact * getNumCells * comment out sdist test temporarily * commenting out `make_sdist` seems to have turned off the other CI jobs * no, it was that we're pushing to the dev branch * keep the sdist job in there, but commented out * ajfriend/more v4 funcs (#241) * get_pentagon_indexes and get_res0_indexes * line * cell area functions * get_faces (#248) * initial pass * thoughts * the results of thinking * this commit is associated with the previous commits * nogil * i'm a bill * remove the typical nonsense * v4: local ij functions to v4 cython (#240) * move local ij functions to v4 implementation * revert some changes and add gridPathCells * swap implementation * revert gridPathCells * ring (#251) * Uncomment cython example, and exclude sdist from wheels test temporarilty (#252) * uncomment cython test * exclude make_sdist temporarily from the wheels tests * v4: polygonToCells (#254) * v4: geo boundary, set to multi polygon (#255) * v4: cell boundary * v4: h3_set_to_multi_polygon * v4: convert edge from h3fake2 (#253) * v4: convert edge from h3fake2 * on error not neighbors * except * * check for neighbor error * Fix sdist build (#239) * make sure `make_sdist` CI job still broken * reproduce locally * update h3lib * still seems broken * try sdist on mac and ubuntu * pyest! * debugging * ensurepath * Disable building docs * just build sdist once * colon Co-authored-by: Isaac Brodsky <[email protected]> * remove h3fake2 transitional library (#257) * Remove more sdist files (#259) * remove scripts/examples/apps * BUILD_FUZZERS OFF * ENABLE_FORMAT off * turn off all the things (and sort options) * mac attack * see if there's a build time difference * back to the future * New error system (#260) * starting to play around with new error system * move enum to h3lib.pxd * this structure is weird :( * error system gets its own file * go back to using H3ErrorCodes * extern the H3ErrorCodes * remove H3Index in favor of just H3int * " to ' * import ordering * something * exception hierarchy * formatting * errors list * remove H3ResolutionError * remove H3EdgeError * remove H3CellError * remove H3DistanceError * H3FailedError * domain error * H3UnrecognizedException * most of em done * remove H3ValueError * remove some checks * ideas ideas * document what the function raises? * thoughts * lint * one half idea for DRYing (kinda) * potentially make the E_SUCCESS path faster * typos * maple syrup * cimport H3ErrorCodes as ec * clean up * whitespace * clarify cases * trying new error idiom * thanks comment * notes * applied idiom a few times now * alternative idiom? * handling some more potential errors * idioms and ideas * clean * removing check * interesting allocation example * lint * just trying to beat 2.7 for sport right now * works? * bow to lint * using enum values directly! * now that's a hierarchy * remove raise_with_msg * ij funcs * lint * remove some checks * remove err * comment * cleaned up `ring`, and i think i fixed a memory leak bug * think i fixed another memory leak bug * another memory leak bug * memory leaks! * clean * removing some comments * meh * H3Exception shouldn't be appearing as a concrete object * renaming some base exceptions * clean up * don't have a test for this anyway * documented * puttering around * words * backing up this nonsense with an airtight argument * air toight * todos * more words * skepticism * rewording * editing * ordering * tests on the error codes * more error code tests * note * address some comments * we raise! * lint * comments * Squash #261: Use new H3MemoryManager system (#268) * Squash #261: Use new H3MemoryManager system Co-authored-by: AJ Friend <[email protected]> * add some error checks * comments * comments * clearer logical flow * arrays are different from memoryviews! * little cleaner * Revert "comments" This reverts commit 80c3f4d. * Revert "Revert "comments"" This reverts commit a9d6590. * Revert "comments" This reverts commit 24904bd. * comments * arrays are different from memoryviews! * try _copy_to_mv * i don't get enough opportunities to consider using the word "penultimate" * ugh, and there i go using it incorrectly... * hmmm. was this really all it was? * That's what it was! This reverts commit 7e72adb. * clarify branches * remove simple_mv * moving around * clean up * comments * comments * comments Co-authored-by: Isaac Brodsky <[email protected]> * remove: from h3 import h3 (#270) * is_valid_cell (#269) * Remove alias functions (#271) * remove alias function hex_range * note test_k_ring and test_hex_range are the same * remove test_hex_range * duplicate tests * remove test_hex_range2 * same test * remove test_hex_range_pentagon * remove alias: k_ring_distances * tests are the same! * same test * combine tests * remove alias: h3_is_res_class_iii * lint * Update most functions to v4.0 names (#272) * grid_distance * cell_to_parent * get_resolution * is_pentagon * is_res_class_III * get_pentagons * get_res0_cells * get_base_cell_number * compact and uncompact * get_faces * cell_to_boundary * cell_to_latlng * latlng_to_cell * cell_to_children * int_to_string and string_to_int * cell_to_center_child * moving things around * is_valid_directed_edge * grid_path_cells * moving around * cells_to_multi_polygon * are_neighbor_cells * cell_to_local_ij and local_ij_to_cell * get_icosahedron_faces * great_circle_distance * get_num_cells * note * remove hex_range_distances and hex_ranges (easily implementable by user) * grid_ring * grid_disk * moving around * origin_to_directed_edges * directed_edge_to_boundary * cells_to_directed_edge * directed_edge_to_cells * todo: directedEdgeToCells * get_directed_edge_origin * get_directed_edge_destination * grouping functions * more grouping * _binding to _b * todos * average_hexagon_area * average_edge_length * average_edge_length and edge_length * average_hexagon_edge_length * ordering * more ordering * Get the makefile working on Linux (#274) * bump core lib to 4.0 rc5 (#275) * bump core lib to 4.0 rc5 * more pythonic names for string<->int conversion functions * cython hex2int to str_to_int to match the python functions * better argument names for great_circle_distance * great_circle_distance comments * revert great_circle_distance tuple unpacking since it breaks python2. drop python 2 before release * polygon_to_cells (#276) * start converting tests over * converted main tests * linting * better null island test * test_compact_and_uncompact_cells * convert a few more tests * add h3.Polygon class * lint * migrating some tests * this shift_circular_list thing makes these tests too hard to understand... * remove shift_circular_list and convert a few more tests * last of the tests in test_h3.py converted * last of the tests converted * note for polygons_to_cells in the future * Try out a __repr__ for h3.Polygon * test_polygon_class.py * use `cells` instead of `hexes` throughout the library and tests * docstring for Polygon * docstrings for functions * use `res` convention for resolution parameters Co-authored-by: Isaac Brodsky <[email protected]> Co-authored-by: David Ellis <[email protected]>
No description provided.