Skip to content

Commit 3abd9a3

Browse files
mrogowskidalcinl
andauthored
Documentation (#5)
Add docstrings --------- Co-authored-by: Lisandro Dalcin <dalcinl@gmail.com>
1 parent 903a743 commit 3abd9a3

File tree

3 files changed

+1282
-294
lines changed

3 files changed

+1282
-294
lines changed

docs/conf.py

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def pkg_version():
7171

7272
autodoc_preserve_defaults = True
7373
autodoc_typehints = 'description'
74+
autodoc_typehints_format = 'short'
7475
autodoc_type_aliases = {}
7576
autodoc_mock_imports = []
7677

@@ -83,7 +84,6 @@ def pkg_version():
8384
napoleon_preprocess_types = True
8485

8586
_numpy_types = [
86-
'NDArray',
8787
'DTypeLike',
8888
'ArrayLike',
8989
]
@@ -97,15 +97,10 @@ def pkg_version():
9797
]
9898

9999
autodoc_type_aliases.update({
100-
typename: f'numpy.typing.{typename}'
100+
typename: f'~numpy.typing.{typename}'
101101
for typename in _numpy_types
102102
})
103103

104-
autodoc_type_aliases.update({
105-
typename: f'{typename}'
106-
for typename in _shmem_types
107-
})
108-
109104
try:
110105
import sphinx_rtd_theme
111106
if 'sphinx_rtd_theme' not in extensions:
@@ -114,56 +109,74 @@ def pkg_version():
114109
sphinx_rtd_theme = None
115110

116111

117-
def _patch_util_inspect():
118-
from sphinx.util.inspect import TypeAliasForwardRef
119-
TypeAliasForwardRef.__getitem__ = lambda self, item: item
112+
def _patch_domain_python():
113+
from sphinx.domains.python import PythonDomain
114+
PythonDomain.object_types['data'].roles += ('class',)
120115

121116

122-
def _patch_domain_python():
123-
from sphinx.domains import python
124-
125-
xref_info = {}
126-
for typename in _numpy_types:
127-
xref_info[f'numpy.typing.{typename}'] = {'reftype': 'data'}
128-
for typename in _shmem_types:
129-
xref_info[f'{typename}'] = {'reftype': 'data'}
130-
131-
def make_xref(self, rolename, domain, target, *args, **kwargs):
132-
if target in ('None', 'True', 'False'):
133-
rolename = 'obj'
134-
pymodule = None
135-
reftype = None
136-
if target in xref_info:
137-
reftype = xref_info[target].get('reftype')
138-
pymodule, _, target = target.rpartition('.')
139-
xref = make_xref_orig(self, rolename, domain, target, *args, *kwargs)
140-
if pymodule:
141-
xref.attributes['py:module'] = pymodule
142-
if reftype:
143-
xref.attributes['reftype'] = reftype
144-
return xref
145-
146-
make_xref_orig = python.PyXrefMixin.make_xref
147-
python.PyXrefMixin.make_xref = make_xref
117+
def _patch_numpy_typing():
118+
import numpy.typing
119+
from types import new_class
120+
from typing import Generic, TypeVar
121+
NDArray = new_class('NDArray', (Generic[TypeVar('T')],))
122+
NDArray.__module__ = numpy.typing.__name__
123+
numpy.typing.NDArray = NDArray
124+
125+
126+
def _patch_cffi_typing():
127+
from types import new_class
128+
CData = new_class('CData')
129+
CData.__module__ = 'ffi'
130+
CData.__qualname__ = 'CData'
131+
from shmem4py import shmem
132+
for attr in _shmem_types:
133+
data = getattr(shmem, attr)
134+
if isinstance(data, typing.NewType):
135+
if data.__supertype__ == shmem.ffi.CData:
136+
data.__supertype__ = CData
137+
138+
139+
def _setup_autodoc(app):
140+
from sphinx.ext import autodoc
141+
from sphinx.ext import autosummary
142+
from sphinx.util import inspect
143+
from sphinx.util import typing
144+
from sphinx.locale import _
145+
146+
#
147+
148+
def stringify_annotation(annotation, mode='fully-qualified-except-typing'):
149+
qualname = getattr(annotation, '__qualname__', '')
150+
module = getattr(annotation, '__module__', '')
151+
args = getattr(annotation, '__args__', None)
152+
if module == 'builtins' and qualname and args is not None:
153+
args = ', '.join(stringify_annotation(a, mode) for a in args)
154+
return f'{qualname}[{args}]'
155+
return stringify_annotation_orig(annotation, mode)
156+
157+
try:
158+
stringify_annotation_orig = typing.stringify_annotation
159+
inspect.stringify_annotation = stringify_annotation
160+
typing.stringify_annotation = stringify_annotation
161+
autodoc.stringify_annotation = stringify_annotation
162+
autodoc.typehints.stringify_annotation = stringify_annotation
163+
except AttributeError:
164+
stringify_annotation_orig = typing.stringify
165+
inspect.stringify_annotation = stringify_annotation
166+
typing.stringify = stringify_annotation
167+
autodoc.stringify_typehint = stringify_annotation
148168

