Skip to content

Commit 304067c

Browse files
authored
[SYCL][CUDA] Image Basic Test (#1970)
Provides new generic testing for SYCL image functionality.
1 parent 4644e63 commit 304067c

10 files changed

+818
-7
lines changed

sycl/test/basic_tests/image.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// UNSUPPORTED: cuda
2-
// CUDA cannot support SYCL 1.2.1 images.
3-
//
41
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
52
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
63
// RUN: %CPU_RUN_PLACEHOLDER %t.out
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
#include "image_read.h"
7+
8+
int main() {
9+
10+
s::default_selector selector;
11+
s::queue myQueue(selector);
12+
13+
bool passed = true;
14+
15+
// Float image
16+
if (!test<s::float4, s::image_channel_type::fp32>(myQueue))
17+
passed = false;
18+
19+
// 32-bit signed integer image
20+
if (!test<s::int4, s::image_channel_type::signed_int32>(myQueue))
21+
passed = false;
22+
23+
// 32-bit unsigned integer image
24+
if (!test<s::uint4, s::image_channel_type::unsigned_int32>(myQueue))
25+
passed = false;
26+
27+
return passed ? 0 : -1;
28+
}
+235
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
/**
2+
* Tests clamp_to_edge addressing mode with nearest and linear filtering modes
3+
* on a 4x4 image.
4+
*
5+
* Expected addressing mode and filtering results are given by the algorithm in
6+
* the OpenCL 1.2 specification, Section 8. Image Addressing and Filtering
7+
*
8+
* https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf#page=329
9+
*
10+
* Confirmed to pass with -fsycl-targets=spir64-unknown-linux-sycldevice
11+
*/
12+
13+
#include <CL/sycl.hpp>
14+
15+
#include <iostream>
16+
17+
template <typename T> class test_1d_class;
18+
template <typename T> class test_2d_class;
19+
template <typename T> class test_3d_class;
20+
21+
namespace s = cl::sycl;
22+
23+
template <typename dataT, typename coordT, s::image_channel_type channelType>
24+
bool test1d_coord(s::queue myQueue, dataT *hostPtr, coordT coord,
25+
dataT expectedColour) {
26+
dataT resultData;
27+
28+
{ // Scope everything to force destruction
29+
s::image<1> image(hostPtr, s::image_channel_order::rgba, channelType,
30+
s::range<1>{3});
31+
32+
s::buffer<dataT, 1> resultDataBuf(&resultData, s::range<1>(1));
33+
34+
// Do the test by reading a single pixel from the image
35+
myQueue.submit([&](s::handler &cgh) {
36+
auto imageAcc = image.get_access<dataT, s::access::mode::read>(cgh);
37+
s::accessor<dataT, 1, s::access::mode::write> resultDataAcc(resultDataBuf,
38+
cgh);
39+
40+
cgh.single_task<test_1d_class<dataT>>([=]() {
41+
dataT RetColor = imageAcc.read(coord);
42+
resultDataAcc[0] = RetColor;
43+
});
44+
});
45+
}
46+
#ifdef DEBUG_OUTPUT
47+
std::cout << "Expected: " << expectedColour.r() << ", " << expectedColour.g()
48+
<< ", " << expectedColour.b() << ", " << expectedColour.a() << "\n";
49+
std::cout << "Got: " << resultData.r() << ", " << resultData.g() << ", "
50+
<< resultData.b() << ", " << resultData.a() << "\n";
51+
#endif // DEBUG_OUTPUT
52+
bool correct = true;
53+
if (resultData.r() != expectedColour.r())
54+
correct = false;
55+
if (resultData.g() != expectedColour.g())
56+
correct = false;
57+
if (resultData.b() != expectedColour.b())
58+
correct = false;
59+
if (resultData.a() != expectedColour.a())
60+
correct = false;
61+
return correct;
62+
}
63+
64+
template <typename dataT, typename coordT, s::image_channel_type channelType>
65+
bool test2d_coord(s::queue myQueue, dataT *hostPtr, coordT coord,
66+
dataT expectedColour) {
67+
dataT resultData;
68+
69+
{ // Scope everything to force destruction
70+
s::image<2> image(hostPtr, s::image_channel_order::rgba, channelType,
71+
s::range<2>{3, 3});
72+
73+
s::buffer<dataT, 1> resultDataBuf(&resultData, s::range<1>(1));
74+
75+
// Do the test by reading a single pixel from the image
76+
myQueue.submit([&](s::handler &cgh) {
77+
auto imageAcc = image.get_access<dataT, s::access::mode::read>(cgh);
78+
s::accessor<dataT, 1, s::access::mode::write> resultDataAcc(resultDataBuf,
79+
cgh);
80+
81+
cgh.single_task<test_2d_class<dataT>>([=]() {
82+
dataT RetColor = imageAcc.read(coord);
83+
resultDataAcc[0] = RetColor;
84+
});
85+
});
86+
}
87+
88+
#ifdef DEBUG_OUTPUT
89+
std::cout << "Got: " << resultData.r() << ", " << resultData.g();
90+
#endif // DEBUG_OUTPUT
91+
92+
bool correct = true;
93+
if (resultData.r() != expectedColour.r())
94+
correct = false;
95+
if (resultData.g() != expectedColour.g())
96+
correct = false;
97+
if (resultData.b() != expectedColour.b())
98+
correct = false;
99+
if (resultData.a() != expectedColour.a())
100+
correct = false;
101+
return correct;
102+
}
103+
104+
template <typename dataT, typename coordT, s::image_channel_type channelType>
105+
bool test3d_coord(s::queue myQueue, dataT *hostPtr, coordT coord,
106+
dataT expectedColour) {
107+
dataT resultData;
108+
109+
{ // Scope everything to force destruction
110+
s::image<3> image(hostPtr, s::image_channel_order::rgba, channelType,
111+
s::range<3>{3, 3, 3});
112+
113+
s::buffer<dataT, 1> resultDataBuf(&resultData, s::range<1>(1));
114+
115+
// Do the test by reading a single pixel from the image
116+
myQueue.submit([&](s::handler &cgh) {
117+
auto imageAcc = image.get_access<dataT, s::access::mode::read>(cgh);
118+
s::accessor<dataT, 1, s::access::mode::write> resultDataAcc(resultDataBuf,
119+
cgh);
120+
121+
cgh.single_task<test_3d_class<dataT>>([=]() {
122+
dataT RetColor = imageAcc.read(coord);
123+
resultDataAcc[0] = RetColor;
124+
});
125+
});
126+
}
127+
#ifdef DEBUG_OUTPUT
128+
std::cout << "Expected: " << expectedColour.r() << ", " << expectedColour.g()
129+
<< ", " << expectedColour.b() << ", " << expectedColour.a() << "\n";
130+
std::cout << "Got: " << resultData.r() << ", " << resultData.g() << ", "
131+
<< resultData.b() << ", " << resultData.a() << "\n";
132+
#endif // DEBUG_OUTPUT
133+
bool correct = true;
134+
if (resultData.r() != expectedColour.r())
135+
correct = false;
136+
if (resultData.g() != expectedColour.g())
137+
correct = false;
138+
if (resultData.b() != expectedColour.b())
139+
correct = false;
140+
if (resultData.a() != expectedColour.a())
141+
correct = false;
142+
return correct;
143+
}
144+
145+
template <typename dataT, typename coordT, s::image_channel_type channelType>
146+
bool test1d(s::queue myQueue, coordT coord, dataT expectedResult) {
147+
dataT hostPtr[3];
148+
for (int i = 0; i < 3; i++)
149+
hostPtr[i] = dataT(0 + i, 20 + i, 40 + i, 60 + i);
150+
151+
return test1d_coord<dataT, coordT, channelType>(myQueue, hostPtr, coord,
152+
expectedResult);
153+
}
154+
155+
template <typename dataT, typename coordT, s::image_channel_type channelType>
156+
bool test2d(s::queue myQueue, coordT coord, dataT expectedResult) {
157+
dataT hostPtr[9];
158+
for (int i = 0; i < 9; i++)
159+
hostPtr[i] = dataT(0 + i, 20 + i, 40 + i, 60 + i);
160+
161+
return test2d_coord<dataT, coordT, channelType>(myQueue, hostPtr, coord,
162+
expectedResult);
163+
}
164+
165+
template <typename dataT, typename coordT, s::image_channel_type channelType>
166+
bool test3d(s::queue myQueue, coordT coord, dataT expectedResult) {
167+
dataT hostPtr[27];
168+
for (int i = 0; i < 27; i++)
169+
hostPtr[i] = dataT(0 + i, 20 + i, 40 + i, 60 + i);
170+
171+
return test3d_coord<dataT, coordT, channelType>(myQueue, hostPtr, coord,
172+
expectedResult);
173+
}
174+
175+
template <typename dataT, s::image_channel_type channelType>
176+
bool test(s::queue myQueue) {
177+
bool passed = true;
178+
// 1d image tests
179+
if (!test1d<dataT, int, channelType>(myQueue, 0, dataT(0, 20, 40, 60)))
180+
passed = false;
181+
182+
if (!test1d<dataT, int, channelType>(myQueue, 1, dataT(1, 21, 41, 61)))
183+
passed = false;
184+
185+
if (!test1d<dataT, int, channelType>(myQueue, 2, dataT(2, 22, 42, 62)))
186+
passed = false;
187+
188+
// 2d image tests
189+
if (!test2d<dataT, s::int2, channelType>(myQueue, s::int2(0, 0),
190+
dataT(0, 20, 40, 60)))
191+
passed = false;
192+
193+
if (!test2d<dataT, s::int2, channelType>(myQueue, s::int2(1, 0),
194+
dataT(1, 21, 41, 61)))
195+
passed = false;
196+
197+
if (!test2d<dataT, s::int2, channelType>(myQueue, s::int2(0, 1),
198+
dataT(3, 23, 43, 63)))
199+
passed = false;
200+
201+
if (!test2d<dataT, s::int2, channelType>(myQueue, s::int2(1, 1),
202+
dataT(4, 24, 44, 64)))
203+
passed = false;
204+
205+
// 3d image tests
206+
if (!test3d<dataT, s::int4, channelType>(myQueue, s::int4(0, 0, 0, 0),
207+
dataT(0, 20, 40, 60)))
208+
passed = false;
209+
210+
if (!test3d<dataT, s::int4, channelType>(myQueue, s::int4(1, 0, 0, 0),
211+
dataT(1, 21, 41, 61)))
212+
passed = false;
213+
214+
if (!test3d<dataT, s::int4, channelType>(myQueue, s::int4(0, 1, 0, 0),
215+
dataT(3, 23, 43, 63)))
216+
passed = false;
217+
218+
if (!test3d<dataT, s::int4, channelType>(myQueue, s::int4(1, 1, 0, 0),
219+
dataT(4, 24, 44, 64)))
220+
passed = false;
221+
222+
if (!test3d<dataT, s::int4, channelType>(myQueue, s::int4(1, 0, 1, 0),
223+
dataT(10, 30, 50, 70)))
224+
passed = false;
225+
226+
if (!test3d<dataT, s::int4, channelType>(myQueue, s::int4(0, 1, 1, 0),
227+
dataT(12, 32, 52, 72)))
228+
passed = false;
229+
230+
if (!test3d<dataT, s::int4, channelType>(myQueue, s::int4(1, 1, 1, 0),
231+
dataT(13, 33, 53, 73)))
232+
passed = false;
233+
234+
return passed;
235+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
6+
#include "image_read.h"
7+
8+
int main() {
9+
s::default_selector selector;
10+
s::queue myQueue(selector);
11+
12+
// Device doesn't support cl_khr_fp16 extension - skip.
13+
if (!myQueue.get_device().has_extension("cl_khr_fp16"))
14+
return 0;
15+
16+
// Half image
17+
if (!test<s::half4, s::image_channel_type::fp16>(myQueue))
18+
return -1;
19+
20+
return 0;
21+
}

0 commit comments

Comments
 (0)