-
Notifications
You must be signed in to change notification settings - Fork 1k
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 topic about "Lazy Initiliazation" pattern to Design patterns #790
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
01d69b0
Add lazy initialization pattern to list, introduce Problem and Context
isaevil b3b01c1
Add image and first words for example section
isaevil d613740
Fix typo and reduce image size
isaevil 928f7a2
Insert example class with stub text
isaevil 3549d82
More comments for the code and intro for example
isaevil 6312add
Add example of using segtree
isaevil 7baf419
Fix last code sample indenting
isaevil 898b23a
Comment
isaevil 957ec11
Add missed oneapi::
isaevil 6f47751
Codespell fix
isaevil 5ebd730
Change example
isaevil e36e72b
Change pic size
isaevil 695fc00
Add one more image and little more explanation
isaevil 6916ca5
Update picture
isaevil 6f1475d
Apply comments
isaevil 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 109 additions & 0 deletions
109
doc/main/tbb_userguide/design_patterns/Lazy_Initialization.rst
This file contains 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,109 @@ | ||
.. _Lazy_Initialization: | ||
|
||
Lazy Initialization | ||
================== | ||
|
||
|
||
.. container:: section | ||
|
||
|
||
.. rubric:: Problem | ||
:class: sectiontitle | ||
|
||
Delay the creation of an object, potentially expensive, until it is accessed. | ||
In parallel programming, initialization must also be guarded against race conditions. | ||
|
||
|
||
.. container:: section | ||
|
||
|
||
.. rubric:: Context | ||
:class: sectiontitle | ||
|
||
The cost of operations that take place during the initialization | ||
of the object may be considerably high. In that case, the object | ||
should be initialized only when needed. Lazy initialization is | ||
the common tactic that allows implementing such an approach. | ||
|
||
|
||
.. container:: section | ||
|
||
|
||
.. rubric:: Solution | ||
:class: sectiontitle | ||
|
||
Using ``oneapi::tbb::collaborative_call_once`` with ``oneapi::tbb::collaborative_once_flag`` | ||
helps to implement thread-safe lazy initialization for a user object. | ||
|
||
|
||
In addition, ``collaborative_call_once`` allows other thread blocked on | ||
the same ``collaborative_once_flag`` to join other |short_name| | ||
parallel constructions called within the initializing function. | ||
|
||
|
||
.. container:: section | ||
|
||
|
||
.. rubric:: Example | ||
:class: sectiontitle | ||
|
||
This example illustrates the implementation of lazy initialization | ||
for the calculation of the Fibonacci numbers. Here is a graphical | ||
representation of the Fibonacci recursion tree for N=4. | ||
|
||
|
||
|image0| | ||
|
||
|
||
As seen in the diagram, some elements are recalculated more than once. These operations are redundant, | ||
so the "lazy initialized" Fibonacci numbers are relevant here. | ||
|
||
|
||
An implementation without the use of lazy initialization would have *O(2^N)* time complexity due to | ||
the full recursion tree traversal and recalculation of values. Since all the nodes are traversed once, | ||
the tree becomes a list, making the time complexity *O(N)*. | ||
|
||
|
||
|image1| | ||
|
||
|
||
Here you can see the code for the implementation. Already calculated values are stored in a buffer | ||
paired with ``collaborative_once_flag`` and will not be recalculated when ``collaborative_call_once`` | ||
is invoked when initialization has already been done. | ||
|
||
|
||
:: | ||
|
||
|
||
using FibBuffer = std::vector<std::pair<oneapi::tbb::collaborative_once_flag, std::uint64_t>>; | ||
|
||
std::uint64_t LazyFibHelper(int n, FibBuffer& buffer) { | ||
// Base case | ||
if (n <= 1) { | ||
return n; | ||
} | ||
// Calculate nth value only once and store it in the buffer. | ||
// Other threads won't be blocked on already taken collaborative_once_flag | ||
// but join parallelism inside functor | ||
oneapi::tbb::collaborative_call_once(buffer[n].first, [&]() { | ||
std::uint64_t a, b; | ||
oneapi::tbb::parallel_invoke([&] { a = LazyFibHelper(n - 2, buffer); }, | ||
[&] { b = LazyFibHelper(n - 1, buffer); }); | ||
buffer[n].second = a + b; | ||
}); | ||
|
||
return buffer[n].second; | ||
} | ||
|
||
std::uint64_t Fib(int n) { | ||
FibBuffer buffer(n+1); | ||
return LazyFibHelper(n, buffer); | ||
} | ||
|
||
|
||
.. |image0| image:: Images/image008a.jpg | ||
:width: 744px | ||
:height: 367px | ||
.. |image1| image:: Images/image009a.jpg | ||
:width: 744px | ||
:height: 367px |
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.
can you remake screenshots so it would have a space above and beneath the figures?
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.
Updated images.