149169

150170
def setup(app):
151-
_patch_util_inspect()
152171
_patch_domain_python()
172+
_setup_autodoc(app)
153173

174+
_patch_numpy_typing()
154175
import numpy.typing
155176
typing.TYPE_CHECKING = True
156177
import shmem4py.shmem
157178
typing.TYPE_CHECKING = False
158-
159-
class CData: pass
160-
CData.__module__ = 'ffi'
161-
CData.__qualname__ = 'CData'
162-
for attr in _shmem_types:
163-
data = getattr(shmem4py.shmem, attr)
164-
if isinstance(data, typing.NewType):
165-
if data.__supertype__ == shmem4py.shmem.ffi.CData:
166-
data.__supertype__ = CData
179+
_patch_cffi_typing()
167180

168181

169182
# -- Options for HTML output -------------------------------------------------

docs/shmem.rst

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ Library Setup and Exit
2222
init
2323
finalize
2424
global_exit
25-
THREAD
2625
init_thread
2726
query_thread
27+
THREAD
2828

2929
.. autofunction:: init
3030
.. autofunction:: finalize
3131
.. autofunction:: global_exit
32-
.. autoclass:: THREAD
3332
.. autofunction:: init_thread
3433
.. autofunction:: query_thread
3534

35+
.. autoclass:: THREAD
3636

37-
Accesibility Queries
38-
--------------------
37+
38+
Accessibility Queries
39+
---------------------
3940

4041
.. autosummary::
4142
my_pe
@@ -65,6 +66,8 @@ Memory Management
6566
zeros
6667
ones
6768
full
69+
MALLOC
70+
6871

6972
.. autofunction:: alloc
7073
.. autofunction:: free
@@ -77,15 +80,53 @@ Memory Management
7780
.. autofunction:: ones
7881
.. autofunction:: full
7982

83+
.. autoclass:: MALLOC
84+
85+
Team Management
86+
---------------
87+
.. autosummary::
88+
Team
89+
Team.destroy
90+
Team.split_strided
91+
Team.get_config
92+
Team.my_pe
93+
Team.n_pes
94+
Team.translate_pe
95+
Team.create_ctx
96+
Team.sync
97+
98+
.. autoclass:: Team
99+
100+
.. automethod:: destroy
101+
.. automethod:: split_strided
102+
.. automethod:: get_config
103+
.. automethod:: my_pe
104+
.. automethod:: n_pes
105+
.. automethod:: translate_pe
106+
.. automethod:: create_ctx
107+
.. automethod:: sync
80108

81109
Communication Management
82110
------------------------
83111

84112
.. autosummary::
85113
Ctx
114+
Ctx.create
115+
Ctx.destroy
116+
Ctx.get_team
117+
Ctx.fence
118+
Ctx.quiet
119+
CTX
86120

87121
.. autoclass:: Ctx
88122

123+
.. automethod:: create
124+
.. automethod:: destroy
125+
.. automethod:: get_team
126+
.. automethod:: fence
127+
.. automethod:: quiet
128+
129+
.. autoclass:: CTX
89130

90131
Remote Memory Access
91132
--------------------
@@ -109,6 +150,12 @@ Remote Memory Access
109150
Atomic Memory Operations
110151
------------------------
111152

