Repository navigation
docs: document resources #1025
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
Open
matclim
wants to merge
2
commits into
Framework-R-D:main
Choose a base branch
from
matclim:docs/resources
base: main
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
docs: document resources #1025
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,128 @@ | ||
| Resources | ||
| ========= | ||
|
|
||
| A *resource* is an object that the framework owns for the whole job and | ||
| hands to the algorithms that declare they use it, such as a calibration | ||
| service, a geometry, or a library that may only be used by one caller at a | ||
| time. | ||
| Resources were introduced in Phlex v0.4.0. | ||
|
|
||
| Kinds of resources | ||
| ------------------ | ||
|
|
||
| Unlimited resources | ||
| Any number of calls may use the resource at the same time. | ||
| The type must *not* define a ``token_type`` member. | ||
| Algorithms receive a ``T const*``. | ||
|
|
||
| Serialized resources | ||
| Across *all* algorithms that declare the resource, at most one call | ||
| holding it runs at a time. | ||
| The type must define a ``token_type`` member, and algorithms receive a | ||
| value of that type: | ||
|
|
||
| - a pointer to the resource, if ``T*`` converts to ``token_type`` | ||
| (for example ``using token_type = T const*;``); | ||
| - a copy of the resource, if ``token_type`` is ``T`` itself, in which | ||
| case ``T`` must be copy-constructible; | ||
| - otherwise, the result of ``T::token()``, which must return something | ||
| convertible to ``token_type`` and be either a ``static`` member | ||
| function or a ``const``-qualified member function. | ||
| A ``token()`` that is neither is rejected by ``add_serialized_resource``. | ||
|
|
||
| This is the framework's way to protect a non-thread-safe library that | ||
| several algorithms share. | ||
| A serialized resource limits how many calls run at once; it does not make | ||
| the calls run on one particular thread. | ||
|
|
||
| Each resource type may be registered only once per job. | ||
|
|
||
| Registering resources | ||
| --------------------- | ||
|
|
||
| Resources are registered by a plugin that uses ``PHLEX_REGISTER_RESOURCES`` | ||
| from ``phlex/resource.hpp`` and links to the ``phlex::resource`` CMake | ||
| target: | ||
|
|
||
| .. code-block:: cpp | ||
|
|
||
| #include "phlex/configuration.hpp" | ||
| #include "phlex/resource.hpp" | ||
|
|
||
| struct calibration { | ||
| explicit calibration(int version) : version{version} {} | ||
| int version; | ||
| }; | ||
|
|
||
| struct legacy_generator { | ||
| using token_type = legacy_generator*; | ||
| // ... wraps a library that is not thread-safe | ||
| }; | ||
|
|
||
| PHLEX_REGISTER_RESOURCES(r, config) | ||
| { | ||
| r.add_unlimited_resource<calibration>(config.get<int>("version")); | ||
| r.add_serialized_resource<legacy_generator>(); | ||
| } | ||
|
|
||
| Define the resource types in a header that is included both by the plugin | ||
| that registers them and by every algorithm module that uses them, as | ||
| ``test/plugins/resources_for_testing.hpp`` does. | ||
| Header-only types like these need no separate shared library. | ||
|
|
||
| The arguments of ``add_unlimited_resource`` and ``add_serialized_resource`` | ||
| are forwarded to the resource's constructor. | ||
|
|
||
| Resource plugins are configured in the top-level ``resources`` table and | ||
| are loaded before all other plugins. | ||
| Only C++ resource plugins are supported: | ||
|
|
||
| .. code-block:: jsonnet | ||
|
|
||
| { | ||
| resources: { | ||
| services: { cpp: 'my_resources', version: 3 }, | ||
| }, | ||
| } | ||
|
|
||
| Using resources | ||
| --------------- | ||
|
|
||
| An algorithm declares the resources it uses by passing ``resource<T>{}`` | ||
| (from ``phlex/core/resource_api.hpp``) to ``input_family(...)`` after all | ||
| product selectors. | ||
| The algorithm receives one extra parameter per resource, after its product | ||
| parameters and in the same order: | ||
|
|
||
| .. code-block:: cpp | ||
|
|
||
| m.transform( | ||
| "generate", | ||
| [](int seed, calibration const* calib, legacy_generator* gen) -> double { /* ... */ }, | ||
| concurrency::unlimited) | ||
| .input_family(product_selector{.creator = "seeds", .layer = "event"}, | ||
| resource<calibration>{}, | ||
| resource<legacy_generator>{}); | ||
|
|
||
| A node can run no more calls at once than both its own ``concurrency`` | ||
| argument and its serialized resources allow. | ||
| Using a resource type that no plugin registered is an error. | ||
|
|
||
| Limitations | ||
| ----------- | ||
|
|
||
| - Providers cannot use resources | ||
| (`issue #945 <https://github.com/Framework-R-D/phlex/issues/945>`_). | ||
| - Output modules registered with ``output(...)`` cannot use resources, | ||
| because they have no ``input_family(...)``. | ||
| - Algorithms written in Python cannot use resources. | ||
| - For an unfold, the resource parameters go to the unfold function, not to | ||
| the unfolder's constructor | ||
| (`issue #946 <https://github.com/Framework-R-D/phlex/issues/946>`_). | ||
| - Resources with a pool of several tokens are not supported yet. | ||
| - Phlex needs `oneTBB 2023.0.0 or later | ||
| <https://github.com/uxlfoundation/oneTBB/releases#release-v2023.0.0>`_, | ||
| which provides the preview ``flow::resource_limiter`` class; CMake | ||
| configuration fails without it. | ||
| The ``phlex::core`` target defines ``TBB_PREVIEW_FLOW_GRAPH_RESOURCE_LIMITING`` | ||
| for its users. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
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.
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.
Nonblocking: please add a sentence explaining that these resource definitions should live in a common header included by both the registering resource plugin and every consuming algorithm module. That is the pattern used by test/plugins/resources_for_testing.hpp. The geometry example in phlex-examples likewise shares declarations through my_geometry.hpp, although it currently supplies geometry as a job-layer data product rather than a registered resource. A separate shared resource library is not generally required for the header-only types shown here: the existing resource integration test links the registration and consumer plugins separately.
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.
Added in 6d45107 right after the registration example. Resource types should be defined in a header included by both the registering plugin and every consuming module, which is what test/plugins/resources_for_testing.hpp does. The page also notes that header-only types like these need no separate shared library.