Skip to content

Commit 631dc54

Browse files
authored
Merge pull request #2509 from andreyfe1/rng_verification
[oneMKL] Add verification for oneMKL RNG Samples
2 parents 54ab942 + c53facc commit 631dc54

File tree

11 files changed

+139
-51
lines changed

11 files changed

+139
-51
lines changed

Libraries/oneMKL/monte_carlo_pi/README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,35 @@ Monte Carlo pi Calculation Simulation
7979
Buffer API
8080
-------------------------------------
8181
Number of points = 120000000
82-
Estimated value of Pi = 3.14139
82+
Estimated value of Pi = 3.14106
8383
Exact value of Pi = 3.14159
84-
Absolute error = 0.000207487
84+
Absolute error = 0.000530387
85+
86+
TEST PASSED
8587
8688
./mc_pi_usm
8789
8890
Monte Carlo pi Calculation Simulation
8991
Unified Shared Memory API
9092
-------------------------------------
9193
Number of points = 120000000
92-
Estimated value of Pi = 3.14139
94+
Estimated value of Pi = 3.14106
9395
Exact value of Pi = 3.14159
94-
Absolute error = 0.000207487
96+
Absolute error = 0.000530387
97+
98+
TEST PASSED
9599
96100
./mc_pi_device_api
97101
98102
Monte Carlo pi Calculation Simulation
99103
Device API
100104
-------------------------------------
101105
Number of points = 120000000
102-
Estimated value of Pi = 3.14139
106+
Estimated value of Pi = 3.14159
103107
Exact value of Pi = 3.14159
104-
Absolute error = 0.000207487
108+
Absolute error = 5.95359e-06
109+
110+
TEST PASSED
105111
```
106112

107113
### Troubleshooting

Libraries/oneMKL/monte_carlo_pi/mc_pi.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/*
88
*
99
* Content:
10-
* This file contains Monte Carlo Pi number evaluation benchmark for DPC++
10+
* This file contains Monte Carlo Pi number evaluation benchmark for DPC++
1111
* interface of random number generators.
1212
*
1313
*******************************************************************************/
@@ -126,10 +126,17 @@ int main(int argc, char ** argv) {
126126
}
127127

128128
// Printing results
129+
double abs_error = std::fabs(pi - estimated_pi);
129130
std::cout << "Estimated value of Pi = " << estimated_pi << std::endl;
130131
std::cout << "Exact value of Pi = " << pi << std::endl;
131-
std::cout << "Absolute error = " << fabs(pi-estimated_pi) << std::endl;
132+
std::cout << "Absolute error = " << abs_error << std::endl;
132133
std::cout << std::endl;
133134

135+
if(abs_error > 1.0e-3) {
136+
std::cout << "TEST FAILED" << std::endl;
137+
return 1;
138+
}
139+
140+
std::cout << "TEST PASSED" << std::endl;
134141
return 0;
135142
}

Libraries/oneMKL/monte_carlo_pi/mc_pi_device_api.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,17 @@ int main(int argc, char ** argv) {
116116
}
117117

118118
// Printing results
119+
double abs_error = std::fabs(pi - estimated_pi);
119120
std::cout << "Estimated value of Pi = " << estimated_pi << std::endl;
120121
std::cout << "Exact value of Pi = " << pi << std::endl;
121-
std::cout << "Absolute error = " << fabs(pi-estimated_pi) << std::endl;
122+
std::cout << "Absolute error = " << abs_error << std::endl;
122123
std::cout << std::endl;
123124

125+
if(abs_error > 1.0e-4) {
126+
std::cout << "TEST FAILED" << std::endl;
127+
return 1;
128+
}
129+
130+
std::cout << "TEST PASSED" << std::endl;
124131
return 0;
125132
}

Libraries/oneMKL/monte_carlo_pi/mc_pi_usm.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/*
88
*
99
* Content:
10-
* This file contains Monte Carlo Pi number evaluation benchmark for DPC++
10+
* This file contains Monte Carlo Pi number evaluation benchmark for DPC++
1111
* USM-based interface of random number generators.
1212
*
1313
*******************************************************************************/
@@ -70,7 +70,7 @@ double estimate_pi(sycl::queue& q, size_t n_points) {
7070
count += 1;
7171
}
7272
}
73-
count_ptr[item.get_group_linear_id()] = reduce_over_group(item.get_group(), count, std::plus<size_t>());
73+
count_ptr[item.get_group_linear_id()] = sycl::reduce_over_group(item.get_group(), count, std::plus<size_t>());
7474
});
7575
});
7676

@@ -129,10 +129,17 @@ int main(int argc, char ** argv) {
129129
}
130130

131131
// Printing results
132+
double abs_error = std::fabs(pi - estimated_pi);
132133
std::cout << "Estimated value of Pi = " << estimated_pi << std::endl;
133134
std::cout << "Exact value of Pi = " << pi << std::endl;
134-
std::cout << "Absolute error = " << fabs(pi-estimated_pi) << std::endl;
135+
std::cout << "Absolute error = " << abs_error << std::endl;
135136
std::cout << std::endl;
136137

138+
if(abs_error > 1.0e-3) {
139+
std::cout << "TEST FAILED" << std::endl;
140+
return 1;
141+
}
142+
143+
std::cout << "TEST PASSED" << std::endl;
137144
return 0;
138145
}

Libraries/oneMKL/random_sampling_without_replacement/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ After building, if everything is working correctly, you will see the step-by-ste
8282
./lottery
8383
8484
Multiple Simple Random Sampling without replacement
85-
Unified Shared Memory API
85+
Buffer Api
8686
---------------------------------------------------
8787
M = 6, N = 49, Number of experiments = 11969664
8888
Sample 11969661 of lottery of 11969664: 19, 5, 17, 27, 44, 34,
8989
Sample 11969662 of lottery of 11969664: 31, 39, 6, 19, 48, 15,
9090
Sample 11969663 of lottery of 11969664: 24, 11, 29, 44, 2, 20,
9191
92+
TEST PASSED
93+
9294
./lottery_usm
9395
9496
Multiple Simple Random Sampling without replacement
@@ -100,6 +102,8 @@ Sample 11969661 of lottery of 11969664: 19, 5, 17, 27, 44, 34,
100102
Sample 11969662 of lottery of 11969664: 31, 39, 6, 19, 48, 15,
101103
Sample 11969663 of lottery of 11969664: 24, 11, 29, 44, 2, 20,
102104
105+
TEST PASSED
106+
103107
./lottery_device_api
104108
105109
Multiple Simple Random Sampling without replacement
@@ -109,6 +113,8 @@ M = 6, N = 49, Number of experiments = 11969664
109113
Sample 11969661 of lottery of 11969664: 19, 5, 17, 27, 44, 34,
110114
Sample 11969662 of lottery of 11969664: 31, 39, 6, 19, 48, 15,
111115
Sample 11969663 of lottery of 11969664: 24, 11, 29, 44, 2, 20,
116+
117+
TEST PASSED
112118
```
113119

