Skip to content

Commit fd7323c

Browse files
authored
[SYCL][ESIMD] Add function that let iterating over types and dims (intel#588)
* [SYCL][ESIMD] Add functions that let iterating over types and dims - Added function for_types_and_dims and was updated "ctor_default" test - New struct names with lesser character names provides write our statements in one string - Now ctor_copy.cpp used "for_all_types_and_dims" function - Add factory method get_tested_dimensions, so we have same logic with retreiving tested types - Reverted comment that how "for_all_types" works
1 parent b3dfd11 commit fd7323c

File tree

3 files changed

+67
-82
lines changed

3 files changed

+67
-82
lines changed

SYCL/ESIMD/api/functional/ctors/ctor_copy.cpp

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct initializer {
4343

4444
// Descriptor class for the case of calling constructor in variable declaration
4545
// context.
46-
struct var_declaration {
46+
struct var_decl {
4747
static std::string get_description() { return "variable declaration"; }
4848

4949
template <typename DataT, int NumElems>
@@ -57,7 +57,7 @@ struct var_declaration {
5757

5858
// Descriptor class for the case of calling constructor in rvalue in an
5959
// expression context.
60-
struct rval_in_expression {
60+
struct rval_in_expr {
6161
static std::string get_description() { return "rvalue in an expression"; }
6262

6363
template <typename DataT, int NumElems>
@@ -92,42 +92,19 @@ class const_ref {
9292
}
9393
};
9494

95-
template <typename DataT, typename TestT>
96-
using run_test_with_one_elem = test<DataT, 1, TestT>;
97-
98-
template <typename DataT, typename TestT>
99-
using run_test_with_eight_elems = test<DataT, 8, TestT>;
100-
101-
template <typename DataT, typename TestT>
102-
using run_test_with_sixteen_elems = test<DataT, 16, TestT>;
103-
104-
template <typename DataT, typename TestT>
105-
using run_test_with_thirty_two_elems = test<DataT, 32, TestT>;
106-
107-
template <typename TestT, typename... T>
108-
bool run_verification_for_type(sycl::queue &queue,
109-
const named_type_pack<T...> &types) {
110-
bool passed{true};
111-
112-
passed &= for_all_types<run_test_with_one_elem, TestT>(types, queue);
113-
passed &= for_all_types<run_test_with_eight_elems, TestT>(types, queue);
114-
passed &= for_all_types<run_test_with_sixteen_elems, TestT>(types, queue);
115-
passed &= for_all_types<run_test_with_thirty_two_elems, TestT>(types, queue);
116-
return passed;
117-
}
118-
11995
int main(int argc, char **argv) {
12096
sycl::queue queue{esimd_test::ESIMDSelector{},
12197
esimd_test::createExceptionHandler()};
12298

12399
bool passed{true};
124100

125-
auto types{get_tested_types<tested_types::all>()};
101+
const auto types{get_tested_types<tested_types::all>()};
102+
const auto dims{get_all_dimensions()};
126103

127-
passed &= run_verification_for_type<initializer>(queue, types);
128-
passed &= run_verification_for_type<var_declaration>(queue, types);
129-
passed &= run_verification_for_type<rval_in_expression>(queue, types);
130-
passed &= run_verification_for_type<const_ref>(queue, types);
104+
passed &= for_all_types_and_dims<test, initializer>(types, dims, queue);
105+
passed &= for_all_types_and_dims<test, var_decl>(types, dims, queue);
106+
passed &= for_all_types_and_dims<test, rval_in_expr>(types, dims, queue);
107+
passed &= for_all_types_and_dims<test, const_ref>(types, dims, queue);
131108

132109
std::cout << (passed ? "=== Test passed\n" : "=== Test FAILED\n");
133110
return passed ? 0 : 1;

SYCL/ESIMD/api/functional/ctors/ctor_default.cpp

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct initializer {
4242

4343
// Descriptor class for the case of calling constructor in variable declaration
4444
// context
45-
struct var_declaration {
45+
struct var_decl {
4646
static std::string get_description() { return "variable declaration"; }
4747

4848
template <typename DataT, int NumElems>
@@ -54,7 +54,7 @@ struct var_declaration {
5454

5555
// Descriptor class for the case of calling constructor in rvalue in an
5656
// expression context
57-
struct rval_in_expression {
57+
struct rval_in_expr {
5858
static std::string get_description() { return "rvalue in an expression"; }
5959

6060
template <typename DataT, int NumElems>
@@ -116,44 +116,19 @@ template <typename DataT, int NumElems, typename TestCaseT> struct test {
116116
}
117117
};
118118

119-
template <typename DataT, typename TestT>
120-
using run_test_with_one_elem = test<DataT, 1, TestT>;
121-
122-
template <typename DataT, typename TestT>
123-
using run_test_with_eight_elems = test<DataT, 8, TestT>;
124-
125-
template <typename DataT, typename TestT>
126-
using run_test_with_sixteen_elems = test<DataT, 16, TestT>;
127-
128-
template <typename DataT, typename TestT>
129-
using run_test_with_thirty_two_elems = test<DataT, 32, TestT>;
130-
131-
template <typename TestT, typename... T>
132-
bool run_verification_with_chosen_test_type(
133-
sycl::queue &queue, const named_type_pack<T...> &types) {
134-
bool passed{true};
135-
136-
passed &= for_all_types<run_test_with_one_elem, TestT>(types, queue);
137-
passed &= for_all_types<run_test_with_eight_elems, TestT>(types, queue);
138-
passed &= for_all_types<run_test_with_sixteen_elems, TestT>(types, queue);
139-
passed &= for_all_types<run_test_with_thirty_two_elems, TestT>(types, queue);
140-
return passed;
141-
}
142-
143119
int main(int argc, char **argv) {
144120
sycl::queue queue{esimd_test::ESIMDSelector{},
145121
esimd_test::createExceptionHandler()};
146122

147123
bool passed{true};
148124

149125
const auto types{get_tested_types<tested_types::all>()};
126+
const auto dims{get_all_dimensions()};
150127

151-
passed &= run_verification_with_chosen_test_type<initializer>(queue, types);
152-
passed &=
153-
run_verification_with_chosen_test_type<var_declaration>(queue, types);
154-
passed &=
155-
run_verification_with_chosen_test_type<rval_in_expression>(queue, types);
156-
passed &= run_verification_with_chosen_test_type<const_ref>(queue, types);
128+
passed &= for_all_types_and_dims<test, initializer>(types, dims, queue);
129+
passed &= for_all_types_and_dims<test, var_decl>(types, dims, queue);
130+
passed &= for_all_types_and_dims<test, rval_in_expr>(types, dims, queue);
131+
passed &= for_all_types_and_dims<test, const_ref>(types, dims, queue);
157132

158133
std::cout << (passed ? "=== Test passed\n" : "=== Test FAILED\n");
159134
return passed ? 0 : 1;

SYCL/ESIMD/api/functional/type_coverage.hpp

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace esimd_test {
2121
namespace api {
2222
namespace functional {
2323

24+
// Integer pack to store provided int values
25+
template <int... T> struct values_pack {
26+
values_pack() {}
27+
};
28+
2429
// Type pack to store types and underlying data type names to use with
2530
// type_name_string
2631
template <typename... T> struct named_type_pack {
@@ -86,28 +91,56 @@ template <tested_types required> auto get_tested_types() {
8691
}
8792
}
8893

94+
// Factory method to retrieve pre-defined values_pack, to have the same
95+
// default dimensions over the tests
96+
auto get_all_dimensions() { return values_pack<1, 8, 16, 32>(); }
97+
8998
// Run action for each of types given by named_type_pack instance
90-
template <template <typename, typename...> class action,
91-
typename... actionArgsT, typename... types, typename... argsT>
92-
inline bool for_all_types(const named_type_pack<types...> &typeList,
93-
argsT &&... args) {
99+
template <template <typename, int, typename...> class Action, int N,
100+
typename... ActionArgsT, typename... Types, typename... ArgsT>
101+
inline bool for_all_types(const named_type_pack<Types...> &type_list,
102+
ArgsT &&... args) {
103+
bool passed{true};
104+
105+
size_t type_name_index = 0;
106+
107+
// Run action for each type from named_type_pack... parameter pack
108+
((passed &= Action<Types, N, ActionArgsT...>{}(
109+
std::forward<ArgsT>(args)..., type_list.names[type_name_index]),
110+
++type_name_index),
111+
...);
112+
// The unary right fold expression is used for parameter pack expansion.
113+
// Every expression with comma operator is strictly sequenced, so we can
114+
// increment safely. And of course the fold expression would not be optimized
115+
// out due to side-effects.
116+
// Additional pair of brackets is required because of precedence of increment
117+
// operator relative to the comma operator.
118+
//
119+
// Note that there is actually no difference in left or right fold expression
120+
// for the comma operator, as it would give the same order of actions
121+
// execution and the same order of the type name index increment: both the
122+
// "(expr0, (exr1, expr2))" and "((expr0, expr1), expr2)" would give the same
123+
// result as simple "expr0, expr1, expr2"
124+
125+
return passed;
126+
}
127+
128+
// Calls for_all_types for each vector length by values_pack instance
129+
template <template <typename, int, typename...> class Action,
130+
typename... ActionArgsT, typename... Types, int... Dims,
131+
typename... ArgsT>
132+
inline bool for_all_types_and_dims(const named_type_pack<Types...> &type_list,
133+
const values_pack<Dims...> &dim_list,
134+
ArgsT &&... args) {
94135
bool passed{true};
95136

96-
// run action for each type from types... parameter pack
97-
size_t typeNameIndex = 0;
98-
99-
int packExpansion[] = {
100-
(passed &= action<types, actionArgsT...>{}(std::forward<argsT>(args)...,
101-
typeList.names[typeNameIndex]),
102-
++typeNameIndex,
103-
0 // Dummy initialization value
104-
)...};
105-
// Every initializer clause is sequenced before any initializer clause that
106-
// follows it in the braced-init-list. Every expression in comma operator is
107-
// also strictly sequenced. So we can use increment safely. We still should
108-
// discard dummy results, but this initialization should not be optimized out
109-
// due side-effects
110-
static_cast<void>(packExpansion);
137+
// Run action for each value from values_pack... parameter pack
138+
((passed &= for_all_types<Action, Dims, ActionArgsT...>(
139+
type_list, std::forward<ArgsT>(args)...)),
140+
...);
141+
// The unary right fold expression is used for parameter pack expansion.
142+
// An additional pair of brackets is required because of precedence of any
143+
// operator relatively to the comma operator.
111144

112145
return passed;
113146
}

0 commit comments

Comments
 (0)