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

Add pickling support for arrays, buffers #786

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

matthiasdiener
Copy link
Contributor

@matthiasdiener matthiasdiener commented Sep 16, 2024

Based on the meshmode.DOFArray pickling support.

TODOs:

  • support MemoryPools
  • add to docs

Please squash

pyopencl/array.py Outdated Show resolved Hide resolved
Comment on lines 786 to 790
if self.allocator is None:
context = queue.context
self.base_data = cl.Buffer(context, cl.mem_flags.READ_WRITE, self.nbytes)
else:
self.base_data = self.allocator(self.nbytes)
Copy link
Owner

Choose a reason for hiding this comment

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

It looks like this would allocate redundantly if there were multiple arrays using the same buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, do you have a suggestion on how to handle this case?

Copy link
Owner

Choose a reason for hiding this comment

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

b = a[:] will make another array using the same buffer. (Any slice really.)

A way around this would be to make buffers picklable instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is 490bb94 going in the direction you had in mind? Should this be a separate PR?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, it does. I'm just not clear on why we need two separate queue_for_pickling decorators.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just not clear on why we need two separate queue_for_pickling decorators.

60e71ee removes the duplicate.

Comment on lines 786 to 790
if self.allocator is None:
context = queue.context
self.base_data = cl.Buffer(context, cl.mem_flags.READ_WRITE, self.nbytes)
else:
self.base_data = self.allocator(self.nbytes)
Copy link
Owner

Choose a reason for hiding this comment

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

We probably want to allow the user to supply an allocator via the context manager.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think of 25ebc7b?

@@ -2393,6 +2393,26 @@ def test_xdg_cache_home(ctx_factory):
# }}}


# {{{ test pickling

def test_array_pickling(ctx_factory):
Copy link
Owner

Choose a reason for hiding this comment

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

Test that this works for SVM-backed arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done as part of 25ebc7b

@@ -705,6 +736,63 @@ def __init__(
"than expected, potentially leading to crashes.",
InconsistentOpenCLQueueWarning, stacklevel=2)

# {{{ Pickling

def __getstate__(self):
Copy link
Owner

Choose a reason for hiding this comment

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

I think it'd be useful if this worked for subclasses (liked TaggedCLArray), too. Should be tested, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think of fdb3525 ?

@inducer
Copy link
Owner

inducer commented Sep 17, 2024

Unsubscribing... @-mention or request review once it's ready for a look or needs attention.

@matthiasdiener matthiasdiener force-pushed the pickle-arrays branch 2 times, most recently from 3713741 to 498d7e2 Compare September 18, 2024 20:26
@matthiasdiener matthiasdiener changed the title Add pickling support for arrays Add pickling support for arrays, buffers Oct 3, 2024
@matthiasdiener
Copy link
Contributor Author

matthiasdiener commented Oct 11, 2024

pooled allocations: look at using __reduce__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants