This repository was archived by the owner on Jul 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathglobal_memory.cpp
More file actions
170 lines (154 loc) · 4.88 KB
/
Copy pathglobal_memory.cpp
File metadata and controls
170 lines (154 loc) · 4.88 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
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
// ====------ global_memory.cpp---------- -*- C++ -* ----===////
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// ===----------------------------------------------------------------------===//
#define DPCT_NAMED_LAMBDA
#define DPCT_USM_LEVEL_NONE
#include <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
class TestStruct {
public:
void test() {}
template<class T> void testTemplate() {}
};
template<class T>
class TemplateStuct {
public:
void test() {}
template<class Ty> void testTemplate() {}
};
dpct::global_memory<volatile int, 0> d1_a(0);
dpct::global_memory<int, 1> d2_a(36);
dpct::global_memory<TemplateStuct<int>, 0> d3_a;
dpct::global_memory<TestStruct, 0> d4_a;
dpct::constant_memory<int, 1> c1_a(16);
dpct::constant_memory<int, 0> c2_a;
dpct::constant_memory<TemplateStuct<int>, 0> c3_a;
dpct::constant_memory<TestStruct, 0> c4_a;
dpct::constant_memory<int, 2> c_2d_a(sycl::range<2>(5, 3),
{{0, 10, 20},
{30, 40, 50},
{60, 70, 80},
{90, 100, 110},
{120, 130, 140}});
dpct::constant_memory<int, 2> c_2d_b(sycl::range<2>(3, 5),
{{0, 10, 20, 30, 40},
{50, 60, 70, 80, 90},
{100, 110, 120, 130, 140}});
dpct::constant_memory<int, 2> c_2d_c(sycl::range<2>(3, 5),
{0, 10, 20, 30, 40,
50, 60, 70, 80, 90,
100, 110, 120, 130, 140});
dpct::constant_memory<int, 3> c_3d(sycl::range<3>(2, 2, 4),
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
110, 120, 130, 140});
dpct::constant_memory<int, 1> c_1d(sycl::range<1>(15),
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
110, 120, 130, 140});
bool verify_init(int *data) {
for(auto i = 0; i < 15; ++i) {
if (data[i] != i * 10)
return false;
}
return true;
}
bool verify() {
const int size = 15;
auto size_bytes = 15 * sizeof(int);
int h_result[15];
dpct::dpct_memcpy(h_result, c_2d_a.get_ptr(), size_bytes);
if (!verify_init(h_result))
return false;
dpct::dpct_memcpy(h_result, c_2d_b.get_ptr(), size_bytes);
if (!verify_init(h_result))
return false;
dpct::dpct_memcpy(h_result, c_2d_c.get_ptr(), size_bytes);
if (!verify_init(h_result))
return false;
dpct::dpct_memcpy(h_result, c_3d.get_ptr(), size_bytes);
if (!verify_init(h_result))
return false;
dpct::dpct_memcpy(h_result, c_1d.get_ptr(), size_bytes);
if (!verify_init(h_result))
return false;
return true;
}
void test4(TemplateStuct<int> *d3, TestStruct *d4) {
d3->test();
d3->testTemplate<int>();
d4->test();
d4->testTemplate<int>();
}
void test3(TemplateStuct<int> c3, TestStruct c4) {
c3.test();
c3.testTemplate<int>();
c4.test();
c4.testTemplate<int>();
}
void test2(volatile int &a) {
a = 3;
}
void test1(volatile int *acc_d1, int *acc_d2, int const *c1, int c2) {
unsigned d_a = 1;
*acc_d1 = 0;
*acc_d2 = d_a;
unsigned d_c = (unsigned)(*acc_d1);
unsigned *d_d = (unsigned *)acc_d2;
unsigned *d_e = (unsigned *)(acc_d2 + 5);
int *d_f = acc_d2 - 6;
test2(*acc_d1);
}
int main() try {
dpct::get_default_queue().submit(
[&](sycl::handler &cgh) {
d1_a.init();
d2_a.init();
c1_a.init();
c2_a.init();
auto d1_acc = d1_a.get_access(cgh);
auto d2_acc = d2_a.get_access(cgh);
auto c1_acc = c1_a.get_access(cgh);
auto c2_acc = c2_a.get_access(cgh);
cgh.parallel_for<dpct_kernel_name<class kernel_test1>>(
sycl::nd_range<3>(sycl::range<3>(1, 1, 1), sycl::range<3>(1, 1, 1)),
[=] (sycl::nd_item<3> item) {
test1(d1_acc.get_pointer(), d2_acc.get_pointer(), c1_acc.get_pointer(), c2_acc);
});
});
dpct::get_default_queue().submit(
[&](sycl::handler &cgh) {
c3_a.init();
c4_a.init();
auto c3_acc = c3_a.get_access(cgh);
auto c4_acc = c4_a.get_access(cgh);
cgh.parallel_for<dpct_kernel_name<class kernel_test2>>(
sycl::nd_range<3>(sycl::range<3>(1, 1, 1), sycl::range<3>(1, 1, 1)),
[=] (sycl::nd_item<3> item) {
test3(c3_acc, c4_acc);
});
});
sycl::queue *q = dpct::get_current_device().create_queue();
q->submit(
[&](sycl::handler &cgh) {
d3_a.init(*q);
d4_a.init(*q);
auto d3_acc = d3_a.get_access(cgh);
auto d4_acc = d4_a.get_access(cgh);
cgh.parallel_for<dpct_kernel_name<class kernel_test3>>(
sycl::nd_range<3>(sycl::range<3>(1, 1, 1), sycl::range<3>(1, 1, 1)),
[=] (sycl::nd_item<3> item) {
test4(d3_acc.get_pointer(), d4_acc.get_pointer());
});
});
if (verify()) {
printf("Init Constant Memory Success!\n");
} else {
printf("Init Constant Memory Fail!\n");
}
return 0;
}
catch(sycl::exception const &exc){}