Skip to content

Commit

Permalink
Added base f/w for host API as an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
antonwolfy committed May 6, 2024
1 parent 4af00d5 commit 8baa052
Show file tree
Hide file tree
Showing 7 changed files with 616 additions and 0 deletions.
1 change: 1 addition & 0 deletions dpnp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ add_subdirectory(backend)
add_subdirectory(backend/extensions/blas)
add_subdirectory(backend/extensions/lapack)
add_subdirectory(backend/extensions/rng/device)
add_subdirectory(backend/extensions/rng/host)
add_subdirectory(backend/extensions/vm)
add_subdirectory(backend/extensions/sycl_ext)

Expand Down
77 changes: 77 additions & 0 deletions dpnp/backend/extensions/rng/host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# *****************************************************************************
# Copyright (c) 2023, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************


set(python_module_name _rng_host_impl)
pybind11_add_module(${python_module_name} MODULE
rng_py.cpp
gaussian.cpp
)

if (WIN32)
if (${CMAKE_VERSION} VERSION_LESS "3.27")
# this is a work-around for target_link_options inserting option after -link option, cause
# linker to ignore it.
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -fsycl-device-code-split=per_kernel")
endif()
endif()

set_target_properties(${python_module_name} PROPERTIES CMAKE_POSITION_INDEPENDENT_CODE ON)

target_include_directories(${python_module_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(${python_module_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/engine)
target_include_directories(${python_module_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
target_include_directories(${python_module_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../src)

target_include_directories(${python_module_name} PUBLIC ${Dpctl_INCLUDE_DIRS})
target_include_directories(${python_module_name} PUBLIC ${Dpctl_TENSOR_INCLUDE_DIR})

if (WIN32)
target_compile_options(${python_module_name} PRIVATE
/clang:-fno-approx-func
/clang:-fno-finite-math-only
)
else()
target_compile_options(${python_module_name} PRIVATE
-fno-approx-func
-fno-finite-math-only
)
endif()

target_link_options(${python_module_name} PUBLIC -fsycl-device-code-split=per_kernel)
if (UNIX)
# this option is support on Linux only
target_link_options(${python_module_name} PUBLIC -fsycl-link-huge-device-code)
endif()

if (DPNP_GENERATE_COVERAGE)
target_link_options(${python_module_name} PRIVATE -fprofile-instr-generate -fcoverage-mapping)
endif()

target_link_libraries(${python_module_name} PUBLIC MKL::MKL_DPCPP)

install(TARGETS ${python_module_name}
DESTINATION "dpnp/backend/extensions/rng/host"
)
65 changes: 65 additions & 0 deletions dpnp/backend/extensions/rng/host/dispatch/matrix.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//*****************************************************************************
// Copyright (c) 2024, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#pragma once

#include <oneapi/mkl/rng/device.hpp>

#include "utils/type_dispatch.hpp"

namespace dpnp::backend::ext::rng::host::dispatch
{
namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
namespace mkl_rng = oneapi::mkl::rng;

template <typename Ty, typename ArgTy, typename Method, typename argMethod>
struct TypePairDefinedEntry
: std::bool_constant<std::is_same_v<Ty, ArgTy> &&
std::is_same_v<Method, argMethod>>
{
static constexpr bool is_defined = true;
};

template <typename T, typename M>
struct GaussianTypePairSupportFactory
{
static constexpr bool is_defined = std::disjunction<
TypePairDefinedEntry<T,
double,
M,
mkl_rng::gaussian_method::by_default>,
TypePairDefinedEntry<T,
double,
M,
mkl_rng::gaussian_method::box_muller2>,
TypePairDefinedEntry<T, float, M, mkl_rng::gaussian_method::by_default>,
TypePairDefinedEntry<T,
float,
M,
mkl_rng::gaussian_method::box_muller2>,
// fall-through
dpctl_td_ns::NotDefinedEntry>::is_defined;
};
} // namespace dpnp::backend::ext::rng::host::dispatch
106 changes: 106 additions & 0 deletions dpnp/backend/extensions/rng/host/dispatch/table_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//*****************************************************************************
// Copyright (c) 2024, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#pragma once

#include <oneapi/mkl/rng.hpp>

namespace dpnp::backend::ext::rng::host::dispatch
{
namespace mkl_rng = oneapi::mkl::rng;

template <typename funcPtrT,
template <typename fnT, typename E, typename T, typename M>
typename factory,
int _no_of_engines,
int _no_of_types,
int _no_of_methods>
class Dispatch3DTableBuilder
{
private:
template <typename E, typename T>
const std::vector<funcPtrT> row_per_method() const
{
std::vector<funcPtrT> per_method = {
factory<funcPtrT, E, T, mkl_rng::gaussian_method::by_default>{}
.get(),
factory<funcPtrT, E, T, mkl_rng::gaussian_method::box_muller2>{}
.get(),
};
assert(per_method.size() == _no_of_methods);
return per_method;
}

template <typename E>
auto table_per_type_and_method() const
{
std::vector<std::vector<funcPtrT>> table_by_type = {
row_per_method<E, bool>(),
row_per_method<E, int8_t>(),
row_per_method<E, uint8_t>(),
row_per_method<E, int16_t>(),
row_per_method<E, uint16_t>(),
row_per_method<E, int32_t>(),
row_per_method<E, uint32_t>(),
row_per_method<E, int64_t>(),
row_per_method<E, uint64_t>(),
row_per_method<E, sycl::half>(),
row_per_method<E, float>(),
row_per_method<E, double>(),
row_per_method<E, std::complex<float>>(),
row_per_method<E, std::complex<double>>()};
assert(table_by_type.size() == _no_of_types);
return table_by_type;
}

public:
Dispatch3DTableBuilder() = default;
~Dispatch3DTableBuilder() = default;

void populate(funcPtrT table[][_no_of_types][_no_of_methods]) const
{
const auto map_by_engine = {
table_per_type_and_method<mkl_rng::mrg32k3a>(),
table_per_type_and_method<mkl_rng::philox4x32x10>(),
table_per_type_and_method<mkl_rng::mcg31m1>(),
table_per_type_and_method<mkl_rng::mcg59>()};
assert(map_by_engine.size() == _no_of_engines);

std::uint16_t engine_id = 0;
for (auto &table_by_type : map_by_engine) {
std::uint16_t type_id = 0;
for (auto &row_by_method : table_by_type) {
std::uint16_t method_id = 0;
for (auto &fn_ptr : row_by_method) {
table[engine_id][type_id][method_id] = fn_ptr;
++method_id;
}
++type_id;
}
++engine_id;
}
}
};
} // namespace dpnp::backend::ext::rng::host::dispatch
Loading

0 comments on commit 8baa052

Please sign in to comment.