Skip to content

Commit 1c2649d

Browse files
committed
Make docstring even better
1 parent 3e757a8 commit 1c2649d

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

yt/data_objects/selection_objects/data_selection_objects.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def piter(
525525
indices and the values will be whatever is assigned to the *result*
526526
attribute on the storage during iteration.
527527
barrier : bool
528-
Should a barier be placed at the end of iteration?
528+
Should a barrier be placed at the end of iteration?
529529
dynamic : bool
530530
This governs whether or not dynamic load balancing will be enabled.
531531
This requires one dedicated processor; if this is enabled with a set of
@@ -535,16 +535,28 @@ def piter(
535535
This specifies the reduction operation to be applied to the results
536536
from each processor.
537537
- None: no reduction will be applied and the storage object will
538-
contain one result per chunk in the container.
538+
contain one result per chunk in the container.
539539
- concat: the storage object will contain a flattened list of
540-
each results.
540+
each results.
541541
- cat_on_root: same as concat, but only the root processor will
542-
contain anything.
542+
contain anything.
543543
- sum, min, max: the storage object will contain the result
544-
of applying the operation until getting a single value.
544+
of applying the operation until getting a single value.
545+
546+
Important limitation
547+
--------------------
548+
When using `storage`, the result *must* be a dictionary. See the
549+
examples below.
545550
546551
Example
547552
-------
553+
554+
Here is an example of how to gather all data on root, reading in
555+
parallel. Other MPI tasks will have nothing in `my_storage`.
556+
557+
>>> import yt
558+
>>> ds = yt.load("output_00080")
559+
... ad = ds.all_data()
548560
>>> my_storage = {}
549561
... for sto, chunk in ad.piter(storage=my_storage, reduction="cat_on_root"):
550562
... sto.result = {
@@ -557,18 +569,34 @@ def piter(
557569
... # Contains *all* the gas temperatures
558570
... my_storage["gas", "temperature"]
559571
572+
Here is an example of how to sum the total mass of all gas cells in
573+
the dataset, storing the result in `my_storage` on all processors.
574+
560575
>>> my_storage = {}
561576
... for sto, chunk in ad.piter(storage=my_storage, reduction="sum"):
562577
... sto.result = {
563578
... "total_mass": chunk["gas", "cell_mass"].sum(),
564579
... }
565580
... print("Total mass: ", my_storage["total_mass"])
566581
567-
Note
568-
----
569-
`sto.result` has to be a dictionary.
582+
Here is an example of how to read all data in parallel and
583+
have the results available on all processors.
570584
585+
>>> my_storage = {}
586+
... for sto, chunk in ad.piter(storage=my_storage, reduction="cat"):
587+
... sto.result = {("gas", "density"): chunk["gas", "density"]}
588+
... print(my_storage["gas", "density"])
589+
590+
This is equivalent (but faster, since reading is parallelized) to the
591+
following
592+
593+
>>> my_storage = {("gas", "density"): ad["gas", "density"]}
571594
"""
595+
if reduction is not None and storage is None:
596+
raise ValueError(
597+
"If reduction is specified, you must pass in a storage dictionary."
598+
)
599+
572600
yield from parallel_objects(
573601
self.chunks([], "io"),
574602
njobs=njobs,

yt/utilities/parallel_tools/parallel_analysis_interface.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,23 @@ def parallel_objects(
506506
indices and the values will be whatever is assigned to the *result*
507507
attribute on the storage during iteration.
508508
barrier : bool
509-
Should a barier be placed at the end of iteration?
509+
Should a barrier be placed at the end of iteration?
510510
dynamic : bool
511511
This governs whether or not dynamic load balancing will be enabled.
512512
This requires one dedicated processor; if this is enabled with a set of
513513
128 processors available, only 127 will be available to iterate over
514514
objects as one will be load balancing the rest.
515515
reduction : Literal[None, "sum", "max", "min", "cat", "cat_on_root"]
516516
This specifies the reduction operation to be applied to the results
517-
from each processor. The default is None, which doesn't apply any.
517+
from each processor.
518+
- None: no reduction will be applied and the storage object will
519+
contain one result per chunk in the container.
520+
- concat: the storage object will contain a flattened list of
521+
each results.
522+
- cat_on_root: same as concat, but only the root processor will
523+
contain anything.
524+
- sum, min, max: the storage object will contain the result
525+
of applying the operation until getting a single value.
518526
519527
Examples
520528
--------

0 commit comments

Comments
 (0)