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 topic about "Lazy Initiliazation" pattern to Design patterns #790

Merged
merged 15 commits into from
Mar 9, 2022

Conversation

isaevil
Copy link
Contributor

@isaevil isaevil commented Feb 24, 2022

Description

New topic about "Lazy initialization" pattern and how it can be implemented using oneapi::tbb::collaborative_call_once has been added.

  • - git commit message contains an appropriate signed-off-by string (see CONTRIBUTING.md for details)

Type of change

Choose one or multiple, leave empty if none of the other choices apply

Add a respective label(s) to PR if you have permissions

  • bug fix - change that fixes an issue
  • new feature - change that adds functionality
  • tests - change in tests
  • infrastructure - change in infrastructure and CI
  • documentation - documentation update

Tests

  • added - required for new features and some bug fixes
  • not needed

Documentation

  • updated in # - add PR number
  • needs to be updated
  • not needed

Breaks backward compatibility

  • Yes
  • No
  • Unknown

Notify the following users

@alexey-katranov @alexandraepan

Other information

@isaevil isaevil self-assigned this Feb 24, 2022
isaevil added 11 commits March 4, 2022 17:45
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
@isaevil isaevil force-pushed the dev/isaevil/lazy_initialization_pattern branch from ed14366 to 5ebd730 Compare March 5, 2022 06:37
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
alexey-katranov
alexey-katranov previously approved these changes Mar 5, 2022
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
alexey-katranov
alexey-katranov previously approved these changes Mar 6, 2022
}


.. |image0| image:: Images/image008a.jpg
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated images.

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 it is needed. Lazy initialization
is the common tactic that allows implementing such approach.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
is the common tactic that allows implementing such approach.
is the common tactic that allows implementing such an approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

.. rubric:: Problem
:class: sectiontitle

Delay the creation of an object (potentially expensive) until it is accessed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Delay the creation of an object (potentially expensive) until it is accessed.
Delay the creation of an object, potentially expensive, until it is accessed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

:class: sectiontitle

Delay the creation of an object (potentially expensive) until it is accessed.
In parallel programming initialization also must be guarded against race conditions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In parallel programming initialization also must be guarded against race conditions.
In parallel programming, initialization must also be guarded against race conditions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


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 it is needed. Lazy initialization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
should be initialized only when it is needed. Lazy initialization
should be initialized only when needed. Lazy initialization

Copy link
Contributor Author

@isaevil isaevil Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (also applicable to the comments below).

.. rubric:: Example
:class: sectiontitle

The example presented here illustrate implementation of "lazy initialization" for Fibonacci numbers calculation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The example presented here illustrate implementation of "lazy initialization" for Fibonacci numbers calculation.
This example illustrates the implementation of "lazy initialization" for the calculation of the Fibonacci numbers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is lazy initialization in quotes here but not in the cases above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed quotes.


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 previously shown becomes a list, making the time complexity *O(N)*.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the tree previously shown becomes a list, making the time complexity *O(N)*.
the tree becomes a list, making the time complexity *O(N)*.



The code for the implementation is shown below. Already calculated values are stored in a buffer paired with
``collaborative_once_flag`` and won't be recalculated when ``collaborative_call_once`` is invoked
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``collaborative_once_flag`` and won't be recalculated when ``collaborative_call_once`` is invoked
``collaborative_once_flag`` and will not be recalculated when ``collaborative_call_once`` is invoked

|image1|


The code for the implementation is shown below. Already calculated values are stored in a buffer paired with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The code for the implementation is shown below. Already calculated values are stored in a buffer paired with
Here you can see the code for the implementation. Already calculated values are stored in a buffer paired with

Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
@isaevil isaevil merged commit 1da8f0d into master Mar 9, 2022
@isaevil isaevil deleted the dev/isaevil/lazy_initialization_pattern branch March 9, 2022 13:56
ValentinaKats pushed a commit to ValentinaKats/oneTBB that referenced this pull request May 20, 2022
…foundation#790)

New topic about Lazy initialization pattern and how it can be implemented using oneapi::tbb::collaborative_call_once has been added.

Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
(cherry picked from commit 1da8f0d)
timmiesmith pushed a commit that referenced this pull request Aug 9, 2022
* Update pull_request_template.md (#751)

Signed-off-by: Alexandra Epanchinzeva alexandra.epanchinzeva@intel.com
(cherry picked from commit 4eda0f9)

* Update CONTRIBUTING.md (#765)

(cherry picked from commit e274a9e)

* Documentation update for unpreview `task_handle` and related stuff (#755)

* Unpreview task_handle and related stuff

Signed-off-by: Anton Potapov <anton.potapov@intel.com>
Co-authored-by: Alexandra <alexandra.epanchinzeva@intel.com>
(cherry picked from commit fd76f45)

* Update conf.py (#774)

(cherry picked from commit 6666292)

* Actualize documentation about proportional splitting constructor in Range (#728)

Actualize documentation about proportional splitting

Signed-off-by: Fedotov, Aleksei <aleksei.fedotov@intel.com>
(cherry picked from commit e5cbe50)

* Update doc structure and add new files (#791)

(cherry picked from commit ce0d258)

* Instruction for building the docs locally  (#778)

(cherry picked from commit e386960)

* Document a way to flow graph can be attached to arbitrary task_arena (#785)

* Document a way to flow graph can be attached to arbitrary task_arena

task_arena interface provides mechanisms to guide tasks execution within the arena by
setting the preferred computation units or restricting part of computation units. In some
cases, you may want to use mechanisms within a flow graph.

Signed-off-by: Serov, Vladimir <vladimir.serov@intel.com>
Co-authored-by: Aleksei Fedotov <aleksei.fedotov@intel.com>
Co-authored-by: Alexandra Epanchinzeva <alexandra.epanchinzeva@intel.com>
(cherry picked from commit a938322)

* Add topic about "Lazy Initiliazation" pattern to Design patterns (#790)

New topic about Lazy initialization pattern and how it can be implemented using oneapi::tbb::collaborative_call_once has been added.

Signed-off-by: Ilya Isaev <ilya.isaev@intel.com>
(cherry picked from commit 1da8f0d)

* Update Get Started Guide (#803)

(cherry picked from commit 0502372)

* Update intro_gsg.rst (#808)

(cherry picked from commit 2c4f282)

* Update conf.py (#810)

(cherry picked from commit 0a0a592)

* TBB DOC : Dev Guide: Task Scheduler Bypass and How Does Task Scheduler Works (#521)

* TBB DOC : Dev Guide: Task Scheduler Bypass and How Task Scheduler
Works

Signed-off-by: Anton Potapov <anton.potapov@intel.com>
Co-authored-by: Alexandra <alexandra.epanchinzeva@intel.com>

(cherry picked from commit ed9d4b5)

* Update intro_gsg.rst (#811)

(cherry picked from commit efea993)

* Update conf.py (#812)

(cherry picked from commit 3859d11)

* Update examples.rst (#816)

(cherry picked from commit 4aa0b0b)

* Update layout.html (#815)

(cherry picked from commit 3e352b4)

* Update RELEASE_NOTES.md for oneTBB 2021.6 (#835)

(cherry picked from commit faaf43c)

Co-authored-by: Alexandra <alexandra.epanchinzeva@intel.com>
Co-authored-by: Anton Potapov <potapov.slash.co@gmail.com>
Co-authored-by: Aleksei Fedotov <aleksei.fedotov@intel.com>
Co-authored-by: Vladimir Serov <vladimir.serov@intel.com>
Co-authored-by: Ilya Isaev <ilya.isaev@intel.com>
Co-authored-by: Anton Potapov <anton.potapov@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants