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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
|
/*
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/device/device.h"
#include "shared/source/helpers/gfx_core_helper.h"
#include "shared/source/helpers/local_memory_access_modes.h"
#include "shared/source/memory_manager/unified_memory_manager.h"
#include "shared/source/memory_manager/usm_pool_params.h"
#include "shared/source/os_interface/device_factory.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/libult/ult_command_stream_receiver.h"
#include "shared/test/common/mocks/mock_deferred_deleter.h"
#include "shared/test/common/mocks/mock_device.h"
#include "shared/test/common/mocks/mock_memory_manager.h"
#include "shared/test/common/mocks/mock_product_helper.h"
#include "shared/test/common/mocks/mock_usm_memory_pool.h"
#include "shared/test/common/mocks/ult_device_factory.h"
#include "shared/test/common/test_macros/hw_test.h"
#include "opencl/source/cl_device/cl_device.h"
#include "opencl/source/command_queue/command_queue.h"
#include "opencl/source/context/context.inl"
#include "opencl/source/mem_obj/buffer.h"
#include "opencl/source/sharings/sharing.h"
#include "opencl/test/unit_test/fixtures/platform_fixture.h"
#include "opencl/test/unit_test/mocks/mock_buffer.h"
#include "opencl/test/unit_test/mocks/mock_cl_device.h"
#include "opencl/test/unit_test/mocks/mock_command_queue.h"
#include "opencl/test/unit_test/mocks/mock_context.h"
#include "opencl/test/unit_test/mocks/mock_platform.h"
#include "opencl/test/unit_test/mocks/ult_cl_device_factory_with_platform.h"
using namespace NEO;
class WhiteBoxContext : public Context {
public:
MemoryManager *getMM() {
return this->memoryManager;
}
const cl_context_properties *getProperties() const {
return properties;
}
size_t getNumProperties() const {
return numProperties;
}
WhiteBoxContext(void(CL_CALLBACK *pfnNotify)(const char *, const void *, size_t, void *), void *userData) : Context(pfnNotify, userData){};
};
struct ContextTest : public PlatformFixture,
public ::testing::Test {
using PlatformFixture::setUp;
void SetUp() override {
PlatformFixture::setUp();
properties.push_back(CL_CONTEXT_PLATFORM);
properties.push_back(reinterpret_cast<cl_context_properties>(pPlatform));
properties.push_back(0);
context = Context::create<WhiteBoxContext>(properties.data(), ClDeviceVector(devices, numDevices), nullptr, nullptr, retVal);
ASSERT_NE(nullptr, context);
}
void TearDown() override {
delete context;
PlatformFixture::tearDown();
}
uint32_t getRootDeviceIndex() {
return context->getDevice(0)->getRootDeviceIndex();
}
cl_int retVal = CL_SUCCESS;
WhiteBoxContext *context = nullptr;
std::vector<cl_context_properties> properties;
};
TEST_F(ContextTest, WhenCreatingContextThenDevicesAllDevicesExist) {
for (size_t deviceOrdinal = 0; deviceOrdinal < context->getNumDevices(); ++deviceOrdinal) {
EXPECT_NE(nullptr, context->getDevice(deviceOrdinal));
}
}
TEST_F(ContextTest, WhenCreatingContextThenMemoryManagerForContextIsSet) {
EXPECT_NE(nullptr, context->getMM());
}
TEST_F(ContextTest, WhenCreatingContextThenPropertiesAreCopied) {
auto contextProperties = context->getProperties();
EXPECT_NE(properties.data(), contextProperties);
}
TEST_F(ContextTest, WhenCreatingContextThenPropertiesAreValid) {
auto contextProperties = context->getProperties();
ASSERT_NE(nullptr, contextProperties);
EXPECT_EQ(3u, context->getNumProperties());
while (*contextProperties) {
switch (*contextProperties) {
case CL_CONTEXT_PLATFORM:
++contextProperties;
break;
default:
ASSERT_FALSE(!"Unknown context property");
break;
}
++contextProperties;
}
}
TEST_F(ContextTest, WhenCreatingContextThenSpecialQueueIsAvailable) {
auto specialQ = context->getSpecialQueue(0u);
EXPECT_NE(specialQ, nullptr);
}
TEST_F(ContextTest, WhenSettingSpecialQueueThenQueueIsAvailable) {
MockContext context((ClDevice *)devices[0], true);
auto specialQ = context.specialQueues[0];
EXPECT_EQ(specialQ, nullptr);
auto cmdQ = new MockCommandQueue(&context, (ClDevice *)devices[0], 0, false);
context.setSpecialQueue(cmdQ, 0u);
specialQ = context.getSpecialQueue(0u);
EXPECT_NE(specialQ, nullptr);
}
TEST_F(ContextTest, givenCmdQueueWithoutContextWhenBeingCreatedNextDeletedThenContextRefCountShouldNeitherBeIncrementedNorNextDecremented) {
MockContext context((ClDevice *)devices[0]);
EXPECT_EQ(1, context.getRefInternalCount());
auto cmdQ1 = new MockCommandQueue();
EXPECT_EQ(1, context.getRefInternalCount());
delete cmdQ1;
EXPECT_EQ(1, context.getRefInternalCount());
auto cmdQ2 = new MockCommandQueue(nullptr, (ClDevice *)devices[0], 0, false);
EXPECT_EQ(1, context.getRefInternalCount());
delete cmdQ2;
EXPECT_EQ(1, context.getRefInternalCount());
}
TEST_F(ContextTest, givenCmdQueueWithContextWhenBeingCreatedNextDeletedThenContextRefCountShouldBeIncrementedNextDecremented) {
MockContext context((ClDevice *)devices[0]);
EXPECT_EQ(1, context.getRefInternalCount());
auto cmdQ = new MockCommandQueue(&context, (ClDevice *)devices[0], 0, false);
EXPECT_EQ(2, context.getRefInternalCount());
delete cmdQ;
EXPECT_EQ(1, context.getRefInternalCount());
}
TEST_F(ContextTest, givenContextWhenItIsCreatedFromDeviceThenItAddsRefCountToThisDevice) {
auto device = castToObject<ClDevice>(devices[0]);
EXPECT_EQ(2, device->getRefInternalCount());
cl_device_id deviceID = devices[0];
std::unique_ptr<Context> context(Context::create<Context>(0, ClDeviceVector(&deviceID, 1), nullptr, nullptr, retVal));
EXPECT_EQ(3, device->getRefInternalCount());
context.reset(nullptr);
EXPECT_EQ(2, device->getRefInternalCount());
}
TEST_F(ContextTest, givenContextWhenItIsCreatedFromMultipleDevicesThenItAddsRefCountToThoseDevices) {
auto device = castToObject<ClDevice>(devices[0]);
EXPECT_EQ(2, device->getRefInternalCount());
ClDeviceVector devicesVector;
devicesVector.push_back(device);
devicesVector.push_back(device);
std::unique_ptr<Context> context(Context::create<Context>(0, devicesVector, nullptr, nullptr, retVal));
EXPECT_EQ(4, device->getRefInternalCount());
context.reset(nullptr);
EXPECT_EQ(2, device->getRefInternalCount());
}
TEST_F(ContextTest, givenSpecialCmdQueueWithContextWhenBeingCreatedNextAutoDeletedThenContextRefCountShouldNeitherBeIncrementedNorNextDecremented) {
MockContext context((ClDevice *)devices[0], true);
EXPECT_EQ(1, context.getRefInternalCount());
auto cmdQ = new MockCommandQueue(&context, (ClDevice *)devices[0], 0, false);
context.overrideSpecialQueueAndDecrementRefCount(cmdQ, 0u);
EXPECT_EQ(1, context.getRefInternalCount());
// special queue is to be deleted implicitly by context
}
TEST_F(ContextTest, givenSpecialCmdQueueWithContextWhenBeingCreatedNextDeletedThenContextRefCountShouldNeitherBeIncrementedNorNextDecremented) {
MockContext context((ClDevice *)devices[0], true);
EXPECT_EQ(1, context.getRefInternalCount());
auto cmdQ = new MockCommandQueue(&context, (ClDevice *)devices[0], 0, false);
context.overrideSpecialQueueAndDecrementRefCount(cmdQ, 0u);
EXPECT_EQ(1, context.getRefInternalCount());
delete cmdQ;
EXPECT_EQ(1, context.getRefInternalCount());
context.setSpecialQueue(nullptr, 0u);
}
TEST_F(ContextTest, GivenInteropSyncParamWhenCreateContextThenSetContextParam) {
cl_device_id deviceID = devices[0];
auto pPlatform = NEO::platform();
cl_platform_id pid[1];
pid[0] = pPlatform;
cl_context_properties validProperties[5] = {CL_CONTEXT_PLATFORM, (cl_context_properties)pid[0],
CL_CONTEXT_INTEROP_USER_SYNC, 1, 0};
cl_int retVal = CL_SUCCESS;
auto context = Context::create<Context>(validProperties, ClDeviceVector(&deviceID, 1), nullptr, nullptr, retVal);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_NE(nullptr, context);
EXPECT_TRUE(context->getInteropUserSyncEnabled());
delete context;
validProperties[3] = 0; // false
context = Context::create<Context>(validProperties, ClDeviceVector(&deviceID, 1), nullptr, nullptr, retVal);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_NE(nullptr, context);
EXPECT_FALSE(context->getInteropUserSyncEnabled());
delete context;
}
class MockSharingFunctions : public SharingFunctions {
public:
uint32_t getId() const override {
return sharingId;
}
static const uint32_t sharingId = 0;
};
TEST_F(ContextTest, givenContextWhenSharingTableEmptyThenReturnsNullptr) {
MockContext context;
context.clearSharingFunctions();
auto *sharingF = context.getSharing<MockSharingFunctions>();
EXPECT_EQ(sharingF, nullptr);
}
TEST_F(ContextTest, givenNullptrWhenRegisteringSharingToContextThenAbortExecution) {
MockContext context;
context.clearSharingFunctions();
EXPECT_THROW(context.registerSharing<MockSharingFunctions>(nullptr), std::exception);
}
TEST_F(ContextTest, givenContextWhenSharingTableIsNotEmptyThenReturnsSharingFunctionPointer) {
MockContext context;
MockSharingFunctions *sharingFunctions = new MockSharingFunctions;
context.registerSharing<MockSharingFunctions>(sharingFunctions);
auto *sharingF = context.getSharing<MockSharingFunctions>();
EXPECT_EQ(sharingF, sharingFunctions);
}
TEST(Context, whenCreateContextThenSpecialQueueUsesInternalEngine) {
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(defaultHwInfo.get()));
cl_device_id clDevice = device.get();
cl_int retVal = CL_SUCCESS;
auto context = std::unique_ptr<MockContext>(Context::create<MockContext>(nullptr, ClDeviceVector(&clDevice, 1), nullptr, nullptr, retVal));
ASSERT_NE(nullptr, context);
EXPECT_EQ(CL_SUCCESS, retVal);
auto specialQueueEngine = context->getSpecialQueue(device->getRootDeviceIndex())->getGpgpuEngine();
auto internalEngine = device->getInternalEngine();
EXPECT_EQ(internalEngine.commandStreamReceiver, specialQueueEngine.commandStreamReceiver);
}
TEST(MultiDeviceContextTest, givenContextWithMultipleDevicesWhenGettingInfoAboutSubDevicesThenCorrectValueIsReturned) {
MockSpecializedContext context1;
MockUnrestrictiveContext context2;
MockDefaultContext context3;
EXPECT_EQ(2u, context1.getNumDevices());
EXPECT_TRUE(context1.containsMultipleSubDevices(0));
EXPECT_EQ(3u, context2.getNumDevices());
EXPECT_TRUE(context2.containsMultipleSubDevices(0));
EXPECT_EQ(3u, context3.getNumDevices());
EXPECT_FALSE(context3.containsMultipleSubDevices(0));
EXPECT_FALSE(context3.containsMultipleSubDevices(1));
EXPECT_FALSE(context3.containsMultipleSubDevices(2));
}
class ContextWithAsyncDeleterTest : public ::testing::WithParamInterface<bool>,
public ::testing::Test {
public:
void SetUp() override {
memoryManager = new MockMemoryManager();
deviceFactory = new UltClDeviceFactoryWithPlatform{1, 0, memoryManager};
device = deviceFactory->rootDevices[0];
deleter = new MockDeferredDeleter();
device->allEngines.clear();
device->device.secondaryEngines.clear();
device->device.regularEngineGroups.clear();
device->createEngines();
memoryManager->setDeferredDeleter(deleter);
}
void TearDown() override {
delete deviceFactory;
}
Context *context;
MockMemoryManager *memoryManager;
MockDeferredDeleter *deleter;
MockClDevice *device;
UltClDeviceFactoryWithPlatform *deviceFactory;
};
TEST_P(ContextWithAsyncDeleterTest, givenContextWithMemoryManagerWhenAsyncDeleterIsEnabledThenUsesDeletersMethods) {
cl_device_id clDevice = device;
cl_int retVal;
ClDeviceVector deviceVector(&clDevice, 1);
bool asyncDeleterEnabled = GetParam();
memoryManager->overrideAsyncDeleterFlag(asyncDeleterEnabled);
EXPECT_EQ(0, deleter->getClientsNum());
context = Context::create<Context>(0, deviceVector, nullptr, nullptr, retVal);
if (asyncDeleterEnabled) {
EXPECT_EQ(1, deleter->getClientsNum());
} else {
EXPECT_EQ(0, deleter->getClientsNum());
}
delete context;
EXPECT_EQ(0, deleter->getClientsNum());
}
INSTANTIATE_TEST_SUITE_P(ContextTests,
ContextWithAsyncDeleterTest,
::testing::Bool());
TEST(DefaultContext, givenDefaultContextWhenItIsQueriedForTypeThenDefaultTypeIsReturned) {
MockContext context;
EXPECT_EQ(ContextType::CONTEXT_TYPE_DEFAULT, context.peekContextType());
}
TEST(Context, givenContextWhenCheckIfAllocationsAreMultiStorageThenReturnProperValueAccordingToContextType) {
MockContext context;
EXPECT_TRUE(context.areMultiStorageAllocationsPreferred());
context.contextType = ContextType::CONTEXT_TYPE_SPECIALIZED;
EXPECT_FALSE(context.areMultiStorageAllocationsPreferred());
context.contextType = ContextType::CONTEXT_TYPE_UNRESTRICTIVE;
EXPECT_TRUE(context.areMultiStorageAllocationsPreferred());
}
TEST(Context, givenContextWhenIsDeviceAssociatedIsCalledWithItsDeviceThenTrueIsReturned) {
MockContext context;
EXPECT_TRUE(context.isDeviceAssociated(*context.getDevice(0)));
}
TEST(Context, givenContextWhenIsDeviceAssociatedIsCalledWithNotAssociatedDeviceThenFalseIsReturned) {
MockContext context0;
MockContext context1;
EXPECT_FALSE(context0.isDeviceAssociated(*context1.getDevice(0)));
EXPECT_FALSE(context1.isDeviceAssociated(*context0.getDevice(0)));
}
TEST(Context, givenContextWithSingleDevicesWhenGettingDeviceBitfieldForAllocationThenDeviceBitfieldForDeviceIsReturned) {
UltClDeviceFactory deviceFactory{1, 3};
auto device = deviceFactory.subDevices[1];
auto expectedDeviceBitfield = device->getDeviceBitfield();
MockContext context(device);
EXPECT_EQ(expectedDeviceBitfield.to_ulong(), context.getDeviceBitfieldForAllocation(device->getRootDeviceIndex()).to_ulong());
}
TEST(Context, givenContextWithMultipleSubDevicesWhenGettingDeviceBitfieldForAllocationThenMergedDeviceBitfieldIsReturned) {
UltClDeviceFactoryWithPlatform deviceFactory{1, 3};
cl_int retVal;
cl_device_id devices[]{deviceFactory.subDevices[0], deviceFactory.subDevices[2]};
ClDeviceVector deviceVector(devices, 2);
auto expectedDeviceBitfield = deviceFactory.subDevices[0]->getDeviceBitfield() | deviceFactory.subDevices[2]->getDeviceBitfield();
auto context = Context::create<Context>(0, deviceVector, nullptr, nullptr, retVal);
EXPECT_NE(nullptr, context);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(expectedDeviceBitfield.to_ulong(), context->getDeviceBitfieldForAllocation(deviceFactory.rootDevices[0]->getRootDeviceIndex()).to_ulong());
context->release();
}
TEST(MultiDeviceContextTest, givenContextWithTwoDifferentSubDevicesFromDifferentRootDevicesWhenGettingDeviceBitfieldForAllocationThenSeparatedDeviceBitfieldsAreReturned) {
DebugManagerStateRestore restorer;
debugManager.flags.EnableMultiRootDeviceContexts.set(true);
UltClDeviceFactoryWithPlatform deviceFactory{2, 2};
cl_int retVal;
cl_device_id devices[]{deviceFactory.subDevices[1], deviceFactory.subDevices[2]};
ClDeviceVector deviceVector(devices, 2);
auto expectedDeviceBitfieldForRootDevice0 = deviceFactory.subDevices[1]->getDeviceBitfield();
auto expectedDeviceBitfieldForRootDevice1 = deviceFactory.subDevices[2]->getDeviceBitfield();
auto context = Context::create<Context>(0, deviceVector, nullptr, nullptr, retVal);
EXPECT_NE(nullptr, context);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(expectedDeviceBitfieldForRootDevice0.to_ulong(), context->getDeviceBitfieldForAllocation(deviceFactory.rootDevices[0]->getRootDeviceIndex()).to_ulong());
EXPECT_EQ(expectedDeviceBitfieldForRootDevice1.to_ulong(), context->getDeviceBitfieldForAllocation(deviceFactory.rootDevices[1]->getRootDeviceIndex()).to_ulong());
context->release();
}
TEST(MultiDeviceContextTest, givenMultipleRootDevicesWhenCreatingMultiRootDeviceContextCrossDeviceTagAllocationsAreCreated) {
DebugManagerStateRestore restorer;
UltClDeviceFactoryWithPlatform deviceFactory{3, 0};
cl_int retVal;
for (auto &csr : deviceFactory.pUltDeviceFactory->rootDevices[0]->commandStreamReceivers) {
auto tagsMultiAllocation = csr->getTagsMultiAllocation();
EXPECT_NE(nullptr, tagsMultiAllocation->getGraphicsAllocation(0));
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(1));
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(2));
}
for (auto &csr : deviceFactory.pUltDeviceFactory->rootDevices[1]->commandStreamReceivers) {
auto tagsMultiAllocation = csr->getTagsMultiAllocation();
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(0));
EXPECT_NE(nullptr, tagsMultiAllocation->getGraphicsAllocation(1));
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(2));
}
for (auto &csr : deviceFactory.pUltDeviceFactory->rootDevices[2]->commandStreamReceivers) {
auto tagsMultiAllocation = csr->getTagsMultiAllocation();
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(0));
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(1));
EXPECT_NE(nullptr, tagsMultiAllocation->getGraphicsAllocation(2));
}
cl_device_id devices[]{deviceFactory.rootDevices[0], deviceFactory.rootDevices[2]};
ClDeviceVector deviceVector(devices, 2);
auto context = Context::create<Context>(0, deviceVector, nullptr, nullptr, retVal);
EXPECT_NE(nullptr, context);
EXPECT_EQ(CL_SUCCESS, retVal);
for (auto &csr : deviceFactory.pUltDeviceFactory->rootDevices[0]->commandStreamReceivers) {
auto tagsMultiAllocation = csr->getTagsMultiAllocation();
EXPECT_NE(nullptr, tagsMultiAllocation->getGraphicsAllocation(0));
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(1));
EXPECT_NE(nullptr, tagsMultiAllocation->getGraphicsAllocation(2));
}
for (auto &csr : deviceFactory.pUltDeviceFactory->rootDevices[1]->commandStreamReceivers) {
auto tagsMultiAllocation = csr->getTagsMultiAllocation();
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(0));
EXPECT_NE(nullptr, tagsMultiAllocation->getGraphicsAllocation(1));
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(2));
}
for (auto &csr : deviceFactory.pUltDeviceFactory->rootDevices[2]->commandStreamReceivers) {
auto tagsMultiAllocation = csr->getTagsMultiAllocation();
EXPECT_NE(nullptr, tagsMultiAllocation->getGraphicsAllocation(0));
EXPECT_EQ(nullptr, tagsMultiAllocation->getGraphicsAllocation(1));
EXPECT_NE(nullptr, tagsMultiAllocation->getGraphicsAllocation(2));
}
context->release();
}
TEST(Context, WhenSettingContextDestructorCallbackThenCallOrderIsPreserved) {
struct UserDataType {
cl_context expectedContext;
std::vector<size_t> &vectorToModify;
size_t valueToAdd;
};
auto callback = [](cl_context context, void *userData) -> void {
auto pUserData = reinterpret_cast<UserDataType *>(userData);
EXPECT_EQ(pUserData->expectedContext, context);
pUserData->vectorToModify.push_back(pUserData->valueToAdd);
};
auto pContext = new MockContext{};
std::vector<size_t> callbacksReturnValues;
UserDataType userDataArray[]{
{pContext, callbacksReturnValues, 1},
{pContext, callbacksReturnValues, 2},
{pContext, callbacksReturnValues, 3}};
for (auto &userData : userDataArray) {
cl_int retVal = clSetContextDestructorCallback(pContext, callback, &userData);
ASSERT_EQ(CL_SUCCESS, retVal);
}
delete pContext;
ASSERT_EQ(3u, callbacksReturnValues.size());
EXPECT_EQ(3u, callbacksReturnValues[0]);
EXPECT_EQ(2u, callbacksReturnValues[1]);
EXPECT_EQ(1u, callbacksReturnValues[2]);
}
TEST(Context, givenContextAndDevicesWhenIsTileOnlyThenProperValueReturned) {
UltClDeviceFactory deviceFactoryWithSubDevices{1, 2};
UltClDeviceFactory deviceFactoryWithMultipleDevices{2, 0};
cl_device_id devices[] = {deviceFactoryWithMultipleDevices.rootDevices[0], deviceFactoryWithMultipleDevices.rootDevices[1]};
MockContext tileOnlyContext(deviceFactoryWithMultipleDevices.rootDevices[0]);
MockContext subDevicesContext(deviceFactoryWithSubDevices.rootDevices[0]);
MockContext multipleDevicesContext(ClDeviceVector(devices, 2));
EXPECT_TRUE(tileOnlyContext.isSingleDeviceContext());
EXPECT_FALSE(subDevicesContext.isSingleDeviceContext());
EXPECT_FALSE(multipleDevicesContext.isSingleDeviceContext());
}
TEST(InvalidExtraPropertiesTests, givenInvalidExtraPropertiesWhenCreatingContextThenContextIsNotCreated) {
constexpr cl_context_properties invalidPropertyType = (1 << 31);
constexpr cl_context_properties invalidContextFlag = (1 << 31);
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
cl_device_id deviceID = device.get();
cl_int retVal = 0;
std::unique_ptr<Context> context;
{
cl_context_properties properties[] = {invalidPropertyType, invalidContextFlag, 0};
context.reset(Context::create<Context>(properties, ClDeviceVector(&deviceID, 1), nullptr, nullptr, retVal));
EXPECT_EQ(CL_INVALID_PROPERTY, retVal);
EXPECT_EQ(nullptr, context.get());
}
}
using ContextCreateTests = ::testing::Test;
HWCMDTEST_F(IGFX_XE_HP_CORE, ContextCreateTests, givenLocalMemoryAllocationWhenBlitMemoryToAllocationIsCalledThenSuccessIsReturned) {
if (is32bit) {
GTEST_SKIP();
}
DebugManagerStateRestore restore;
debugManager.flags.EnableLocalMemory.set(true);
debugManager.flags.ForceLocalMemoryAccessMode.set(static_cast<int32_t>(LocalMemoryAccessMode::defaultMode));
VariableBackup<HardwareInfo> backupHwInfo(defaultHwInfo.get());
defaultHwInfo->capabilityTable.blitterOperationsSupported = true;
{
auto productHelper = ProductHelper::create(defaultHwInfo->platform.eProductFamily);
auto defaultBcsIndex = EngineHelpers::getBcsIndex(productHelper->getDefaultCopyEngine());
if (0u != defaultBcsIndex) {
defaultHwInfo->featureTable.ftrBcsInfo.set(defaultBcsIndex, true);
defaultHwInfo->featureTable.ftrBcsInfo.set(EngineHelpers::getBcsIndex(aub_stream::ENGINE_BCS3), true); // enable BCS3 for internal operations
}
}
UltClDeviceFactory deviceFactory{1, 2};
ClDevice *devicesToTest[] = {deviceFactory.rootDevices[0], deviceFactory.subDevices[0], deviceFactory.subDevices[1]};
for (const auto &testedDevice : devicesToTest) {
MockContext context(testedDevice);
cl_int retVal;
auto buffer = std::unique_ptr<Buffer>(Buffer::create(&context, {}, 1, nullptr, retVal));
auto memory = buffer->getGraphicsAllocation(testedDevice->getRootDeviceIndex());
uint8_t hostMemory[1];
auto executionEnv = testedDevice->getExecutionEnvironment();
executionEnv->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.blitterOperationsSupported = false;
EXPECT_EQ(BlitOperationResult::unsupported, BlitHelper::blitMemoryToAllocation(buffer->getContext()->getDevice(0)->getDevice(), memory, buffer->getOffset(), hostMemory, {1, 1, 1}));
executionEnv->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.blitterOperationsSupported = true;
EXPECT_EQ(BlitOperationResult::success, BlitHelper::blitMemoryToAllocation(buffer->getContext()->getDevice(0)->getDevice(), memory, buffer->getOffset(), hostMemory, {1, 1, 1}));
}
}
HWCMDTEST_F(IGFX_XE_HP_CORE, ContextCreateTests, givenGpuHangOnFlushBcsTaskAndLocalMemoryAllocationWhenBlitMemoryToAllocationIsCalledThenGpuHangIsReturned) {
if (is32bit) {
GTEST_SKIP();
}
DebugManagerStateRestore restore;
debugManager.flags.EnableLocalMemory.set(true);
debugManager.flags.ForceLocalMemoryAccessMode.set(static_cast<int32_t>(LocalMemoryAccessMode::defaultMode));
VariableBackup<HardwareInfo> backupHwInfo(defaultHwInfo.get());
defaultHwInfo->capabilityTable.blitterOperationsSupported = true;
{
auto productHelper = ProductHelper::create(defaultHwInfo->platform.eProductFamily);
auto defaultBcsIndex = EngineHelpers::getBcsIndex(productHelper->getDefaultCopyEngine());
if (0u != defaultBcsIndex) {
defaultHwInfo->featureTable.ftrBcsInfo.set(defaultBcsIndex, true);
defaultHwInfo->featureTable.ftrBcsInfo.set(EngineHelpers::getBcsIndex(aub_stream::ENGINE_BCS3), true); // enable BCS3 for internal operations
}
}
UltClDeviceFactory deviceFactory{1, 2};
auto testedDevice = deviceFactory.rootDevices[0];
MockContext context(testedDevice);
cl_int retVal;
auto buffer = std::unique_ptr<Buffer>(Buffer::create(&context, {}, 1, nullptr, retVal));
auto memory = buffer->getGraphicsAllocation(testedDevice->getRootDeviceIndex());
uint8_t hostMemory[1];
auto executionEnv = testedDevice->getExecutionEnvironment();
executionEnv->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.blitterOperationsSupported = false;
EXPECT_EQ(BlitOperationResult::unsupported, BlitHelper::blitMemoryToAllocation(buffer->getContext()->getDevice(0)->getDevice(), memory, buffer->getOffset(), hostMemory, {1, 1, 1}));
executionEnv->rootDeviceEnvironments[0]->getMutableHardwareInfo()->capabilityTable.blitterOperationsSupported = true;
const auto rootDevice = testedDevice->getDevice().getRootDevice();
const auto leastOccupiedBankDevice = rootDevice->getRTMemoryBackedBuffer() ? 1u : 0u;
const auto blitDevice = rootDevice->getNearestGenericSubDevice(leastOccupiedBankDevice);
auto &selectorCopyEngine = blitDevice->getSelectorCopyEngine();
auto deviceBitfield = blitDevice->getDeviceBitfield();
auto &rootDeviceEnvironment = testedDevice->getRootDeviceEnvironment();
auto &gfxCoreHelper = rootDeviceEnvironment.getHelper<GfxCoreHelper>();
auto internalUsage = true;
auto bcsEngineType = EngineHelpers::getBcsEngineType(rootDeviceEnvironment, deviceBitfield, selectorCopyEngine, internalUsage);
auto bcsEngineUsage = gfxCoreHelper.preferInternalBcsEngine() ? EngineUsage::internal : EngineUsage::regular;
auto bcsEngine = blitDevice->tryGetEngine(bcsEngineType, bcsEngineUsage);
ASSERT_NE(nullptr, bcsEngine);
auto ultBcsCsr = static_cast<UltCommandStreamReceiver<FamilyType> *>(bcsEngine->commandStreamReceiver);
ultBcsCsr->callBaseFlushBcsTask = false;
ultBcsCsr->flushBcsTaskReturnValue = CompletionStamp::gpuHang;
EXPECT_EQ(BlitOperationResult::gpuHang, BlitHelper::blitMemoryToAllocation(buffer->getContext()->getDevice(0)->getDevice(), memory, buffer->getOffset(), hostMemory, {1, 1, 1}));
}
struct AllocationReuseContextTest : ContextTest {
void addMappedPtr(Buffer &buffer, void *ptr, size_t ptrLength) {
auto &handler = context->getMapOperationsStorage().getHandler(&buffer);
MemObjSizeArray size{};
MemObjSizeArray offset{};
cl_map_flags mapFlag = CL_MAP_READ;
EXPECT_TRUE(handler.add(ptr, ptrLength, mapFlag, size, offset, 0, buffer.getMultiGraphicsAllocation().getDefaultGraphicsAllocation()));
}
void addSvmPtr(InternalMemoryType type, GraphicsAllocation &allocation) {
SvmAllocationData svmEntry{getRootDeviceIndex()};
svmEntry.memoryType = type;
svmEntry.size = allocation.getUnderlyingBufferSize();
svmEntry.gpuAllocations.addAllocation(&allocation);
if (type != InternalMemoryType::deviceUnifiedMemory) {
svmEntry.cpuAllocation = &allocation;
}
context->getSVMAllocsManager()->insertSVMAlloc(svmEntry);
}
};
TEST_F(AllocationReuseContextTest, givenSharedSvmAllocPresentWhenGettingExistingHostPtrAllocThenRetrieveTheAllocation) {
uint64_t svmPtrGpu = 0x1234;
void *svmPtr = reinterpret_cast<void *>(svmPtrGpu);
MockGraphicsAllocation allocation{svmPtr, svmPtrGpu, 400};
addSvmPtr(InternalMemoryType::sharedUnifiedMemory, allocation);
GraphicsAllocation *retrievedAllocation{};
InternalMemoryType retrievedMemoryType{};
bool retrievedCpuCopyStatus = true;
retVal = context->tryGetExistingHostPtrAllocation(svmPtr, allocation.getUnderlyingBufferSize(), getRootDeviceIndex(),
retrievedAllocation, retrievedMemoryType, retrievedCpuCopyStatus);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(&allocation, retrievedAllocation);
EXPECT_EQ(InternalMemoryType::sharedUnifiedMemory, retrievedMemoryType);
EXPECT_TRUE(retrievedCpuCopyStatus);
}
TEST_F(AllocationReuseContextTest, givenHostSvmAllocPresentWhenGettingExistingHostPtrAllocThenRetrieveTheAllocation) {
uint64_t svmPtrGpu = 0x1234;
void *svmPtr = reinterpret_cast<void *>(svmPtrGpu);
MockGraphicsAllocation allocation{svmPtr, svmPtrGpu, 400};
addSvmPtr(InternalMemoryType::hostUnifiedMemory, allocation);
GraphicsAllocation *retrievedAllocation{};
InternalMemoryType retrievedMemoryType{};
bool retrievedCpuCopyStatus = true;
retVal = context->tryGetExistingHostPtrAllocation(svmPtr, allocation.getUnderlyingBufferSize(), getRootDeviceIndex(),
retrievedAllocation, retrievedMemoryType, retrievedCpuCopyStatus);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(&allocation, retrievedAllocation);
EXPECT_EQ(InternalMemoryType::hostUnifiedMemory, retrievedMemoryType);
EXPECT_TRUE(retrievedCpuCopyStatus);
}
TEST_F(AllocationReuseContextTest, givenDeviceSvmAllocPresentWhenGettingExistingHostPtrAllocThenRetrieveTheAllocationAndDisallowCpuCopy) {
uint64_t svmPtrGpu = 0x1234;
void *svmPtr = reinterpret_cast<void *>(svmPtrGpu);
MockGraphicsAllocation allocation{svmPtr, svmPtrGpu, 400};
addSvmPtr(InternalMemoryType::deviceUnifiedMemory, allocation);
GraphicsAllocation *retrievedAllocation{};
InternalMemoryType retrievedMemoryType{};
bool retrievedCpuCopyStatus = true;
retVal = context->tryGetExistingHostPtrAllocation(svmPtr, allocation.getUnderlyingBufferSize(), getRootDeviceIndex(),
retrievedAllocation, retrievedMemoryType, retrievedCpuCopyStatus);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(&allocation, retrievedAllocation);
EXPECT_EQ(InternalMemoryType::deviceUnifiedMemory, retrievedMemoryType);
EXPECT_FALSE(retrievedCpuCopyStatus);
}
TEST_F(AllocationReuseContextTest, givenHostSvmAllocPresentButRequestingTooBigSizeWhenGettingExistingHostPtrAllocThenReturnError) {
uint64_t svmPtrGpu = 0x1234;
void *svmPtr = reinterpret_cast<void *>(svmPtrGpu);
MockGraphicsAllocation allocation{svmPtr, svmPtrGpu, 400};
addSvmPtr(InternalMemoryType::hostUnifiedMemory, allocation);
size_t ptrSizeToRetrieve = allocation.getUnderlyingBufferSize() + 1;
GraphicsAllocation *retrievedAllocation{};
InternalMemoryType retrievedMemoryType{};
bool retrievedCpuCopyStatus = true;
retVal = context->tryGetExistingHostPtrAllocation(svmPtr, ptrSizeToRetrieve, getRootDeviceIndex(),
retrievedAllocation, retrievedMemoryType, retrievedCpuCopyStatus);
EXPECT_EQ(CL_INVALID_OPERATION, retVal);
}
TEST_F(AllocationReuseContextTest, givenHostPtrStoredInMapOperationsStorageWhenGettingExistingHostPtrAllocThenRetrieveTheAllocation) {
MockGraphicsAllocation allocation{};
MockBuffer buffer{context, allocation};
void *mappedPtr = reinterpret_cast<void *>(0x1234);
size_t mappedPtrSize = 10u;
addMappedPtr(buffer, mappedPtr, mappedPtrSize);
GraphicsAllocation *retrievedAllocation{};
InternalMemoryType retrievedMemoryType{};
bool retrievedCpuCopyStatus = true;
retVal = context->tryGetExistingHostPtrAllocation(mappedPtr, mappedPtrSize, getRootDeviceIndex(),
retrievedAllocation, retrievedMemoryType, retrievedCpuCopyStatus);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(&allocation, retrievedAllocation);
EXPECT_EQ(InternalMemoryType::notSpecified, retrievedMemoryType);
EXPECT_TRUE(retrievedCpuCopyStatus);
}
TEST_F(AllocationReuseContextTest, givenHostPtrNotStoredInMapOperationsStorageWhenGettingExistingHostPtrAllocThenFailToRetrieveTheAllocation) {
MockGraphicsAllocation allocation{};
MockBuffer buffer{context, allocation};
void *mappedPtr = reinterpret_cast<void *>(0x1234);
size_t mappedPtrSize = 10u;
addMappedPtr(buffer, mappedPtr, mappedPtrSize);
void *differentPtr = reinterpret_cast<void *>(0x12345);
GraphicsAllocation *retrievedAllocation{};
InternalMemoryType retrievedMemoryType{};
bool retrievedCpuCopyStatus = true;
retVal = context->tryGetExistingHostPtrAllocation(differentPtr, mappedPtrSize, getRootDeviceIndex(),
retrievedAllocation, retrievedMemoryType, retrievedCpuCopyStatus);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(nullptr, retrievedAllocation);
EXPECT_EQ(InternalMemoryType::notSpecified, retrievedMemoryType);
EXPECT_TRUE(retrievedCpuCopyStatus);
}
TEST_F(AllocationReuseContextTest, givenHostPtrStoredInMapOperationsStorageAndRequestedPtrToBigWhenGettingExistingHostPtrAllocThenFailRetrieveTheAllocation) {
MockGraphicsAllocation allocation{};
MockBuffer buffer{context, allocation};
void *mappedPtr = reinterpret_cast<void *>(0x1234);
size_t mappedPtrSize = 10u;
addMappedPtr(buffer, mappedPtr, mappedPtrSize);
size_t ptrSizeToRetrieve = mappedPtrSize + 1;
GraphicsAllocation *retrievedAllocation{};
InternalMemoryType retrievedMemoryType{};
bool retrievedCpuCopyStatus = true;
retVal = context->tryGetExistingHostPtrAllocation(mappedPtr, ptrSizeToRetrieve, getRootDeviceIndex(),
retrievedAllocation, retrievedMemoryType, retrievedCpuCopyStatus);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(nullptr, retrievedAllocation);
EXPECT_EQ(InternalMemoryType::notSpecified, retrievedMemoryType);
EXPECT_TRUE(retrievedCpuCopyStatus);
}
struct ContextUsmPoolParamsTest : public ::testing::Test {
void SetUp() override {
device = deviceFactory.rootDevices[0];
mockNeoDevice = static_cast<MockDevice *>(&device->getDevice());
mockProductHelper = new MockProductHelper;
mockNeoDevice->getRootDeviceEnvironmentRef().productHelper.reset(mockProductHelper);
}
bool compareUsmPoolParams(const UsmPoolParams &first, const UsmPoolParams &second) {
return first.poolSize == second.poolSize &&
first.minServicedSize == second.minServicedSize &&
first.maxServicedSize == second.maxServicedSize;
}
UltClDeviceFactoryWithPlatform deviceFactory{1, 0};
MockClDevice *device;
MockDevice *mockNeoDevice;
MockProductHelper *mockProductHelper;
std::unique_ptr<MockContext> context;
cl_int retVal = CL_SUCCESS;
DebugManagerStateRestore restore;
};
TEST_F(ContextUsmPoolParamsTest, whenGettingUsmPoolParamsThenReturnCorrectValues) {
cl_device_id devices[] = {device};
context.reset(Context::create<MockContext>(nullptr, ClDeviceVector(devices, 1), nullptr, nullptr, retVal));
EXPECT_EQ(CL_SUCCESS, retVal);
const UsmPoolParams expectedPoolParams{
.poolSize = UsmPoolParams::getUsmPoolSize(context->getDevice(0)->getGfxCoreHelper()),
.minServicedSize = 0u,
.maxServicedSize = context->getDevice(0)->getGfxCoreHelper().isExtendedUsmPoolSizeEnabled() ? 2 * MemoryConstants::megaByte : MemoryConstants::megaByte};
EXPECT_TRUE(compareUsmPoolParams(expectedPoolParams, UsmPoolParams::getUsmPoolParams(context->getDevice(0)->getGfxCoreHelper())));
}
TEST_F(ContextUsmPoolParamsTest, GivenUsmPoolAllocatorSupportedWhenInitializingUsmPoolsThenPoolsAreInitializedWithCorrectParams) {
mockProductHelper->isHostUsmPoolAllocatorSupportedResult = true;
mockProductHelper->isDeviceUsmPoolAllocatorSupportedResult = true;
cl_device_id devices[] = {device};
context.reset(Context::create<MockContext>(nullptr, ClDeviceVector(devices, 1), nullptr, nullptr, retVal));
EXPECT_EQ(CL_SUCCESS, retVal);
auto platform = context->getDevice(0)->getPlatform();
EXPECT_NE(nullptr, platform);
context->initializeDeviceUsmAllocationPool();
platform->initializeHostUsmAllocationPool();
EXPECT_TRUE(platform->getHostMemAllocPool().isInitialized());
EXPECT_TRUE(context->getDeviceMemAllocPool().isInitialized());
{
auto mockHostUsmMemAllocPool = static_cast<MockUsmMemAllocPool *>(&platform->getHostMemAllocPool());
const UsmPoolParams givenUsmHostPoolParams{
.poolSize = mockHostUsmMemAllocPool->poolInfo.poolSize,
.minServicedSize = mockHostUsmMemAllocPool->poolInfo.minServicedSize,
.maxServicedSize = mockHostUsmMemAllocPool->poolInfo.maxServicedSize};
const UsmPoolParams expectedUsmHostPoolParams = UsmPoolParams::getUsmPoolParams(context->getDevice(0)->getGfxCoreHelper());
EXPECT_TRUE(compareUsmPoolParams(expectedUsmHostPoolParams, givenUsmHostPoolParams));
}
}
TEST_F(ContextUsmPoolParamsTest, GivenUsmPoolAllocatorSupportedWhenInitializingUsmPoolsThenPoolsAreInitializedOnlyWithHwCsr) {
mockProductHelper->isHostUsmPoolAllocatorSupportedResult = true;
mockProductHelper->isDeviceUsmPoolAllocatorSupportedResult = true;
cl_device_id devices[] = {device};
context.reset(Context::create<MockContext>(nullptr, ClDeviceVector(devices, 1), nullptr, nullptr, retVal));
EXPECT_EQ(CL_SUCCESS, retVal);
for (int32_t csrType = 0; csrType < static_cast<int32_t>(CommandStreamReceiverType::typesNum); ++csrType) {
DebugManagerStateRestore restorer;
debugManager.flags.SetCommandStreamReceiver.set(static_cast<int32_t>(csrType));
auto platform = static_cast<MockPlatform *>(context->getDevice(0)->getPlatform());
EXPECT_NE(nullptr, platform);
EXPECT_FALSE(platform->getHostMemAllocPool().isInitialized());
EXPECT_FALSE(context->getDeviceMemAllocPool().isInitialized());
context->initializeDeviceUsmAllocationPool();
platform->initializeHostUsmAllocationPool();
EXPECT_EQ(DeviceFactory::isHwModeSelected(), platform->getHostMemAllocPool().isInitialized());
EXPECT_EQ(DeviceFactory::isHwModeSelected(), context->getDeviceMemAllocPool().isInitialized());
context->getDeviceMemAllocPool().cleanup();
platform->getHostMemAllocPool().cleanup();
context->usmPoolInitialized = false;
platform->usmPoolInitialized = false;
}
}
|