Skip to content

Commit cebc064

Browse files
Introduced tests/test_sycl_usm_interface.cpp
Added DPPLDevice_AreEq to check if two devices are pointer equal. Used in the test.
1 parent 242eb3a commit cebc064

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

backends/include/dppl_sycl_device_interface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,15 @@ DPPLDevice_GetVendorName (__dppl_keep const DPPLSyclDeviceRef DRef);
133133
DPPL_API
134134
bool DPPLDevice_IsHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef);
135135

136+
/*!
137+
* @brief Checks if two DPPLSyclDeviceRef objects point to the same
138+
* sycl::device.
139+
*
140+
* @param DevRef1 First opaque pointer to the sycl device.
141+
* @param DevRef2 Second opaque pointer to the sycl device.
142+
* @return True if the underlying sycl::device are same, false otherwise.
143+
*/
144+
DPPL_API
145+
bool DPPLDevice_AreEq (__dppl_keep const DPPLSyclDeviceRef DevRef1,
146+
__dppl_keep const DPPLSyclDeviceRef DevRef2);
136147
DPPL_C_EXTERN_C_END

backends/source/dppl_sycl_device_interface.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,12 @@ bool DPPLDevice_IsHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef)
153153
{
154154
return unwrap(DRef)->get_info<info::device::host_unified_memory>();
155155
}
156+
157+
bool DPPLDevice_AreEq(__dppl_keep const DPPLSyclDeviceRef DevRef1,
158+
__dppl_keep const DPPLSyclDeviceRef DevRef2)
159+
{
160+
if(!(DevRef1 && DevRef2))
161+
// \todo handle error
162+
return false;
163+
return (*unwrap(DevRef1) == *unwrap(DevRef2));
164+
}

backends/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ else()
2828
test_sycl_program_interface
2929
test_sycl_queue_interface
3030
test_sycl_queue_manager
31+
test_sycl_usm_interface
3132
)
3233

