This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathuse-onchip-memory.cpp
More file actions
115 lines (102 loc) · 4.07 KB
/
Copy pathuse-onchip-memory.cpp
File metadata and controls
115 lines (102 loc) · 4.07 KB
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
/***************************************************************************
*
* Copyright Codeplay Software Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* For your convenience, a copy of the License has been included in this
* repository.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Codeplay's ComputeCpp SDK
*
* use-onchip-memory.cpp
*
* Description:
* Sample code that demonstrates the use of the use_onchip_memory extension
* to SYCL provided by ComputeCpp.
*
**************************************************************************/
#include <CL/sycl.hpp>
#include <SYCL/codeplay.hpp>
#include <iostream>
namespace sycl = cl::sycl;
namespace access = sycl::access;
namespace sycl_kernel {
// This kernel performs the same operation as std::iota, but also scales
// the result by two.
template <class>
class scaled_iota;
} // namespace sycl_kernel
namespace codeplay = sycl::codeplay;
template <class Policy>
void use_with_policy(Policy policy, sycl::queue& queue) {
auto hostData = sycl::vector_class<int>(1024);
{
auto taskContext = queue.get_context();
// Notice that the on_chip_memory property takes a policy argument: this is
// used to indicate whether the property is advantageous or genuinely
// necessary.
auto deviceData = sycl::buffer<int, 1>{
hostData.data(), sycl::range<1>(hostData.size()),
sycl::property_list{
sycl::property::buffer::context_bound(taskContext),
codeplay::property::buffer::use_onchip_memory(policy)}};
queue.submit([&](sycl::handler& cgh) {
static constexpr size_t dims = 2;
auto r =
sycl::nd_range<dims>{sycl::range<dims>{hostData.size() / dims, dims},
sycl::range<dims>{dims, 1}};
const auto access =
deviceData.get_access<access::mode::discard_write>(cgh);
cgh.parallel_for<sycl_kernel::scaled_iota<Policy>>(
r, [=](sycl::nd_item<2> id) noexcept {
const auto linearId = id.get_global_linear_id();
access[linearId] = linearId * 2;
});
});
queue.wait_and_throw();
}
}
// use_onchip_memory has two different enabling mechanisms: the first is to
// indicate that a policy is preferred. Using this policy means that if the
// system supports the feature, then the feature will be enabled. If the
// feature is not present on the system, then it will not be enabled.
//
// Puns aside, this is the preferred default.
void how_to_use_with_prefer(sycl::queue& queue) {
::use_with_policy(codeplay::property::prefer, queue);
}
// Alternatively, if you can guarantee that your system will support this
// policy, or if it is expected any system using your software must support
// the policy, then you can use the require tag to indicate that the feature
// is required by your software.
//
// In the event that the property isn't supported, a SYCL exception will be
// thrown.
void how_to_use_with_require(sycl::queue& queue) {
try {
::use_with_policy(codeplay::property::require, queue);
} catch (const sycl::exception& e) {
std::cerr << "An error occurred: " << e.what()
<< "\n\nThis particular error has occurred because you are "
"requiring the policy use_onchip_memory be available, and "
"your hardware doesn't support the use_onchip_memory, so the "
"SYCL implementation will raise an error.\n";
}
}
int main() {
auto queue = sycl::queue{};
// Using the on-chip memory policy with the require tag.
how_to_use_with_require(queue);
// Using the on-chip memory policy with the prefer tag.
how_to_use_with_prefer(queue);
}