Skip to content
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

Expose class structure in libucxx.pxd #372

Merged
merged 19 commits into from
Feb 18, 2025

Conversation

pentschev
Copy link
Member

@pentschev pentschev commented Feb 11, 2025

Expose class structure in libucxx.pxd. This change is useful so that projects building on top of the Python interface can take shared_ptr<ucxx::Component> objects and plug those objects into their C++ backends.

Although it's not possible to define private attributes in a cdef class, we attempt to mark them private with the leading underscore in their names, such as UCXContext._context, and then provide a UCXContext.get_ucxx_shared_ptr for those members that are expected to be externally usable.

Testing will be handled as part of #376.

@pentschev pentschev added feature request New feature or request non-breaking Introduces a non-breaking change DO NOT MERGE Hold off on merging; see PR for details labels Feb 11, 2025
Copy link

copy-pr-bot bot commented Feb 11, 2025

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@pentschev pentschev marked this pull request as ready for review February 17, 2025 11:22
@pentschev pentschev requested review from a team as code owners February 17, 2025 11:22
@pentschev pentschev requested a review from msarahan February 17, 2025 11:22
@pentschev pentschev removed the DO NOT MERGE Hold off on merging; see PR for details label Feb 17, 2025
Copy link
Contributor

@wence- wence- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks fine with one question/suggestion. Though I realise you're just mostly moving code around.

Comment on lines +30 to +47
cdef class UCXContext:
cdef:
shared_ptr[Context] _context
dict _config

cpdef dict get_config(self)

cdef shared_ptr[Context] get_ucxx_shared_ptr(self) nogil


cdef class UCXAddress:
cdef:
shared_ptr[Address] _address
size_t _length
ucp_address_t *_handle
string _string

cdef shared_ptr[Address] get_ucxx_shared_ptr(self) nogil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion/question: What does this get_ucxx_shared_ptr setup buy us?

Is it reasonable to instead write:

cdef class UCXFoo:
    cdef:
        shared_ptr[Foo] c_obj

And just access c_obj directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Cython lacks private/public member protection, the getter doesn't really prevent the user from doing something unsupported. However, IMO, a getter signals this is a public API that may be used without risk by the user, the lack of a setter signals it shouldn't be modified. Exposing c_obj directly, without the leading underscore, may give the impression that the object can be read and modified, which is definitely not the case.

Does that make sense to you or is it just mumbo-jumbo from my head? Perhaps we should document it better to make the intent clear, but so we should do for the entire Cython interface, which is currently very poorly/not at all documented.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, makes sense.

test_getters.pyx Outdated
Comment on lines 17 to 39
# assert <uint64_t>ptr.get() == context.ucxx_ptr


cdef get_context(UCXContext context):
# test_shared(context.get_ucxx_shared_ptr())
assert <uintptr_t>context.get_ucxx_shared_ptr() == context.ucx_ptr


def test_context_getter():
# cdef shared_ptr[Context] ucxx_shared_ptr

feature_flags = [ucx_api.Feature.WAKEUP, ucx_api.Feature.TAG]

context = ucx_api.UCXContext(feature_flags=tuple(feature_flags))

get_context(context)

# test_shared(<UCXContext>context.get_ucxx_shared_ptr())

# ucxx_shared_ptr = context.get_ucxx_shared_ptr()

# assert ucxx_shared_ptr.get() != NULL
# assert <uint64_t>ucxx_shared_ptr.get() == context.ucxx_ptr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIP: commented tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I committed those locally while doing some cleanup with the intent to remove them later but forgot to do that. Removed now in 960fab8 .

@pentschev
Copy link
Member Author

Getting tests done here is getting too complicated and I'd like to make the changes available for projects that currently need those changes. Due to that, I've removed the tests and infrastructure to #376 and will try to get this merged as soon as tests pass.

@pentschev
Copy link
Member Author

/merge

@rapids-bot rapids-bot bot merged commit fb62499 into rapidsai:branch-0.43 Feb 18, 2025
63 checks passed
@pentschev
Copy link
Member Author

Thanks all, I'll continue pushing on #376 for the remaining changes.

@pentschev pentschev deleted the cython-pxd-structure branch February 21, 2025 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request non-breaking Introduces a non-breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants