|
| 1 | +//===--- test_sycl_context_interface.cpp - Test cases for device interface ===// |
| 2 | +// |
| 3 | +// Data Parallel Control (dpCtl) |
| 4 | +// |
| 5 | +// Copyright 2020-2021 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 | +/// dpctl_sycl_context_interface.h. |
| 24 | +/// |
| 25 | +//===----------------------------------------------------------------------===// |
| 26 | + |
| 27 | +#include "Support/CBindingWrapping.h" |
| 28 | +#include "dpctl_sycl_context_interface.h" |
| 29 | +#include "dpctl_sycl_device_interface.h" |
| 30 | +#include "dpctl_sycl_device_selector_interface.h" |
| 31 | +#include "dpctl_sycl_types.h" |
| 32 | +#include <CL/sycl.hpp> |
| 33 | +#include <gtest/gtest.h> |
| 34 | + |
| 35 | +using namespace cl::sycl; |
| 36 | + |
| 37 | +namespace |
| 38 | +{ |
| 39 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::device, DPCTLSyclDeviceRef); |
| 40 | +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(vector_class<DPCTLSyclDeviceRef>, |
| 41 | + DPCTLDeviceVectorRef) |
| 42 | +} // namespace |
| 43 | + |
| 44 | +struct TestDPCTLContextInterface : public ::testing::TestWithParam<const char *> |
| 45 | +{ |
| 46 | + DPCTLSyclDeviceSelectorRef DSRef = nullptr; |
| 47 | + |
| 48 | + TestDPCTLContextInterface() |
| 49 | + { |
| 50 | + EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLFilterSelector_Create(GetParam())); |
| 51 | + } |
| 52 | + |
| 53 | + void SetUp() |
| 54 | + { |
| 55 | + if (!DSRef) { |
| 56 | + auto message = "Skipping as no device of type " + |
| 57 | + std::string(GetParam()) + "."; |
| 58 | + GTEST_SKIP_(message.c_str()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + ~TestDPCTLContextInterface() |
| 63 | + { |
| 64 | + EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef)); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +TEST_P(TestDPCTLContextInterface, Chk_Create) |
| 69 | +{ |
| 70 | + DPCTLSyclContextRef CRef = nullptr; |
| 71 | + DPCTLSyclDeviceRef DRef = nullptr; |
| 72 | + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); |
| 73 | + if (!DRef) |
| 74 | + GTEST_SKIP_("Device not found"); |
| 75 | + EXPECT_NO_FATAL_FAILURE(CRef = DPCTLContext_Create(DRef, nullptr, 0)); |
| 76 | + ASSERT_TRUE(CRef); |
| 77 | + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); |
| 78 | + EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef)); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_P(TestDPCTLContextInterface, Chk_CreateWithDevices) |
| 82 | +{ |
| 83 | + size_t nCUs = 0; |
| 84 | + DPCTLSyclContextRef CRef = nullptr; |
| 85 | + DPCTLSyclDeviceRef DRef = nullptr; |
| 86 | + DPCTLDeviceVectorRef DVRef = nullptr; |
| 87 | + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); |
| 88 | + if (!DRef) |
| 89 | + GTEST_SKIP_("Device not found"); |
| 90 | + |
| 91 | + /* TODO: Once we have wrappers for sub-device creation let us use those |
| 92 | + * functions. |
| 93 | + */ |
| 94 | + EXPECT_NO_FATAL_FAILURE(nCUs = DPCTLDevice_GetMaxComputeUnits(DRef)); |
| 95 | + if (nCUs) { |
| 96 | + auto D = unwrap(DRef); |
| 97 | + try { |
| 98 | + auto subDevices = D->create_sub_devices< |
| 99 | + info::partition_property::partition_equally>(nCUs / 2); |
| 100 | + EXPECT_NO_FATAL_FAILURE(DVRef = DPCTLDeviceVector_Create()); |
| 101 | + for (auto &sd : subDevices) { |
| 102 | + unwrap(DVRef)->emplace_back(wrap(new device(sd))); |
| 103 | + } |
| 104 | + EXPECT_NO_FATAL_FAILURE( |
| 105 | + CRef = DPCTLContext_CreateFromDevices(DVRef, nullptr, 0)); |
| 106 | + ASSERT_TRUE(CRef); |
| 107 | + } catch (feature_not_supported const &fnse) { |
| 108 | + GTEST_SKIP_("Skipping creating context for sub-devices"); |
| 109 | + } |
| 110 | + } |
| 111 | + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); |
| 112 | + EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef)); |
| 113 | + EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef)); |
| 114 | +} |
| 115 | + |
| 116 | +TEST_P(TestDPCTLContextInterface, Chk_AreEq) |
| 117 | +{ |
| 118 | + DPCTLSyclContextRef CRef1 = nullptr, CRef2 = nullptr, CRef3 = nullptr; |
| 119 | + DPCTLSyclDeviceRef DRef = nullptr; |
| 120 | + bool are_eq = true, are_not_eq = false; |
| 121 | + |
| 122 | + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); |
| 123 | + if (!DRef) |
| 124 | + GTEST_SKIP_("Device not found"); |
| 125 | + EXPECT_NO_FATAL_FAILURE(CRef1 = DPCTLContext_Create(DRef, nullptr, 0)); |
| 126 | + EXPECT_NO_FATAL_FAILURE(CRef2 = DPCTLContext_Copy(CRef1)); |
| 127 | + // TODO: This work till DPC++ does not have a default context per device, |
| 128 | + // after that we need to change the test case some how. |
| 129 | + EXPECT_NO_FATAL_FAILURE(CRef3 = DPCTLContext_Create(DRef, nullptr, 0)); |
| 130 | + ASSERT_TRUE(CRef3); |
| 131 | + ASSERT_TRUE(CRef2); |
| 132 | + ASSERT_TRUE(CRef1); |
| 133 | + |
| 134 | + EXPECT_NO_FATAL_FAILURE(are_eq = DPCTLContext_AreEq(CRef1, CRef2)); |
| 135 | + EXPECT_NO_FATAL_FAILURE(are_not_eq = DPCTLContext_AreEq(CRef1, CRef3)); |
| 136 | + EXPECT_TRUE(are_eq); |
| 137 | + EXPECT_FALSE(are_not_eq); |
| 138 | + |
| 139 | + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); |
| 140 | + EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef1)); |
| 141 | + EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef2)); |
| 142 | + EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef3)); |
| 143 | +} |
| 144 | + |
| 145 | +TEST_P(TestDPCTLContextInterface, Chk_IsHost) |
| 146 | +{ |
| 147 | + DPCTLSyclContextRef CRef = nullptr; |
| 148 | + DPCTLSyclDeviceRef DRef = nullptr; |
| 149 | + bool is_host_device = false, is_host_context = false; |
| 150 | + |
| 151 | + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); |
| 152 | + if (!DRef) |
| 153 | + GTEST_SKIP_("Device not found"); |
| 154 | + EXPECT_NO_FATAL_FAILURE(CRef = DPCTLContext_Create(DRef, nullptr, 0)); |
| 155 | + ASSERT_TRUE(CRef); |
| 156 | + |
| 157 | + EXPECT_NO_FATAL_FAILURE(is_host_device = DPCTLDevice_IsHost(DRef)); |
| 158 | + EXPECT_NO_FATAL_FAILURE(is_host_context = DPCTLContext_IsHost(CRef)); |
| 159 | + EXPECT_TRUE(is_host_device == is_host_context); |
| 160 | + |
| 161 | + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); |
| 162 | + EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef)); |
| 163 | +} |
| 164 | + |
| 165 | +TEST_P(TestDPCTLContextInterface, Chk_GetBackend) |
| 166 | +{ |
| 167 | + DPCTLSyclContextRef CRef = nullptr; |
| 168 | + DPCTLSyclDeviceRef DRef = nullptr; |
| 169 | + DPCTLSyclBackendType context_backend = DPCTL_UNKNOWN_BACKEND, |
| 170 | + device_backend = DPCTL_UNKNOWN_BACKEND; |
| 171 | + |
| 172 | + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); |
| 173 | + if (!DRef) |
| 174 | + GTEST_SKIP_("Device not found"); |
| 175 | + EXPECT_NO_FATAL_FAILURE(CRef = DPCTLContext_Create(DRef, nullptr, 0)); |
| 176 | + ASSERT_TRUE(CRef); |
| 177 | + |
| 178 | + EXPECT_NO_FATAL_FAILURE(device_backend = DPCTLDevice_GetBackend(DRef)); |
| 179 | + EXPECT_NO_FATAL_FAILURE(context_backend = DPCTLContext_GetBackend(CRef)); |
| 180 | + EXPECT_TRUE(device_backend == context_backend); |
| 181 | + |
| 182 | + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); |
| 183 | + EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef)); |
| 184 | +} |
| 185 | + |
| 186 | +INSTANTIATE_TEST_SUITE_P(DPCTLContextTests, |
| 187 | + TestDPCTLContextInterface, |
| 188 | + ::testing::Values("opencl", |
| 189 | + "opencl:gpu", |
| 190 | + "opencl:cpu", |
| 191 | + "opencl:gpu:0", |
| 192 | + "gpu", |
| 193 | + "cpu", |
| 194 | + "level_zero", |
| 195 | + "level_zero:gpu", |
| 196 | + "opencl:cpu:0", |
| 197 | + "level_zero:gpu:0", |
| 198 | + "gpu:0", |
| 199 | + "gpu:1", |
| 200 | + "1")); |
0 commit comments