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 documentation for CPU Affinity usage #6922

Merged
merged 26 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
40bf25d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 15, 2023
5f378df
add manual for cpu affinity
JakubPietrakIntel Mar 15, 2023
1d0430f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 16, 2023
780053f
added references and improved intro
JakubPietrakIntel Mar 16, 2023
5f23a69
merge
JakubPietrakIntel Mar 16, 2023
fbeb8f5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 16, 2023
b79547b
update results
JakubPietrakIntel Mar 16, 2023
4d35a91
Merge branch 'affinity_docs' of https://github.com/JakubPietrakIntel/…
JakubPietrakIntel Mar 16, 2023
05a1ef4
update
rusty1s Mar 16, 2023
3c8425c
Merge branch 'affinity_docs' of github.com:JakubPietrakIntel/pytorch_…
rusty1s Mar 16, 2023
f877d10
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 16, 2023
1af5708
update
rusty1s Mar 16, 2023
b454420
formatting
JakubPietrakIntel Mar 16, 2023
fa92353
Merge branch 'affinity_docs' of https://github.com/JakubPietrakIntel/…
JakubPietrakIntel Mar 16, 2023
fa0b344
update
rusty1s Mar 16, 2023
14868d2
Merge branch 'affinity_docs' of github.com:JakubPietrakIntel/pytorch_…
rusty1s Mar 16, 2023
bbc6c4e
Merge branch 'affinity_docs' of github.com:JakubPietrakIntel/pytorch_…
rusty1s Mar 16, 2023
437ba33
update docs for AffinityMixin and display
JakubPietrakIntel Mar 16, 2023
d7eaaea
Merge branch 'affinity_docs' of https://github.com/JakubPietrakIntel/…
JakubPietrakIntel Mar 16, 2023
80a7800
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 16, 2023
e868757
rm not used inference_affinity.png
JakubPietrakIntel Mar 16, 2023
c19f5d2
changelog
rusty1s Mar 20, 2023
794ca1b
changelog
rusty1s Mar 20, 2023
51773c5
changelog
rusty1s Mar 20, 2023
092a505
changelog
rusty1s Mar 20, 2023
c3e725f
Merge branch 'master' into affinity_docs
rusty1s Mar 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions torch_geometric/loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@
from .mixin import AffinityMixin

__all__ = classes = [
'DataLoader',
'NodeLoader',
'LinkLoader',
'DataLoader',
'NodeLoader',
'LinkLoader',
'NeighborLoader',
'LinkNeighborLoader',
'HGTLoader',
'ClusterData',
'LinkNeighborLoader',
'HGTLoader',
'ClusterData',
'ClusterLoader',
'GraphSAINTSampler',
'GraphSAINTNodeSampler',
'GraphSAINTSampler',
'GraphSAINTNodeSampler',
'GraphSAINTEdgeSampler',
'GraphSAINTRandomWalkSampler',
'ShaDowKHopSampler',
'GraphSAINTRandomWalkSampler',
'ShaDowKHopSampler',
'RandomNodeLoader',
'ZipLoader',
'DataListLoader',
'DenseDataLoader',
'ZipLoader',
'DataListLoader',
'DenseDataLoader',
'TemporalDataLoader',
'NeighborSampler',
'ImbalancedSampler',
'NeighborSampler',
'ImbalancedSampler',
'DynamicBatchSampler',
'AffinityMixin',
'AffinityMixin'
]

RandomNodeSampler = deprecated(
Expand Down
68 changes: 33 additions & 35 deletions torch_geometric/loader/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,43 +72,41 @@ def __call__(self, worker_id):


class AffinityMixin:
r"""A context manager to enable CPU affinity for data loader workers
(only used when running on CPU devices).

Affinitization places data loader workers threads on specific CPU
cores. In effect, it allows for more efficient local memory allocation
and reduces remote memory calls.
Every time a process or thread moves from one core to another,
registers and caches need to be flushed and reloaded. This can become
very costly if it happens often, and our threads may also no longer be
close to their data, or be able to share data in a cache.

.. warning::

To correctly affinitize compute threads
(*i.e.* with OMP), please make sure that you exclude
:obj:`loader_cores` from the list of cores available for the main process.
This will cause core oversubsription and exacerbate performance.
For the instructions on CPU affinity you can refer to the manual page:
https://pytorch-geometric.readthedocs.io/en/latest/advanced/cpu_affinity.html

.. code-block:: python

loader = NeigborLoader(data, num_workers=3)
with loader.enable_cpu_affinity(loader_cores=[0, 1, 2]):
for batch in loader:
pass

Args:
loader_cores ([int], optional): List of CPU cores to which data
loader workers should affinitize to. By default, will affinitize
to cores starting at :obj:`cpu0`.
(default: :obj:`node0_cores[:num_workers]`)
"""
@contextmanager
def enable_cpu_affinity(self, loader_cores):
r"""A context manager to enable CPU affinity for data loader workers
(only used when running on CPU devices).

Affinitization places data loader workers threads on specific CPU
cores. In effect, it allows for more efficient local memory allocation
and reduces remote memory calls.
Every time a process or thread moves from one core to another,
registers and caches need to be flushed and reloaded. This can become
very costly if it happens often, and our threads may also no longer be
close to their data, or be able to share data in a cache.

.. warning::
If you want to further affinitize compute threads
(*i.e.* with OMP), please make sure that you exclude
:obj:`loader_cores` from the list of cores available for compute.
This will cause core oversubsription and exacerbate performance.

.. code-block:: python

loader = NeigborLoader(data, num_workers=3)
with loader.enable_cpu_affinity(loader_cores=[0, 1, 2]):
for batch in loader:
pass

This will be gradually extended to increase performance on dual socket
CPUs.

Args:
loader_cores ([int], optional): List of CPU cores to which data
loader workers should affinitize to.
By default, :obj:`cpu0` is reserved for all auxiliary threads
and ops.
The :class:`DataLoader` wil affinitize to cores starting at
:obj:`cpu0`. (default: :obj:`node0_cores[:num_workers]`)
"""
if not self.num_workers > 0:
raise ValueError(
f"'enable_cpu_affinity' should be used with at least one "
Expand Down