-
-
Notifications
You must be signed in to change notification settings - Fork 31
WIP: Adding config to nx-parallel #68
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
Closed
Schefflera-Arboricola
wants to merge
24
commits into
networkx:main
from
Schefflera-Arboricola:config_1
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a95ceb4
initial commit
Schefflera-Arboricola 390078f
bug_fix
Schefflera-Arboricola 27205d1
Merge branch 'networkx:main' into config_1
Schefflera-Arboricola ead0927
added default_config to get_info
Schefflera-Arboricola 273d53d
added get_curr_configs
Schefflera-Arboricola a7ca076
updated configs
Schefflera-Arboricola 4982fc0
rm 3 configs
Schefflera-Arboricola d4aaedb
Merge branch 'networkx:main' into config_1
Schefflera-Arboricola 4465712
using joblib.parallel_config
Schefflera-Arboricola 9ce30d7
indentation
Schefflera-Arboricola d7ba93d
updated cpu_count
Schefflera-Arboricola 79e6881
integrating nx config
Schefflera-Arboricola ced3898
rm get_default_configs
Schefflera-Arboricola 61dcbb7
renamed get_n_jobs
Schefflera-Arboricola 788dbf8
n_job=-1s
Schefflera-Arboricola 9085093
changing n_jobs=None
Schefflera-Arboricola 9870bad
added backend=loky for testing
Schefflera-Arboricola f08780c
Merge branch 'networkx:main' into config_1
Schefflera-Arboricola 75604c8
rm inconsistency - using config(singular)
Schefflera-Arboricola 3979f2e
removed __all__ from config.py
Schefflera-Arboricola 9c8b57b
changing the default backend to loky
Schefflera-Arboricola de99170
enhanced config documentation
Schefflera-Arboricola d354351
rm fetching list of configs
Schefflera-Arboricola e4bf668
made n_jobs=-1 as default
Schefflera-Arboricola 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 hidden or 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,83 @@ | ||
# Using Configurations in nx-parallel | ||
|
||
nx-parallel algorithms internally use the [`joblib.parallel_config`](https://joblib.readthedocs.io/en/latest/generated/joblib.parallel_config.html) context manager. You can change the configuration parameters of this internal `joblib.parallel_config` context manager using [NetworkX's config](https://networkx.org/documentation/latest/reference/backends.html#module-networkx.utils.configs) context manager. The internal `joblib.parallel_config` context manager uses the [`joblib.Parallel`](https://joblib.readthedocs.io/en/latest/generated/joblib.Parallel.html) call, which is responsible for all the parallel process creation. | ||
|
||
## Default Configuration | ||
|
||
When you import NetworkX, the default configurations for all the installed backends are set as shown below: | ||
|
||
```python | ||
import networkx as nx | ||
|
||
nx.config | ||
``` | ||
|
||
Output: | ||
|
||
``` | ||
NetworkXConfig(backend_priority=[], backends=Config(parallel=ParallelConfig(backend='loky', n_jobs=None, verbose=0, temp_folder=None, max_nbytes='1M', mmap_mode='r', prefer=None, require=None, inner_max_num_threads=None, backend_params={})), cache_converted_graphs=True) | ||
``` | ||
|
||
### Note | ||
|
||
The default settings of `joblib.parallel_config` are the same as the default configs in the `ParallelConfig` class, except for the `backend` config, which is `None` in `joblib.parallel_config` and `"loky"` in `ParallelConfig`. This prevents errors when using the NetworkX config, as the internal `joblib.Parallel`'s `backend` default is `"loky"` when not specified. This consistency in user experience is maintained for ease of use. Additinally, by default the `n_jobs` value is `-1`. | ||
|
||
## Modifying Configuration | ||
|
||
The `ParallelConfig` class inherits from NetworkX's `Config` class. You can use this as a `dataclass` or as a context manager to change the nx-parallel configurations. | ||
|
||
### As a config variable | ||
|
||
You can directly set the desired parameters: | ||
|
||
```python | ||
nxp_config = nx.config.backends.parallel | ||
nxp_config.n_jobs = 6 | ||
nxp_config.verbose = 15 | ||
|
||
G = nx.complete_graph(20) | ||
|
||
# backend -> loky, n_jobs -> 6, verbose -> 15 | ||
nx.square_clustering(G, backend="parallel") | ||
``` | ||
|
||
### As a Context Manager | ||
|
||
You can use the context manager to temporarily change configuration parameters for specific code blocks: | ||
|
||
```python | ||
# in continuation with the above code block | ||
|
||
with nxp_config(n_jobs=4): | ||
# backend -> loky, n_jobs -> 4, verbose -> 15 | ||
nx.square_clustering(G, backend="parallel") | ||
with nxp_config(backend="threading", verbose=0): | ||
# backend -> threading, n_jobs -> 4, verbose -> 0 | ||
nx.betweenness_centrality(G, backend="parallel") | ||
with nxp_config(n_jobs=8): | ||
# backend -> threading, n_jobs -> 8, verbose -> 0 | ||
nx.number_of_isolates(G, backend="parallel") | ||
``` | ||
|
||
From the comments, you can observe how the context managers acquire the configurations from the outer context manager or the global configurations when the context manager is not inside any context manager. | ||
|
||
Note that using `joblib.parallel_config` will output unexpected results. We recommend using the NetworkX's config context manager, as it is the same as the `joblib.parallel_config` context manager because it only provides the configurations to the internal `joblib.parallel_config` context manager. | ||
|
||
Also, modifying the global config inside a context manager will update the configuration inside as well as outside the context manager permanently, as shown below: | ||
|
||
```python | ||
nxp_config.n_jobs = 6 | ||
|
||
nx.square_clustering(G, backend="parallel") # n_jobs -> 6 | ||
|
||
with nxp_config(n_jobs=4): | ||
nx.square_clustering(G, backend="parallel") # n_jobs -> 4 | ||
nxp_config.n_jobs = 8 | ||
nx.square_clustering(G, backend="parallel") # n_jobs -> 8 | ||
|
||
nx.square_clustering(G, backend="parallel") # n_jobs -> 8 | ||
``` | ||
|
||
Please feel free to create issues or PRs if you want to improve this documentation or if you have any feedback. | ||
|
||
Thank you :) |
This file contains hidden or 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 hidden or 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 hidden or 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,20 @@ | ||
from networkx.utils.configs import Config | ||
from typing import Union | ||
from dataclasses import dataclass, field | ||
|
||
|
||
@dataclass | ||
class ParallelConfig(Config): | ||
backend: str = "loky" | ||
n_jobs: int = -1 | ||
verbose: int = 0 | ||
temp_folder: str = None | ||
max_nbytes: Union[int, str] = "1M" | ||
mmap_mode: str = "r" | ||
prefer: str = None | ||
require: str = None | ||
inner_max_num_threads: int = None | ||
backend_params: dict = field(default_factory=dict) | ||
|
||
|
||
_config = ParallelConfig() |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.