114120
### Troubleshooting

Libraries/oneMKL/random_sampling_without_replacement/lottery.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414

1515
#include <iostream>
16+
#include <algorithm>
1617

1718
#include <sycl/sycl.hpp>
1819
#include "oneapi/mkl.hpp"
@@ -59,10 +60,7 @@ void lottery(sycl::queue& q, size_t m, size_t n, size_t num_exp, size_t* result_
5960
// Generate random natural number j from {i,...,N-1}
6061
auto j = i + (size_t)(rng_buf[id * m + i] * (float)(n - i));
6162
// Swap local_buf[i] and local_buf[j]
62-
auto tmp = local_buf[i];
63-
local_buf[i] = local_buf[j];
64-
65-
local_buf[j] = tmp;
63+
std::swap(local_buf[i], local_buf[j]);
6664
}
6765
for (size_t i = 0; i < m; ++i) {
6866
// Copy shuffled buffer
@@ -139,5 +137,20 @@ int main(int argc, char ** argv) {
139137
// Print output
140138
print_results(result_ptr, m, num_exp);
141139

140+
// Check correctness whether experiment contains unique numbers or not
141+
for(size_t i = 0; i < num_exp; ++i){
142+
auto first_iter = result_ptr + m * i;
143+
std::sort(first_iter, first_iter + m);
144+
if(std::adjacent_find(first_iter, first_iter + m) != first_iter + m &&
145+
// if all elements are in the [0, n] range
146+
std::count_if(first_iter, first_iter + m, [n](auto val){return val > n && val >= 0;}) == 0)
147+
{
148+
std::cout << "TEST FAILED" << std::endl;
149+
std::cout << "Error: the experiment "<< i <<" contains duplicates" << std::endl;
150+
return 1;
151+
}
152+
}
153+
154+
std::cout << "TEST PASSED" << std::endl;
142155
return 0;
143156
}

Libraries/oneMKL/random_sampling_without_replacement/lottery_device_api.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414

1515
#include <iostream>
16+
#include <algorithm>
1617

1718
#include <sycl/sycl.hpp>
1819
#include "oneapi/mkl/rng/device.hpp"
@@ -52,10 +53,7 @@ void lottery_device_api(sycl::queue& q, size_t m, size_t n, size_t num_exp, std:
5253
// Generate random natural number j from {i,...,N-1}
5354
auto j = i + (size_t)(res * (float)(n - i));
5455
// Swap local_buf[i] and local_buf[j]
55-
auto tmp = local_buf[i];
56-
local_buf[i] = local_buf[j];
57-
58-
local_buf[j] = tmp;
56+
std::swap(local_buf[i], local_buf[j]);
5957
}
6058
for (size_t i = 0; i < m; ++i) {
6159
// Copy shuffled buffer
@@ -128,5 +126,20 @@ int main(int argc, char ** argv) {
128126
// Print output
129127
print_results(result_vec, m);
130128

129+
// Check correctness whether experiment contains unique numbers or not
130+
for(size_t i = 0; i < num_exp; ++i){
131+
auto first_iter = result_vec.begin() + m * i;
132+
std::sort(first_iter, first_iter + m);
133+
if(std::adjacent_find(first_iter, first_iter + m) != first_iter + m &&
134+
// if all elements are in the [0, n] range
135+
std::count_if(first_iter, first_iter + m, [n](auto val){return val > n && val >= 0;}) == 0)
136+
{
137+
std::cout << "TEST FAILED" << std::endl;
138+
std::cout << "Error: the experiment "<< i <<" contains duplicates" << std::endl;
139+
return 1;
140+
}
141+
}
142+
143+
std::cout << "TEST PASSED" << std::endl;
131144
return 0;
132145
}

Libraries/oneMKL/random_sampling_without_replacement/lottery_usm.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414

1515
#include <iostream>
16+
#include <algorithm>
1617

1718
#include <sycl/sycl.hpp>
1819
#include "oneapi/mkl.hpp"
@@ -59,10 +60,7 @@ void lottery(sycl::queue& q, size_t m, size_t n, size_t num_exp, size_t* result_
5960
// Generate random natural number j from {i,...,N-1}
6061
auto j = i + (size_t)(rng_buf[id * m + i] * (float)(n - i));
6162
// Swap local_buf[i] and local_buf[j]
62-
auto tmp = local_buf[i];
63-
local_buf[i] = local_buf[j];
64-
65-
local_buf[j] = tmp;
63+
std::swap(local_buf[i], local_buf[j]);
6664
}
6765
for (size_t i = 0; i < m; ++i) {
6866
// Copy shuffled buffer
@@ -140,5 +138,20 @@ int main(int argc, char ** argv) {
140138
std::cout << "Results with Host API:" << std::endl;
141139
print_results(result_ptr, m, num_exp);
142140

141+
// Check correctness whether experiment contains unique numbers or not
142+
for(size_t i = 0; i < num_exp; ++i){
143+
auto first_iter = result_ptr + m * i;
144+
std::sort(first_iter, first_iter + m);
145+
if(std::adjacent_find(first_iter, first_iter + m) != first_iter + m &&
146+
// if all elements are in the [0, n] range
147+
std::count_if(first_iter, first_iter + m, [n](auto val){return val > n && val >= 0;}) == 0)
148+
{
149+
std::cout << "TEST FAILED" << std::endl;
150+
std::cout << "Error: the experiment "<< i <<" contains duplicates" << std::endl;
151+
return 1;
152+
}
153+
}
154+
155+
std::cout << "TEST PASSED" << std::endl;
143156
return 0;
144157
}

Libraries/oneMKL/student_t_test/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Number of random samples = 1000000 with mean = 0, std_dev = 1
7070
T-test result with expected mean: 1
7171
T-test result with two input arrays: 1
7272
73+
TEST PASSED
74+
7375
./t_test_usm
7476
7577
Student's T-test Simulation
@@ -78,6 +80,8 @@ Unified Shared Memory Api
7880
Number of random samples = 1000000 with mean = 0, std_dev = 1
7981
T-test result with expected mean: 1
8082
T-test result with two input arrays: 1
83+
84+
TEST PASSED
8185
```
8286

8387
### Troubleshooting

Libraries/oneMKL/student_t_test/t_test.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@
1919

2020
#include "oneapi/mkl.hpp"
2121

22-
using fp_type = float;
2322
// Initialization value for random number generator
24-
static const auto seed = 7777;
23+
static const int seed = 7777;
2524
// Quantity of samples to check using Students' T-test
26-
static const auto n_samples = 1000000;
25+
static const int n_samples = 1000000;
2726
// Expected mean value of random samples
28-
static const auto expected_mean = 0.0f;
27+
static const float expected_mean = 0.0f;
2928
// Expected standard deviation of random samples
30-
static const auto expected_std_dev = 1.0f;
29+
static const float expected_std_dev = 1.0f;
3130
// T-test threshold which corresponds to 5% significance level and infinite
3231
// degrees of freedom
33-
static const auto threshold = 1.95996f;
32+
static const float threshold = 1.95996f;
3433

3534
// T-test function with expected mean
3635
// Returns: -1 if something went wrong, 1 - in case of NULL hypothesis should be
@@ -123,10 +122,11 @@ std::int32_t t_test(sycl::queue &q, sycl::buffer<RealType, 1> &r1,
123122
}
124123

125124
int main(int argc, char **argv) {
126-
std::cout << "\nStudent's T-test Simulation\n";
127-
std::cout << "Buffer Api\n";
128-
std::cout << "-------------------------------------\n";
125+
std::cout << "\nStudent's T-test Simulation" << std::endl;
126+
std::cout << "Buffer Api" << std::endl;
127+
std::cout << "-------------------------------------" << std::endl;
129128

129+
using fp_type = float;
130130
size_t n_points = n_samples;
131131
fp_type mean = expected_mean;
132132
fp_type std_dev = expected_std_dev;
@@ -153,7 +153,7 @@ int main(int argc, char **argv) {
153153
}
154154

155155
std::cout << "Number of random samples = " << n_points
156-
<< " with mean = " << mean << ", std_dev = " << std_dev << "\n";
156+
<< " with mean = " << mean << ", std_dev = " << std_dev << std::endl;
157157

158158
// This exception handler with catch async exceptions
159159
auto exception_handler = [](sycl::exception_list exceptions) {
@@ -189,13 +189,19 @@ int main(int argc, char **argv) {
189189
res1 = t_test(q, rng_buf0, n_points, rng_buf1, n_points);
190190
} catch (...) {
191191
// Some other exception detected
192-
std::cout << "Failure\n";
192+
std::cout << "Failure" << std::endl;
193193
std::terminate();
194194
}
195195

196196
// Printing results
197-
std::cout << "T-test result with expected mean: " << res0 << "\n";
198-
std::cout << "T-test result with two input arrays: " << res1 << "\n\n";
197+
std::cout << "T-test result with expected mean: " << res0 << std::endl;
198+
std::cout << "T-test result with two input arrays: " << res1 << std::endl << std::endl;
199199

200+
if(res0 != 1 || res1 != 1) {
201+
std::cout << "TEST FAILED" << std::endl;
202+
return 1;
203+
}
204+
205+
std::cout << "TEST PASSED" << std::endl;
200206
return 0;
201207
}

0 commit comments

Comments
 (0)