Skip to content

Commit 995a37d

Browse files
TEST: add checking statistics for distr (#530)
* TEST: add checking statistics for distr
1 parent d233ec3 commit 995a37d

File tree

1 file changed

+222
-138
lines changed

1 file changed

+222
-138
lines changed

dpnp/backend/tests/test_random.cpp

Lines changed: 222 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,222 @@
1-
#include <dpnp_iface.hpp>
2-
#include <dpnp_iface_fptr.hpp>
3-
4-
#include <vector>
5-
6-
#include "gtest/gtest.h"
7-
8-
TEST(TestBackendRandomBeta, test_seed)
9-
{
10-
const size_t size = 256;
11-
size_t seed = 10;
12-
double a = 0.4;
13-
double b = 0.5;
14-
15-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
16-
17-
for (auto device_selector : QueueOptionsDevices)
18-
{
19-
dpnp_queue_initialize_c(device_selector);
20-
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
21-
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
22-
23-
dpnp_rng_srand_c(seed);
24-
dpnp_rng_beta_c<double>(result1, a, b, size);
25-
26-
dpnp_rng_srand_c(seed);
27-
dpnp_rng_beta_c<double>(result2, a, b, size);
28-
29-
for (size_t i = 0; i < size; ++i)
30-
{
31-
EXPECT_NEAR(result1[i], result2[i], 0.004);
32-
}
33-
}
34-
}
35-
36-
TEST(TestBackendRandomF, test_seed)
37-
{
38-
const size_t size = 256;
39-
size_t seed = 10;
40-
double dfnum = 10.4;
41-
double dfden = 12.5;
42-
43-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
44-
45-
for (auto device_selector : QueueOptionsDevices)
46-
{
47-
dpnp_queue_initialize_c(device_selector);
48-
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
49-
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
50-
51-
dpnp_rng_srand_c(seed);
52-
dpnp_rng_f_c<double>(result1, dfnum, dfden, size);
53-
54-
dpnp_rng_srand_c(seed);
55-
dpnp_rng_f_c<double>(result2, dfnum, dfden, size);
56-
57-
for (size_t i = 0; i < size; ++i)
58-
{
59-
EXPECT_NEAR(result1[i], result2[i], 0.004);
60-
}
61-
}
62-
}
63-
64-
TEST(TestBackendRandomNormal, test_seed)
65-
{
66-
const size_t size = 256;
67-
size_t seed = 10;
68-
double loc = 2.56;
69-
double scale = 0.8;
70-
71-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
72-
73-
for (auto device_selector : QueueOptionsDevices)
74-
{
75-
dpnp_queue_initialize_c(device_selector);
76-
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
77-
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
78-
79-
dpnp_rng_srand_c(seed);
80-
dpnp_rng_normal_c<double>(result1, loc, scale, size);
81-
82-
dpnp_rng_srand_c(seed);
83-
dpnp_rng_normal_c<double>(result2, loc, scale, size);
84-
85-
for (size_t i = 0; i < size; ++i)
86-
{
87-
EXPECT_NEAR(result1[i], result2[i], 0.004);
88-
}
89-
}
90-
}
91-
92-
TEST(TestBackendRandomUniform, test_seed)
93-
{
94-
const size_t size = 256;
95-
size_t seed = 10;
96-
long low = 1;
97-
long high = 120;
98-
99-
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
100-
101-
for (auto device_selector : QueueOptionsDevices)
102-
{
103-
dpnp_queue_initialize_c(device_selector);
104-
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
105-
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
106-
107-
dpnp_rng_srand_c(seed);
108-
dpnp_rng_uniform_c<double>(result1, low, high, size);
109-
110-
dpnp_rng_srand_c(seed);
111-
dpnp_rng_uniform_c<double>(result2, low, high, size);
112-
113-
for (size_t i = 0; i < size; ++i)
114-
{
115-
EXPECT_NEAR(result1[i], result2[i], 0.004);
116-
}
117-
}
118-
}
119-
120-
TEST(TestBackendRandomSrand, test_func_ptr)
121-
{
122-
void* fptr = nullptr;
123-
DPNPFuncData kernel_data = get_dpnp_function_ptr(
124-
DPNPFuncName::DPNP_FN_RNG_SRAND, DPNPFuncType::DPNP_FT_DOUBLE, DPNPFuncType::DPNP_FT_DOUBLE);
125-
126-
fptr = get_dpnp_function_ptr1(kernel_data.return_type,
127-
DPNPFuncName::DPNP_FN_RNG_SRAND,
128-
DPNPFuncType::DPNP_FT_DOUBLE,
129-
DPNPFuncType::DPNP_FT_DOUBLE);
130-
131-
EXPECT_TRUE(fptr != nullptr);
132-
}
133-
134-
int main(int argc, char** argv)
135-
{
136-
::testing::InitGoogleTest(&argc, argv);
137-
return RUN_ALL_TESTS();
138-
}
1+
//*****************************************************************************
2+
// Copyright (c) 2016-2020, 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+
#include <dpnp_iface.hpp>
27+
#include <dpnp_iface_fptr.hpp>
28+
29+
#include <math.h>
30+
#include <vector>
31+
32+
#include "gtest/gtest.h"
33+
34+
template <typename _DataType>
35+
bool check_statistics(_DataType* r, double tM, double tD, double tQ, size_t size)
36+
{
37+
double tD2;
38+
double sM, sD;
39+
double sum, sum2;
40+
double n, s;
41+
double DeltaM, DeltaD;
42+
43+
/***** Sample moments *****/
44+
sum = 0.0;
45+
sum2 = 0.0;
46+
for (size_t i = 0; i < size; i++) {
47+
sum += (double)r[i];
48+
sum2 += (double)r[i] * (double)r[i];
49+
}
50+
sM = sum / ((double)size);
51+
sD = sum2 / (double)size - (sM * sM);
52+
53+
/***** Comparison of theoretical and sample moments *****/
54+
n = (double)size;
55+
tD2 = tD * tD;
56+
s = ((tQ - tD2) / n) - (2 * (tQ - 2 * tD2) / (n * n)) + ((tQ - 3 * tD2) / (n * n * n));
57+
58+
DeltaM = (tM - sM) / sqrt(tD / n);
59+
DeltaD = (tD - sD) / sqrt(s);
60+
if (fabs(DeltaM) > 3.0 || fabs(DeltaD) > 3.0)
61+
return false;
62+
else
63+
return true;
64+
}
65+
66+
TEST(TestBackendRandomBeta, test_seed)
67+
{
68+
const size_t size = 256;
69+
size_t seed = 10;
70+
double a = 0.4;
71+
double b = 0.5;
72+
73+
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
74+
75+
for (auto device_selector : QueueOptionsDevices)
76+
{
77+
dpnp_queue_initialize_c(device_selector);
78+
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
79+
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
80+
81+
dpnp_rng_srand_c(seed);
82+
dpnp_rng_beta_c<double>(result1, a, b, size);
83+
84+
dpnp_rng_srand_c(seed);
85+
dpnp_rng_beta_c<double>(result2, a, b, size);
86+
87+
for (size_t i = 0; i < size; ++i)
88+
{
89+
EXPECT_NEAR(result1[i], result2[i], 0.004);
90+
}
91+
}
92+
}
93+
94+
TEST(TestBackendRandomF, test_seed)
95+
{
96+
const size_t size = 256;
97+
size_t seed = 10;
98+
double dfnum = 10.4;
99+
double dfden = 12.5;
100+
101+
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
102+
103+
for (auto device_selector : QueueOptionsDevices)
104+
{
105+
dpnp_queue_initialize_c(device_selector);
106+
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
107+
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
108+
109+
dpnp_rng_srand_c(seed);
110+
dpnp_rng_f_c<double>(result1, dfnum, dfden, size);
111+
112+
dpnp_rng_srand_c(seed);
113+
dpnp_rng_f_c<double>(result2, dfnum, dfden, size);
114+
115+
for (size_t i = 0; i < size; ++i)
116+
{
117+
EXPECT_NEAR(result1[i], result2[i], 0.004);
118+
}
119+
}
120+
}
121+
122+
TEST(TestBackendRandomNormal, test_seed)
123+
{
124+
const size_t size = 256;
125+
size_t seed = 10;
126+
double loc = 2.56;
127+
double scale = 0.8;
128+
129+
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
130+
131+
for (auto device_selector : QueueOptionsDevices)
132+
{
133+
dpnp_queue_initialize_c(device_selector);
134+
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
135+
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
136+
137+
dpnp_rng_srand_c(seed);
138+
dpnp_rng_normal_c<double>(result1, loc, scale, size);
139+
140+
dpnp_rng_srand_c(seed);
141+
dpnp_rng_normal_c<double>(result2, loc, scale, size);
142+
143+
for (size_t i = 0; i < size; ++i)
144+
{
145+
EXPECT_NEAR(result1[i], result2[i], 0.004);
146+
}
147+
}
148+
}
149+
150+
TEST(TestBackendRandomUniform, test_seed)
151+
{
152+
const size_t size = 256;
153+
size_t seed = 10;
154+
long low = 1;
155+
long high = 120;
156+
157+
auto QueueOptionsDevices = std::vector<QueueOptions>{QueueOptions::CPU_SELECTOR, QueueOptions::GPU_SELECTOR};
158+
159+
for (auto device_selector : QueueOptionsDevices)
160+
{
161+
dpnp_queue_initialize_c(device_selector);
162+
double* result1 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
163+
double* result2 = (double*)dpnp_memory_alloc_c(size * sizeof(double));
164+
165+
dpnp_rng_srand_c(seed);
166+
dpnp_rng_uniform_c<double>(result1, low, high, size);
167+
168+
dpnp_rng_srand_c(seed);
169+
dpnp_rng_uniform_c<double>(result2, low, high, size);
170+
171+
for (size_t i = 0; i < size; ++i)
172+
{
173+
EXPECT_NEAR(result1[i], result2[i], 0.004);
174+
}
175+
}
176+
}
177+
178+
TEST(TestBackendRandomUniform, test_statistics) {
179+
const size_t size = 256;
180+
size_t seed = 10;
181+
long a = 1;
182+
long b = 120;
183+
bool check_statistics_res = false;
184+
185+
/***** Theoretical moments *****/
186+
double tM = (b + a) / 2.0;
187+
double tD = ((b - a) * (b - a)) / 12.0;
188+
double tQ = ((b - a) * (b - a) * (b - a) * (b - a)) / 80.0;
189+
190+
auto QueueOptionsDevices = std::vector<QueueOptions>{ QueueOptions::CPU_SELECTOR,
191+
QueueOptions::GPU_SELECTOR };
192+
193+
for (auto device_selector : QueueOptionsDevices) {
194+
dpnp_queue_initialize_c(device_selector);
195+
double* result = (double*)dpnp_memory_alloc_c(size * sizeof(double));
196+
dpnp_rng_srand_c(seed);
197+
dpnp_rng_uniform_c<double>(result, a, b, size);
198+
check_statistics_res = check_statistics<double>(result, tM, tD, tQ, size);
199+
200+
ASSERT_TRUE(check_statistics_res);
201+
}
202+
}
203+
204+
TEST(TestBackendRandomSrand, test_func_ptr) {
205+
206+
void* fptr = nullptr;
207+
DPNPFuncData kernel_data = get_dpnp_function_ptr(
208+
DPNPFuncName::DPNP_FN_RNG_SRAND, DPNPFuncType::DPNP_FT_DOUBLE, DPNPFuncType::DPNP_FT_DOUBLE);
209+
210+
fptr = get_dpnp_function_ptr1(kernel_data.return_type,
211+
DPNPFuncName::DPNP_FN_RNG_SRAND,
212+
DPNPFuncType::DPNP_FT_DOUBLE,
213+
DPNPFuncType::DPNP_FT_DOUBLE);
214+
215+
EXPECT_TRUE(fptr != nullptr);
216+
}
217+
218+
int main(int argc, char** argv)
219+
{
220+
::testing::InitGoogleTest(&argc, argv);
221+
return RUN_ALL_TESTS();
222+
}

0 commit comments

Comments
 (0)