Skip to content

Commit

Permalink
all failing tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
egrabovskaya committed Oct 29, 2024
1 parent 87a7df4 commit 34d761e
Show file tree
Hide file tree
Showing 24 changed files with 3,392 additions and 3,470 deletions.
476 changes: 476 additions & 0 deletions test/xpu_api/random/statistics_tests/common_for_distributions.hpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,32 @@
//
//===----------------------------------------------------------===//

#ifndef _ONEDPL_RANDOM_STATISTICS_TESTS_EXTREME_VALUE_COMMON_H
#define _ONEDPL_RANDOM_STATISTICS_TESTS_EXTREME_VALUE_COMMON_H
#ifndef _ONEDPL_RANDOM_STATISTICS_TESTS_COMMON_FOR_PHILOX_UNIFORM_REAL_H
#define _ONEDPL_RANDOM_STATISTICS_TESTS_COMMON_FOR_PHILOX_UNIFORM_REAL_H

#include <iostream>
#include <vector>
#include <random>
#include <limits>
#include <oneapi/dpl/random>
#include <math.h>

#include "statistics_common.h"

// Engine parameters
constexpr auto a = 40014u;
constexpr auto c = 200u;
constexpr auto m = 2147483563u;
constexpr auto seed = 777;

template <typename ScalarRealType>
int
statistics_check(int nsamples, ScalarRealType _a, ScalarRealType _b, const std::vector<ScalarRealType>& samples)
template <typename RealType>
std::int32_t
statistics_check(int nsamples, RealType left, RealType right, const std::vector<RealType>& samples)
{
// theoretical moments
const double y = 0.5772156649015328606065120;
const double pi = 3.1415926535897932384626433;
double tM = _a + _b * y;
double tD = pi * pi / 6.0 * _b * _b;
double tQ = 27.0 / 5.0 * tD * tD;
double tM = (right + left) / 2.0;
double tD = ((right - left) * (right - left)) / 12.0;
double tQ = ((right - left) * (right - left) * (right - left) * (right - left)) / 80.0;

return compare_moments(nsamples, samples, tM, tD, tQ);
}