153+
.. autosummary::
154+
atomic_op
155+
atomic_fetch_op
156+
atomic_fetch_op_nbi
157+
AMO
158+
112159
.. autosummary::
113160
atomic_set
114161
atomic_inc
@@ -134,10 +181,10 @@ Atomic Memory Operations
134181
atomic_fetch_or_nbi
135182
atomic_fetch_xor_nbi
136183

137-
AMO
138-
atomic_op
139-
atomic_fetch_op
140-
atomic_fetch_op_nbi
184+
.. autofunction:: atomic_op
185+
.. autofunction:: atomic_fetch_op
186+
.. autofunction:: atomic_fetch_op_nbi
187+
.. autoclass:: AMO
141188

142189
.. autofunction:: atomic_set
143190
.. autofunction:: atomic_inc
@@ -163,10 +210,6 @@ Atomic Memory Operations
163210
.. autofunction:: atomic_fetch_or_nbi
164211
.. autofunction:: atomic_fetch_xor_nbi
165212

166-
.. autoclass:: AMO
167-
.. autofunction:: atomic_op
168-
.. autofunction:: atomic_fetch_op
169-
.. autofunction:: atomic_fetch_op_nbi
170213

171214

172215
Signaling Operations
@@ -176,24 +219,22 @@ Signaling Operations
176219
new_signal
177220
del_signal
178221
signal_fetch
179-
SIGNAL
180222
put_signal
181223
put_signal_nbi
224+
SIGNAL
182225

183226
.. autofunction:: new_signal
184227
.. autofunction:: del_signal
185228
.. autofunction:: signal_fetch
186-
.. autoclass:: SIGNAL
187229
.. autofunction:: put_signal
188230
.. autofunction:: put_signal_nbi
231+
.. autoclass:: SIGNAL
189232

190233

191234
Collective Operations
192235
---------------------
193236

194237
.. autosummary::
195-
Team
196-
197238
barrier_all
198239
sync_all
199240
sync
@@ -204,8 +245,8 @@ Collective Operations
204245
alltoall
205246
alltoalls
206247

207-
OP
208248
reduce
249+
OP
209250
and_reduce
210251
or_reduce
211252
xor_reduce
@@ -214,9 +255,6 @@ Collective Operations
214255
sum_reduce
215256
prod_reduce
216257

217-
218-
.. autoclass:: Team
219-
220258
.. autofunction:: barrier_all
221259
.. autofunction:: sync_all
222260

@@ -227,8 +265,8 @@ Collective Operations
227265
.. autofunction:: alltoall
228266
.. autofunction:: alltoalls
229267

230-
.. autoclass:: OP
231268
.. autofunction:: reduce
269+
.. autoclass:: OP
232270
.. autofunction:: and_reduce
233271
.. autofunction:: or_reduce
234272
.. autofunction:: xor_reduce
@@ -242,7 +280,6 @@ Point-To-Point Synchronization
242280
------------------------------
243281

244282
.. autosummary::
245-
CMP
246283
wait_until
247284
wait_until_all
248285
wait_until_any
@@ -257,8 +294,9 @@ Point-To-Point Synchronization
257294
test_all_vector
258295
test_any_vector
259296
test_some_vector
297+
signal_wait_until
298+
CMP
260299

261-
.. autoclass:: CMP
262300
.. autofunction:: wait_until
263301
.. autofunction:: wait_until_all
264302
.. autofunction:: wait_until_any
@@ -273,6 +311,8 @@ Point-To-Point Synchronization
273311
.. autofunction:: test_all_vector
274312
.. autofunction:: test_any_vector
275313
.. autofunction:: test_some_vector
314+
.. autofunction:: signal_wait_until
315+
.. autoclass:: CMP
276316

277317

278318
Memory Ordering
@@ -302,6 +342,20 @@ Distributed Locking
302342
.. autofunction:: test_lock
303343
.. autofunction:: clear_lock
304344

345+
Distributed Locking (Object-Oriented)
346+
-------------------------------------
347+
.. autosummary::
348+
Lock
349+
Lock.destroy
350+
Lock.acquire
351+
Lock.release
352+
353+
.. autoclass:: Lock
354+
355+
.. automethod:: destroy
356+
.. automethod:: acquire
357+
.. automethod:: release
358+
305359

306360
Profiling Control
307361
-----------------

0 commit comments

Comments
 (0)