Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions cpp/daal/src/externals/service_rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,24 @@ class RNGs
int uniformWithoutReplacement(const SizeType n, DstType * r, void * state, const Type a, const Type b,
const int method = __DAAL_RNG_METHOD_UNIFORM_STD)
{
Type * buffer = (Type *)daal_malloc(sizeof(Type) * n);
if (n > (b - a))
{
return -1; // Error: n is greater than the range
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use error indices from "error_indexes.h" instead of integer constants. For example:

Suggested change
return -1; // Error: n is greater than the range
return services::ErrorIncorrectSizeOfArray; // Error: n is greater than the range

You can also define new index in case there is no index that suites your needs.

}
if (n == 0)
{
return 0; // No numbers to generate
}

const SizeType range = b - a;
Type * buffer = (Type *)daal_malloc(sizeof(Type) * range);
if (!buffer) return -2; // Allocation failure
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
if (!buffer) return -2; // Allocation failure
if (!buffer) return services::ErrorMemoryAllocationFailed; // Allocation failure


for (SizeType i = 0; i < range; i++)
{
buffer[i] = i + a;
}

int errorcode = uniformWithoutReplacement(n, r, buffer, state, a, b, method);
daal_free(buffer);
return errorcode;
Expand All @@ -128,17 +145,10 @@ class RNGs
int errorcode = 0;
for (SizeType i = 0; i < n; i++)
{
errorcode = uniform(1, buffer + i, state, a + i, b, method);
int value = buffer[i];

for (SizeType j = i; j > 0; j--)
{
if (value == buffer[j - 1])
{
value = (DstType)(j - 1 + a);
}
}
r[i] = value;
int j;
errorcode = uniform(1, &j, state, a + i, b, method);
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 add a reference to the method that is used here?

std::swap(buffer[i], buffer[j]);
r[i] = buffer[i];
}
return errorcode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

namespace oneapi::dal::kmeans_init::backend {

namespace pr = dal::backend::primitives;

template <typename Float, typename Method, typename Task>
struct compute_kernel_distr {
compute_result<Task> operator()(const dal::backend::context_gpu& ctx,
Expand All @@ -33,9 +35,13 @@ namespace misc {
using ctx_t = dal::backend::context_gpu;
using ids_arr_t = array<std::int64_t>;

ids_arr_t generate_random_indices(std::int64_t count, std::int64_t scount, std::int64_t seed = 777);
ids_arr_t generate_random_indices(std::int64_t count,
std::int64_t scount,
std::int64_t seed,
pr::host_engine engine);

ids_arr_t generate_random_indices_distr(const ctx_t& ctx,
const detail::descriptor_base<task::by_default>& params,
std::int64_t count,
std::int64_t scount,
std::int64_t rseed = 777);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "oneapi/dal/detail/error_messages.hpp"
#include "oneapi/dal/detail/profiler.hpp"
#include "oneapi/dal/table/row_accessor.hpp"
#include "oneapi/dal/backend/primitives/rng/host_engine.hpp"

namespace oneapi::dal::kmeans_init::backend {

Expand Down Expand Up @@ -54,7 +55,7 @@ compute_result<Task> compute_kernel_distr<Float, Method, Task>::operator()(
pr::ndview<Float, 2>::wrap(resa.get_mutable_data(), { cluster_count, feature_count });

const auto indices =
misc::generate_random_indices_distr(ctx, cluster_count, sample_count, seed);
misc::generate_random_indices_distr(ctx, params, cluster_count, sample_count, seed);
const auto ndids =
pr::ndarray<std::int64_t, 1>::wrap(indices.get_data(), { cluster_count }).to_device(queue);

Expand All @@ -71,26 +72,32 @@ template struct compute_kernel_distr<double, method::random_dense, task::init>;

namespace misc {

ids_arr_t generate_random_indices(std::int64_t count, std::int64_t scount, std::int64_t seed) {
ids_arr_t generate_random_indices(std::int64_t count,
std::int64_t scount,
std::int64_t seed,
pr::host_engine host_engine) {
ids_arr_t result = ids_arr_t::empty(count);
auto ndres = pr::ndview<std::int64_t, 1>::wrap(result.get_mutable_data(), { count });
ONEDAL_ASSERT(count < scount);
pr::partial_fisher_yates_shuffle(ndres, scount, seed);
pr::partial_fisher_yates_shuffle(ndres, scount, seed, host_engine);
return result;
}

ids_arr_t generate_random_indices_distr(const ctx_t& ctx,
const detail::descriptor_base<task::by_default>& params,
std::int64_t count,
std::int64_t scount,
std::int64_t rseed) {
auto& comm = ctx.get_communicator();
const auto rank_count = comm.get_rank_count();

ids_arr_t root_rand = ids_arr_t::empty(rank_count);
auto engine_type = pr::convert_engine_method(params.get_engine_type());

pr::host_engine eng_ = pr::host_engine(params.get_seed(), engine_type);
if (comm.is_root_rank()) {
const auto maxval = rank_count + 1;
root_rand = generate_random_indices(rank_count, maxval, rseed);
root_rand = generate_random_indices(rank_count, maxval, rseed, eng_);
}

{
Expand All @@ -101,7 +108,7 @@ ids_arr_t generate_random_indices_distr(const ctx_t& ctx,
ONEDAL_ASSERT(root_rand.get_count() == rank_count);

const auto seed = root_rand[comm.get_rank()];
return generate_random_indices(count, scount, seed);
return generate_random_indices(count, scount, seed, eng_);
}

} // namespace misc
Expand Down
7 changes: 7 additions & 0 deletions cpp/oneapi/dal/algo/kmeans_init/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class descriptor_impl : public base {
public:
std::int64_t local_trials_count = -1;
std::int64_t cluster_count = 2;

engine_type df_engine_type = engine_type::philox4x32x10;
std::int64_t seed = 777;
};

Expand All @@ -41,6 +43,11 @@ std::int64_t descriptor_base<Task>::get_seed() const {
return impl_->seed;
}

template <typename Task>
engine_type descriptor_base<Task>::get_engine_type() const {
return impl_->df_engine_type;
}

template <typename Task>
std::int64_t descriptor_base<Task>::get_local_trials_count() const {
return impl_->local_trials_count;
Expand Down
13 changes: 13 additions & 0 deletions cpp/oneapi/dal/algo/kmeans_init/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "oneapi/dal/detail/common.hpp"
#include "oneapi/dal/table/common.hpp"
#include "oneapi/dal/rng.hpp"

namespace oneapi::dal::kmeans_init {

Expand Down Expand Up @@ -119,6 +120,7 @@ class descriptor_base : public base {

std::int64_t get_local_trials_count() const;
std::int64_t get_cluster_count() const;
engine_type get_engine_type() const;
std::int64_t get_seed() const;

protected:
Expand Down Expand Up @@ -183,6 +185,17 @@ class descriptor : public detail::descriptor_base<Task> {
return *this;
}

/// Engine method for the random numbers generator used by the algorithm
/// @remark default = engine_method::philox4x32x10
engine_type get_engine_type() const {
return base_t::get_engine_type();
}

auto& set_engine_type(engine_type value) {
base_t::set_engine_type_impl(value);
return *this;
}

template <typename M = Method, typename = detail::v1::enable_if_not_default_dense<M>>
auto& get_seed() const {
return base_t::get_seed();
Expand Down
1 change: 1 addition & 0 deletions cpp/oneapi/dal/algo/kmeans_init/test/spmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class kmeans_init_spmd_test : public kmeans_init_test<TestType, kmeans_init_spmd
std::int64_t rank_count_ = -1;
};

// TODO: Add tests for other methods
using kmeans_init_types = _TE_COMBINE_TYPES_2((float, double),
(kmeans_init::method::plus_plus_dense));

Expand Down
14 changes: 7 additions & 7 deletions cpp/oneapi/dal/backend/primitives/rng/device_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,13 @@ sycl::event shuffle(sycl::queue& queue,
/// @param[in] method The rng engine type. Defaults to `mt19937`.
/// @param[in] deps Dependencies for the SYCL event.
template <typename Type>
sycl::event partial_fisher_yates_shuffle(
sycl::queue& queue_,
ndview<Type, 1>& result_array,
std::int64_t top,
std::int64_t seed,
engine_type_internal method = engine_type_internal::mt19937,
const event_vector& deps = {});
sycl::event partial_fisher_yates_shuffle(sycl::queue& queue_,
ndview<Type, 1>& result_array,
std::int64_t top,
std::int64_t seed,
device_engine& engine_,
const event_vector& deps = {});

#endif

} // namespace oneapi::dal::backend::primitives

This file was deleted.

Loading
Loading