Skip to content

Commit

Permalink
[SYCL] Implement accessor default constructor (#6940)
Browse files Browse the repository at this point in the history
This patch implements SYCL 2020 accessor default constructor
  • Loading branch information
dm-vodopyanov authored Oct 6, 2022
1 parent f3d245d commit 04928f9
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 39 deletions.
11 changes: 10 additions & 1 deletion sycl/include/sycl/accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,15 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
return reinterpret_cast<PtrType>(AccessorBaseHost::getPtr());
}

public:
accessor()
: AccessorBaseHost(
/*Offset=*/{0, 0, 0}, /*AccessRange=*/{0, 0, 0},
/*MemoryRange=*/{0, 0, 0},
/*AccessMode=*/getAdjustedMode({}),
/*SYCLMemObject=*/nullptr, /*Dims=*/0, /*ElemSize=*/0,
/*OffsetInBytes=*/0, /*IsSubBuffer=*/false, /*PropertyList=*/{}){};

#endif // __SYCL_DEVICE_ONLY__

private:
Expand Down Expand Up @@ -1937,7 +1946,7 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
size_t byte_size() const noexcept { return size() * sizeof(DataT); }

size_t max_size() const noexcept {
return (std::numeric_limits<difference_type>::max)();
return empty() ? 0 : (std::numeric_limits<difference_type>::max)();
}

