forked from codeplaysoftware/syclacademy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
174 lines (131 loc) · 4.72 KB
/
solution.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
SYCL Academy (c)
SYCL Academy is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License.
You should have received a copy of the license along with this
work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
*/
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <benchmark.h>
#include <iostream>
#include <iterator>
#include <numeric>
#include <CL/sycl.hpp>
class naive;
class local_mem;
static constexpr int WIDTH = 128;
static constexpr int HEIGHT = 128;
static constexpr int WORK_GROUP_WIDTH = 16;
static constexpr int WORK_GROUP_HEIGHT = 16;
template <typename T, int W, int H>
class matrix {
public:
void print() const noexcept {
for (int r = 0; r < H; ++r) {
for (int c = 0; c < W; ++c) {
std::cout << data_[(r * W) + c] << ", ";
}
std::cout << "\n";
}
std::cout << "\n";
}
T* data() noexcept { return data_; }
T* begin() noexcept { return data_; }
T* end() noexcept { return &(data_[W * H]); }
size_t size() const noexcept { return W * H; }
size_t width() const noexcept { return W; }
size_t height() const noexcept { return H; }
private:
T data_[W * H];
};
TEST_CASE("naive", "sycl_06_matrix_transpose") {
auto inputMat = matrix<float, WIDTH, HEIGHT>{};
auto outputMat = matrix<float, WIDTH, HEIGHT>{};
std::iota(inputMat.begin(), inputMat.end(), 0.0f);
std::fill(outputMat.begin(), outputMat.end(), 0.0f);
// inputMat.print();
// outputMat.print();
cl::sycl::queue defaultQueue;
{
cl::sycl::buffer<float, 1> inputMatBuf(inputMat.data(), inputMat.size());
cl::sycl::buffer<float, 1> outputMatBuf(outputMat.data(), outputMat.size());
cppcon::benchmark(
[&]() {
defaultQueue.submit([&](cl::sycl::handler& cgh) {
auto inputMatAcc =
inputMatBuf.template get_access<cl::sycl::access::mode::read>(
cgh);
auto outputMatAcc =
outputMatBuf.template get_access<cl::sycl::access::mode::write>(
cgh);
const auto width = inputMat.width();
const auto height = inputMat.height();
cgh.parallel_for<naive>(
cl::sycl::range<2>(width, height),
[=](cl::sycl::id<2> idx) {
auto rowMajorId = (idx[1] * width) + idx[0];
auto columnMajorId = (idx[0] * height) + idx[1];
outputMatAcc[rowMajorId] = inputMatAcc[columnMajorId];
});
});
defaultQueue.wait_and_throw();
},
100, "naive");
}
// inputMat.print();
// outputMat.print();
REQUIRE(true);
}
TEST_CASE("local_mem", "sycl_06_matrix_transpose") {
auto inputMat = matrix<float, WIDTH, HEIGHT>{};
auto outputMat = matrix<float, WIDTH, HEIGHT>{};
std::iota(inputMat.begin(), inputMat.end(), 0.0f);
std::fill(outputMat.begin(), outputMat.end(), 0.0f);
// inputMat.print();
// outputMat.print();
cl::sycl::queue defaultQueue;
{
cl::sycl::buffer<float, 1> inputMatBuf(inputMat.data(), inputMat.size());
cl::sycl::buffer<float, 1> outputMatBuf(outputMat.data(), outputMat.size());
cppcon::benchmark(
[&]() {
defaultQueue.submit([&](cl::sycl::handler& cgh) {
auto inputMatAcc =
inputMatBuf.template get_access<cl::sycl::access::mode::read>(
cgh);
auto outputMatAcc =
outputMatBuf.template get_access<cl::sycl::access::mode::write>(
cgh);
auto scratchpad =
cl::sycl::accessor<float, 1, cl::sycl::access::mode::read_write,
cl::sycl::access::target::local>(
cl::sycl::range<1>(WORK_GROUP_WIDTH * WORK_GROUP_HEIGHT),
cgh);
cgh.parallel_for<local_mem>(
cl::sycl::nd_range<2>(
cl::sycl::range<2>(inputMat.width(), inputMat.height()),
cl::sycl::range<2>(WORK_GROUP_WIDTH, WORK_GROUP_HEIGHT)),
[=](cl::sycl::nd_item<2> item) {
auto columnMajorId =
(item.get_global_id(1) * item.get_global_range(0)) +
item.get_global_id(0);
auto rowMajorLocalId =
(item.get_local_id(0) * item.get_local_range(1)) +
item.get_local_id(1);
auto columnMajorLocalId =
(item.get_local_id(1) * item.get_local_range(0)) +
item.get_local_id(0);
scratchpad[columnMajorLocalId] = inputMatAcc[columnMajorId];
item.barrier(cl::sycl::access::fence_space::global_and_local);
outputMatAcc[columnMajorId] = scratchpad[rowMajorLocalId];
});
});
defaultQueue.wait_and_throw();
},
100, "local_mem");
}
// inputMat.print();
// outputMat.print();
REQUIRE(true);
}