Skip to content

Commit a7dbbed

Browse files
committed
changes for lottery sample
1 parent 3ea4783 commit a7dbbed

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

Libraries/oneMKL/random_sampling_without_replacement/lottery.cpp

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

1515
#include <iostream>
16+
#include <unordered_set>
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
@@ -136,6 +134,22 @@ int main(int argc, char ** argv) {
136134
std::terminate();
137135
}
138136

137+
// check correctness whether experiment contains unique numbers or not
138+
for(size_t i = 0; i < num_exp; ++i){
139+
auto first_iter = result_ptr + m * i;
140+
std::unordered_set<size_t> unique_set(first_iter, first_iter + m);
141+
if(unique_set.size() != m &&
142+
// if all elements are in the [0, n] range
143+
std::count_if(unique_set.begin(), unique_set.end(), [n](auto val){return val > n && val >= 0;}) == 0)
144+
{
145+
std::cout << "TEST FAILED" << std::endl;
146+
std::cout << "Error: the experiment "<< i <<" contains duplicates" << std::endl;
147+
std::cout << "Number of unique elements in the result: " << unique_set.size() << std::endl;
148+
return 1;
149+
}
150+
}
151+
std::cout << "TEST PASSED" << std::endl;
152+
139153
// Print output
140154
print_results(result_ptr, m, num_exp);
141155

Libraries/oneMKL/random_sampling_without_replacement/lottery_device_api.cpp

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

1515
#include <iostream>
16+
#include <unordered_set>
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
@@ -125,6 +123,22 @@ int main(int argc, char ** argv) {
125123
std::terminate();
126124
}
127125

126+
// check correctness whether experiment contains unique numbers or not
127+
for(size_t i = 0; i < num_exp; ++i){
128+
auto first_iter = result_vec.begin() + m * i;
129+
std::unordered_set<size_t> unique_set(first_iter, first_iter + m);
130+
if(unique_set.size() != m &&
131+
// if all elements are in the [0, n] range
132+
std::count_if(unique_set.begin(), unique_set.end(), [n](auto val){return val > n && val >= 0;}) == 0)
133+
{
134+
std::cout << "TEST FAILED" << std::endl;
135+
std::cout << "Error: the experiment "<< i <<" contains duplicates" << std::endl;
136+
std::cout << "Number of unique elements in the result: " << unique_set.size() << std::endl;
137+
return 1;
138+
}
139+
}
140+
std::cout << "TEST PASSED" << std::endl;
141+
128142
// Print output
129143
print_results(result_vec, m);
130144

Libraries/oneMKL/random_sampling_without_replacement/lottery_usm.cpp

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

1515
#include <iostream>
16+
#include <unordered_set>
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
@@ -136,6 +134,22 @@ int main(int argc, char ** argv) {
136134
std::terminate();
137135
}
138136

137+
// check correctness whether experiment contains unique numbers or not
138+
for(size_t i = 0; i < num_exp; ++i){
139+
auto first_iter = result_ptr + m * i;
140+
std::unordered_set<size_t> unique_set(first_iter, first_iter + m);
141+
if(unique_set.size() != m &&
142+
// if all elements are in the [0, n] range
143+
std::count_if(unique_set.begin(), unique_set.end(), [n](auto val){return val > n && val >= 0;}) == 0)
144+
{
145+
std::cout << "TEST FAILED" << std::endl;
146+
std::cout << "Error: the experiment "<< i <<" contains duplicates" << std::endl;
147+
std::cout << "Number of unique elements in the result: " << unique_set.size() << std::endl;
148+
return 1;
149+
}
150+
}
151+
std::cout << "TEST PASSED" << std::endl;
152+
139153
// Print output
140154
std::cout << "Results with Host API:" << std::endl;
141155
print_results(result_ptr, m, num_exp);

0 commit comments

Comments
 (0)