3334
# Copy the spir-v input files to test build directory
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//===-------- test_sycl_usm_interface.cpp - dpctl-C_API ---*--- C++ --*--===//
2+
//
3+
// Data Parallel Control Library (dpCtl)
4+
//
5+
// Copyright 2020 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file has unit test cases for functions defined in
23+
/// dppl_sycl_usm_interface.h.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#include "dppl_sycl_context_interface.h"
28+
#include "dppl_sycl_device_interface.h"
29+
#include "dppl_sycl_event_interface.h"
30+
#include "dppl_sycl_queue_interface.h"
31+
#include "dppl_sycl_queue_manager.h"
32+
#include "dppl_sycl_usm_interface.h"
33+
#include "Support/CBindingWrapping.h"
34+
#include <CL/sycl.hpp>
35+
#include <gtest/gtest.h>
36+
37+
using namespace cl::sycl;
38+
39+
namespace
40+
{
41+
constexpr size_t SIZE = 1024;
42+
43+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPPLSyclUSMRef);
44+
45+
bool has_devices ()
46+
{
47+
bool ret = false;
48+
for (auto &p : platform::get_platforms()) {
49+
if (p.is_host())
50+
continue;
51+
if(!p.get_devices().empty()) {
52+
ret = true;
53+
break;
54+
}
55+
}
56+
return ret;
57+
}
58+
59+
void common_test_body(size_t nbytes, const DPPLSyclUSMRef Ptr, const DPPLSyclQueueRef Q, const char *expected) {
60+
61+
auto Ctx = DPPLQueue_GetContext(Q);
62+
63+
auto kind = DPPLUSM_GetPointerType(Ptr, Ctx);
64+
EXPECT_TRUE(0 == std::strncmp(kind, expected, 4));
65+
66+
auto Dev = DPPLUSM_GetPointerDevice(Ptr, Ctx);
67+
auto QueueDev = DPPLQueue_GetDevice(Q);
68+
EXPECT_TRUE(DPPLDevice_AreEq(Dev, QueueDev));
69+
70+
DPPLQueue_Prefetch(Q, Ptr, nbytes);
71+
}
72+
73+
}
74+
75+
struct TestDPPLSyclUSMInterface : public ::testing::Test
76+
{
77+
78+
TestDPPLSyclUSMInterface ()
79+
{ }
80+
81+
~TestDPPLSyclUSMInterface ()
82+
{ }
83+
};
84+
85+
TEST_F(TestDPPLSyclUSMInterface, MallocShared)
86+
{
87+
if (!has_devices())
88+
GTEST_SKIP_("Skipping: No Sycl Devices.\n");
89+
90+
auto Q = DPPLQueueMgr_GetCurrentQueue();
91+
const size_t nbytes = 1024;
92+
93+
auto Ptr = DPPLmalloc_shared(nbytes, Q);
94+
EXPECT_TRUE(bool(Ptr));
95+
96+
common_test_body(nbytes, Ptr, Q, "shared");
97+
DPPLfree_with_queue(Ptr, Q);
98+
}
99+
100+
TEST_F(TestDPPLSyclUSMInterface, MallocDevice)
101+
{
102+
if (!has_devices())
103+
GTEST_SKIP_("Skipping: No Sycl Devices.\n");
104+
105+
auto Q = DPPLQueueMgr_GetCurrentQueue();
106+
const size_t nbytes = 1024;
107+
108+
auto Ptr = DPPLmalloc_device(nbytes, Q);
109+
EXPECT_TRUE(bool(Ptr));
110+
111+
common_test_body(nbytes, Ptr, Q, "device");
112+
DPPLfree_with_queue(Ptr, Q);
113+
}
114+
115+
TEST_F(TestDPPLSyclUSMInterface, MallocHost)
116+
{
117+
if (!has_devices())
118+
GTEST_SKIP_("Skipping: No Sycl Devices.\n");
119+
120+
auto Q = DPPLQueueMgr_GetCurrentQueue();
121+
const size_t nbytes = 1024;
122+
123+
auto Ptr = DPPLmalloc_host(nbytes, Q);
124+
EXPECT_TRUE(bool(Ptr));
125+
126+
common_test_body(nbytes, Ptr, Q, "host");
127+
DPPLfree_with_queue(Ptr, Q);
128+
}
129+
130+
TEST_F(TestDPPLSyclUSMInterface, AlignedAllocShared)
131+
{
132+
if (!has_devices())
133+
GTEST_SKIP_("Skipping: No Sycl Devices.\n");
134+
135+
auto Q = DPPLQueueMgr_GetCurrentQueue();
136+
const size_t nbytes = 1024;
137+
138+
auto Ptr = DPPLaligned_alloc_shared(64, nbytes, Q);
139+
EXPECT_TRUE(bool(Ptr));
140+
141+
common_test_body(nbytes, Ptr, Q, "shared");
142+
DPPLfree_with_queue(Ptr, Q);
143+
}
144+
145+
TEST_F(TestDPPLSyclUSMInterface, AlignedAllocDevice)
146+
{
147+
if (!has_devices())
148+
GTEST_SKIP_("Skipping: No Sycl Devices.\n");
149+
150+
auto Q = DPPLQueueMgr_GetCurrentQueue();
151+
const size_t nbytes = 1024;
152+
153+
auto Ptr = DPPLaligned_alloc_device(64, nbytes, Q);
154+
EXPECT_TRUE(bool(Ptr));
155+
156+
common_test_body(nbytes, Ptr, Q, "device");
157+
DPPLfree_with_queue(Ptr, Q);
158+
}
159+
160+
TEST_F(TestDPPLSyclUSMInterface, AlignedAllocHost)
161+
{
162+
if (!has_devices())
163+
GTEST_SKIP_("Skipping: No Sycl Devices.\n");
164+
165+
auto Q = DPPLQueueMgr_GetCurrentQueue();
166+
const size_t nbytes = 1024;
167+
168+
auto Ptr = DPPLaligned_alloc_host(64, nbytes, Q);
169+
EXPECT_TRUE(bool(Ptr));
170+
171+
common_test_body(nbytes, Ptr, Q, "host");
172+
DPPLfree_with_queue(Ptr, Q);
173+
}
174+
175+
int
176+
main (int argc, char** argv)
177+
{
178+
::testing::InitGoogleTest(&argc, argv);
179+
int ret = RUN_ALL_TESTS();
180+
return ret;
181+
}

0 commit comments

Comments
 (0)