Skip to content

Commit 5cef0ab

Browse files
authored
[oneMath][RNG] Added random engine adaptors (#616)
* added the engine adaptor Signed-off-by: Fedorov, Andrey <[email protected]> * fixed small error * fixed the same mistake in one more files * correct the link to the previous page * added an example * reword a bit --------- Signed-off-by: Fedorov, Andrey <[email protected]>
1 parent 5c52a0d commit 5cef0ab

File tree

4 files changed

+134
-6
lines changed

4 files changed

+134
-6
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. SPDX-FileCopyrightText: 2025 Intel Corporation
2+
..
3+
.. SPDX-License-Identifier: CC-BY-4.0
4+
5+
.. _onemath_device_rng_adaptors:
6+
7+
Engine Adaptors
8+
===============
9+
10+
oneMath RNG provides following device engine adaptors:
11+
12+
.. tabularcolumns:: |\Y{0.4}|\Y{0.6}|
13+
14+
.. list-table::
15+
:header-rows: 1
16+
:class: longtable
17+
18+
* - Routine
19+
- Description
20+
21+
* - :ref:`onemath_device_rng_count_engine_adaptor`
22+
- Provide a method to count how many random numbers were taken from an
23+
engine during the ``generate`` call
24+
25+
.. toctree::
26+
:hidden:
27+
28+
device-rng-count_engine_adaptor.rst
29+
30+
**Parent topic:** :ref:`onemath_device_rng_routines`
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
.. SPDX-FileCopyrightText: 2025 Intel Corporation
2+
..
3+
.. SPDX-License-Identifier: CC-BY-4.0
4+
5+
.. _onemath_device_rng_count_engine_adaptor:
6+
7+
Count Engine Adaptor
8+
====================
9+
10+
``count_engine_adaptor`` is a random number engine adaptor that counts how many times
11+
random 32 bits were taken from the engine during the ``generate`` call.
12+
13+
.. rubric:: Description
14+
15+
This adaptor helps to query how many random numbers were taken from
16+
an engine during the ``generate`` call. Especially it's useful for distributions
17+
based on the acceptance-rejection algorithms, e.g. ``beta``, ``gamma``.
18+
19+
class count_engine_adaptor
20+
--------------------------
21+
22+
.. rubric:: Syntax
23+
24+
.. code-block:: cpp
25+
26+
namespace oneapi::math::rng::device {
27+
template <typename Engine>
28+
class count_engine_adaptor {
29+
public:
30+
static constexpr std::int32_t vec_size = Engine::vec_size;
31+
32+
// ctors
33+
explicit count_engine_adaptor(const Engine& engine);
34+
explicit count_engine_adaptor(Engine&& engine);
35+
36+
template <typename... Params>
37+
count_engine_adaptor(Params... params);
38+
39+
// getters
40+
std::int64_t get_count() const;
41+
const Engine& base() const;
42+
};
43+
}
44+
45+
.. container:: section
46+
47+
.. rubric:: Class Template Parameters
48+
49+
Engine
50+
Describes the type of the engine that ``count_engine_adaptor``
51+
is constructed over.
52+
53+
.. container:: section
54+
55+
.. rubric:: Class Members
56+
57+
.. list-table::
58+
:header-rows: 1
59+
60+
* - Routine
61+
- Description
62+
* - ``explicit count_engine_adaptor(const Engine& engine)``
63+
- Constructs the adaptor over ``engine``.
64+
* - ``explicit count_engine_adaptor(Engine&& engine)``
65+
- Constructs the adaptor over ``engine`` by moving.
66+
* - ``count_engine_adaptor(Params... params)``
67+
- Constructs the adaptor generating an engine inside. ``params`` is a
68+
pack of input parameters to create an engine.
69+
* - ``std::int64_t get_count() const``
70+
- This method returns the amount of random numbers generated by ``generate``.
71+
* - ``const Engine& base() const``
72+
- Returns the underlying engine.
73+
74+
.. rubric:: Example
75+
76+
.. code-block:: cpp
77+
78+
namespace rng_device = oneapi::math::rng::device;
79+
80+
sycl::event my_event = q.parallel_for({n}, [=](std::size_t idx) {
81+
// Some code...
82+
83+
rng_device::count_engine_adaptor<rng_device::mcg59<VecSize>> adaptor(seed, idx);
84+
r[idx] = rng_device::generate(distr, adaptor);
85+
86+
consumed[idx] = adaptor.get_count();
87+
88+
// continue work with engine if needed
89+
auto engine = adaptor.base();
90+
// ...
91+
});
92+
93+
94+
**Parent topic:** :ref:`onemath_device_rng_adaptors`

source/elements/oneMath/source/domains/rng/device_api/device-routines.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ The main purpose of Device routines is to make them callable from your SYCL kern
2323
2424
.. rubric:: Structure
2525

26-
RNG domain contains two classes types:
26+
RNG domain contains three classes types:
2727

2828
- Engines (basic random number generators) classes, which holds
29-
the state of generator and is a source of independent and identically distributed random variables.
29+
the state of generator and is a source of independent and identically distributed random variables.
3030
Refer to :ref:`onemath_rng_engines_basic_random_number_generators`
3131
for a detailed description.
3232
- Distribution classes templates (transformation classes) for different types of statistical
3333
distributions, for example, uniform, normal (Gaussian), binomial,
34-
etc. These classes contain all of the distributions parameters
34+
etc. These classes contain all of the distribution's parameters
3535
(including generation method). Refer to :ref:`onemath_device_rng_distributions` for
3636
a detailed description of the distributions.
37+
- Engine adaptors classes are wrappers over random number generators that provide extra functionality,
38+
e.g. count engine adaptor. Refer to :ref:`onemath_device_rng_adaptors` for
39+
a detailed description.
3740

3841
The RNG domain also contains two types of free functions:
3942

@@ -56,7 +59,8 @@ section for the description of typical RNG scenario.
5659
device-rng-generate-routines.rst
5760
device-engines.rst
5861
device-distributions.rst
62+
device-adaptors.rst
5963
device-service-routines.rst
6064
../bibliography.rst
6165

62-
**Parent topic:** :ref:`onemath_rng`
66+
**Parent topic:** :ref:`onemath_rng_overview`

source/elements/oneMath/source/domains/rng/host_api/rng-host-routines.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Random Number Generators Host Routines
1212
RNG domain contains two classes types:
1313

1414
- Engines (basic random number generators) classes, which holds
15-
the state of generator and is a source of independent and identically distributed random variables.
15+
the state of generator and is a source of independent and identically distributed random variables.
1616
Refer to :ref:`onemath_rng_engines_basic_random_number_generators`
1717
for a detailed description.
1818
- Distribution classes templates (transformation classes) for different types of statistical
@@ -45,4 +45,4 @@ section for the description of typical RNG scenario.
4545
distributions.rst
4646
../bibliography.rst
4747

48-
**Parent topic:** :ref:`onemath_rng`
48+
**Parent topic:** :ref:`onemath_rng_overview`

0 commit comments

Comments
 (0)