Skip to content

Commit 4955941

Browse files
Improve Program::create functions
accept multiple devices in createFromGenBinary remove redundant arguments rename functions for creating built in program Related-To: NEO-5001 Change-Id: Ic894fa3014d6eadce3747a6f7530e9848ae1f948 Signed-off-by: Mateusz Jablonski <[email protected]>
1 parent c50dc43 commit 4955941

File tree

16 files changed

+157
-84
lines changed

16 files changed

+157
-84
lines changed

opencl/source/api/api.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,12 +1308,13 @@ cl_program CL_API_CALL clCreateProgramWithSource(cl_context context,
13081308
"count", count,
13091309
"strings", strings,
13101310
"lengths", lengths);
1311-
retVal = validateObjects(context, count, strings);
1311+
Context *pContext = nullptr;
1312+
retVal = validateObjects(WithCastToInternal(context, &pContext), count, strings);
13121313
cl_program program = nullptr;
13131314

13141315
if (CL_SUCCESS == retVal) {
13151316
program = Program::create(
1316-
context,
1317+
pContext,
13171318
count,
13181319
strings,
13191320
lengths,

opencl/source/built_ins/builtins_dispatch_builder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,10 @@ std::unique_ptr<Program> BuiltinDispatchInfoBuilder::createProgramFromCode(const
799799
break;
800800
case BuiltinCode::ECodeType::Source:
801801
case BuiltinCode::ECodeType::Intermediate:
802-
ret.reset(Program::create(data, nullptr, deviceVector, true, &err));
802+
ret.reset(Program::createBuiltInFromSource(data, nullptr, deviceVector, &err));
803803
break;
804804
case BuiltinCode::ECodeType::Binary:
805-
ret.reset(Program::createFromGenBinary(*deviceVector[0]->getExecutionEnvironment(), nullptr, data, dataLen, true, nullptr, &deviceVector[0]->getDevice()));
805+
ret.reset(Program::createBuiltInFromGenBinary(nullptr, deviceVector, data, dataLen, &err));
806806
break;
807807
}
808808
return ret;

opencl/source/built_ins/vme_builtin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Program *Vme::createBuiltInProgram(
6868
}
6969

7070
Program *pBuiltInProgram = nullptr;
71-
pBuiltInProgram = Program::create(programSourceStr.c_str(), &context, deviceVector, true, nullptr);
71+
pBuiltInProgram = Program::createBuiltInFromSource(programSourceStr.c_str(), &context, deviceVector, nullptr);
7272

7373
auto &device = *deviceVector[0];
7474

opencl/source/context/context.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,11 @@ SchedulerKernel &Context::getSchedulerKernel() {
385385
auto device = &getDevice(0)->getDevice();
386386
auto src = SchedulerKernel::loadSchedulerKernel(device);
387387

388-
auto program = Program::createFromGenBinary(*device->getExecutionEnvironment(),
389-
this,
390-
src.resource.data(),
391-
src.resource.size(),
392-
true,
393-
&retVal,
394-
device);
388+
auto program = Program::createBuiltInFromGenBinary(this,
389+
devices,
390+
src.resource.data(),
391+
src.resource.size(),
392+
&retVal);
395393
DEBUG_BREAK_IF(retVal != CL_SUCCESS);
396394
DEBUG_BREAK_IF(!program);
397395

opencl/source/program/create.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ CreateFromILFunc createFromIL = Program::createFromIL<Program>;
1616
} // namespace ProgramFunctions
1717

1818
template Program *Program::create<Program>(Context *, const ClDeviceVector &, const size_t *, const unsigned char **, cl_int *, cl_int &);
19-
template Program *Program::create<Program>(cl_context, cl_uint, const char **, const size_t *, cl_int &);
20-
template Program *Program::create<Program>(const char *, Context *, const ClDeviceVector &, bool, cl_int *);
19+
template Program *Program::create<Program>(Context *, cl_uint, const char **, const size_t *, cl_int &);
20+
template Program *Program::createBuiltInFromSource<Program>(const char *, Context *, const ClDeviceVector &, cl_int *);
2121
template Program *Program::createFromIL<Program>(Context *, const void *, size_t length, cl_int &);
22-
template Program *Program::createFromGenBinary<Program>(ExecutionEnvironment &executionEnvironment, Context *context, const void *binary, size_t size, bool isBuiltIn, cl_int *errcodeRet, Device *device);
22+
template Program *Program::createBuiltInFromGenBinary<Program>(Context *context, const ClDeviceVector &, const void *binary, size_t size, cl_int *errcodeRet);
2323
} // namespace NEO

opencl/source/program/create.inl

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,14 @@ T *Program::create(
5656

5757
template <typename T>
5858
T *Program::create(
59-
cl_context context,
59+
Context *pContext,
6060
cl_uint count,
6161
const char **strings,
6262
const size_t *lengths,
6363
cl_int &errcodeRet) {
6464
std::string combinedString;
6565
size_t combinedStringSize = 0;
6666
T *program = nullptr;
67-
auto pContext = castToObject<Context>(context);
68-
DEBUG_BREAK_IF(!pContext);
6967

7068
auto retVal = createCombinedString(
7169
combinedString,
@@ -85,11 +83,10 @@ T *Program::create(
8583
}
8684

8785
template <typename T>
88-
T *Program::create(
86+
T *Program::createBuiltInFromSource(
8987
const char *nullTerminatedString,
9088
Context *context,
9189
const ClDeviceVector &deviceVector,
92-
bool isBuiltIn,
9390
cl_int *errcodeRet) {
9491
cl_int retVal = CL_SUCCESS;
9592
T *program = nullptr;
@@ -99,7 +96,7 @@ T *Program::create(
9996
}
10097

10198
if (retVal == CL_SUCCESS) {
102-
program = new T(context, isBuiltIn, deviceVector);
99+
program = new T(context, true, deviceVector);
103100
program->sourceCode = nullTerminatedString;
104101
program->createdFrom = CreatedFrom::SOURCE;
105102
}
@@ -112,14 +109,12 @@ T *Program::create(
112109
}
113110

114111
template <typename T>
115-
T *Program::createFromGenBinary(
116-
ExecutionEnvironment &executionEnvironment,
112+
T *Program::createBuiltInFromGenBinary(
117113
Context *context,
114+
const ClDeviceVector &deviceVector,
118115
const void *binary,
119116
size_t size,
120-
bool isBuiltIn,
121-
cl_int *errcodeRet,
122-
Device *device) {
117+
cl_int *errcodeRet) {
123118
cl_int retVal = CL_SUCCESS;
124119
T *program = nullptr;
125120

@@ -129,11 +124,12 @@ T *Program::createFromGenBinary(
129124

130125
if (CL_SUCCESS == retVal) {
131126

132-
ClDeviceVector deviceVector;
133-
deviceVector.push_back(device->getSpecializedDevice<ClDevice>());
134-
program = new T(context, isBuiltIn, deviceVector);
135-
program->numDevices = 1;
136-
program->replaceDeviceBinary(makeCopy(binary, size), size, device->getRootDeviceIndex());
127+
program = new T(context, true, deviceVector);
128+
for (const auto &device : deviceVector) {
129+
if (program->buildInfos[device->getRootDeviceIndex()].packedDeviceBinarySize == 0) {
130+
program->replaceDeviceBinary(makeCopy(binary, size), size, device->getRootDeviceIndex());
131+
}
132+
}
137133
program->isCreatedFromBinary = true;
138134
program->programBinaryType = CL_PROGRAM_BINARY_TYPE_EXECUTABLE;
139135
program->buildStatus = CL_BUILD_SUCCESS;

opencl/source/program/program.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,26 @@ class Program : public BaseObject<_cl_program> {
9393
// Create program from source
9494
template <typename T = Program>
9595
static T *create(
96-
cl_context context,
96+
Context *pContext,
9797
cl_uint count,
9898
const char **strings,
9999
const size_t *lengths,
100100
cl_int &errcodeRet);
101101

102102
template <typename T = Program>
103-
static T *create(
103+
static T *createBuiltInFromSource(
104104
const char *nullTerminatedString,
105105
Context *context,
106106
const ClDeviceVector &deviceVector,
107-
bool isBuiltIn,
108107
cl_int *errcodeRet);
109108

110109
template <typename T = Program>
111-
static T *createFromGenBinary(
112-
ExecutionEnvironment &executionEnvironment,
110+
static T *createBuiltInFromGenBinary(
113111
Context *context,
112+
const ClDeviceVector &deviceVector,
114113
const void *binary,
115114
size_t size,
116-
bool isBuiltIn,
117-
cl_int *errcodeRet,
118-
Device *device);
115+
cl_int *errcodeRet);
119116

120117
template <typename T = Program>
121118
static T *createFromIL(Context *context,

opencl/test/unit_test/api/cl_get_program_info_tests.inl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,100 @@ TEST(clGetProgramInfoTest, GivenMultiDeviceProgramCreatedWithBuiltInKernelsWhenG
407407
retVal = clReleaseProgram(pProgram);
408408
EXPECT_EQ(CL_SUCCESS, retVal);
409409
}
410+
411+
TEST(clGetProgramInfoTest, GivenMultiDeviceBuiltInProgramCreatedWithGenBinaryWhenGettingDevicesThenCorrectDevicesAreReturned) {
412+
MockUnrestrictiveContextMultiGPU context;
413+
414+
auto expectedNumDevices = context.getNumDevices();
415+
416+
auto devicesForProgram = std::make_unique<cl_device_id[]>(expectedNumDevices);
417+
418+
for (auto i = 0u; i < expectedNumDevices; i++) {
419+
devicesForProgram[i] = context.getDevice(i);
420+
}
421+
422+
std::unique_ptr<char[]> pBinary = nullptr;
423+
size_t binarySize = 0;
424+
std::string testFile;
425+
retrieveBinaryKernelFilename(testFile, "CopyBuffer_simd16_", ".bin");
426+
427+
pBinary = loadDataFromFile(
428+
testFile.c_str(),
429+
binarySize);
430+
431+
ASSERT_NE(0u, binarySize);
432+
ASSERT_NE(nullptr, pBinary);
433+
434+
cl_program pProgram = nullptr;
435+
436+
cl_int retVal = CL_INVALID_PROGRAM;
437+
pProgram = Program::createBuiltInFromGenBinary(&context, context.getDevices(), pBinary.get(), binarySize, &retVal);
438+
439+
EXPECT_NE(nullptr, pProgram);
440+
EXPECT_EQ(CL_SUCCESS, retVal);
441+
442+
verifyDevices(pProgram, expectedNumDevices, devicesForProgram.get());
443+
444+
retVal = clReleaseProgram(pProgram);
445+
EXPECT_EQ(CL_SUCCESS, retVal);
446+
}
447+
448+
TEST(clGetProgramInfoTest, GivenMultiDeviceBuiltInProgramCreatedWithGenBinaryWhenGettingDevicesThenCorrectBinariesAreReturned) {
449+
MockUnrestrictiveContextMultiGPU context;
450+
451+
auto expectedNumDevices = context.getNumDevices();
452+
453+
auto devicesForProgram = std::make_unique<cl_device_id[]>(expectedNumDevices);
454+
455+
for (auto i = 0u; i < expectedNumDevices; i++) {
456+
devicesForProgram[i] = context.getDevice(i);
457+
}
458+
459+
std::unique_ptr<char[]> pBinary = nullptr;
460+
size_t binarySize = 0;
461+
std::string testFile;
462+
retrieveBinaryKernelFilename(testFile, "CopyBuffer_simd16_", ".bin");
463+
464+
pBinary = loadDataFromFile(
465+
testFile.c_str(),
466+
binarySize);
467+
468+
ASSERT_NE(0u, binarySize);
469+
ASSERT_NE(nullptr, pBinary);
470+
471+
cl_program pProgram = nullptr;
472+
473+
cl_int retVal = CL_INVALID_PROGRAM;
474+
pProgram = Program::createBuiltInFromGenBinary(&context, context.getDevices(), pBinary.get(), binarySize, &retVal);
475+
476+
EXPECT_NE(nullptr, pProgram);
477+
EXPECT_EQ(CL_SUCCESS, retVal);
478+
479+
auto programBinarySizes = std::make_unique<size_t[]>(expectedNumDevices);
480+
memset(programBinarySizes.get(), 0, expectedNumDevices * sizeof(size_t));
481+
482+
retVal = clGetProgramInfo(pProgram, CL_PROGRAM_BINARY_SIZES, expectedNumDevices * sizeof(size_t), programBinarySizes.get(), nullptr);
483+
EXPECT_EQ(CL_SUCCESS, retVal);
484+
for (auto i = 0u; i < expectedNumDevices; i++) {
485+
EXPECT_EQ(binarySize, programBinarySizes[i]);
486+
}
487+
488+
auto programBinaries = std::make_unique<unsigned char *[]>(expectedNumDevices);
489+
490+
auto binariesBuffer = std::make_unique<unsigned char[]>(expectedNumDevices * binarySize);
491+
memset(binariesBuffer.get(), 0, expectedNumDevices * binarySize);
492+
493+
for (auto i = 0u; i < expectedNumDevices; i++) {
494+
programBinaries[i] = ptrOffset(binariesBuffer.get(), i * binarySize);
495+
}
496+
497+
retVal = clGetProgramInfo(pProgram, CL_PROGRAM_BINARIES, expectedNumDevices * sizeof(unsigned char *), programBinaries.get(), nullptr);
498+
EXPECT_EQ(CL_SUCCESS, retVal);
499+
for (auto i = 0u; i < expectedNumDevices; i++) {
500+
EXPECT_EQ(0, memcmp(programBinaries[i], pBinary.get(), binarySize));
501+
}
502+
503+
retVal = clReleaseProgram(pProgram);
504+
EXPECT_EQ(CL_SUCCESS, retVal);
505+
}
410506
} // namespace ULT

opencl/test/unit_test/built_ins/built_in_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,8 +2007,7 @@ TEST_F(BuiltInTests, WhenGettingSipKernelThenReturnProgramCreatedFromIsaAcquired
20072007
pDevice->getExecutionEnvironment()->rootDeviceEnvironments[rootDeviceIndex]->compilerInterface.reset(mockCompilerInterface);
20082008
mockCompilerInterface->sipKernelBinaryOverride = mockCompilerInterface->getDummyGenBinary();
20092009
cl_int errCode = CL_BUILD_PROGRAM_FAILURE;
2010-
auto p = Program::createFromGenBinary(*pDevice->getExecutionEnvironment(), pContext, mockCompilerInterface->sipKernelBinaryOverride.data(), mockCompilerInterface->sipKernelBinaryOverride.size(),
2011-
false, &errCode, pDevice);
2010+
auto p = Program::createBuiltInFromGenBinary(pContext, pContext->getDevices(), mockCompilerInterface->sipKernelBinaryOverride.data(), mockCompilerInterface->sipKernelBinaryOverride.size(), &errCode);
20122011
ASSERT_EQ(CL_SUCCESS, errCode);
20132012
errCode = p->processGenBinary(rootDeviceIndex);
20142013
ASSERT_EQ(CL_SUCCESS, errCode);

opencl/test/unit_test/command_queue/enqueue_svm_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ TEST_F(EnqueueSvmTest, GivenSvmAllocationWhenEnqueingKernelThenSuccessIsReturned
736736
GraphicsAllocation *pSvmAlloc = svmData->gpuAllocations.getGraphicsAllocation(context->getDevice(0)->getRootDeviceIndex());
737737
EXPECT_NE(nullptr, ptrSVM);
738738

739-
std::unique_ptr<Program> program(Program::create("FillBufferBytes", context, context->getDevices(), true, &retVal));
739+
std::unique_ptr<Program> program(Program::createBuiltInFromSource("FillBufferBytes", context, context->getDevices(), &retVal));
740740
cl_device_id device = pClDevice;
741741
program->build(1, &device, nullptr, nullptr, nullptr, false);
742742
std::unique_ptr<MockKernel> kernel(Kernel::create<MockKernel>(program.get(), *program->getKernelInfo("FillBufferBytes"), &retVal));
@@ -765,7 +765,7 @@ TEST_F(EnqueueSvmTest, givenEnqueueTaskBlockedOnUserEventWhenItIsEnqueuedThenSur
765765
GraphicsAllocation *pSvmAlloc = svmData->gpuAllocations.getGraphicsAllocation(context->getDevice(0)->getRootDeviceIndex());
766766
EXPECT_NE(nullptr, ptrSVM);
767767

768-
auto program = clUniquePtr(Program::create("FillBufferBytes", context, context->getDevices(), true, &retVal));
768+
auto program = clUniquePtr(Program::createBuiltInFromSource("FillBufferBytes", context, context->getDevices(), &retVal));
769769
cl_device_id device = pClDevice;
770770
program->build(1, &device, nullptr, nullptr, nullptr, false);
771771
auto kernel = clUniquePtr(Kernel::create<MockKernel>(program.get(), *program->getKernelInfo("FillBufferBytes"), &retVal));

0 commit comments

Comments
 (0)