|
| 1 | +//***************************************************************************** |
| 2 | +// Copyright (c) 2024, Intel Corporation |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are met: |
| 7 | +// - Redistributions of source code must retain the above copyright notice, |
| 8 | +// this list of conditions and the following disclaimer. |
| 9 | +// - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | +//***************************************************************************** |
| 25 | + |
| 26 | +#pragma once |
| 27 | + |
| 28 | +#include <oneapi/mkl/rng.hpp> |
| 29 | + |
| 30 | +namespace dpnp::backend::ext::rng::host::dispatch |
| 31 | +{ |
| 32 | +namespace mkl_rng = oneapi::mkl::rng; |
| 33 | + |
| 34 | +template <typename funcPtrT, |
| 35 | + template <typename fnT, typename E, typename T, typename M> |
| 36 | + typename factory, |
| 37 | + int _no_of_engines, |
| 38 | + int _no_of_types, |
| 39 | + int _no_of_methods> |
| 40 | +class Dispatch3DTableBuilder |
| 41 | +{ |
| 42 | +private: |
| 43 | + template <typename E, typename T> |
| 44 | + const std::vector<funcPtrT> row_per_method() const |
| 45 | + { |
| 46 | + std::vector<funcPtrT> per_method = { |
| 47 | + factory<funcPtrT, E, T, mkl_rng::gaussian_method::by_default>{} |
| 48 | + .get(), |
| 49 | + factory<funcPtrT, E, T, mkl_rng::gaussian_method::box_muller2>{} |
| 50 | + .get(), |
| 51 | + }; |
| 52 | + assert(per_method.size() == _no_of_methods); |
| 53 | + return per_method; |
| 54 | + } |
| 55 | + |
| 56 | + template <typename E> |
| 57 | + auto table_per_type_and_method() const |
| 58 | + { |
| 59 | + std::vector<std::vector<funcPtrT>> table_by_type = { |
| 60 | + row_per_method<E, bool>(), |
| 61 | + row_per_method<E, int8_t>(), |
| 62 | + row_per_method<E, uint8_t>(), |
| 63 | + row_per_method<E, int16_t>(), |
| 64 | + row_per_method<E, uint16_t>(), |
| 65 | + row_per_method<E, int32_t>(), |
| 66 | + row_per_method<E, uint32_t>(), |
| 67 | + row_per_method<E, int64_t>(), |
| 68 | + row_per_method<E, uint64_t>(), |
| 69 | + row_per_method<E, sycl::half>(), |
| 70 | + row_per_method<E, float>(), |
| 71 | + row_per_method<E, double>(), |
| 72 | + row_per_method<E, std::complex<float>>(), |
| 73 | + row_per_method<E, std::complex<double>>()}; |
| 74 | + assert(table_by_type.size() == _no_of_types); |
| 75 | + return table_by_type; |
| 76 | + } |
| 77 | + |
| 78 | +public: |
| 79 | + Dispatch3DTableBuilder() = default; |
| 80 | + ~Dispatch3DTableBuilder() = default; |
| 81 | + |
| 82 | + void populate(funcPtrT table[][_no_of_types][_no_of_methods]) const |
| 83 | + { |
| 84 | + const auto map_by_engine = { |
| 85 | + table_per_type_and_method<mkl_rng::mrg32k3a>(), |
| 86 | + table_per_type_and_method<mkl_rng::philox4x32x10>(), |
| 87 | + table_per_type_and_method<mkl_rng::mcg31m1>(), |
| 88 | + table_per_type_and_method<mkl_rng::mcg59>()}; |
| 89 | + assert(map_by_engine.size() == _no_of_engines); |
| 90 | + |
| 91 | + std::uint16_t engine_id = 0; |
| 92 | + for (auto &table_by_type : map_by_engine) { |
| 93 | + std::uint16_t type_id = 0; |
| 94 | + for (auto &row_by_method : table_by_type) { |
| 95 | + std::uint16_t method_id = 0; |
| 96 | + for (auto &fn_ptr : row_by_method) { |
| 97 | + table[engine_id][type_id][method_id] = fn_ptr; |
| 98 | + ++method_id; |
| 99 | + } |
| 100 | + ++type_id; |
| 101 | + } |
| 102 | + ++engine_id; |
| 103 | + } |
| 104 | + } |
| 105 | +}; |
| 106 | +} // namespace dpnp::backend::ext::rng::host::dispatch |
0 commit comments