Skip to content

Commit 5630cfc

Browse files
committed
Use std::unique_ptr to hold command objects
1 parent a2b30f8 commit 5630cfc

File tree

10 files changed

+83
-106
lines changed

10 files changed

+83
-106
lines changed

source/adapters/cuda/command_buffer.cpp

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ commandBufferDestroy(ur_exp_command_buffer_handle_t CommandBuffer) try {
3636
return Err;
3737
}
3838

39-
ur_result_t
40-
commandHandleDestroy(ur_exp_command_buffer_command_handle_t Command) try {
39+
ur_result_t commandHandleDestroy(
40+
std::unique_ptr<ur_exp_command_buffer_command_handle_t_> &Command) try {
4141
// We create the ur_event_t returned to the user for a signal node using
4242
// `makeWithNative` which sets `HasOwnership` to false. Therefore destruction
4343
// of the `ur_event_t` object doesn't free the underlying CuEvent_t object and
@@ -49,7 +49,6 @@ commandHandleDestroy(ur_exp_command_buffer_command_handle_t Command) try {
4949
UR_CHECK_ERROR(cuEventDestroy(SignalEvent));
5050
}
5151

52-
delete Command;
5352
return UR_RESULT_SUCCESS;
5453
} catch (ur_result_t Err) {
5554
return Err;
@@ -336,13 +335,14 @@ static ur_result_t enqueueCommandBufferFillHelper(
336335

337336
std::vector<CUgraphNode> WaitNodes =
338337
NumEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
339-
auto NewCommand = new T(CommandBuffer, GraphNode, SignalNode, WaitNodes,
340-
std::move(DecomposedNodes));
341-
CommandBuffer->CommandHandles.push_back(NewCommand);
342-
338+
auto NewCommand = std::make_unique<T>(CommandBuffer, GraphNode, SignalNode,
339+
WaitNodes, std::move(DecomposedNodes));
343340
if (RetCommand) {
344-
*RetCommand = NewCommand;
341+
*RetCommand = NewCommand.get();
345342
}
343+
344+
CommandBuffer->CommandHandles.push_back(std::move(NewCommand));
345+
346346
return UR_RESULT_SUCCESS;
347347
} catch (ur_result_t Err) {
348348
return Err;
@@ -384,7 +384,7 @@ UR_APIEXPORT ur_result_t UR_APICALL
384384
urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
385385
if (hCommandBuffer->decrementReferenceCount() == 0) {
386386
// Ref count has reached zero, release of created commands
387-
for (auto Command : hCommandBuffer->CommandHandles) {
387+
for (auto &Command : hCommandBuffer->CommandHandles) {
388388
commandHandleDestroy(Command);
389389
}
390390

@@ -535,15 +535,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
535535

536536
std::vector<CUgraphNode> WaitNodes =
537537
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
538-
auto NewCommand = new kernel_command_handle(
538+
auto NewCommand = std::make_unique<kernel_command_handle>(
539539
hCommandBuffer, hKernel, GraphNode, NodeParams, workDim,
540540
pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize,
541541
numKernelAlternatives, phKernelAlternatives, SignalNode, WaitNodes);
542-
hCommandBuffer->CommandHandles.push_back(NewCommand);
543542

544543
if (phCommand) {
545-
*phCommand = NewCommand;
544+
*phCommand = NewCommand.get();
546545
}
546+
547+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
547548
} catch (ur_result_t Err) {
548549
return Err;
549550
}
@@ -591,13 +592,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMMemcpyExp(
591592

592593
std::vector<CUgraphNode> WaitNodes =
593594
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
594-
auto NewCommand = new usm_memcpy_command_handle(hCommandBuffer, GraphNode,
595-
SignalNode, WaitNodes);
596-
hCommandBuffer->CommandHandles.push_back(NewCommand);
597-
595+
auto NewCommand = std::make_unique<usm_memcpy_command_handle>(
596+
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
598597
if (phCommand) {
599-
*phCommand = NewCommand;
598+
*phCommand = NewCommand.get();
600599
}
600+
601+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
601602
return UR_RESULT_SUCCESS;
602603
} catch (ur_result_t Err) {
603604
return Err;
@@ -656,13 +657,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyExp(
656657

657658
std::vector<CUgraphNode> WaitNodes =
658659
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
659-
auto NewCommand = new buffer_copy_command_handle(hCommandBuffer, GraphNode,
660-
SignalNode, WaitNodes);
661-
hCommandBuffer->CommandHandles.push_back(NewCommand);
660+
auto NewCommand = std::make_unique<buffer_copy_command_handle>(
661+
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
662662

663663
if (phCommand) {
664-
*phCommand = NewCommand;
664+
*phCommand = NewCommand.get();
665665
}
666+
667+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
666668
return UR_RESULT_SUCCESS;
667669
} catch (ur_result_t Err) {
668670
return Err;
@@ -718,13 +720,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyRectExp(
718720

719721
std::vector<CUgraphNode> WaitNodes =
720722
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
721-
auto NewCommand = new buffer_copy_rect_command_handle(
723+
auto NewCommand = std::make_unique<buffer_copy_rect_command_handle>(
722724
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
723-
hCommandBuffer->CommandHandles.push_back(NewCommand);
724725

725726
if (phCommand) {
726-
*phCommand = NewCommand;
727+
*phCommand = NewCommand.get();
727728
}
729+
730+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
728731
return UR_RESULT_SUCCESS;
729732
} catch (ur_result_t Err) {
730733
return Err;
@@ -776,13 +779,13 @@ ur_result_t UR_APICALL urCommandBufferAppendMemBufferWriteExp(
776779

777780
std::vector<CUgraphNode> WaitNodes =
778781
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
779-
auto NewCommand = new buffer_write_command_handle(hCommandBuffer, GraphNode,
780-
SignalNode, WaitNodes);
781-
hCommandBuffer->CommandHandles.push_back(NewCommand);
782-
782+
auto NewCommand = std::make_unique<buffer_write_command_handle>(
783+
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
783784
if (phCommand) {
784-
*phCommand = NewCommand;
785+
*phCommand = NewCommand.get();
785786
}
787+
788+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
786789
return UR_RESULT_SUCCESS;
787790
} catch (ur_result_t Err) {
788791
return Err;
@@ -833,13 +836,13 @@ ur_result_t UR_APICALL urCommandBufferAppendMemBufferReadExp(
833836

834837
std::vector<CUgraphNode> WaitNodes =
835838
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
836-
auto NewCommand = new buffer_read_command_handle(hCommandBuffer, GraphNode,
837-
SignalNode, WaitNodes);
838-
hCommandBuffer->CommandHandles.push_back(NewCommand);
839-
839+
auto NewCommand = std::make_unique<buffer_read_command_handle>(
840+
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
840841
if (phCommand) {
841-
*phCommand = NewCommand;
842+
*phCommand = NewCommand.get();
842843
}
844+
845+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
843846
return UR_RESULT_SUCCESS;
844847
} catch (ur_result_t Err) {
845848
return Err;
@@ -894,13 +897,14 @@ ur_result_t UR_APICALL urCommandBufferAppendMemBufferWriteRectExp(
894897

895898
std::vector<CUgraphNode> WaitNodes =
896899
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
897-
auto NewCommand = new buffer_write_rect_command_handle(
900+
auto NewCommand = std::make_unique<buffer_write_rect_command_handle>(
898901
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
899-
hCommandBuffer->CommandHandles.push_back(NewCommand);
900902

901903
if (phCommand) {
902-
*phCommand = NewCommand;
904+
*phCommand = NewCommand.get();
903905
}
906+
907+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
904908
return UR_RESULT_SUCCESS;
905909
} catch (ur_result_t Err) {
906910
return Err;
@@ -955,13 +959,14 @@ ur_result_t UR_APICALL urCommandBufferAppendMemBufferReadRectExp(
955959

956960
std::vector<CUgraphNode> WaitNodes =
957961
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
958-
auto NewCommand = new buffer_read_rect_command_handle(
962+
auto NewCommand = std::make_unique<buffer_read_rect_command_handle>(
959963
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
960-
hCommandBuffer->CommandHandles.push_back(NewCommand);
961964

962965
if (phCommand) {
963-
*phCommand = NewCommand;
966+
*phCommand = NewCommand.get();
964967
}
968+
969+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
965970
return UR_RESULT_SUCCESS;
966971
} catch (ur_result_t Err) {
967972
return Err;
@@ -1008,13 +1013,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMPrefetchExp(
10081013

10091014
std::vector<CUgraphNode> WaitNodes =
10101015
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
1011-
auto NewCommand = new usm_prefetch_command_handle(hCommandBuffer, GraphNode,
1012-
SignalNode, WaitNodes);
1013-
hCommandBuffer->CommandHandles.push_back(NewCommand);
1016+
auto NewCommand = std::make_unique<usm_prefetch_command_handle>(
1017+
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
10141018

10151019
if (phCommand) {
1016-
*phCommand = NewCommand;
1020+
*phCommand = NewCommand.get();
10171021
}
1022+
1023+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
10181024
return UR_RESULT_SUCCESS;
10191025
} catch (ur_result_t Err) {
10201026
return Err;
@@ -1061,14 +1067,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendUSMAdviseExp(
10611067

10621068
std::vector<CUgraphNode> WaitNodes =
10631069
numEventsInWaitList ? std::move(DepsList) : std::vector<CUgraphNode>();
1064-
auto NewCommand = new usm_advise_command_handle(hCommandBuffer, GraphNode,
1065-
SignalNode, WaitNodes);
1066-
hCommandBuffer->CommandHandles.push_back(NewCommand);
1070+
auto NewCommand = std::make_unique<usm_advise_command_handle>(
1071+
hCommandBuffer, GraphNode, SignalNode, WaitNodes);
10671072

10681073
if (phCommand) {
1069-
*phCommand = NewCommand;
1074+
*phCommand = NewCommand.get();
10701075
}
10711076

1077+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
1078+
10721079
return UR_RESULT_SUCCESS;
10731080
} catch (ur_result_t Err) {
10741081
return Err;

source/adapters/cuda/command_buffer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,5 +347,6 @@ struct ur_exp_command_buffer_handle_t_ {
347347
ur_exp_command_buffer_sync_point_t NextSyncPoint;
348348

349349
// Handles to individual commands in the command-buffer
350-
std::vector<ur_exp_command_buffer_command_handle_t> CommandHandles;
350+
std::vector<std::unique_ptr<ur_exp_command_buffer_command_handle_t_>>
351+
CommandHandles;
351352
};

source/adapters/hip/command_buffer.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,6 @@ urCommandBufferRetainExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
263263
UR_APIEXPORT ur_result_t UR_APICALL
264264
urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
265265
if (hCommandBuffer->decrementReferenceCount() == 0) {
266-
// Ref count has reached zero, release of created commands
267-
// commands.
268-
for (auto Command : hCommandBuffer->CommandHandles) {
269-
delete Command;
270-
}
271-
272266
delete hCommandBuffer;
273267
}
274268
return UR_RESULT_SUCCESS;
@@ -374,18 +368,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp(
374368
*pSyncPoint = SyncPoint;
375369
}
376370

377-
auto NewCommand = new ur_exp_command_buffer_command_handle_t_{
378-
hCommandBuffer, hKernel, GraphNode,
379-
NodeParams, workDim, pGlobalWorkOffset,
380-
pGlobalWorkSize, pLocalWorkSize, numKernelAlternatives,
381-
phKernelAlternatives};
382-
383-
hCommandBuffer->CommandHandles.push_back(NewCommand);
371+
auto NewCommand = std::make_unique<ur_exp_command_buffer_command_handle_t_>(
372+
hCommandBuffer, hKernel, GraphNode, NodeParams, workDim,
373+
pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize,
374+
numKernelAlternatives, phKernelAlternatives);
384375

385376
if (phCommand) {
386-
*phCommand = NewCommand;
377+
*phCommand = NewCommand.get();
387378
}
388379

380+
hCommandBuffer->CommandHandles.push_back(std::move(NewCommand));
381+
389382
} catch (ur_result_t Err) {
390383
return Err;
391384
}

source/adapters/hip/command_buffer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,6 @@ struct ur_exp_command_buffer_handle_t_ {
151151
ur_exp_command_buffer_sync_point_t NextSyncPoint;
152152

153153
// Handles to individual commands in the command-buffer
154-
std::vector<ur_exp_command_buffer_command_handle_t> CommandHandles;
154+
std::vector<std::unique_ptr<ur_exp_command_buffer_command_handle_t_>>
155+
CommandHandles;
155156
};

source/adapters/level_zero/command_buffer.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,6 @@ void ur_exp_command_buffer_handle_t_::cleanupCommandBufferResources() {
466466
ReleaseIndirectMem(AssociatedKernel);
467467
ur::level_zero::urKernelRelease(AssociatedKernel);
468468
}
469-
470-
for (auto Command : CommandHandles) {
471-
delete Command;
472-
}
473469
}
474470

475471
void ur_exp_command_buffer_handle_t_::registerSyncPoint(
@@ -924,7 +920,7 @@ createCommandHandle(ur_exp_command_buffer_handle_t CommandBuffer,
924920
ur_kernel_handle_t Kernel, uint32_t WorkDim,
925921
const size_t *LocalWorkSize, uint32_t NumKernelAlternatives,
926922
ur_kernel_handle_t *KernelAlternatives,
927-
ur_exp_command_buffer_command_handle_t &Command) {
923+
ur_exp_command_buffer_command_handle_t *Command) {
928924

929925
assert(CommandBuffer->IsUpdatable);
930926

@@ -992,17 +988,19 @@ createCommandHandle(ur_exp_command_buffer_handle_t CommandBuffer,
992988
DEBUG_LOG(CommandId);
993989

994990
try {
995-
Command = new kernel_command_handle(
991+
auto NewCommand = std::make_unique<kernel_command_handle>(
996992
CommandBuffer, Kernel, CommandId, WorkDim, LocalWorkSize != nullptr,
997993
NumKernelAlternatives, KernelAlternatives);
994+
995+
*Command = NewCommand.get();
996+
997+
CommandBuffer->CommandHandles.push_back(std::move(NewCommand));
998998
} catch (const std::bad_alloc &) {
999999
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
10001000
} catch (...) {
10011001
return UR_RESULT_ERROR_UNKNOWN;
10021002
}
10031003

1004-
CommandBuffer->CommandHandles.push_back(Command);
1005-
10061004
return UR_RESULT_SUCCESS;
10071005
}
10081006

@@ -1070,7 +1068,7 @@ ur_result_t urCommandBufferAppendKernelLaunchExp(
10701068
if (Command) {
10711069
UR_CALL(createCommandHandle(CommandBuffer, Kernel, WorkDim, LocalWorkSize,
10721070
NumKernelAlternatives, KernelAlternatives,
1073-
*Command));
1071+
Command));
10741072
}
10751073
std::vector<ze_event_handle_t> ZeEventList;
10761074
ze_event_handle_t ZeLaunchEvent = nullptr;

source/adapters/level_zero/command_buffer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
146146
// any fence or event synchronization to avoid repeated calls to synchronize.
147147
bool NeedsUpdateSynchronization = false;
148148
// Track handle objects to free when command-buffer is destroyed.
149-
std::vector<ur_exp_command_buffer_command_handle_t> CommandHandles;
149+
std::vector<std::unique_ptr<ur_exp_command_buffer_command_handle_t_>>
150+
CommandHandles;
150151
};
151152

152153
struct ur_exp_command_buffer_command_handle_t_ : public _ur_object {

source/adapters/level_zero/v2/api.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,4 @@ ur_result_t urCommandBufferUpdateWaitEventsExp(
396396
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
397397
}
398398

399-
ur_result_t urUSMImportExp(ur_context_handle_t hContext, void *pMem,
400-
size_t size) {
401-
logger::error("{} function not implemented!", __FUNCTION__);
402-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
403-
}
404-
405-
ur_result_t urUSMReleaseExp(ur_context_handle_t hContext, void *pMem) {
406-
logger::error("{} function not implemented!", __FUNCTION__);
407-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
408-
}
409-
410399
} // namespace ur::level_zero

0 commit comments

Comments
 (0)