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

ENH: kernels for random.vonmisses; part 1 #779

Merged
merged 1 commit into from
Jul 12, 2021
Merged
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
60 changes: 32 additions & 28 deletions dpnp/backend/kernels/dpnp_krnl_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ void dpnp_rng_uniform_c(void* result, const long low, const long high, const siz
template <typename _DataType>
void dpnp_rng_vonmises_large_kappa_c(void* result, const _DataType mu, const _DataType kappa, const size_t size)
{
if (!size)
if (!size || !result)
{
return;
}
Expand Down Expand Up @@ -1314,20 +1314,23 @@ void dpnp_rng_vonmises_large_kappa_c(void* result, const _DataType mu, const _Da
dpnp_memory_free_c(Uvec);

mkl_rng::uniform<_DataType> uniform_distribution(d_zero, d_one);
auto event_out = mkl_rng::generate(uniform_distribution, DPNP_RNG_ENGINE, size, Vvec);
event_out.wait();
auto uniform_distr_event = mkl_rng::generate(uniform_distribution, DPNP_RNG_ENGINE, size, Vvec);

// TODO
// kernel
for (size_t i = 0; i < size; i++)
{
_DataType mod, resi;
cl::sycl::range<1> gws(size);

resi = (Vvec[i] < 0.5) ? mu - result1[i] : mu + result1[i];
mod = fabs(resi);
mod = (fmod(mod + M_PI, 2 * M_PI) - M_PI);
result1[i] = (resi < 0) ? -mod : mod;
}
auto paral_kernel_acceptance = [&](cl::sycl::handler& cgh) {
cgh.depends_on({uniform_distr_event});
cgh.parallel_for(gws, [=](cl::sycl::id<1> global_id) {
size_t i = global_id[0];
double mod, resi;
resi = (Vvec[i] < 0.5) ? mu - result1[i] : mu + result1[i];
mod = cl::sycl::fabs(resi);
mod = (cl::sycl::fmod(mod + M_PI, 2 * M_PI) - M_PI);
result1[i] = (resi < 0) ? -mod : mod;
});
};
auto acceptance_event = DPNP_QUEUE.submit(paral_kernel_acceptance);
acceptance_event.wait();

dpnp_memory_free_c(Vvec);
return;
Expand All @@ -1336,7 +1339,7 @@ void dpnp_rng_vonmises_large_kappa_c(void* result, const _DataType mu, const _Da
template <typename _DataType>
void dpnp_rng_vonmises_small_kappa_c(void* result, const _DataType mu, const _DataType kappa, const size_t size)
{
if (!size)
if (!size || !result)
{
return;
}
Expand Down Expand Up @@ -1391,20 +1394,22 @@ void dpnp_rng_vonmises_small_kappa_c(void* result, const _DataType mu, const _Da
dpnp_memory_free_c(Uvec);

mkl_rng::uniform<_DataType> uniform_distribution(d_zero, d_one);
auto event_out = mkl_rng::generate(uniform_distribution, DPNP_RNG_ENGINE, size, Vvec);
event_out.wait();
auto uniform_distr_event = mkl_rng::generate(uniform_distribution, DPNP_RNG_ENGINE, size, Vvec);

// TODO
// kernel
for (size_t i = 0; i < size; i++)
{
double mod, resi;

resi = (Vvec[i] < 0.5) ? mu - result1[i] : mu + result1[i];
mod = fabs(resi);
mod = (fmod(mod + M_PI, 2 * M_PI) - M_PI);
result1[i] = (resi < 0) ? -mod : mod;
}
cl::sycl::range<1> gws(size);
auto paral_kernel_acceptance = [&](cl::sycl::handler& cgh) {
Copy link
Contributor

Choose a reason for hiding this comment

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

it looks like this lambda is mostly the same as in other part of code. I suspect, this will be used for many other part of code.
If it is not - please drop off this message.
So, perhaps it is better to make it as a macro or, that is better, as inline function in this C++ source. Just to avoid code duplication.
This is not quite important. It might be leaved as is, at least for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shssf There is no code duplication here. Please read messages carefully)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shssf Actually you are right. But it is valuable, when we reusing the same code more than 2 times. I vote leave it as is.

cgh.depends_on({uniform_distr_event});
cgh.parallel_for(gws, [=](cl::sycl::id<1> global_id) {
size_t i = global_id[0];
double mod, resi;
resi = (Vvec[i] < 0.5) ? mu - result1[i] : mu + result1[i];
mod = cl::sycl::fabs(resi);
mod = (cl::sycl::fmod(mod + M_PI, 2 * M_PI) - M_PI);
result1[i] = (resi < 0) ? -mod : mod;
});
};
auto acceptance_event = DPNP_QUEUE.submit(paral_kernel_acceptance);
acceptance_event.wait();

dpnp_memory_free_c(Vvec);
return;
Expand All @@ -1423,7 +1428,6 @@ void dpnp_rng_vonmises_c(void* result, const _DataType mu, const _DataType kappa
dpnp_rng_vonmises_large_kappa_c<_DataType>(result, mu, kappa, size);
else
dpnp_rng_vonmises_small_kappa_c<_DataType>(result, mu, kappa, size);
// TODO case when kappa < kappa < 1e-8 (very small)
}

template <typename _KernelNameSpecialization>
Expand Down