-
Notifications
You must be signed in to change notification settings - Fork 7
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
ParallelTable Musings #185
Open
JSKenyon
wants to merge
12
commits into
master
Choose a base branch
from
parallel_tables_casacore
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
81d8d08
Try figuring out how to integrate symlinks with daskms.
JSKenyon 2c6fb4c
Add WIP - seems to work for threaded case. Processes and distributed …
JSKenyon 9d5e9dd
Commit changes - still not entirely satisfactory.
JSKenyon bca8956
Change approach - this is more promising, non-invasive, more intuitive.
JSKenyon bf40c65
Add finalizer to tidy up when a parallel table is GCed.
JSKenyon cdd2d3f
Basic sort of working (not in all cases) parallel table implementation.
JSKenyon 5cad3da
Add reduce method to allow for pickling of Parallel table objects.
JSKenyon c043dcd
No need for symlinks with Ben's PR.
JSKenyon e174581
Remove internal thread pool using a Dummy. PoC.
JSKenyon 3a2e60a
Minor changes before embarking on rework.
JSKenyon c8ff7db
Tidy up.
JSKenyon 3ca905c
Reintroduce hack for now - just an experiment.
JSKenyon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,95 @@ | ||||
import logging | ||||
import threading | ||||
from pyrap.tables import table as Table | ||||
from weakref import finalize | ||||
from pathlib import Path | ||||
|
||||
|
||||
log = logging.getLogger(__name__) | ||||
|
||||
|
||||
def _parallel_table_finalizer(_cached_tables): | ||||
|
||||
for table in _cached_tables.values(): | ||||
link_path = Path(table.name()) | ||||
table.close() | ||||
link_path.unlink() | ||||
|
||||
|
||||
class ParallelTable(Table): | ||||
|
||||
@classmethod | ||||
def _map_create_parallel_table(cls, args, kwargs): | ||||
""" Support pickling of kwargs in ParallelTable.__reduce__ """ | ||||
return cls(*args, **kwargs) | ||||
|
||||
def __init__(self, *args, **kwargs): | ||||
|
||||
self._args = args | ||||
self._kwargs = kwargs | ||||
|
||||
self._cached_tables = {} | ||||
self._table_path = args[0] # TODO: This should be checked. | ||||
|
||||
super().__init__(*args, **kwargs) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
finalize(self, _parallel_table_finalizer, self._cached_tables) | ||||
|
||||
def __reduce__(self): | ||||
""" Defer to _map_create_parallel_table to support kwarg pickling """ | ||||
return ( | ||||
ParallelTable._map_create_parallel_table, | ||||
JSKenyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
(ParallelTable, self._args, self._kwargs) | ||||
) | ||||
|
||||
def getcol(self, *args, **kwargs): | ||||
table = self._get_table(threading.get_ident()) | ||||
return table.getcol(*args, **kwargs) | ||||
|
||||
def getcolslice(self, *args, **kwargs): | ||||
table = self._get_table(threading.get_ident()) | ||||
return table.getcolslice(*args, **kwargs) | ||||
|
||||
def getcolnp(self, *args, **kwargs): | ||||
table = self._get_table(threading.get_ident()) | ||||
return table.getcolnp(*args, **kwargs) | ||||
|
||||
def getvarcol(self, *args, **kwargs): | ||||
table = self._get_table(threading.get_ident()) | ||||
return table.getvarcol(*args, **kwargs) | ||||
|
||||
def getcell(self, *args, **kwargs): | ||||
table = self._get_table(threading.get_ident()) | ||||
return table.getcell(*args, **kwargs) | ||||
|
||||
def getcellslice(self, *args, **kwargs): | ||||
table = self._get_table(threading.get_ident()) | ||||
return table.getcellslice(*args, **kwargs) | ||||
|
||||
def getkeywords(self, *args, **kwargs): | ||||
table = self._get_table(threading.get_ident()) | ||||
return table.getkeywords(*args, **kwargs) | ||||
|
||||
def getcolkeywords(self, *args, **kwargs): | ||||
table = self._get_table(threading.get_ident()) | ||||
return table.getcolkeywords(*args, **kwargs) | ||||
|
||||
def _get_table(self, thread_id): | ||||
|
||||
try: | ||||
table = self._cached_tables[thread_id] | ||||
except KeyError: | ||||
print(f"opening for {thread_id}") | ||||
|
||||
table_path = Path(self._table_path) | ||||
table_name = table_path.name | ||||
link_path = Path(f"/tmp/{thread_id}-{table_name}") | ||||
|
||||
link_path.symlink_to(table_path) | ||||
|
||||
self._cached_tables[thread_id] = table = Table( | ||||
str(link_path), | ||||
**self._kwargs | ||||
) | ||||
|
||||
return table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As I read the ParallelTable class, it is predicated around proxying the encapsulated Table objects as opposed to overriding inherited methods of the Table class.
Therefore it's unnecessary to inherit from Table as proxied table objects are created in
_get_table
.This way, it's also possible to use a metaclass without getting tangled up with a boost python subclass.
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.
https://www.developer.com/design/encapsulation-vs-inheritance/
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 am trying to grasp this, but I am still struggling. The problem I see is that failing to inherit from
pyrap.tables.table
means that the ParallelTable object will not in fact appear to be a table i.e.self._table_future = table = ex.impl.submit(factory, *args, **kwargs)
will not be a future pointing at a table. What I could see working is defining aParralelTableProxy
which simply inherits fromTableProxy
but defines its own metaclass which modifies the behaviour of get* operations. Currently,ParallelTable
is itself a table (i.e. you can do something likept.taql(query, ParallelTable)
) in addition to simply rerouting get* operation through a cache. In other words, there will always be one extra copy open - the "root" table which gets used for unmodified methods. I will take a swing at theParallelTableProxy
idea.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.
Would you mind clarifying what the problem is with this approach? Currently, all the
ParallelTable
does is override some inherited methods ofpyrap.tables.table
prior (!!) to the being embedded in aTableProxy
. This just means that the proxy proxies these special methods, rather than those on the base class. This yields a really simple solution as subsequent operations proceed as normal. TheParallelTable
is a table, and supports allpyrap.tables.table
methods, and will have all the relevant multiton patterns applied inside theTableProxy
. I have tried creating aParallelTableProxy
, but that becomes difficult as one needs to access the cache inside get* methods.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 have implemented a
ParallelTableProxy
. At present it segfaults, but I know why. The problem stems from the fact thatgetter_wrapper
inreads.py
calls methods directly on the underlying table object, rather than via theTableProxy
. This is problematic asTableProxy.method
may not be the same aspyrap.tables.table.method
. This is precisely where my current segfaults come from, asgetter_wrapper
calls the non-threadsafe get* functions on the underlying table.