Skip to content

Commit

Permalink
using vector<unsigned char> instead of vector<bool>
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengChen002 committed Nov 6, 2023
1 parent 870477c commit 6eb9a52
Showing 1 changed file with 15 additions and 80 deletions.
95 changes: 15 additions & 80 deletions tests/usm/usm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,30 +511,34 @@ struct gen_buf_size_selector<0> {
*/
template <typename T, size_t buf_size>
class event_generator {
// Change vector<bool> to vector <unsigned char>, since vector<bool> is
// stored bit-wise, and does not have data() member function.
using ContainType = typename std::conditional<std::is_same_v<bool, T>,
unsigned char, T>::type;
sycl::range<1> rng{buf_size};
std::vector<T> arr_src;
std::vector<T> arr_dst;
sycl::buffer<T, 1> buf_src;
sycl::buffer<T, 1> buf_dst;
std::vector<ContainType> arr_src;
std::vector<ContainType> arr_dst;
sycl::buffer<ContainType, 1> buf_src;
sycl::buffer<ContainType, 1> buf_dst;

public:
event_generator()
: rng{buf_size},
arr_src(buf_size, T{0}),
arr_dst(buf_size, T{0}),
arr_src(buf_size, ContainType{0}),
arr_dst(buf_size, ContainType{0}),
buf_src{arr_src.data(), rng},
buf_dst{arr_dst.data(), rng} {}

/** @brief Initialize arr_src with init_value and return sycl::event of
* queue::submit()
* @param value Some non-zero (non-default) value of type T
* @param value Some non-zero (non-default) value of type ContainType
*/
sycl::event init(sycl::queue &queue, T value) {
sycl::event init(sycl::queue& queue, ContainType value) {
return queue.submit([&](sycl::handler &cgh) {
auto acc_src = buf_src.template get_access<sycl::access_mode::write>(cgh);
// single_task is used to make process long enough for testing purpose
// The function being tested must wait for this task to complete.
cgh.single_task<init_kernel_name<T, buf_size>>([=] {
cgh.single_task<init_kernel_name<ContainType, buf_size>>([=] {
for (size_t i = 0; i < buf_size; ++i) {
acc_src[i] = value;
}
Expand All @@ -545,77 +549,8 @@ class event_generator {
/** @brief Copy data from arr_src to arr_dst for future check
*/
void copy_arrays(sycl::queue &queue) {
queue.submit([&](sycl::handler &cgh) {
using kernel_name = copy_arrays_kernel_name<T, buf_size>;
auto acc_src = buf_src.template get_access<sycl::access_mode::read>(cgh);
auto acc_dst = buf_dst.template get_access<sycl::access_mode::write>(cgh);
// Copy should be much faster then algorithm in 'init()' member function
// to detect situation when tested function doesn't wait for events
// provided as arguments
cgh.parallel_for<kernel_name>(rng, [=](sycl::id<1> idx) {
const size_t i = idx[0];
acc_dst[i] = acc_src[i];
});
});
}

/** @brief Check that elements of arrays are equal to each other and to
* value
* @param value The same value as value passed to init() member function
*/
bool check(T value) {
bool result = true;
auto acc_src = buf_src.template get_access<sycl::access_mode::read>(rng);
auto acc_dst = buf_dst.template get_access<sycl::access_mode::read>(rng);
for (size_t i = buf_size - 1; i + 1 > 0; --i) {
result = result && (acc_src[i] == acc_dst[i]);
result = result && (acc_dst[i] == value);
}
return result;
}
};

// Since the std::vector<bool> type does not have a data() member function,
// std::array is used instead.
template <size_t buf_size>
class event_generator<bool, buf_size> {
sycl::range<1> rng{buf_size};
std::array<bool, buf_size> arr_src;
std::array<bool, buf_size> arr_dst;
sycl::buffer<bool, 1> buf_src;
sycl::buffer<bool, 1> buf_dst;

public:
event_generator()
: rng{buf_size},
buf_src{arr_src.data(), rng},
buf_dst{arr_dst.data(), rng} {
arr_src.fill(bool{0});
arr_dst.fill(bool{0});
}

/** @brief Initialize arr_src with init_value and return sycl::event of
* queue::submit()
* @param value Some non-zero (non-default) value of type bool
*/
sycl::event init(sycl::queue& queue, bool value) {
return queue.submit([&](sycl::handler& cgh) {
auto acc_src = buf_src.template get_access<sycl::access_mode::write>(cgh);
// single_task is used to make process long enough for testing purpose
// The function being tested must wait for this task to complete.
cgh.single_task<init_kernel_name<bool, buf_size>>([=] {
for (size_t i = 0; i < buf_size; ++i) {
acc_src[i] = value;
}
});
});
}

/** @brief Copy data from arr_src to arr_dst for future check
*/
void copy_arrays(sycl::queue& queue) {
queue.submit([&](sycl::handler& cgh) {
using kernel_name = copy_arrays_kernel_name<bool, buf_size>;
using kernel_name = copy_arrays_kernel_name<ContainType, buf_size>;
auto acc_src = buf_src.template get_access<sycl::access_mode::read>(cgh);
auto acc_dst = buf_dst.template get_access<sycl::access_mode::write>(cgh);
// Copy should be much faster then algorithm in 'init()' member function
Expand All @@ -632,7 +567,7 @@ class event_generator<bool, buf_size> {
* value
* @param value The same value as value passed to init() member function
*/
bool check(bool value) {
bool check(ContainType value) {
bool result = true;
auto acc_src = buf_src.template get_access<sycl::access_mode::read>(rng);
auto acc_dst = buf_dst.template get_access<sycl::access_mode::read>(rng);
Expand Down

0 comments on commit 6eb9a52

Please sign in to comment.