template <class RealType, class UIntType>
template <typename RealType, typename UIntType, typename Engine>
int
test(sycl::queue& queue, oneapi::dpl::internal::element_type_t<RealType> _a, oneapi::dpl::internal::element_type_t<RealType> _b, int nsamples)
test(sycl::queue& queue, oneapi::dpl::internal::element_type_t<RealType> left,
oneapi::dpl::internal::element_type_t<RealType> right, int nsamples)
{

// memory allocation
Expand All @@ -55,27 +47,28 @@ test(sycl::queue& queue, oneapi::dpl::internal::element_type_t<RealType> _a, on
constexpr int num_elems = oneapi::dpl::internal::type_traits_t<RealType>::num_elems == 0
? 1
: oneapi::dpl::internal::type_traits_t<RealType>::num_elems;

// generation
{
sycl::buffer<oneapi::dpl::internal::element_type_t<RealType>, 1> buffer(samples.data(), nsamples);
sycl::buffer<oneapi::dpl::internal::element_type_t<RealType>> buffer(samples.data(), nsamples);

queue.submit([&](sycl::handler& cgh) {
sycl::accessor acc(buffer, cgh, sycl::write_only);

cgh.parallel_for<>(sycl::range<1>(nsamples / num_elems), [=](sycl::item<1> idx) {
unsigned long long offset = idx.get_linear_id() * num_elems;
oneapi::dpl::linear_congruential_engine<UIntType, a, c, m> engine(seed, offset);
oneapi::dpl::extreme_value_distribution<RealType> distr(_a, _b);
Engine engine;
engine.discard(offset);
oneapi::dpl::uniform_real_distribution<RealType> distr(left, right);

sycl::vec<oneapi::dpl::internal::element_type_t<RealType>, num_elems> res = distr(engine);
res.store(idx.get_linear_id(), acc);
});
});
queue.wait();
}

// statistics check
int err = statistics_check(nsamples, _a, _b, samples);
int err = statistics_check(nsamples, left, right, samples);

if (err)
{
Expand All @@ -89,40 +82,42 @@ test(sycl::queue& queue, oneapi::dpl::internal::element_type_t<RealType> _a, on
return err;
}

template <class RealType, class UIntType>
template <typename RealType, typename UIntType, typename Engine>
int
test_portion(sycl::queue& queue, oneapi::dpl::internal::element_type_t<RealType> _a, oneapi::dpl::internal::element_type_t<RealType> _b,
int nsamples, unsigned int part)
test_portion(sycl::queue& queue, oneapi::dpl::internal::element_type_t<RealType> left,
oneapi::dpl::internal::element_type_t<RealType> right, int nsamples, unsigned int part)
{

// memory allocation
std::vector<oneapi::dpl::internal::element_type_t<RealType>> samples(nsamples);
constexpr unsigned int num_elems = oneapi::dpl::internal::type_traits_t<RealType>::num_elems == 0
? 1
: oneapi::dpl::internal::type_traits_t<RealType>::num_elems;
constexpr int num_elems = oneapi::dpl::internal::type_traits_t<RealType>::num_elems == 0
? 1
: oneapi::dpl::internal::type_traits_t<RealType>::num_elems;
int n_elems = (part >= num_elems) ? num_elems : part;

// generation
{
sycl::buffer<oneapi::dpl::internal::element_type_t<RealType>, 1> buffer(samples.data(), nsamples);
sycl::buffer<oneapi::dpl::internal::element_type_t<RealType>> buffer(samples.data(), nsamples);

queue.submit([&](sycl::handler& cgh) {
sycl::accessor acc(buffer, cgh, sycl::write_only);

cgh.parallel_for<>(sycl::range<1>(nsamples / n_elems), [=](sycl::item<1> idx) {
unsigned long long offset = idx.get_linear_id() * n_elems;
oneapi::dpl::linear_congruential_engine<UIntType, a, c, m> engine(seed, offset);
oneapi::dpl::extreme_value_distribution<RealType> distr(_a, _b);
Engine engine;
engine.discard(offset);
oneapi::dpl::uniform_real_distribution<RealType> distr(left, right);

sycl::vec<oneapi::dpl::internal::element_type_t<RealType>, num_elems> res = distr(engine, part);
for (int i = 0; i < n_elems; ++i)
acc[offset + i] = res[i];
});
});
queue.wait_and_throw();
queue.wait();
}

// statistics check
int err = statistics_check(nsamples, _a, _b, samples);
int err = statistics_check(nsamples, left, right, samples);

if (err)
{
Expand All @@ -136,42 +131,48 @@ test_portion(sycl::queue& queue, oneapi::dpl::internal::element_type_t<RealType>
return err;
}

template <class RealType, class UIntType>
template <typename RealType, typename UIntType, typename Engine>
int
tests_set(sycl::queue& queue, int nsamples)
{

constexpr int nparams = 2;
oneapi::dpl::internal::element_type_t<RealType> a_array [nparams] = {2.0, -10.0};
oneapi::dpl::internal::element_type_t<RealType> b_array [nparams] = {1.0, 10.0};
oneapi::dpl::internal::element_type_t<RealType> left_array[nparams] = {0.0, -10.0};
oneapi::dpl::internal::element_type_t<RealType> right_array[nparams] = {1.0, 10.0};

// Test for all non-zero parameters
for(int i = 0; i < nparams; ++i) {
std::cout << "extreme_value_distribution test<type>, a = " << a_array[i] << ", b = " << b_array[i] <<
", nsamples = " << nsamples;
if(test<RealType, UIntType>(queue, a_array[i], b_array[i], nsamples)) {
for (int i = 0; i < nparams; ++i)
{
std::cout << "uniform_real_distribution test<type>, left = " << left_array[i] << ", right = " << right_array[i]
<< ", nsamples = " << nsamples;
if (test<RealType, UIntType, Engine>(queue, left_array[i], right_array[i], nsamples))
{
return 1;
}
}
return 0;
}

template <class RealType, class UIntType>
template <typename RealType, typename UIntType, typename Engine>
int
tests_set_portion(sycl::queue& queue, std::int32_t nsamples, unsigned int part)
tests_set_portion(sycl::queue& queue, int nsamples, unsigned int part)
{

constexpr int nparams = 2;
oneapi::dpl::internal::element_type_t<RealType> a_array [nparams] = {2.0, -10.0};
oneapi::dpl::internal::element_type_t<RealType> b_array [nparams] = {1.0, 10.0};
oneapi::dpl::internal::element_type_t<RealType> left_array[nparams] = {0.0, -10.0};
oneapi::dpl::internal::element_type_t<RealType> right_array[nparams] = {1.0, 10.0};

// Test for all non-zero parameters
for(int i = 0; i < nparams; ++i) {
std::cout << "extreme_value_distribution test<type>, a = " << a_array[i] << ", b = " << b_array[i] <<
", nsamples = " << nsamples << ", part = " << part;
if(test_portion<RealType, UIntType>(queue, a_array[i], b_array[i], nsamples, part)) {
for (int i = 0; i < nparams; ++i)
{
std::cout << "uniform_real_distribution test<type>, left = " << left_array[i] << ", right = " << right_array[i]
<< ", nsamples = " << nsamples << ", part = " << part;
if (test_portion<RealType, UIntType, Engine>(queue, left_array[i], right_array[i], nsamples, part))
{
return 1;
}
}
return 0;
}

#endif // _ONEDPL_RANDOM_STATISTICS_TESTS_EXTREME_VALUE_COMMON_H
#endif // _ONEDPL_RANDOM_STATISTICS_TESTS_COMMON_FOR_PHILOX_UNIFORM_REAL_H

This file was deleted.

Loading

0 comments on commit 34d761e

Please sign in to comment.