bool empty() const noexcept { return size() == 0; }
Expand Down
2 changes: 2 additions & 0 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,8 @@ static pi_result SetKernelParamsAndLaunch(
break;
case kernel_param_kind_t::kind_accessor: {
Requirement *Req = (Requirement *)(Arg.MPtr);
if (Req->MAccessRange == range<3>({0, 0, 0}))
break;
if (getMemAllocationFunc == nullptr)
throw sycl::exception(make_error_code(errc::kernel_argument),
"placeholder accessor must be bound by calling "
Expand Down
19 changes: 19 additions & 0 deletions sycl/test/basic_tests/accessor/accessor_default_ctor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out

#include <sycl/sycl.hpp>

int main() {
sycl::accessor<int, 0, sycl::access::mode::read_write, sycl::target::device>
B;
assert(B.empty());
assert(B.size() == 0);
assert(B.max_size() == 0);
assert(B.byte_size() == 0);
// The return values of get_pointer() and get_multi_ptr() are unspecified.
assert(B.get_pointer() == nullptr);
// TODO: uncomment check with get_multi_ptr() when SYCL 2020 mupti_ptr feature
// will be merged
// assert(B.get_multi_ptr() == nullptr);

return 0;
}
42 changes: 42 additions & 0 deletions sycl/unittests/scheduler/AccessorDefaultCtor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "SchedulerTest.hpp"
#include "SchedulerTestUtils.hpp"
#include <detail/handler_impl.hpp>

#include <helpers/PiMock.hpp>
#include <helpers/ScopedEnvVar.hpp>
#include <helpers/TestKernel.hpp>

#include <vector>

using namespace sycl;

TEST_F(SchedulerTest, AccDefaultCtorDoesntAffectDepGraph) {
unittest::PiMock Mock;
platform Plt = Mock.getPlatform();

queue QueueDev(context(Plt), default_selector_v);
MockScheduler MS;

detail::QueueImplPtr QueueDevImpl = detail::getSyclObjImpl(QueueDev);

std::vector<detail::Command *> ToEnqueue;

MockHandlerCustomFinalize MockCGH(QueueDevImpl, false);

sycl::accessor<int, 0, sycl::access::mode::read_write, sycl::target::device>
B;

MockCGH.single_task<class acc_with_zero_dim>([=]() {
int size = B.size();
(void)size;
});

std::unique_ptr<sycl::detail::CG> CmdGroup = MockCGH.finalize();

detail::Command *NewCmd =
MS.addCG(std::move(CmdGroup), QueueDevImpl, ToEnqueue);

// if MDeps is empty, accessor built from default ctor does not affect
// dependency graph in accordance with SYCL 2020
EXPECT_TRUE(NewCmd->MDeps.empty());
}
1 change: 1 addition & 0 deletions sycl/unittests/scheduler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ add_sycl_unittest(SchedulerTests OBJECT
InOrderQueueSyncCheck.cpp
RunOnHostIntelCG.cpp
EnqueueWithDependsOnDeps.cpp
AccessorDefaultCtor.cpp
)
38 changes: 1 addition & 37 deletions sycl/unittests/scheduler/EnqueueWithDependsOnDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "SchedulerTest.hpp"
#include "SchedulerTestUtils.hpp"
#include <detail/handler_impl.hpp>

#include <helpers/PiMock.hpp>
#include <helpers/ScopedEnvVar.hpp>
Expand All @@ -19,48 +18,13 @@
using namespace sycl;
using EventImplPtr = std::shared_ptr<detail::event_impl>;

namespace DependsOnTest {
class MockHandlerCustom : public MockHandler {
public:
MockHandlerCustom(std::shared_ptr<sycl::detail::queue_impl> Queue,
bool IsHost)
: MockHandler(Queue, IsHost) {}

std::unique_ptr<sycl::detail::CG> finalize() {
std::unique_ptr<sycl::detail::CG> CommandGroup;
switch (getType()) {
case sycl::detail::CG::Kernel: {
CommandGroup.reset(new sycl::detail::CGExecKernel(
getNDRDesc(), std::move(getHostKernel()), getKernel(),
std::move(MImpl->MKernelBundle), getArgsStorage(), getAccStorage(),
getSharedPtrStorage(), getRequirements(), getEvents(), getArgs(),
getKernelName(), getOSModuleHandle(), getStreamStorage(),
MImpl->MAuxiliaryResources, getCGType(), getCodeLoc()));
break;
}
case sycl::detail::CG::CodeplayHostTask: {
CommandGroup.reset(new detail::CGHostTask(
std::move(getHostTask()), getQueue(), getQueue()->getContextImplPtr(),
getArgs(), getArgsStorage(), getAccStorage(), getSharedPtrStorage(),
getRequirements(), getEvents(), getCGType(), getCodeLoc()));
break;
}
default:
throw sycl::runtime_error("Unhandled type of command group",
PI_ERROR_INVALID_OPERATION);
}

return CommandGroup;
}
};
} // namespace DependsOnTest
detail::Command *AddTaskCG(bool IsHost, MockScheduler &MS,
detail::QueueImplPtr DevQueue,
const std::vector<EventImplPtr> &Events) {
std::vector<detail::Command *> ToEnqueue;

// Emulating processing of command group function
DependsOnTest::MockHandlerCustom MockCGH(DevQueue, false);
MockHandlerCustomFinalize MockCGH(DevQueue, false);

for (auto EventImpl : Events)
MockCGH.depends_on(detail::createSyclObjFromImpl<event>(EventImpl));
Expand Down
37 changes: 36 additions & 1 deletion sycl/unittests/scheduler/SchedulerTestUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <detail/queue_impl.hpp>
#include <detail/scheduler/commands.hpp>
#include <detail/scheduler/scheduler.hpp>
#include <detail/handler_impl.hpp>
#include <detail/stream_impl.hpp>
#include <sycl/detail/cl.h>

Expand Down Expand Up @@ -270,4 +271,38 @@ class MockHandler : public sycl::handler {

return nullptr;
}
};
};

class MockHandlerCustomFinalize : public MockHandler {
public:
MockHandlerCustomFinalize(std::shared_ptr<sycl::detail::queue_impl> Queue,
bool IsHost)
: MockHandler(Queue, IsHost) {}

std::unique_ptr<sycl::detail::CG> finalize() {
std::unique_ptr<sycl::detail::CG> CommandGroup;
switch (getType()) {
case sycl::detail::CG::Kernel: {
CommandGroup.reset(new sycl::detail::CGExecKernel(
getNDRDesc(), std::move(getHostKernel()), getKernel(),
std::move(MImpl->MKernelBundle), getArgsStorage(), getAccStorage(),
getSharedPtrStorage(), getRequirements(), getEvents(), getArgs(),
getKernelName(), getOSModuleHandle(), getStreamStorage(),
MImpl->MAuxiliaryResources, getCGType(), getCodeLoc()));
break;
}
case sycl::detail::CG::CodeplayHostTask: {
CommandGroup.reset(new sycl::detail::CGHostTask(
std::move(getHostTask()), getQueue(), getQueue()->getContextImplPtr(),
getArgs(), getArgsStorage(), getAccStorage(), getSharedPtrStorage(),
getRequirements(), getEvents(), getCGType(), getCodeLoc()));
break;
}
default:
throw sycl::runtime_error("Unhandled type of command group",
PI_ERROR_INVALID_OPERATION);
}

return CommandGroup;
}
};

0 comments on commit 04928f9

Please sign in to comment.