Newer
Older
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
#include <CL/sycl.hpp>
#include <array>
#include <iostream>
using namespace cl::sycl;
constexpr int SIZE 5;
constexpr int ITER 2;
int main() {
// Loop through available platforms and devices
for (auto const& this_platform : platform::get_platforms() ) {
std::cout << "Found platform: "
<< this_platform.get_info<info::platform::name>() << "\n";
for (auto const& this_device : this_platform.get_devices() ) {
std::cout << " Device: "
<< this_device.get_info<info::device::name>() << "\n";
}
}
// Create SYCL queue
queue q;
// Running platform and device
std::cout << "Running on platform: "
<< q.get_platform().get_info<info::platform::name>() << "\n";
std::cout << " Device: "
<< q.get_device().get_info<info::device::name>() << "\n";
std::cout << "\n";
// Initialize input array
std::array<float,SIZE*SIZE> input;
std::array<float,SIZE*SIZE> output;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
input[i*SIZE+j] = i + j;
}
}
// Create buffers
buffer<float,2> input_buffer{input.data(),range<2>{SIZE,SIZE}};
buffer<float,2> output_buffer{output.data(),range<2>{SIZE,SIZE}};
// Submit command group for execution
q.submit([&](handler& cgh) {
// Create accessors for input and output buffers
accessor input_accessor(input_buffer, cgh, read_only);
accessor output_accessor(output_buffer, cgh, write_only);
// Define the kernel
cgh.parallel_for(SIZE, [=](id<1> idx) {
output_accessor[idx] = input_accessor[idx] * input_accessor[idx];
});
});
// Wait for the command group to finish
q.wait();
// Print the result
host_accessor output_host_accessor(output_buffer, read_only);
for (int i = 0; i < SIZE; i++) {
std::cout << output_host_accessor[i] << " ";
}
std::cout << std::endl;
return 0;
}