forked from KhronosGroup/SyclParallelSTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor_each_n.cpp
135 lines (106 loc) · 4.35 KB
/
for_each_n.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Copyright (c) 2015 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and/or associated documentation files (the
"Materials"), to deal in the Materials without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Materials, and to
permit persons to whom the Materials are furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Materials.
MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
https://www.khronos.org/registry/
THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#include "gmock/gmock.h"
#include <vector>
#include <algorithm>
#include <sycl/execution_policy>
#include <experimental/algorithm>
struct ForEachNAlgorithm : public testing::Test {};
using namespace std::experimental::parallel;
TEST_F(ForEachNAlgorithm, TestStdForEachN) {
std::vector<int> v = {2, 1, 3, 4, 8};
std::vector<int> result = {3, 2, 4, 5, 9};
for_each_n(v.begin(), v.size(), [=](int &val) { val--; });
int adder = 2;
for_each_n(v.begin(), v.size(), [=](int &val) { val += adder; });
#if PRINT_OUTPUT
std::cout << " Elements " << std::endl;
std::for_each(v.begin(), v.end(),
[=](int elem) { std::cout << elem << std::endl; });
#endif // PRINT_OUTPUT
EXPECT_TRUE(std::equal(v.begin(), v.end(), result.begin()));
}
TEST_F(ForEachNAlgorithm, TestStd2ForEachN) {
std::vector<int> v = {2, 1, 3, 4, 8};
std::vector<int> result = {3, 2, 4, 5, 9};
auto iterator1 = for_each_n(v.begin(), v.size(), [=](int &val) { val--; });
for_each_n(v.begin(), v.size(), [=](int &val) { val += 2; });
EXPECT_TRUE(v.end() == iterator1);
}
TEST_F(ForEachNAlgorithm, TestStd3ForEachN) {
std::vector<int> v = {2, 1, 3, 4, 8};
std::vector<int> result = {3, 2, 4, 5, 9};
int minus_size = -v.size();
auto iterator1 = for_each_n(v.begin(), minus_size, [=](int &val) { val++; });
EXPECT_TRUE(v.begin() == iterator1);
}
TEST_F(ForEachNAlgorithm, TestSyclForEachN) {
std::vector<int> v = {2, 1, 3, 4, 8};
std::vector<int> result = {3, 2, 4, 5, 7};
cl::sycl::queue q;
sycl::sycl_execution_policy<class ForEachNAlgorithm> snp(q);
int threshold = 5;
int adder = 1;
for_each_n(snp, v.begin(), v.size(), [=](int &val) {
if (val > threshold) {
val -= adder;
} else {
val += adder;
}
});
#if PRINT_OUTPUT
std::cout << " Elements " << std::endl;
std::for_each(v.begin(), v.end(),
[=](int elem) { std::cout << elem << std::endl; });
#endif // PRINT_OUTPUT
EXPECT_TRUE(std::equal(v.begin(), v.end(), result.begin()));
}
TEST_F(ForEachNAlgorithm, TestSycl2ForEachN) {
std::array<int, 5> v = {2, 1, 3, 4, 8};
cl::sycl::queue q;
sycl::sycl_execution_policy<class ForEachN2Algorithm> snp(q);
int adder = 1;
auto iterator1 =
for_each_n(snp, v.begin(), v.size(), [=](int &val) { val = adder; });
#if PRINT_OUTPUT
std::cout << " Elements " << std::endl;
std::for_each(v.begin(), v.end(),
[=](int elem) { std::cout << elem << std::endl; });
#endif // PRINT_OUTPUT
EXPECT_TRUE(v.end() == iterator1);
}
TEST_F(ForEachNAlgorithm, TestSycl3ForEachN) {
std::vector<int> v = {2, 1, 3, 4, 8};
std::vector<int> result = {3, 2, 4, 5, 9};
cl::sycl::queue q;
sycl::sycl_execution_policy<class ForEachN3Algorithm> snp(q);
int minus_size = -v.size();
auto iterator1 =
for_each_n(snp, v.begin(), minus_size, [=](int &val) { val++; });
#if PRINT_OUTPUT
std::cout << " Elements " << std::endl;
std::for_each(v.begin(), v.end(),
[=](int elem) { std::cout << elem << std::endl; });
#endif // PRINT_OUTPUT
EXPECT_TRUE(v.begin() == iterator1);
}