Skip to content

Commit f46f79d

Browse files
committed
alternative solution to optimize memory transfers
1 parent e0c40a9 commit f46f79d

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

sycl/include/CL/sycl/detail/sycl_mem_obj_t.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ class __SYCL_EXPORT SYCLMemObjT : public SYCLMemObjI {
314314

315315
bool isInterop() const;
316316

317+
bool isHostPointerReadOnly() const{
318+
return MHostPtrReadOnly;
319+
}
320+
317321
protected:
318322
// An allocateMem helper that determines which host ptr to use
319323
void determineHostPtr(const ContextImplPtr &Context, bool InitFromUserData,

sycl/source/detail/memory_manager.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,10 @@ void *MemoryManager::allocateHostMemory(SYCLMemObjI *MemObj, void *UserPtr,
296296
bool HostPtrReadOnly, size_t Size,
297297
const sycl::property_list &) {
298298
// Can return user pointer directly if it points to writable memory.
299-
if (UserPtr && HostPtrReadOnly == false)
299+
if (UserPtr)
300300
return UserPtr;
301301

302302
void *NewMem = MemObj->allocateHostMem();
303-
// Need to initialize new memory if user provides pointer to read only
304-
// memory.
305-
if (UserPtr && HostPtrReadOnly == true)
306-
std::memcpy((char *)NewMem, (char *)UserPtr, Size);
307303
return NewMem;
308304
}
309305

@@ -331,8 +327,7 @@ static RT::PiMemFlags getMemObjCreationFlags(void *UserPtr,
331327
RT::PiMemFlags Result =
332328
HostPtrReadOnly ? PI_MEM_ACCESS_READ_ONLY : PI_MEM_FLAGS_ACCESS_RW;
333329
if (UserPtr)
334-
Result |= HostPtrReadOnly ? PI_MEM_FLAGS_HOST_PTR_COPY
335-
: PI_MEM_FLAGS_HOST_PTR_USE;
330+
Result |= PI_MEM_FLAGS_HOST_PTR_USE;
336331
return Result;
337332
}
338333

sycl/source/detail/scheduler/commands.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,10 +833,10 @@ const char *Command::getBlockReason() const {
833833

834834
AllocaCommandBase::AllocaCommandBase(CommandType Type, QueueImplPtr Queue,
835835
Requirement Req,
836-
AllocaCommandBase *LinkedAllocaCmd)
836+
AllocaCommandBase *LinkedAllocaCmd, bool IsConst)
837837
: Command(Type, Queue), MLinkedAllocaCmd(LinkedAllocaCmd),
838838
MIsLeaderAlloca(nullptr == LinkedAllocaCmd), MRequirement(std::move(Req)),
839-
MReleaseCmd(Queue, this) {
839+
MReleaseCmd(Queue, this), MIsConst(IsConst) {
840840
MRequirement.MAccessMode = access::mode::read_write;
841841
emitInstrumentationDataProxy();
842842
}
@@ -868,9 +868,9 @@ bool AllocaCommandBase::supportsPostEnqueueCleanup() const { return false; }
868868

869869
AllocaCommand::AllocaCommand(QueueImplPtr Queue, Requirement Req,
870870
bool InitFromUserData,
871-
AllocaCommandBase *LinkedAllocaCmd)
871+
AllocaCommandBase *LinkedAllocaCmd, bool IsConst)
872872
: AllocaCommandBase(CommandType::ALLOCA, std::move(Queue), std::move(Req),
873-
LinkedAllocaCmd),
873+
LinkedAllocaCmd, IsConst),
874874
MInitFromUserData(InitFromUserData) {
875875
// Node event must be created before the dependent edge is added to this node,
876876
// so this call must be before the addDep() call.

sycl/source/detail/scheduler/commands.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ class ReleaseCommand : public Command {
364364
class AllocaCommandBase : public Command {
365365
public:
366366
AllocaCommandBase(CommandType Type, QueueImplPtr Queue, Requirement Req,
367-
AllocaCommandBase *LinkedAllocaCmd);
367+
AllocaCommandBase *LinkedAllocaCmd,
368+
bool IsConst = false);
368369

369370
ReleaseCommand *getReleaseCmd() { return &MReleaseCmd; }
370371

@@ -394,6 +395,8 @@ class AllocaCommandBase : public Command {
394395
/// Indicates that the command owns memory allocation in case of connected
395396
/// alloca command.
396397
bool MIsLeaderAlloca = true;
398+
// Indicates tha thte data in this allocation must not be modified
399+
bool MIsConst = false;
397400

398401
protected:
399402
Requirement MRequirement;
@@ -406,7 +409,8 @@ class AllocaCommand : public AllocaCommandBase {
406409
public:
407410
AllocaCommand(QueueImplPtr Queue, Requirement Req,
408411
bool InitFromUserData = true,
409-
AllocaCommandBase *LinkedAllocaCmd = nullptr);
412+
AllocaCommandBase *LinkedAllocaCmd = nullptr,
413+
bool IsConst = false);
410414

411415
void *getMemAllocation() const final { return MMemAllocation; }
412416
void printDot(std::ostream &Stream) const final;

sycl/source/detail/scheduler/graph_builder.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,14 +630,16 @@ DepDesc Scheduler::GraphBuilder::findDepForRecord(Command *Cmd,
630630
AllocaCommandBase *
631631
Scheduler::GraphBuilder::findAllocaForReq(MemObjRecord *Record,
632632
const Requirement *Req,
633-
const ContextImplPtr &Context) {
634-
auto IsSuitableAlloca = [&Context, Req](AllocaCommandBase *AllocaCmd) {
633+
const ContextImplPtr &Context,
634+
bool allowConst) {
635+
auto IsSuitableAlloca = [&Context, Req, allowConst](AllocaCommandBase *AllocaCmd) {
635636
bool Res = sameCtx(AllocaCmd->getQueue()->getContextImplPtr(), Context);
636637
if (IsSuitableSubReq(Req)) {
637638
const Requirement *TmpReq = AllocaCmd->getRequirement();
638639
Res &= AllocaCmd->getType() == Command::CommandType::ALLOCA_SUB_BUF;
639640
Res &= TmpReq->MOffsetInBytes == Req->MOffsetInBytes;
640641
Res &= TmpReq->MSYCLMemObj->getSize() == Req->MSYCLMemObj->getSize();
642+
Res &= allowConst || !AllocaCmd->MIsConst;
641643
}
642644
return Res;
643645
};
@@ -669,7 +671,7 @@ AllocaCommandBase *Scheduler::GraphBuilder::getOrCreateAllocaForReq(
669671
std::vector<Command *> &ToEnqueue) {
670672

671673
AllocaCommandBase *AllocaCmd =
672-
findAllocaForReq(Record, Req, Queue->getContextImplPtr());
674+
findAllocaForReq(Record, Req, Queue->getContextImplPtr(), false);
673675

674676
if (!AllocaCmd) {
675677
std::vector<Command *> ToCleanUp;
@@ -722,7 +724,7 @@ AllocaCommandBase *Scheduler::GraphBuilder::getOrCreateAllocaForReq(
722724
Scheduler::getInstance().getDefaultHostQueue();
723725
AllocaCommand *HostAllocaCmd = new AllocaCommand(
724726
DefaultHostQueue, FullReq, true /* InitFromUserData */,
725-
nullptr /* LinkedAllocaCmd */);
727+
nullptr /* LinkedAllocaCmd */, MemObj->isHostPointerReadOnly() /* IsConst */);
726728
Record->MAllocaCommands.push_back(HostAllocaCmd);
727729
Record->MWriteLeaves.push_back(HostAllocaCmd, ToEnqueue);
728730
++(HostAllocaCmd->MLeafCounter);
@@ -755,7 +757,7 @@ AllocaCommandBase *Scheduler::GraphBuilder::getOrCreateAllocaForReq(
755757
: HostUnifiedMemory;
756758
if (PinnedHostMemory || HostUnifiedMemoryOnNonHostDevice) {
757759
AllocaCommandBase *LinkedAllocaCmdCand =
758-
findAllocaForReq(Record, Req, Record->MCurContext);
760+
findAllocaForReq(Record, Req, Record->MCurContext, false);
759761

760762
// Cannot setup link if candidate is linked already
761763
if (LinkedAllocaCmdCand &&

sycl/source/detail/scheduler/scheduler.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ class Scheduler {
618618
/// Searches for suitable alloca in memory record.
619619
AllocaCommandBase *findAllocaForReq(MemObjRecord *Record,
620620
const Requirement *Req,
621-
const ContextImplPtr &Context);
621+
const ContextImplPtr &Context,
622+
bool allowConst = true);
622623

623624
friend class Command;
624625

0 commit comments

Comments
 (0)